ArrayHelperTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\ArrayHelper;
  4. class ArrayHelperTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testFlattenEmpty()
  7. {
  8. $this->assertEquals(
  9. [],
  10. ArrayHelper::flatten([])
  11. );
  12. }
  13. public function testFlattenFlat()
  14. {
  15. $this->assertEquals(
  16. [1, 2, 3],
  17. ArrayHelper::flatten([1, 2, 3])
  18. );
  19. }
  20. public function testFlatten2D()
  21. {
  22. $this->assertEquals(
  23. [1, 2, 3, 4, 5],
  24. ArrayHelper::flatten([1, [2, 3], [4], 5])
  25. );
  26. }
  27. public function testFlatten2DEmpty()
  28. {
  29. $this->assertEquals(
  30. [1, 2, 3, 4, 5],
  31. ArrayHelper::flatten([[], 1, [2, 3], [], [4], 5])
  32. );
  33. }
  34. public function testFlatten3D()
  35. {
  36. $this->assertEquals(
  37. [1, 2, 3, 4, 5, 6],
  38. ArrayHelper::flatten([1, [[2, 3], 4], [[5], [6]]])
  39. );
  40. }
  41. public function testFlatten3DEmpty()
  42. {
  43. $this->assertEquals(
  44. [1, 2, 3, 4, 5, 6],
  45. ArrayHelper::flatten([1, [[2, [], 3], 4, []], [[5], [6]], []])
  46. );
  47. }
  48. public function testFlattenAssociativeLetterFlat()
  49. {
  50. $this->assertEquals(
  51. [1, 2, 3, 4],
  52. ArrayHelper::flatten(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])
  53. );
  54. }
  55. public function testFlattenAssociativeDigit()
  56. {
  57. $this->assertEquals(
  58. [1, 2, 3, 4],
  59. ArrayHelper::flatten([1 => 1, 2 => 2, 3 => 3, 4 => 4])
  60. );
  61. }
  62. public function testFlattenAssociativeDigitReverse()
  63. {
  64. $this->assertEquals(
  65. [1, 2, 3, 4],
  66. ArrayHelper::flatten([3 => 1, 2 => 2, 1 => 3, 0 => 4])
  67. );
  68. }
  69. public function testFlattenMixed2D()
  70. {
  71. $this->assertEquals(
  72. [1, 2, 3, 4, 5, 6, 7, 8],
  73. ArrayHelper::flatten([
  74. 'a' => 1,
  75. 'b' => [2, 3],
  76. 'c' => ['#' => 4, '@' => 5],
  77. 'd' => [6, '$' => 7],
  78. 8,
  79. ])
  80. );
  81. }
  82. public function mapProvider()
  83. {
  84. return [
  85. [
  86. null,
  87. function($v) { return 1; },
  88. 1,
  89. ],
  90. [
  91. 'string',
  92. function($v) { return strrev($v); },
  93. 'gnirts',
  94. ],
  95. [
  96. ['a' => 1, 'b' => 2, 'c' => 3],
  97. function($v) { return $v; },
  98. ['a' => 1, 'b' => 2, 'c' => 3],
  99. ],
  100. [
  101. ['a' => 1, 'b' => 2, 'c' => 3],
  102. function($v) { return $v < 2 ? null : $v; },
  103. ['a' => null, 'b' => 2, 'c' => 3],
  104. ],
  105. [
  106. ['a' => 1, 'b' => null, 'c' => 3],
  107. function($v) { return $v; },
  108. ['a' => 1, 'b' => null, 'c' => 3],
  109. ],
  110. [
  111. ['a' => 1, 'b' => 2, 'b' => 3],
  112. function($v) { return $v * $v; },
  113. ['a' => 1, 'b' => 4, 'b' => 9],
  114. ],
  115. ];
  116. }
  117. /**
  118. * @dataProvider mapProvider
  119. */
  120. public function testMap($source, $callback, $expected)
  121. {
  122. $this->assertSame($expected, ArrayHelper::map($source, $callback));
  123. }
  124. public function multimapProvider()
  125. {
  126. return [
  127. [
  128. ['a' => 1, 'b' => 2, 'c' => 3],
  129. function($k, $v) { return [$k => $v]; },
  130. ['a' => 1, 'b' => 2, 'c' => 3],
  131. ],
  132. [
  133. ['a' => 1, 'b' => 2, 'c' => 3],
  134. function($k, $v) { return [$v => $k]; },
  135. [1 => 'a', 2 => 'b', 3 => 'c'],
  136. ],
  137. [
  138. ['a' => 1, 'b' => 2, 'c' => 3],
  139. function($k, $v) { return [strtoupper($k) => $v * $v]; },
  140. ['A' => 1, 'B' => 4, 'C' => 9],
  141. ],
  142. [
  143. ['a' => 1, 'b' => 2, 'c' => 3],
  144. function($k, $v) { return [$k => null]; },
  145. ['a' => null, 'b' => null, 'c' => null],
  146. ],
  147. [
  148. ['a' => 1, 'b' => 2, 'b' => 3],
  149. function($k, $v) { return [$k => $v]; },
  150. ['a' => 1, 'b' => 3],
  151. ],
  152. [
  153. ['a' => 1, 'b' => 2, 'c' => 3],
  154. function($k, $v) { return ($v % 2) ? [$k => $v * $v] : null; },
  155. ['a' => 1, 'c' => 9],
  156. ],
  157. [
  158. ['a' => 1, 'b' => 2, 'c' => 3],
  159. function($k, $v) { return ($v % 2) ? [$k => $v * $v] : []; },
  160. ['a' => 1, 'c' => 9],
  161. ],
  162. [
  163. ['a' => 1, 'b' => 2, 'c' => 3],
  164. function($k, $v) { return [$k => $v, $k.'^2' => $v * $v]; },
  165. ['a' => 1, 'a^2' => 1, 'b' => 2, 'b^2' => 4, 'c' => 3, 'c^2' => 9],
  166. ],
  167. [
  168. ['a' => 1, 'b' => 2, 'c' => 3],
  169. function($k, $v) { return [$k => [[$v]], strtoupper($k) => []]; },
  170. ['a' => [[1]], 'A' => [], 'b' => [[2]], 'B' => [], 'c' => [[3]], 'C' => []],
  171. ],
  172. ];
  173. }
  174. /**
  175. * @dataProvider multimapProvider
  176. */
  177. public function testMultimap($source, $callback, $expected)
  178. {
  179. $this->assertSame($expected, ArrayHelper::multimap($source, $callback));
  180. }
  181. public function multimapUnexpectedValueProvider()
  182. {
  183. return [
  184. [
  185. ['a' => 1, 'b' => 2, 'c' => 3],
  186. function($k, $v) { return $v; },
  187. ],
  188. [
  189. ['a' => 1, 'b' => 2, 'c' => 3],
  190. function($k, $v) { return 'string'; },
  191. ],
  192. ];
  193. }
  194. /**
  195. * @dataProvider multimapUnexpectedValueProvider
  196. * @expectedException \UnexpectedValueException
  197. */
  198. public function testMultimapUnexpectedValue($source, $callback)
  199. {
  200. ArrayHelper::multimap($source, $callback);
  201. }
  202. }