ArrayHelperTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. 'string',
  97. function($v) { return null; },
  98. null,
  99. ],
  100. [
  101. 'string',
  102. function($k, $v) { return [$v => $k]; },
  103. ['string' => null],
  104. ],
  105. [
  106. ['a' => 1, 'b' => 2, 'c' => 3],
  107. function($v) { return $v; },
  108. ['a' => 1, 'b' => 2, 'c' => 3],
  109. ],
  110. [
  111. ['a' => 1, 'b' => 2, 'c' => 3],
  112. function($v) { return $v < 2 ? null : $v; },
  113. ['a' => null, 'b' => 2, 'c' => 3],
  114. ],
  115. [
  116. ['a' => 1, 'b' => null, 'c' => 3],
  117. function($v) { return $v; },
  118. ['a' => 1, 'b' => null, 'c' => 3],
  119. ],
  120. [
  121. ['a' => 1, 'b' => 2, 'c' => 3],
  122. function($v) { return $v * $v; },
  123. ['a' => 1, 'b' => 4, 'c' => 9],
  124. ],
  125. [
  126. ['a' => 1, 'b' => 2, 'c' => 3],
  127. function($k, $v) { return $k . $v; },
  128. ['a' => 'a1', 'b' => 'b2', 'c' => 'c3'],
  129. ],
  130. [
  131. ['a' => 1, 'b' => 2, 'c' => 3],
  132. function($k, $v) { return [strtoupper($k) => $v]; },
  133. ['a' => ['A' => 1], 'b' => ['B' => 2], 'c' => ['C' => 3]],
  134. ],
  135. [
  136. ['a' => 1, 'b' => 2, 'c' => null],
  137. function($k, $v) { return $k . (is_null($v) ? ' null' : ' not null'); },
  138. ['a' => 'a not null', 'b' => 'b not null', 'c' => 'c null'],
  139. ],
  140. ];
  141. }
  142. /**
  143. * @dataProvider mapProvider
  144. */
  145. public function testMap($source, $callback, $expected)
  146. {
  147. $this->assertSame($expected, ArrayHelper::map($source, $callback));
  148. }
  149. public function mapIfSetProvider()
  150. {
  151. return [
  152. [
  153. null,
  154. function($v) { return 1; },
  155. null,
  156. ],
  157. [
  158. 'string',
  159. function($v) { return strrev($v); },
  160. 'gnirts',
  161. ],
  162. [
  163. 'string',
  164. function($v) { return null; },
  165. null,
  166. ],
  167. [
  168. ['a' => 1, 'b' => 2, 'c' => 3],
  169. function($v) { return $v; },
  170. ['a' => 1, 'b' => 2, 'c' => 3],
  171. ],
  172. [
  173. ['a' => 1, 'b' => 2, 'c' => 3],
  174. function($v) { return $v < 2 ? null : $v; },
  175. ['a' => null, 'b' => 2, 'c' => 3],
  176. ],
  177. [
  178. ['a' => 1, 'b' => null, 'c' => 3],
  179. function($v) { return (string)$v; },
  180. ['a' => '1', 'b' => null, 'c' => '3'],
  181. ],
  182. [
  183. ['a' => 1, 'b' => 2, 'b' => 3],
  184. function($v) { return $v * $v; },
  185. ['a' => 1, 'b' => 4, 'b' => 9],
  186. ],
  187. [
  188. ['a' => 1, 'b' => 2, 'c' => 3],
  189. function($k, $v) { return $k . $v; },
  190. ['a' => 'a1', 'b' => 'b2', 'c' => 'c3'],
  191. ],
  192. [
  193. ['a' => 1, 'b' => 2, 'c' => 3],
  194. function($k, $v) { return [strtoupper($k) => $v]; },
  195. ['a' => ['A' => 1], 'b' => ['B' => 2], 'c' => ['C' => 3]],
  196. ],
  197. [
  198. ['a' => 1, 'b' => 2, 'c' => null],
  199. function($k, $v) { return $k . (is_null($v) ? ' null' : ' not null'); },
  200. ['a' => 'a not null', 'b' => 'b not null', 'c' => null],
  201. ],
  202. ];
  203. }
  204. /**
  205. * @dataProvider mapIfSetProvider
  206. */
  207. public function testMapIfSet($source, $callback, $expected)
  208. {
  209. $this->assertSame($expected, ArrayHelper::mapIfSet($source, $callback));
  210. }
  211. public function multimapProvider()
  212. {
  213. return [
  214. [
  215. ['a' => 1, 'b' => 2, 'c' => 3],
  216. function($k, $v) { return [$k => $v]; },
  217. ['a' => 1, 'b' => 2, 'c' => 3],
  218. ],
  219. [
  220. ['a' => 1, 'b' => 2, 'c' => 3],
  221. function($k, $v) { return [$v => $k]; },
  222. [1 => 'a', 2 => 'b', 3 => 'c'],
  223. ],
  224. [
  225. ['a' => 1, 'b' => 2, 'c' => 3],
  226. function($k, $v) { return [strtoupper($k) => $v * $v]; },
  227. ['A' => 1, 'B' => 4, 'C' => 9],
  228. ],
  229. [
  230. ['a' => 1, 'b' => 2, 'c' => 3],
  231. function($k, $v) { return [$k => null]; },
  232. ['a' => null, 'b' => null, 'c' => null],
  233. ],
  234. [
  235. ['a' => 1, 'b' => 2, 'b' => 3],
  236. function($k, $v) { return [$k => $v]; },
  237. ['a' => 1, 'b' => 3],
  238. ],
  239. [
  240. ['a' => 1, 'b' => 2, 'c' => 3],
  241. function($k, $v) { return ($v % 2) ? [$k => $v * $v] : null; },
  242. ['a' => 1, 'c' => 9],
  243. ],
  244. [
  245. ['a' => 1, 'b' => 2, 'c' => 3],
  246. function($k, $v) { return ($v % 2) ? [$k => $v * $v] : []; },
  247. ['a' => 1, 'c' => 9],
  248. ],
  249. [
  250. ['a' => 1, 'b' => 2, 'c' => 3],
  251. function($k, $v) { return [$k => $v, $k.'^2' => $v * $v]; },
  252. ['a' => 1, 'a^2' => 1, 'b' => 2, 'b^2' => 4, 'c' => 3, 'c^2' => 9],
  253. ],
  254. [
  255. ['a' => 1, 'b' => 2, 'c' => 3],
  256. function($k, $v) { return [$k => [[$v]], strtoupper($k) => []]; },
  257. ['a' => [[1]], 'A' => [], 'b' => [[2]], 'B' => [], 'c' => [[3]], 'C' => []],
  258. ],
  259. ];
  260. }
  261. /**
  262. * @dataProvider multimapProvider
  263. */
  264. public function testMultimap($source, $callback, $expected)
  265. {
  266. $this->assertSame($expected, ArrayHelper::multimap($source, $callback));
  267. }
  268. public function multimapUnexpectedValueProvider()
  269. {
  270. return [
  271. [
  272. ['a' => 1, 'b' => 2, 'c' => 3],
  273. function($k, $v) { return $v; },
  274. ],
  275. [
  276. ['a' => 1, 'b' => 2, 'c' => 3],
  277. function($k, $v) { return 'string'; },
  278. ],
  279. ];
  280. }
  281. /**
  282. * @dataProvider multimapUnexpectedValueProvider
  283. * @expectedException \UnexpectedValueException
  284. */
  285. public function testMultimapUnexpectedValue($source, $callback)
  286. {
  287. ArrayHelper::multimap($source, $callback);
  288. }
  289. }