CollectionTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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, $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, $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
  240. *
  241. * @return void
  242. */
  243. public function testReduce()
  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 extract
  264. *
  265. * @return void
  266. */
  267. public function testExtract()
  268. {
  269. $items = [['a' => ['b' => ['c' => 1]]], 2];
  270. $collection = new Collection($items);
  271. $map = $collection->extract('a.b.c');
  272. $this->assertInstanceOf('Cake\Collection\Iterator\ExtractIterator', $map);
  273. $this->assertEquals([1, null], iterator_to_array($map));
  274. }
  275. /**
  276. * Tests sort
  277. *
  278. * @return void
  279. */
  280. public function testSortString()
  281. {
  282. $items = [
  283. ['a' => ['b' => ['c' => 4]]],
  284. ['a' => ['b' => ['c' => 10]]],
  285. ['a' => ['b' => ['c' => 6]]]
  286. ];
  287. $collection = new Collection($items);
  288. $map = $collection->sortBy('a.b.c');
  289. $this->assertInstanceOf('Cake\Collection\Collection', $map);
  290. $expected = [
  291. 2 => ['a' => ['b' => ['c' => 10]]],
  292. 1 => ['a' => ['b' => ['c' => 6]]],
  293. 0 => ['a' => ['b' => ['c' => 4]]],
  294. ];
  295. $this->assertEquals($expected, iterator_to_array($map));
  296. }
  297. /**
  298. * Tests max
  299. *
  300. * @return void
  301. */
  302. public function testMax()
  303. {
  304. $items = [
  305. ['a' => ['b' => ['c' => 4]]],
  306. ['a' => ['b' => ['c' => 10]]],
  307. ['a' => ['b' => ['c' => 6]]]
  308. ];
  309. $collection = new Collection($items);
  310. $this->assertEquals(['a' => ['b' => ['c' => 10]]], $collection->max('a.b.c'));
  311. $callback = function ($e) {
  312. return $e['a']['b']['c'] * - 1;
  313. };
  314. $this->assertEquals(['a' => ['b' => ['c' => 4]]], $collection->max($callback));
  315. }
  316. /**
  317. * Tests min
  318. *
  319. * @return void
  320. */
  321. public function testMin()
  322. {
  323. $items = [
  324. ['a' => ['b' => ['c' => 4]]],
  325. ['a' => ['b' => ['c' => 10]]],
  326. ['a' => ['b' => ['c' => 6]]]
  327. ];
  328. $collection = new Collection($items);
  329. $this->assertEquals(['a' => ['b' => ['c' => 4]]], $collection->min('a.b.c'));
  330. }
  331. /**
  332. * Tests groupBy
  333. *
  334. * @return void
  335. */
  336. public function testGroupBy()
  337. {
  338. $items = [
  339. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  340. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  341. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  342. ];
  343. $collection = new Collection($items);
  344. $grouped = $collection->groupBy('parent_id');
  345. $expected = [
  346. 10 => [
  347. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  348. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  349. ],
  350. 11 => [
  351. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  352. ]
  353. ];
  354. $this->assertEquals($expected, iterator_to_array($grouped));
  355. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  356. $grouped = $collection->groupBy(function ($element) {
  357. return $element['parent_id'];
  358. });
  359. $this->assertEquals($expected, iterator_to_array($grouped));
  360. }
  361. /**
  362. * Tests grouping by a deep key
  363. *
  364. * @return void
  365. */
  366. public function testGroupByDeepKey()
  367. {
  368. $items = [
  369. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  370. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  371. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  372. ];
  373. $collection = new Collection($items);
  374. $grouped = $collection->groupBy('thing.parent_id');
  375. $expected = [
  376. 10 => [
  377. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  378. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  379. ],
  380. 11 => [
  381. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  382. ]
  383. ];
  384. $this->assertEquals($expected, iterator_to_array($grouped));
  385. }
  386. /**
  387. * Tests indexBy
  388. *
  389. * @return void
  390. */
  391. public function testIndexBy()
  392. {
  393. $items = [
  394. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  395. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  396. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  397. ];
  398. $collection = new Collection($items);
  399. $grouped = $collection->indexBy('id');
  400. $expected = [
  401. 1 => ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  402. 3 => ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  403. 2 => ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  404. ];
  405. $this->assertEquals($expected, iterator_to_array($grouped));
  406. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  407. $grouped = $collection->indexBy(function ($element) {
  408. return $element['id'];
  409. });
  410. $this->assertEquals($expected, iterator_to_array($grouped));
  411. }
  412. /**
  413. * Tests indexBy with a deep property
  414. *
  415. * @return void
  416. */
  417. public function testIndexByDeep()
  418. {
  419. $items = [
  420. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  421. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  422. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  423. ];
  424. $collection = new Collection($items);
  425. $grouped = $collection->indexBy('thing.parent_id');
  426. $expected = [
  427. 10 => ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  428. 11 => ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  429. ];
  430. $this->assertEquals($expected, iterator_to_array($grouped));
  431. }
  432. /**
  433. * Tests countBy
  434. *
  435. * @return void
  436. */
  437. public function testCountBy()
  438. {
  439. $items = [
  440. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  441. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  442. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  443. ];
  444. $collection = new Collection($items);
  445. $grouped = $collection->countBy('parent_id');
  446. $expected = [
  447. 10 => 2,
  448. 11 => 1
  449. ];
  450. $this->assertEquals($expected, iterator_to_array($grouped));
  451. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  452. $grouped = $collection->countBy(function ($element) {
  453. return $element['parent_id'];
  454. });
  455. $this->assertEquals($expected, iterator_to_array($grouped));
  456. }
  457. /**
  458. * Tests shuffle
  459. *
  460. * @return void
  461. */
  462. public function testShuffle()
  463. {
  464. $data = [1, 2, 3, 4];
  465. $collection = (new Collection($data))->shuffle();
  466. $this->assertEquals(count($data), count(iterator_to_array($collection)));
  467. foreach ($collection as $value) {
  468. $this->assertContains($value, $data);
  469. }
  470. }
  471. /**
  472. * Tests sample
  473. *
  474. * @return void
  475. */
  476. public function testSample()
  477. {
  478. $data = [1, 2, 3, 4];
  479. $collection = (new Collection($data))->sample(2);
  480. $this->assertEquals(2, count(iterator_to_array($collection)));
  481. foreach ($collection as $value) {
  482. $this->assertContains($value, $data);
  483. }
  484. }
  485. /**
  486. * Test toArray method
  487. *
  488. * @return void
  489. */
  490. public function testToArray()
  491. {
  492. $data = [1, 2, 3, 4];
  493. $collection = new Collection($data);
  494. $this->assertEquals($data, $collection->toArray());
  495. }
  496. /**
  497. * Test json enconding
  498. *
  499. * @return void
  500. */
  501. public function testToJson()
  502. {
  503. $data = [1, 2, 3, 4];
  504. $collection = new Collection($data);
  505. $this->assertEquals(json_encode($data), json_encode($collection));
  506. }
  507. /**
  508. * Tests that only arrays and Traversables are allowed in the constructor
  509. *
  510. * @expectedException \InvalidArgumentException
  511. * @expectedExceptionMessage Only an array or \Traversable is allowed for Collection
  512. * @return void
  513. */
  514. public function testInvalidConstructorArgument()
  515. {
  516. new Collection('Derp');
  517. }
  518. /**
  519. * Tests take method
  520. *
  521. * @return void
  522. */
  523. public function testTake()
  524. {
  525. $data = [1, 2, 3, 4];
  526. $collection = new Collection($data);
  527. $taken = $collection->take(2);
  528. $this->assertEquals([1, 2], $taken->toArray());
  529. $taken = $collection->take(3);
  530. $this->assertEquals([1, 2, 3], $taken->toArray());
  531. $taken = $collection->take(500);
  532. $this->assertEquals([1, 2, 3, 4], $taken->toArray());
  533. $taken = $collection->take(1);
  534. $this->assertEquals([1], $taken->toArray());
  535. $taken = $collection->take();
  536. $this->assertEquals([1], $taken->toArray());
  537. $taken = $collection->take(2, 2);
  538. $this->assertEquals([2 => 3, 3 => 4], $taken->toArray());
  539. }
  540. /**
  541. * Tests match
  542. *
  543. * @return void
  544. */
  545. public function testMatch()
  546. {
  547. $items = [
  548. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  549. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  550. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  551. ];
  552. $collection = new Collection($items);
  553. $matched = $collection->match(['thing.parent_id' => 10, 'name' => 'baz']);
  554. $this->assertEquals([2 => $items[2]], $matched->toArray());
  555. $matched = $collection->match(['thing.parent_id' => 10]);
  556. $this->assertEquals(
  557. [0 => $items[0], 2 => $items[2]],
  558. $matched->toArray()
  559. );
  560. $matched = $collection->match(['thing.parent_id' => 500]);
  561. $this->assertEquals([], $matched->toArray());
  562. $matched = $collection->match(['parent_id' => 10, 'name' => 'baz']);
  563. $this->assertEquals([], $matched->toArray());
  564. }
  565. /**
  566. * Tests firstMatch
  567. *
  568. * @return void
  569. */
  570. public function testFirstMatch()
  571. {
  572. $items = [
  573. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  574. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  575. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  576. ];
  577. $collection = new Collection($items);
  578. $matched = $collection->firstMatch(['thing.parent_id' => 10]);
  579. $this->assertEquals(
  580. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  581. $matched
  582. );
  583. $matched = $collection->firstMatch(['thing.parent_id' => 10, 'name' => 'baz']);
  584. $this->assertEquals(
  585. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  586. $matched
  587. );
  588. }
  589. /**
  590. * Tests the append method
  591. *
  592. * @return void
  593. */
  594. public function testAppend()
  595. {
  596. $collection = new Collection([1, 2, 3]);
  597. $combined = $collection->append([4, 5, 6]);
  598. $this->assertEquals([1, 2, 3, 4, 5, 6], $combined->toArray(false));
  599. $collection = new Collection(['a' => 1, 'b' => 2]);
  600. $combined = $collection->append(['c' => 3, 'a' => 4]);
  601. $this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray());
  602. }
  603. /**
  604. * Tests that by calling compile internal iteration operations are not done
  605. * more than once
  606. *
  607. * @return void
  608. */
  609. public function testCompile()
  610. {
  611. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  612. $collection = new Collection($items);
  613. $callable = $this->getMock('stdClass', ['__invoke']);
  614. $callable->expects($this->at(0))
  615. ->method('__invoke')
  616. ->with(1, 'a')
  617. ->will($this->returnValue(4));
  618. $callable->expects($this->at(1))
  619. ->method('__invoke')
  620. ->with(2, 'b')
  621. ->will($this->returnValue(5));
  622. $callable->expects($this->at(2))
  623. ->method('__invoke')
  624. ->with(3, 'c')
  625. ->will($this->returnValue(6));
  626. $compiled = $collection->map($callable)->compile();
  627. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
  628. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
  629. }
  630. /**
  631. * Tests converting a non rewindable iterator into a rewindable one using
  632. * the buffered method.
  633. *
  634. * @return void
  635. */
  636. public function testBuffered()
  637. {
  638. $items = new NoRewindIterator(new ArrayIterator(['a' => 4, 'b' => 5, 'c' => 6]));
  639. $buffered = (new Collection($items))->buffered();
  640. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
  641. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
  642. }
  643. /**
  644. * Tests the combine method
  645. *
  646. * @return void
  647. */
  648. public function testCombine()
  649. {
  650. $items = [
  651. ['id' => 1, 'name' => 'foo', 'parent' => 'a'],
  652. ['id' => 2, 'name' => 'bar', 'parent' => 'b'],
  653. ['id' => 3, 'name' => 'baz', 'parent' => 'a']
  654. ];
  655. $collection = (new Collection($items))->combine('id', 'name');
  656. $expected = [1 => 'foo', 2 => 'bar', 3 => 'baz'];
  657. $this->assertEquals($expected, $collection->toArray());
  658. $expected = ['foo' => 1, 'bar' => 2, 'baz' => 3];
  659. $collection = (new Collection($items))->combine('name', 'id');
  660. $this->assertEquals($expected, $collection->toArray());
  661. $collection = (new Collection($items))->combine('id', 'name', 'parent');
  662. $expected = ['a' => [1 => 'foo', 3 => 'baz'], 'b' => [2 => 'bar']];
  663. $this->assertEquals($expected, $collection->toArray());
  664. $expected = [
  665. '0-1' => ['foo-0-1' => '0-1-foo'],
  666. '1-2' => ['bar-1-2' => '1-2-bar'],
  667. '2-3' => ['baz-2-3' => '2-3-baz']
  668. ];
  669. $collection = (new Collection($items))->combine(
  670. function ($value, $key) {
  671. return $value['name'] . '-' . $key;
  672. },
  673. function ($value, $key) {
  674. return $key . '-' . $value['name'];
  675. },
  676. function ($value, $key) {
  677. return $key . '-' . $value['id'];
  678. }
  679. );
  680. $this->assertEquals($expected, $collection->toArray());
  681. $collection = (new Collection($items))->combine('id', 'crazy');
  682. $this->assertEquals([1 => null, 2 => null, 3 => null], $collection->toArray());
  683. }
  684. /**
  685. * Tests the nest method with only one level
  686. *
  687. * @return void
  688. */
  689. public function testNest()
  690. {
  691. $items = [
  692. ['id' => 1, 'parent_id' => null],
  693. ['id' => 2, 'parent_id' => 1],
  694. ['id' => 3, 'parent_id' => 1],
  695. ['id' => 4, 'parent_id' => 1],
  696. ['id' => 5, 'parent_id' => 6],
  697. ['id' => 6, 'parent_id' => null],
  698. ['id' => 7, 'parent_id' => 1],
  699. ['id' => 8, 'parent_id' => 6],
  700. ['id' => 9, 'parent_id' => 6],
  701. ['id' => 10, 'parent_id' => 6]
  702. ];
  703. $collection = (new Collection($items))->nest('id', 'parent_id');
  704. $expected = [
  705. [
  706. 'id' => 1,
  707. 'parent_id' => null,
  708. 'children' => [
  709. ['id' => 2, 'parent_id' => 1, 'children' => []],
  710. ['id' => 3, 'parent_id' => 1, 'children' => []],
  711. ['id' => 4, 'parent_id' => 1, 'children' => []],
  712. ['id' => 7, 'parent_id' => 1, 'children' => []]
  713. ]
  714. ],
  715. [
  716. 'id' => 6,
  717. 'parent_id' => null,
  718. 'children' => [
  719. ['id' => 5, 'parent_id' => 6, 'children' => []],
  720. ['id' => 8, 'parent_id' => 6, 'children' => []],
  721. ['id' => 9, 'parent_id' => 6, 'children' => []],
  722. ['id' => 10, 'parent_id' => 6, 'children' => []]
  723. ]
  724. ]
  725. ];
  726. $this->assertEquals($expected, $collection->toArray());
  727. }
  728. /**
  729. * Tests the nest method with more than one level
  730. *
  731. * @return void
  732. */
  733. public function testNestMultiLevel()
  734. {
  735. $items = [
  736. ['id' => 1, 'parent_id' => null],
  737. ['id' => 2, 'parent_id' => 1],
  738. ['id' => 3, 'parent_id' => 2],
  739. ['id' => 4, 'parent_id' => 2],
  740. ['id' => 5, 'parent_id' => 3],
  741. ['id' => 6, 'parent_id' => null],
  742. ['id' => 7, 'parent_id' => 3],
  743. ['id' => 8, 'parent_id' => 4],
  744. ['id' => 9, 'parent_id' => 6],
  745. ['id' => 10, 'parent_id' => 6]
  746. ];
  747. $collection = (new Collection($items))->nest('id', 'parent_id');
  748. $expected = [
  749. [
  750. 'id' => 1,
  751. 'parent_id' => null,
  752. 'children' => [
  753. [
  754. 'id' => 2,
  755. 'parent_id' => 1,
  756. 'children' => [
  757. [
  758. 'id' => 3,
  759. 'parent_id' => 2,
  760. 'children' => [
  761. ['id' => 5, 'parent_id' => 3, 'children' => []],
  762. ['id' => 7, 'parent_id' => 3, 'children' => []]
  763. ]
  764. ],
  765. [
  766. 'id' => 4,
  767. 'parent_id' => 2,
  768. 'children' => [
  769. ['id' => 8, 'parent_id' => 4, 'children' => []]
  770. ]
  771. ]
  772. ]
  773. ]
  774. ]
  775. ],
  776. [
  777. 'id' => 6,
  778. 'parent_id' => null,
  779. 'children' => [
  780. ['id' => 9, 'parent_id' => 6, 'children' => []],
  781. ['id' => 10, 'parent_id' => 6, 'children' => []]
  782. ]
  783. ]
  784. ];
  785. $this->assertEquals($expected, $collection->toArray());
  786. }
  787. /**
  788. * Tests the nest method with more than one level
  789. *
  790. * @return void
  791. */
  792. public function testNestObjects()
  793. {
  794. $items = [
  795. new ArrayObject(['id' => 1, 'parent_id' => null]),
  796. new ArrayObject(['id' => 2, 'parent_id' => 1]),
  797. new ArrayObject(['id' => 3, 'parent_id' => 2]),
  798. new ArrayObject(['id' => 4, 'parent_id' => 2]),
  799. new ArrayObject(['id' => 5, 'parent_id' => 3]),
  800. new ArrayObject(['id' => 6, 'parent_id' => null]),
  801. new ArrayObject(['id' => 7, 'parent_id' => 3]),
  802. new ArrayObject(['id' => 8, 'parent_id' => 4]),
  803. new ArrayObject(['id' => 9, 'parent_id' => 6]),
  804. new ArrayObject(['id' => 10, 'parent_id' => 6])
  805. ];
  806. $collection = (new Collection($items))->nest('id', 'parent_id');
  807. $expected = [
  808. new ArrayObject([
  809. 'id' => 1,
  810. 'parent_id' => null,
  811. 'children' => [
  812. new ArrayObject([
  813. 'id' => 2,
  814. 'parent_id' => 1,
  815. 'children' => [
  816. new ArrayObject([
  817. 'id' => 3,
  818. 'parent_id' => 2,
  819. 'children' => [
  820. new ArrayObject(['id' => 5, 'parent_id' => 3, 'children' => []]),
  821. new ArrayObject(['id' => 7, 'parent_id' => 3, 'children' => []])
  822. ]
  823. ]),
  824. new ArrayObject([
  825. 'id' => 4,
  826. 'parent_id' => 2,
  827. 'children' => [
  828. new ArrayObject(['id' => 8, 'parent_id' => 4, 'children' => []])
  829. ]
  830. ])
  831. ]
  832. ])
  833. ]
  834. ]),
  835. new ArrayObject([
  836. 'id' => 6,
  837. 'parent_id' => null,
  838. 'children' => [
  839. new ArrayObject(['id' => 9, 'parent_id' => 6, 'children' => []]),
  840. new ArrayObject(['id' => 10, 'parent_id' => 6, 'children' => []])
  841. ]
  842. ])
  843. ];
  844. $this->assertEquals($expected, $collection->toArray());
  845. }
  846. /**
  847. * Tests insert
  848. *
  849. * @return void
  850. */
  851. public function testInsert()
  852. {
  853. $items = [['a' => 1], ['b' => 2]];
  854. $collection = new Collection($items);
  855. $iterator = $collection->insert('c', [3, 4]);
  856. $this->assertInstanceOf('Cake\Collection\Iterator\InsertIterator', $iterator);
  857. $this->assertEquals(
  858. [['a' => 1, 'c' => 3], ['b' => 2, 'c' => 4]],
  859. iterator_to_array($iterator)
  860. );
  861. }
  862. /**
  863. * Provider for testing each of the direcations for listNested
  864. *
  865. * @return void
  866. */
  867. public function nestedListProvider()
  868. {
  869. return [
  870. ['desc', [1, 2, 3, 5, 7, 4, 8, 6, 9, 10]],
  871. ['asc', [5, 7, 3, 8, 4, 2, 1, 9, 10, 6]],
  872. ['leaves', [5, 7, 8, 9, 10]]
  873. ];
  874. }
  875. /**
  876. * Tests the listNested method with the default 'children' nesting key
  877. *
  878. * @dataProvider nestedListProvider
  879. * @return void
  880. */
  881. public function testListNested($dir, $expected)
  882. {
  883. $items = [
  884. ['id' => 1, 'parent_id' => null],
  885. ['id' => 2, 'parent_id' => 1],
  886. ['id' => 3, 'parent_id' => 2],
  887. ['id' => 4, 'parent_id' => 2],
  888. ['id' => 5, 'parent_id' => 3],
  889. ['id' => 6, 'parent_id' => null],
  890. ['id' => 7, 'parent_id' => 3],
  891. ['id' => 8, 'parent_id' => 4],
  892. ['id' => 9, 'parent_id' => 6],
  893. ['id' => 10, 'parent_id' => 6]
  894. ];
  895. $collection = (new Collection($items))->nest('id', 'parent_id')->listNested($dir);
  896. $this->assertEquals($expected, $collection->extract('id')->toArray(false));
  897. }
  898. /**
  899. * Tests using listNested with a different nesting key
  900. *
  901. * @return void
  902. */
  903. public function testListNestedCustomKey()
  904. {
  905. $items = [
  906. ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
  907. ['id' => 4, 'stuff' => [['id' => 5]]]
  908. ];
  909. $collection = (new Collection($items))->listNested('desc', 'stuff');
  910. $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
  911. }
  912. /**
  913. * Tests flattening the collection using a custom callable function
  914. *
  915. * @return void
  916. */
  917. public function testListNestedWithCallable()
  918. {
  919. $items = [
  920. ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
  921. ['id' => 4, 'stuff' => [['id' => 5]]]
  922. ];
  923. $collection = (new Collection($items))->listNested('desc', function ($item) {
  924. return isset($item['stuff']) ? $item['stuff'] : [];
  925. });
  926. $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
  927. }
  928. /**
  929. * Tests the sumOf method
  930. *
  931. * @return void
  932. */
  933. public function testSumOf()
  934. {
  935. $items = [
  936. ['invoice' => ['total' => 100]],
  937. ['invoice' => ['total' => 200]]
  938. ];
  939. $this->assertEquals(300, (new Collection($items))->sumOf('invoice.total'));
  940. $sum = (new Collection($items))->sumOf(function ($v) {
  941. return $v['invoice']['total'] * 2;
  942. });
  943. $this->assertEquals(600, $sum);
  944. }
  945. /**
  946. * Tests the stopWhen method with a callable
  947. *
  948. * @return void
  949. */
  950. public function testStopWhenCallable()
  951. {
  952. $items = [10, 20, 40, 10, 5];
  953. $collection = (new Collection($items))->stopWhen(function ($v) {
  954. return $v > 20;
  955. });
  956. $this->assertEquals([10, 20], $collection->toArray());
  957. }
  958. /**
  959. * Tests the stopWhen method with a matching array
  960. *
  961. * @return void
  962. */
  963. public function testStopWhenWithArray()
  964. {
  965. $items = [
  966. ['foo' => 'bar'],
  967. ['foo' => 'baz'],
  968. ['foo' => 'foo']
  969. ];
  970. $collection = (new Collection($items))->stopWhen(['foo' => 'baz']);
  971. $this->assertEquals([['foo' => 'bar']], $collection->toArray());
  972. }
  973. /**
  974. * Tests the unfold method
  975. *
  976. * @return void
  977. */
  978. public function testUnfold()
  979. {
  980. $items = [
  981. [1, 2, 3, 4],
  982. [5, 6],
  983. [7, 8]
  984. ];
  985. $collection = (new Collection($items))->unfold();
  986. $this->assertEquals(range(1, 8), $collection->toArray(false));
  987. $items = [
  988. [1, 2],
  989. new Collection([3, 4])
  990. ];
  991. $collection = (new Collection($items))->unfold();
  992. $this->assertEquals(range(1, 4), $collection->toArray(false));
  993. }
  994. /**
  995. * Tests the unfold method with empty levels
  996. *
  997. * @return void
  998. */
  999. public function testUnfoldEmptyLevels()
  1000. {
  1001. $items = [[], [1, 2], []];
  1002. $collection = (new Collection($items))->unfold();
  1003. $this->assertEquals(range(1, 2), $collection->toArray(false));
  1004. $items = [];
  1005. $collection = (new Collection($items))->unfold();
  1006. $this->assertEmpty($collection->toArray(false));
  1007. }
  1008. /**
  1009. * Tests the unfold when passing a callable
  1010. *
  1011. * @return void
  1012. */
  1013. public function testUnfoldWithCallable()
  1014. {
  1015. $items = [1, 2, 3];
  1016. $collection = (new Collection($items))->unfold(function ($item) {
  1017. return range($item, $item * 2);
  1018. });
  1019. $expected = [1, 2, 2, 3, 4, 3, 4, 5, 6];
  1020. $this->assertEquals($expected, $collection->toArray(false));
  1021. }
  1022. }