PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/anahkiasen/underscore-php/tests/Types/Arrays.test.php

https://bitbucket.org/larryg/powerhut
PHP | 530 lines | 405 code | 124 blank | 1 comment | 7 complexity | c4d98ec31dd3c791db4684de206fae38 MD5 | raw file
  1. <?php
  2. use Underscore\Types\Arrays;
  3. use Underscore\Underscore;
  4. class ArraysTest extends UnderscoreWrapper
  5. {
  6. // Tests --------------------------------------------------------- /
  7. public function testCanCreateArray()
  8. {
  9. $array = Arrays::create();
  10. $this->assertEquals(array(), $array->obtain());
  11. }
  12. public function testCanUseClassDirectly()
  13. {
  14. $under = Arrays::get($this->array, 'foo');
  15. $this->assertEquals('bar', $under);
  16. }
  17. public function testCanCreateChainableObject()
  18. {
  19. $under = Underscore::from($this->arrayNumbers);
  20. $under = $under->get(1);
  21. $this->assertEquals(2, $under);
  22. }
  23. public function testCanGetKeys()
  24. {
  25. $array = Arrays::keys($this->array);
  26. $this->assertEquals(array('foo', 'bis'), $array);
  27. }
  28. public function testCanGetValues()
  29. {
  30. $array = Arrays::values($this->array);
  31. $this->assertEquals(array('bar', 'ter'), $array);
  32. }
  33. public function testCanSetValues()
  34. {
  35. $array = array('foo' => array('foo' => 'bar'), 'bar' => 'bis');
  36. $array = Arrays::set($array, 'foo.bar.bis', 'ter');
  37. $this->assertEquals('ter', $array['foo']['bar']['bis']);
  38. $this->assertArrayHasKey('bar', $array);
  39. }
  40. public function testCanRemoveValues()
  41. {
  42. $array = Arrays::remove($this->arrayMulti, '0.foo');
  43. $matcher = $this->arrayMulti;
  44. unset($matcher[0]['foo']);
  45. $this->assertEquals($matcher, $array);
  46. }
  47. public function testCanRemoveMultipleValues()
  48. {
  49. $array = Arrays::remove($this->arrayMulti, array('0.foo', '1.foo'));
  50. $matcher = $this->arrayMulti;
  51. unset($matcher[0]['foo']);
  52. unset($matcher[1]['foo']);
  53. $this->assertEquals($matcher, $array);
  54. }
  55. public function testCanReturnAnArrayWithoutSomeValues()
  56. {
  57. $array = array('foo', 'foo', 'bar', 'bis', 'bar', 'bis', 'ter');
  58. $array = Arrays::without($array, 'foo', 'bar');
  59. $this->assertEquals(array(3 => 'bis', 5 => 'bis', 6 => 'ter'), $array);
  60. }
  61. public function testCanGetSumOfArray()
  62. {
  63. $array = Arrays::sum(array(1, 2, 3));
  64. $this->assertEquals(6, $array);
  65. }
  66. public function testCanGetSizeOfArray()
  67. {
  68. $array = Arrays::size(array(1, 2, 3));
  69. $this->assertEquals(3, $array);
  70. }
  71. public function testCanSeeIfArrayContainsValue()
  72. {
  73. $true = Arrays::contains(array(1, 2, 3), 2);
  74. $false = Arrays::contains(array(1, 2, 3), 5);
  75. $this->assertTrue($true);
  76. $this->assertFalse($false);
  77. }
  78. public function testCanCheckIfHasValue()
  79. {
  80. $under = Arrays::has($this->array, 'foo');
  81. $this->assertTrue($under);
  82. }
  83. public function testCanGetValueFromArray()
  84. {
  85. $array = array('foo' => array('bar' => 'bis'));
  86. $under = Arrays::get($array, 'foo.bar');
  87. $this->assertEquals('bis', $under);
  88. }
  89. public function testCantConflictWithNativeFunctions()
  90. {
  91. $array = array('foo' => array('bar' => 'bis'));
  92. $under = Arrays::get($array, 'ter', 'str_replace');
  93. $this->assertEquals('str_replace', $under);
  94. }
  95. public function testCanFallbackClosure()
  96. {
  97. $array = array('foo' => array('bar' => 'bis'));
  98. $under = Arrays::get($array, 'ter', function() {
  99. return 'closure';
  100. });
  101. $this->assertEquals('closure', $under);
  102. }
  103. public function testCanDoSomethingAtEachValue()
  104. {
  105. $closure = function($value, $key) {
  106. echo $key.':'.$value.':';
  107. };
  108. Arrays::at($this->array, $closure);
  109. $result = 'foo:bar:bis:ter:';
  110. $this->expectOutputString($result);
  111. }
  112. public function testCanActOnEachValueFromArray()
  113. {
  114. $closure = function($value, $key) {
  115. return $key.':'.$value;
  116. };
  117. $under = Arrays::each($this->array, $closure);
  118. $result = array('foo' => 'foo:bar', 'bis' => 'bis:ter');
  119. $this->assertEquals($result, $under);
  120. }
  121. public function testCanFindAValueInAnArray()
  122. {
  123. $under = Arrays::find($this->arrayNumbers, function($value) {
  124. return $value % 2 == 0;
  125. });
  126. $this->assertEquals(2, $under);
  127. $unfound = Arrays::find($this->arrayNumbers, function($value) {
  128. return $value == 5;
  129. });
  130. $this->assertEquals($this->arrayNumbers, $unfound);
  131. }
  132. public function testCanFilterValuesFromAnArray()
  133. {
  134. $under = Arrays::filter($this->arrayNumbers, function($value) {
  135. return $value % 2 != 0;
  136. });
  137. $this->assertEquals(array(0 => 1, 2 => 3), $under);
  138. }
  139. public function testCanFilterRejectedValuesFromAnArray()
  140. {
  141. $under = Arrays::reject($this->arrayNumbers, function($value) {
  142. return $value % 2 != 0;
  143. });
  144. $this->assertEquals(array(1 => 2), $under);
  145. }
  146. public function testCanMatchAnArrayContent()
  147. {
  148. $under = Arrays::matches($this->arrayNumbers, function($value) {
  149. return is_int($value);
  150. });
  151. $this->assertTrue($under);
  152. }
  153. public function testCanMatchPathOfAnArrayContent()
  154. {
  155. $under = Arrays::matchesAny($this->arrayNumbers, function($value) {
  156. return $value == 2;
  157. });
  158. $this->assertTrue($under);
  159. }
  160. public function testCanInvokeFunctionsOnValues()
  161. {
  162. $array = array(' foo ', ' bar ');
  163. $array = Arrays::invoke($array, 'trim');
  164. $this->assertEquals(array('foo', 'bar'), $array);
  165. }
  166. public function testCanInvokeFunctionsOnValuesWithSingleArgument()
  167. {
  168. $array = array('_____foo', '____bar ');
  169. $array = Arrays::invoke($array, 'trim', ' _');
  170. $this->assertEquals(array('foo', 'bar'), $array);
  171. }
  172. public function testCanInvokeFunctionsWithDifferentArguments()
  173. {
  174. $array = array('_____foo ', '__bar ');
  175. $array = Arrays::invoke($array, 'trim', array('_', ' '));
  176. $this->assertEquals(array('foo ', '__bar'), $array);
  177. }
  178. public function testCanPluckColumns()
  179. {
  180. $under = Arrays::pluck($this->arrayMulti, 'foo');
  181. $matcher = array('bar', 'bar', null);
  182. $this->assertEquals($matcher, $under);
  183. }
  184. public function testCanCalculateAverageValue()
  185. {
  186. $average1 = array(5, 10, 15, 20);
  187. $average2 = array('foo', 'b', 'ar');
  188. $average3 = array(array('lol'), 10, 20);
  189. $average1 = Arrays::average($average1);
  190. $average2 = Arrays::average($average2);
  191. $average3 = Arrays::average($average3);
  192. $this->assertEquals(13, $average1);
  193. $this->assertEquals(0, $average2);
  194. $this->assertEquals(10, $average3);
  195. }
  196. public function testCanGetFirstValue()
  197. {
  198. $under1 = Arrays::first($this->array);
  199. $under2 = Arrays::first($this->arrayNumbers, 2);
  200. $this->assertEquals('bar', $under1);
  201. $this->assertEquals(array(1, 2), $under2);
  202. }
  203. public function testCanGetLastValue()
  204. {
  205. $under = Arrays::last($this->array);
  206. $this->assertEquals('ter', $under);
  207. }
  208. public function testCanGetLastElements()
  209. {
  210. $under = Arrays::last($this->arrayNumbers, 2);
  211. $this->assertEquals(array(2, 3), $under);
  212. }
  213. public function testCanXInitialElements()
  214. {
  215. $under = Arrays::initial($this->arrayNumbers, 1);
  216. $this->assertEquals(array(1, 2), $under);
  217. }
  218. public function testCanGetRestFromArray()
  219. {
  220. $under = Arrays::rest($this->arrayNumbers, 1);
  221. $this->assertEquals(array(2, 3), $under);
  222. }
  223. public function testCanCleanArray()
  224. {
  225. $array = array(false, true, 0, 1, 'full', '');
  226. $array = Arrays::clean($array);
  227. $this->assertEquals(array(1 => true, 3 => 1, 4 => 'full'), $array);
  228. }
  229. public function testCanGetMaxValueFromAnArray()
  230. {
  231. $under = Arrays::max($this->arrayNumbers);
  232. $this->assertEquals(3, $under);
  233. }
  234. public function testCanGetMaxValueFromAnArrayWithClosure()
  235. {
  236. $under = Arrays::max($this->arrayNumbers, function($value) {
  237. return $value * -1;
  238. });
  239. $this->assertEquals(-1, $under);
  240. }
  241. public function testCanGetMinValueFromAnArray()
  242. {
  243. $under = Arrays::min($this->arrayNumbers);
  244. $this->assertEquals(1, $under);
  245. }
  246. public function testCanGetMinValueFromAnArrayWithClosure()
  247. {
  248. $under = Arrays::min($this->arrayNumbers, function($value) {
  249. return $value * -1;
  250. });
  251. $this->assertEquals(-3, $under);
  252. }
  253. public function testCanSortKeys()
  254. {
  255. $under = Arrays::sortKeys(array('z' => 0, 'b' => 1, 'r' => 2));
  256. $this->assertEquals(array('b' => 1, 'r' => 2, 'z' => 0), $under);
  257. $under = Arrays::sortKeys(array('z' => 0, 'b' => 1, 'r' => 2), 'desc');
  258. $this->assertEquals(array('z' => 0, 'r' => 2, 'b' => 1), $under);
  259. }
  260. public function testCanSortValues()
  261. {
  262. $under = Arrays::sort(array(5, 3, 1, 2, 4), null, 'desc');
  263. $this->assertEquals(array(5, 4, 3, 2, 1), $under);
  264. $under = Arrays::sort(range(1, 5), function($value) {
  265. return $value % 2 == 0;
  266. });
  267. $this->assertEquals(array(1, 3, 5, 2, 4), $under);
  268. }
  269. public function testCanGroupValues()
  270. {
  271. $under = Arrays::group(range(1, 5), function($value) {
  272. return $value % 2 == 0;
  273. });
  274. $matcher = array(
  275. array(1, 3, 5), array(2, 4),
  276. );
  277. $this->assertEquals($matcher, $under);
  278. }
  279. public function testCanCreateFromRange()
  280. {
  281. $range = Arrays::range(5);
  282. $this->assertEquals(array(1, 2, 3, 4, 5), $range);
  283. $range = Arrays::range(-2, 2);
  284. $this->assertEquals(array(-2, -1, 0, 1, 2), $range);
  285. $range = Arrays::range(1, 10, 2);
  286. $this->assertEquals(array(1, 3, 5, 7, 9), $range);
  287. }
  288. public function testCantChainRange()
  289. {
  290. $this->setExpectedException('Exception');
  291. Arrays::from($this->arrayNumbers)->range(5);
  292. }
  293. public function testCanCreateFromRepeat()
  294. {
  295. $repeat = Arrays::repeat('foo', 3);
  296. $this->assertEquals(array('foo', 'foo', 'foo'), $repeat);
  297. }
  298. public function testCanMergeArrays()
  299. {
  300. $array = Arrays::merge($this->array, array('foo' => 3), array('kal' => 'mon'));
  301. $this->assertEquals(array('foo' => 3, 'bis' => 'ter', 'kal' => 'mon'), $array);
  302. }
  303. public function testCanGetRandomValue()
  304. {
  305. $array = Arrays::random($this->array);
  306. $this->assertContains($array, $this->array);
  307. }
  308. public function testCanGetSeveralRandomValue()
  309. {
  310. $array = Arrays::random($this->arrayNumbers, 2);
  311. foreach ($array as $a) $this->assertContains($a, $this->arrayNumbers);
  312. }
  313. public function testCanSearchForAValue()
  314. {
  315. $array = Arrays::search($this->array, 'ter');
  316. $this->assertEquals('bis', $array);
  317. }
  318. public function testCanDiffBetweenArrays()
  319. {
  320. $array = Arrays::diff($this->array, array('foo' => 'bar', 'ter' => 'kal'));
  321. $chain = Arrays::from($this->array)->diff(array('foo' => 'bar', 'ter' => 'kal'));
  322. $this->assertEquals(array('bis' => 'ter'), $array);
  323. $this->assertEquals(array('bis' => 'ter'), $chain->obtain());
  324. }
  325. public function testCanRemoveFirstValueFromAnArray()
  326. {
  327. $array = Arrays::removeFirst($this->array);
  328. $this->assertEquals(array('bis' => 'ter'), $array);
  329. }
  330. public function testCanRemoveLasttValueFromAnArray()
  331. {
  332. $array = Arrays::removeLast($this->array);
  333. $this->assertEquals(array('foo' => 'bar'), $array);
  334. }
  335. public function testCanImplodeAnArray()
  336. {
  337. $array = Arrays::implode($this->array, ',');
  338. $this->assertEquals('bar,ter', $array);
  339. }
  340. public function testCanFlattenArraysToDotNotation()
  341. {
  342. $array = array(
  343. 'foo' => 'bar',
  344. 'kal' => array(
  345. 'foo' => array(
  346. 'bar', 'ter',
  347. ),
  348. ),
  349. );
  350. $flattened = array(
  351. 'foo' => 'bar',
  352. 'kal.foo.0' => 'bar',
  353. 'kal.foo.1' => 'ter',
  354. );
  355. $flatten = Arrays::flatten($array);
  356. $this->assertEquals($flatten, $flattened);
  357. }
  358. public function testCanFlattenArraysToCustomNotation()
  359. {
  360. $array = array(
  361. 'foo' => 'bar',
  362. 'kal' => array(
  363. 'foo' => array(
  364. 'bar', 'ter',
  365. ),
  366. ),
  367. );
  368. $flattened = array(
  369. 'foo' => 'bar',
  370. 'kal/foo/0' => 'bar',
  371. 'kal/foo/1' => 'ter',
  372. );
  373. $flatten = Arrays::flatten($array, '/');
  374. $this->assertEquals($flatten, $flattened);
  375. }
  376. public function testCanReplaceValues()
  377. {
  378. $array = Arrays::replace($this->array, 'foo', 'notfoo', 'notbar');
  379. $matcher = array('notfoo' => 'notbar', 'bis' => 'ter');
  380. $this->assertEquals($matcher, $array);
  381. }
  382. public function testCanPrependValuesToArrays()
  383. {
  384. $array = Arrays::prepend($this->array, 'foo');
  385. $matcher = array(0 => 'foo', 'foo' => 'bar', 'bis' => 'ter');
  386. $this->assertEquals($matcher, $array);
  387. }
  388. public function testCanAppendValuesToArrays()
  389. {
  390. $array = Arrays::append($this->array, 'foo');
  391. $matcher = array('foo' => 'bar', 'bis' => 'ter', 0 => 'foo');
  392. $this->assertEquals($matcher, $array);
  393. }
  394. public function testCanReplaceValuesInArrays()
  395. {
  396. $array = $this->array;
  397. $array = Arrays::replaceValue($array, 'bar', 'replaced');
  398. $this->assertEquals('replaced', $array['foo']);
  399. }
  400. public function testCanReplaceKeysInArray()
  401. {
  402. $array = $this->array;
  403. $array = Arrays::replaceKeys($array, array('bar', 'ter'));
  404. $this->assertEquals(array('bar' => 'bar', 'ter' => 'ter'), $array);
  405. }
  406. }