CollectionTest.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Collection;
  16. use ArrayIterator;
  17. use ArrayObject;
  18. use Cake\Collection\Collection;
  19. use Cake\TestSuite\TestCase;
  20. use NoRewindIterator;
  21. /**
  22. * CollectionTest
  23. *
  24. */
  25. class CollectionTest extends TestCase
  26. {
  27. /**
  28. * Tests that it is possible to convert an array into a collection
  29. *
  30. * @return void
  31. */
  32. public function testArrayIsWrapped()
  33. {
  34. $items = [1, 2, 3];
  35. $collection = new Collection($items);
  36. $this->assertEquals($items, iterator_to_array($collection));
  37. }
  38. /**
  39. * Tests that it is possible to convert an iterator into a collection
  40. *
  41. * @return void
  42. */
  43. public function testIteratorIsWrapped()
  44. {
  45. $items = new \ArrayObject([1, 2, 3]);
  46. $collection = new Collection($items);
  47. $this->assertEquals(iterator_to_array($items), iterator_to_array($collection));
  48. }
  49. /**
  50. * Test running a method over all elements in the collection
  51. *
  52. * @return void
  53. */
  54. public function testEeach()
  55. {
  56. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  57. $collection = new Collection($items);
  58. $callable = $this->getMock('stdClass', ['__invoke']);
  59. $callable->expects($this->at(0))
  60. ->method('__invoke')
  61. ->with(1, 'a');
  62. $callable->expects($this->at(1))
  63. ->method('__invoke')
  64. ->with(2, 'b');
  65. $callable->expects($this->at(2))
  66. ->method('__invoke')
  67. ->with(3, 'c');
  68. $collection->each($callable);
  69. }
  70. /**
  71. * Test filter() with no callback.
  72. *
  73. * @return void
  74. */
  75. public function testFilterNoCallback()
  76. {
  77. $items = [1, 2, 0, 3, false, 4, null, 5, ''];
  78. $collection = new Collection($items);
  79. $result = $collection->filter()->toArray();
  80. $expected = [1, 2, 3, 4, 5];
  81. $this->assertEquals($expected, array_values($result));
  82. }
  83. /**
  84. * Tests that it is possible to chain filter() as it returns a collection object
  85. *
  86. * @return void
  87. */
  88. public function testFilterChaining()
  89. {
  90. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  91. $collection = new Collection($items);
  92. $callable = $this->getMock('stdClass', ['__invoke']);
  93. $callable->expects($this->once())
  94. ->method('__invoke')
  95. ->with(3, 'c');
  96. $filtered = $collection->filter(function ($value, $key, $iterator) {
  97. return $value > 2;
  98. });
  99. $this->assertInstanceOf('Cake\Collection\Collection', $filtered);
  100. $filtered->each($callable);
  101. }
  102. /**
  103. * Tests reject
  104. *
  105. * @return void
  106. */
  107. public function testReject()
  108. {
  109. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  110. $collection = new Collection($items);
  111. $result = $collection->reject(function ($v, $k, $items) use ($collection) {
  112. $this->assertSame($collection->getInnerIterator(), $items);
  113. return $v > 2;
  114. });
  115. $this->assertEquals(['a' => 1, 'b' => 2], iterator_to_array($result));
  116. $this->assertInstanceOf('Cake\Collection\Collection', $result);
  117. }
  118. /**
  119. * Tests every when the callback returns true for all elements
  120. *
  121. * @return void
  122. */
  123. public function testEveryReturnTrue()
  124. {
  125. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  126. $collection = new Collection($items);
  127. $callable = $this->getMock('stdClass', ['__invoke']);
  128. $callable->expects($this->at(0))
  129. ->method('__invoke')
  130. ->with(1, 'a')
  131. ->will($this->returnValue(true));
  132. $callable->expects($this->at(1))
  133. ->method('__invoke')
  134. ->with(2, 'b')
  135. ->will($this->returnValue(true));
  136. $callable->expects($this->at(2))
  137. ->method('__invoke')
  138. ->with(3, 'c')
  139. ->will($this->returnValue(true));
  140. $this->assertTrue($collection->every($callable));
  141. }
  142. /**
  143. * Tests every when the callback returns false for one of the elements
  144. *
  145. * @return void
  146. */
  147. public function testEveryReturnFalse()
  148. {
  149. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  150. $collection = new Collection($items);
  151. $callable = $this->getMock('stdClass', ['__invoke']);
  152. $callable->expects($this->at(0))
  153. ->method('__invoke')
  154. ->with(1, 'a')
  155. ->will($this->returnValue(true));
  156. $callable->expects($this->at(1))
  157. ->method('__invoke')
  158. ->with(2, 'b')
  159. ->will($this->returnValue(false));
  160. $callable->expects($this->exactly(2))->method('__invoke');
  161. $this->assertFalse($collection->every($callable));
  162. }
  163. /**
  164. * Tests some() when one of the calls return true
  165. *
  166. * @return void
  167. */
  168. public function testSomeReturnTrue()
  169. {
  170. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  171. $collection = new Collection($items);
  172. $callable = $this->getMock('stdClass', ['__invoke']);
  173. $callable->expects($this->at(0))
  174. ->method('__invoke')
  175. ->with(1, 'a')
  176. ->will($this->returnValue(false));
  177. $callable->expects($this->at(1))
  178. ->method('__invoke')
  179. ->with(2, 'b')
  180. ->will($this->returnValue(true));
  181. $callable->expects($this->exactly(2))->method('__invoke');
  182. $this->assertTrue($collection->some($callable));
  183. }
  184. /**
  185. * Tests some() when none of the calls return true
  186. *
  187. * @return void
  188. */
  189. public function testSomeReturnFalse()
  190. {
  191. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  192. $collection = new Collection($items);
  193. $callable = $this->getMock('stdClass', ['__invoke']);
  194. $callable->expects($this->at(0))
  195. ->method('__invoke')
  196. ->with(1, 'a')
  197. ->will($this->returnValue(false));
  198. $callable->expects($this->at(1))
  199. ->method('__invoke')
  200. ->with(2, 'b')
  201. ->will($this->returnValue(false));
  202. $callable->expects($this->at(2))
  203. ->method('__invoke')
  204. ->with(3, 'c')
  205. ->will($this->returnValue(false));
  206. $this->assertFalse($collection->some($callable));
  207. }
  208. /**
  209. * Tests contains
  210. *
  211. * @return void
  212. */
  213. public function testContains()
  214. {
  215. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  216. $collection = new Collection($items);
  217. $this->assertTrue($collection->contains(2));
  218. $this->assertTrue($collection->contains(1));
  219. $this->assertFalse($collection->contains(10));
  220. $this->assertFalse($collection->contains('2'));
  221. }
  222. /**
  223. * Tests map
  224. *
  225. * @return void
  226. */
  227. public function testMap()
  228. {
  229. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  230. $collection = new Collection($items);
  231. $map = $collection->map(function ($v, $k, $it) use ($collection) {
  232. $this->assertSame($collection->getInnerIterator(), $it);
  233. return $v * $v;
  234. });
  235. $this->assertInstanceOf('Cake\Collection\Iterator\ReplaceIterator', $map);
  236. $this->assertEquals(['a' => 1, 'b' => 4, 'c' => 9], iterator_to_array($map));
  237. }
  238. /**
  239. * Tests reduce with initial value
  240. *
  241. * @return void
  242. */
  243. public function testReduceWithInitialValue()
  244. {
  245. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  246. $collection = new Collection($items);
  247. $callable = $this->getMock('stdClass', ['__invoke']);
  248. $callable->expects($this->at(0))
  249. ->method('__invoke')
  250. ->with(10, 1, 'a')
  251. ->will($this->returnValue(11));
  252. $callable->expects($this->at(1))
  253. ->method('__invoke')
  254. ->with(11, 2, 'b')
  255. ->will($this->returnValue(13));
  256. $callable->expects($this->at(2))
  257. ->method('__invoke')
  258. ->with(13, 3, 'c')
  259. ->will($this->returnValue(16));
  260. $this->assertEquals(16, $collection->reduce($callable, 10));
  261. }
  262. /**
  263. * Tests reduce without initial value
  264. *
  265. * @return void
  266. */
  267. public function testReduceWithoutInitialValue()
  268. {
  269. $items = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
  270. $collection = new Collection($items);
  271. $callable = $this->getMock('stdClass', ['__invoke']);
  272. $callable->expects($this->at(0))
  273. ->method('__invoke')
  274. ->with(1, 2, 'b')
  275. ->will($this->returnValue(3));
  276. $callable->expects($this->at(1))
  277. ->method('__invoke')
  278. ->with(3, 3, 'c')
  279. ->will($this->returnValue(6));
  280. $callable->expects($this->at(2))
  281. ->method('__invoke')
  282. ->with(6, 4, 'd')
  283. ->will($this->returnValue(10));
  284. $this->assertEquals(10, $collection->reduce($callable));
  285. }
  286. /**
  287. * Tests extract
  288. *
  289. * @return void
  290. */
  291. public function testExtract()
  292. {
  293. $items = [['a' => ['b' => ['c' => 1]]], 2];
  294. $collection = new Collection($items);
  295. $map = $collection->extract('a.b.c');
  296. $this->assertInstanceOf('Cake\Collection\Iterator\ExtractIterator', $map);
  297. $this->assertEquals([1, null], iterator_to_array($map));
  298. }
  299. /**
  300. * Tests sort
  301. *
  302. * @return void
  303. */
  304. public function testSortString()
  305. {
  306. $items = [
  307. ['a' => ['b' => ['c' => 4]]],
  308. ['a' => ['b' => ['c' => 10]]],
  309. ['a' => ['b' => ['c' => 6]]]
  310. ];
  311. $collection = new Collection($items);
  312. $map = $collection->sortBy('a.b.c');
  313. $this->assertInstanceOf('Cake\Collection\Collection', $map);
  314. $expected = [
  315. ['a' => ['b' => ['c' => 10]]],
  316. ['a' => ['b' => ['c' => 6]]],
  317. ['a' => ['b' => ['c' => 4]]],
  318. ];
  319. $this->assertEquals($expected, $map->toList());
  320. }
  321. /**
  322. * Tests max
  323. *
  324. * @return void
  325. */
  326. public function testMax()
  327. {
  328. $items = [
  329. ['a' => ['b' => ['c' => 4]]],
  330. ['a' => ['b' => ['c' => 10]]],
  331. ['a' => ['b' => ['c' => 6]]]
  332. ];
  333. $collection = new Collection($items);
  334. $this->assertEquals(['a' => ['b' => ['c' => 10]]], $collection->max('a.b.c'));
  335. $callback = function ($e) {
  336. return $e['a']['b']['c'] * - 1;
  337. };
  338. $this->assertEquals(['a' => ['b' => ['c' => 4]]], $collection->max($callback));
  339. }
  340. /**
  341. * Tests min
  342. *
  343. * @return void
  344. */
  345. public function testMin()
  346. {
  347. $items = [
  348. ['a' => ['b' => ['c' => 4]]],
  349. ['a' => ['b' => ['c' => 10]]],
  350. ['a' => ['b' => ['c' => 6]]]
  351. ];
  352. $collection = new Collection($items);
  353. $this->assertEquals(['a' => ['b' => ['c' => 4]]], $collection->min('a.b.c'));
  354. }
  355. /**
  356. * Tests groupBy
  357. *
  358. * @return void
  359. */
  360. public function testGroupBy()
  361. {
  362. $items = [
  363. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  364. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  365. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  366. ];
  367. $collection = new Collection($items);
  368. $grouped = $collection->groupBy('parent_id');
  369. $expected = [
  370. 10 => [
  371. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  372. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  373. ],
  374. 11 => [
  375. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  376. ]
  377. ];
  378. $this->assertEquals($expected, iterator_to_array($grouped));
  379. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  380. $grouped = $collection->groupBy(function ($element) {
  381. return $element['parent_id'];
  382. });
  383. $this->assertEquals($expected, iterator_to_array($grouped));
  384. }
  385. /**
  386. * Tests grouping by a deep key
  387. *
  388. * @return void
  389. */
  390. public function testGroupByDeepKey()
  391. {
  392. $items = [
  393. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  394. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  395. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  396. ];
  397. $collection = new Collection($items);
  398. $grouped = $collection->groupBy('thing.parent_id');
  399. $expected = [
  400. 10 => [
  401. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  402. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  403. ],
  404. 11 => [
  405. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  406. ]
  407. ];
  408. $this->assertEquals($expected, iterator_to_array($grouped));
  409. }
  410. /**
  411. * Tests indexBy
  412. *
  413. * @return void
  414. */
  415. public function testIndexBy()
  416. {
  417. $items = [
  418. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  419. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  420. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  421. ];
  422. $collection = new Collection($items);
  423. $grouped = $collection->indexBy('id');
  424. $expected = [
  425. 1 => ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  426. 3 => ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  427. 2 => ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  428. ];
  429. $this->assertEquals($expected, iterator_to_array($grouped));
  430. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  431. $grouped = $collection->indexBy(function ($element) {
  432. return $element['id'];
  433. });
  434. $this->assertEquals($expected, iterator_to_array($grouped));
  435. }
  436. /**
  437. * Tests indexBy with a deep property
  438. *
  439. * @return void
  440. */
  441. public function testIndexByDeep()
  442. {
  443. $items = [
  444. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  445. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  446. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  447. ];
  448. $collection = new Collection($items);
  449. $grouped = $collection->indexBy('thing.parent_id');
  450. $expected = [
  451. 10 => ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  452. 11 => ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  453. ];
  454. $this->assertEquals($expected, iterator_to_array($grouped));
  455. }
  456. /**
  457. * Tests countBy
  458. *
  459. * @return void
  460. */
  461. public function testCountBy()
  462. {
  463. $items = [
  464. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  465. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  466. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  467. ];
  468. $collection = new Collection($items);
  469. $grouped = $collection->countBy('parent_id');
  470. $expected = [
  471. 10 => 2,
  472. 11 => 1
  473. ];
  474. $this->assertEquals($expected, iterator_to_array($grouped));
  475. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  476. $grouped = $collection->countBy(function ($element) {
  477. return $element['parent_id'];
  478. });
  479. $this->assertEquals($expected, iterator_to_array($grouped));
  480. }
  481. /**
  482. * Tests shuffle
  483. *
  484. * @return void
  485. */
  486. public function testShuffle()
  487. {
  488. $data = [1, 2, 3, 4];
  489. $collection = (new Collection($data))->shuffle();
  490. $this->assertEquals(count($data), count(iterator_to_array($collection)));
  491. foreach ($collection as $value) {
  492. $this->assertContains($value, $data);
  493. }
  494. }
  495. /**
  496. * Tests sample
  497. *
  498. * @return void
  499. */
  500. public function testSample()
  501. {
  502. $data = [1, 2, 3, 4];
  503. $collection = (new Collection($data))->sample(2);
  504. $this->assertEquals(2, count(iterator_to_array($collection)));
  505. foreach ($collection as $value) {
  506. $this->assertContains($value, $data);
  507. }
  508. }
  509. /**
  510. * Test toArray method
  511. *
  512. * @return void
  513. */
  514. public function testToArray()
  515. {
  516. $data = [1, 2, 3, 4];
  517. $collection = new Collection($data);
  518. $this->assertEquals($data, $collection->toArray());
  519. }
  520. /**
  521. * Test toList method
  522. *
  523. * @return void
  524. */
  525. public function testToList()
  526. {
  527. $data = [100 => 1, 300 => 2, 500 => 3, 1 => 4];
  528. $collection = new Collection($data);
  529. $this->assertEquals(array_values($data), $collection->toList());
  530. }
  531. /**
  532. * Test json encoding
  533. *
  534. * @return void
  535. */
  536. public function testToJson()
  537. {
  538. $data = [1, 2, 3, 4];
  539. $collection = new Collection($data);
  540. $this->assertEquals(json_encode($data), json_encode($collection));
  541. }
  542. /**
  543. * Tests that only arrays and Traversables are allowed in the constructor
  544. *
  545. * @expectedException \InvalidArgumentException
  546. * @expectedExceptionMessage Only an array or \Traversable is allowed for Collection
  547. * @return void
  548. */
  549. public function testInvalidConstructorArgument()
  550. {
  551. new Collection('Derp');
  552. }
  553. /**
  554. * Tests take method
  555. *
  556. * @return void
  557. */
  558. public function testTake()
  559. {
  560. $data = [1, 2, 3, 4];
  561. $collection = new Collection($data);
  562. $taken = $collection->take(2);
  563. $this->assertEquals([1, 2], $taken->toArray());
  564. $taken = $collection->take(3);
  565. $this->assertEquals([1, 2, 3], $taken->toArray());
  566. $taken = $collection->take(500);
  567. $this->assertEquals([1, 2, 3, 4], $taken->toArray());
  568. $taken = $collection->take(1);
  569. $this->assertEquals([1], $taken->toArray());
  570. $taken = $collection->take();
  571. $this->assertEquals([1], $taken->toArray());
  572. $taken = $collection->take(2, 2);
  573. $this->assertEquals([2 => 3, 3 => 4], $taken->toArray());
  574. }
  575. /**
  576. * Tests match
  577. *
  578. * @return void
  579. */
  580. public function testMatch()
  581. {
  582. $items = [
  583. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  584. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  585. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  586. ];
  587. $collection = new Collection($items);
  588. $matched = $collection->match(['thing.parent_id' => 10, 'name' => 'baz']);
  589. $this->assertEquals([2 => $items[2]], $matched->toArray());
  590. $matched = $collection->match(['thing.parent_id' => 10]);
  591. $this->assertEquals(
  592. [0 => $items[0], 2 => $items[2]],
  593. $matched->toArray()
  594. );
  595. $matched = $collection->match(['thing.parent_id' => 500]);
  596. $this->assertEquals([], $matched->toArray());
  597. $matched = $collection->match(['parent_id' => 10, 'name' => 'baz']);
  598. $this->assertEquals([], $matched->toArray());
  599. }
  600. /**
  601. * Tests firstMatch
  602. *
  603. * @return void
  604. */
  605. public function testFirstMatch()
  606. {
  607. $items = [
  608. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  609. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  610. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  611. ];
  612. $collection = new Collection($items);
  613. $matched = $collection->firstMatch(['thing.parent_id' => 10]);
  614. $this->assertEquals(
  615. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  616. $matched
  617. );
  618. $matched = $collection->firstMatch(['thing.parent_id' => 10, 'name' => 'baz']);
  619. $this->assertEquals(
  620. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  621. $matched
  622. );
  623. }
  624. /**
  625. * Tests the append method
  626. *
  627. * @return void
  628. */
  629. public function testAppend()
  630. {
  631. $collection = new Collection([1, 2, 3]);
  632. $combined = $collection->append([4, 5, 6]);
  633. $this->assertEquals([1, 2, 3, 4, 5, 6], $combined->toArray(false));
  634. $collection = new Collection(['a' => 1, 'b' => 2]);
  635. $combined = $collection->append(['c' => 3, 'a' => 4]);
  636. $this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray());
  637. }
  638. /**
  639. * Tests that by calling compile internal iteration operations are not done
  640. * more than once
  641. *
  642. * @return void
  643. */
  644. public function testCompile()
  645. {
  646. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  647. $collection = new Collection($items);
  648. $callable = $this->getMock('stdClass', ['__invoke']);
  649. $callable->expects($this->at(0))
  650. ->method('__invoke')
  651. ->with(1, 'a')
  652. ->will($this->returnValue(4));
  653. $callable->expects($this->at(1))
  654. ->method('__invoke')
  655. ->with(2, 'b')
  656. ->will($this->returnValue(5));
  657. $callable->expects($this->at(2))
  658. ->method('__invoke')
  659. ->with(3, 'c')
  660. ->will($this->returnValue(6));
  661. $compiled = $collection->map($callable)->compile();
  662. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
  663. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
  664. }
  665. /**
  666. * Tests converting a non rewindable iterator into a rewindable one using
  667. * the buffered method.
  668. *
  669. * @return void
  670. */
  671. public function testBuffered()
  672. {
  673. $items = new NoRewindIterator(new ArrayIterator(['a' => 4, 'b' => 5, 'c' => 6]));
  674. $buffered = (new Collection($items))->buffered();
  675. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
  676. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
  677. }
  678. /**
  679. * Tests the combine method
  680. *
  681. * @return void
  682. */
  683. public function testCombine()
  684. {
  685. $items = [
  686. ['id' => 1, 'name' => 'foo', 'parent' => 'a'],
  687. ['id' => 2, 'name' => 'bar', 'parent' => 'b'],
  688. ['id' => 3, 'name' => 'baz', 'parent' => 'a']
  689. ];
  690. $collection = (new Collection($items))->combine('id', 'name');
  691. $expected = [1 => 'foo', 2 => 'bar', 3 => 'baz'];
  692. $this->assertEquals($expected, $collection->toArray());
  693. $expected = ['foo' => 1, 'bar' => 2, 'baz' => 3];
  694. $collection = (new Collection($items))->combine('name', 'id');
  695. $this->assertEquals($expected, $collection->toArray());
  696. $collection = (new Collection($items))->combine('id', 'name', 'parent');
  697. $expected = ['a' => [1 => 'foo', 3 => 'baz'], 'b' => [2 => 'bar']];
  698. $this->assertEquals($expected, $collection->toArray());
  699. $expected = [
  700. '0-1' => ['foo-0-1' => '0-1-foo'],
  701. '1-2' => ['bar-1-2' => '1-2-bar'],
  702. '2-3' => ['baz-2-3' => '2-3-baz']
  703. ];
  704. $collection = (new Collection($items))->combine(
  705. function ($value, $key) {
  706. return $value['name'] . '-' . $key;
  707. },
  708. function ($value, $key) {
  709. return $key . '-' . $value['name'];
  710. },
  711. function ($value, $key) {
  712. return $key . '-' . $value['id'];
  713. }
  714. );
  715. $this->assertEquals($expected, $collection->toArray());
  716. $collection = (new Collection($items))->combine('id', 'crazy');
  717. $this->assertEquals([1 => null, 2 => null, 3 => null], $collection->toArray());
  718. }
  719. /**
  720. * Tests the nest method with only one level
  721. *
  722. * @return void
  723. */
  724. public function testNest()
  725. {
  726. $items = [
  727. ['id' => 1, 'parent_id' => null],
  728. ['id' => 2, 'parent_id' => 1],
  729. ['id' => 3, 'parent_id' => 1],
  730. ['id' => 4, 'parent_id' => 1],
  731. ['id' => 5, 'parent_id' => 6],
  732. ['id' => 6, 'parent_id' => null],
  733. ['id' => 7, 'parent_id' => 1],
  734. ['id' => 8, 'parent_id' => 6],
  735. ['id' => 9, 'parent_id' => 6],
  736. ['id' => 10, 'parent_id' => 6]
  737. ];
  738. $collection = (new Collection($items))->nest('id', 'parent_id');
  739. $expected = [
  740. [
  741. 'id' => 1,
  742. 'parent_id' => null,
  743. 'children' => [
  744. ['id' => 2, 'parent_id' => 1, 'children' => []],
  745. ['id' => 3, 'parent_id' => 1, 'children' => []],
  746. ['id' => 4, 'parent_id' => 1, 'children' => []],
  747. ['id' => 7, 'parent_id' => 1, 'children' => []]
  748. ]
  749. ],
  750. [
  751. 'id' => 6,
  752. 'parent_id' => null,
  753. 'children' => [
  754. ['id' => 5, 'parent_id' => 6, 'children' => []],
  755. ['id' => 8, 'parent_id' => 6, 'children' => []],
  756. ['id' => 9, 'parent_id' => 6, 'children' => []],
  757. ['id' => 10, 'parent_id' => 6, 'children' => []]
  758. ]
  759. ]
  760. ];
  761. $this->assertEquals($expected, $collection->toArray());
  762. }
  763. /**
  764. * Tests the nest method with more than one level
  765. *
  766. * @return void
  767. */
  768. public function testNestMultiLevel()
  769. {
  770. $items = [
  771. ['id' => 1, 'parent_id' => null],
  772. ['id' => 2, 'parent_id' => 1],
  773. ['id' => 3, 'parent_id' => 2],
  774. ['id' => 4, 'parent_id' => 2],
  775. ['id' => 5, 'parent_id' => 3],
  776. ['id' => 6, 'parent_id' => null],
  777. ['id' => 7, 'parent_id' => 3],
  778. ['id' => 8, 'parent_id' => 4],
  779. ['id' => 9, 'parent_id' => 6],
  780. ['id' => 10, 'parent_id' => 6]
  781. ];
  782. $collection = (new Collection($items))->nest('id', 'parent_id');
  783. $expected = [
  784. [
  785. 'id' => 1,
  786. 'parent_id' => null,
  787. 'children' => [
  788. [
  789. 'id' => 2,
  790. 'parent_id' => 1,
  791. 'children' => [
  792. [
  793. 'id' => 3,
  794. 'parent_id' => 2,
  795. 'children' => [
  796. ['id' => 5, 'parent_id' => 3, 'children' => []],
  797. ['id' => 7, 'parent_id' => 3, 'children' => []]
  798. ]
  799. ],
  800. [
  801. 'id' => 4,
  802. 'parent_id' => 2,
  803. 'children' => [
  804. ['id' => 8, 'parent_id' => 4, 'children' => []]
  805. ]
  806. ]
  807. ]
  808. ]
  809. ]
  810. ],
  811. [
  812. 'id' => 6,
  813. 'parent_id' => null,
  814. 'children' => [
  815. ['id' => 9, 'parent_id' => 6, 'children' => []],
  816. ['id' => 10, 'parent_id' => 6, 'children' => []]
  817. ]
  818. ]
  819. ];
  820. $this->assertEquals($expected, $collection->toArray());
  821. }
  822. /**
  823. * Tests the nest method with more than one level
  824. *
  825. * @return void
  826. */
  827. public function testNestObjects()
  828. {
  829. $items = [
  830. new ArrayObject(['id' => 1, 'parent_id' => null]),
  831. new ArrayObject(['id' => 2, 'parent_id' => 1]),
  832. new ArrayObject(['id' => 3, 'parent_id' => 2]),
  833. new ArrayObject(['id' => 4, 'parent_id' => 2]),
  834. new ArrayObject(['id' => 5, 'parent_id' => 3]),
  835. new ArrayObject(['id' => 6, 'parent_id' => null]),
  836. new ArrayObject(['id' => 7, 'parent_id' => 3]),
  837. new ArrayObject(['id' => 8, 'parent_id' => 4]),
  838. new ArrayObject(['id' => 9, 'parent_id' => 6]),
  839. new ArrayObject(['id' => 10, 'parent_id' => 6])
  840. ];
  841. $collection = (new Collection($items))->nest('id', 'parent_id');
  842. $expected = [
  843. new ArrayObject([
  844. 'id' => 1,
  845. 'parent_id' => null,
  846. 'children' => [
  847. new ArrayObject([
  848. 'id' => 2,
  849. 'parent_id' => 1,
  850. 'children' => [
  851. new ArrayObject([
  852. 'id' => 3,
  853. 'parent_id' => 2,
  854. 'children' => [
  855. new ArrayObject(['id' => 5, 'parent_id' => 3, 'children' => []]),
  856. new ArrayObject(['id' => 7, 'parent_id' => 3, 'children' => []])
  857. ]
  858. ]),
  859. new ArrayObject([
  860. 'id' => 4,
  861. 'parent_id' => 2,
  862. 'children' => [
  863. new ArrayObject(['id' => 8, 'parent_id' => 4, 'children' => []])
  864. ]
  865. ])
  866. ]
  867. ])
  868. ]
  869. ]),
  870. new ArrayObject([
  871. 'id' => 6,
  872. 'parent_id' => null,
  873. 'children' => [
  874. new ArrayObject(['id' => 9, 'parent_id' => 6, 'children' => []]),
  875. new ArrayObject(['id' => 10, 'parent_id' => 6, 'children' => []])
  876. ]
  877. ])
  878. ];
  879. $this->assertEquals($expected, $collection->toArray());
  880. }
  881. /**
  882. * Tests insert
  883. *
  884. * @return void
  885. */
  886. public function testInsert()
  887. {
  888. $items = [['a' => 1], ['b' => 2]];
  889. $collection = new Collection($items);
  890. $iterator = $collection->insert('c', [3, 4]);
  891. $this->assertInstanceOf('Cake\Collection\Iterator\InsertIterator', $iterator);
  892. $this->assertEquals(
  893. [['a' => 1, 'c' => 3], ['b' => 2, 'c' => 4]],
  894. iterator_to_array($iterator)
  895. );
  896. }
  897. /**
  898. * Provider for testing each of the directions for listNested
  899. *
  900. * @return void
  901. */
  902. public function nestedListProvider()
  903. {
  904. return [
  905. ['desc', [1, 2, 3, 5, 7, 4, 8, 6, 9, 10]],
  906. ['asc', [5, 7, 3, 8, 4, 2, 1, 9, 10, 6]],
  907. ['leaves', [5, 7, 8, 9, 10]]
  908. ];
  909. }
  910. /**
  911. * Tests the listNested method with the default 'children' nesting key
  912. *
  913. * @dataProvider nestedListProvider
  914. * @return void
  915. */
  916. public function testListNested($dir, $expected)
  917. {
  918. $items = [
  919. ['id' => 1, 'parent_id' => null],
  920. ['id' => 2, 'parent_id' => 1],
  921. ['id' => 3, 'parent_id' => 2],
  922. ['id' => 4, 'parent_id' => 2],
  923. ['id' => 5, 'parent_id' => 3],
  924. ['id' => 6, 'parent_id' => null],
  925. ['id' => 7, 'parent_id' => 3],
  926. ['id' => 8, 'parent_id' => 4],
  927. ['id' => 9, 'parent_id' => 6],
  928. ['id' => 10, 'parent_id' => 6]
  929. ];
  930. $collection = (new Collection($items))->nest('id', 'parent_id')->listNested($dir);
  931. $this->assertEquals($expected, $collection->extract('id')->toArray(false));
  932. }
  933. /**
  934. * Tests using listNested with a different nesting key
  935. *
  936. * @return void
  937. */
  938. public function testListNestedCustomKey()
  939. {
  940. $items = [
  941. ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
  942. ['id' => 4, 'stuff' => [['id' => 5]]]
  943. ];
  944. $collection = (new Collection($items))->listNested('desc', 'stuff');
  945. $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
  946. }
  947. /**
  948. * Tests flattening the collection using a custom callable function
  949. *
  950. * @return void
  951. */
  952. public function testListNestedWithCallable()
  953. {
  954. $items = [
  955. ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
  956. ['id' => 4, 'stuff' => [['id' => 5]]]
  957. ];
  958. $collection = (new Collection($items))->listNested('desc', function ($item) {
  959. return isset($item['stuff']) ? $item['stuff'] : [];
  960. });
  961. $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
  962. }
  963. /**
  964. * Tests the sumOf method
  965. *
  966. * @return void
  967. */
  968. public function testSumOf()
  969. {
  970. $items = [
  971. ['invoice' => ['total' => 100]],
  972. ['invoice' => ['total' => 200]]
  973. ];
  974. $this->assertEquals(300, (new Collection($items))->sumOf('invoice.total'));
  975. $sum = (new Collection($items))->sumOf(function ($v) {
  976. return $v['invoice']['total'] * 2;
  977. });
  978. $this->assertEquals(600, $sum);
  979. }
  980. /**
  981. * Tests the stopWhen method with a callable
  982. *
  983. * @return void
  984. */
  985. public function testStopWhenCallable()
  986. {
  987. $items = [10, 20, 40, 10, 5];
  988. $collection = (new Collection($items))->stopWhen(function ($v) {
  989. return $v > 20;
  990. });
  991. $this->assertEquals([10, 20], $collection->toArray());
  992. }
  993. /**
  994. * Tests the stopWhen method with a matching array
  995. *
  996. * @return void
  997. */
  998. public function testStopWhenWithArray()
  999. {
  1000. $items = [
  1001. ['foo' => 'bar'],
  1002. ['foo' => 'baz'],
  1003. ['foo' => 'foo']
  1004. ];
  1005. $collection = (new Collection($items))->stopWhen(['foo' => 'baz']);
  1006. $this->assertEquals([['foo' => 'bar']], $collection->toArray());
  1007. }
  1008. /**
  1009. * Tests the unfold method
  1010. *
  1011. * @return void
  1012. */
  1013. public function testUnfold()
  1014. {
  1015. $items = [
  1016. [1, 2, 3, 4],
  1017. [5, 6],
  1018. [7, 8]
  1019. ];
  1020. $collection = (new Collection($items))->unfold();
  1021. $this->assertEquals(range(1, 8), $collection->toArray(false));
  1022. $items = [
  1023. [1, 2],
  1024. new Collection([3, 4])
  1025. ];
  1026. $collection = (new Collection($items))->unfold();
  1027. $this->assertEquals(range(1, 4), $collection->toArray(false));
  1028. }
  1029. /**
  1030. * Tests the unfold method with empty levels
  1031. *
  1032. * @return void
  1033. */
  1034. public function testUnfoldEmptyLevels()
  1035. {
  1036. $items = [[], [1, 2], []];
  1037. $collection = (new Collection($items))->unfold();
  1038. $this->assertEquals(range(1, 2), $collection->toArray(false));
  1039. $items = [];
  1040. $collection = (new Collection($items))->unfold();
  1041. $this->assertEmpty($collection->toArray(false));
  1042. }
  1043. /**
  1044. * Tests the unfold when passing a callable
  1045. *
  1046. * @return void
  1047. */
  1048. public function testUnfoldWithCallable()
  1049. {
  1050. $items = [1, 2, 3];
  1051. $collection = (new Collection($items))->unfold(function ($item) {
  1052. return range($item, $item * 2);
  1053. });
  1054. $expected = [1, 2, 2, 3, 4, 3, 4, 5, 6];
  1055. $this->assertEquals($expected, $collection->toArray(false));
  1056. }
  1057. }