CollectionTest.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. /**
  1058. * Tests the through() method
  1059. *
  1060. * @return void
  1061. */
  1062. public function testThrough()
  1063. {
  1064. $items = [1, 2, 3];
  1065. $collection = (new Collection($items))->through(function ($collection) {
  1066. return $collection->append($collection->toList());
  1067. });
  1068. $this->assertEquals([1, 2, 3, 1, 2, 3], $collection->toList());
  1069. }
  1070. /**
  1071. * Tests the through method when it returns an array
  1072. *
  1073. * @return void
  1074. */
  1075. public function testThroughReturnArray()
  1076. {
  1077. $items = [1, 2, 3];
  1078. $collection = (new Collection($items))->through(function ($collection) {
  1079. $list = $collection->toList();
  1080. return array_merge($list, $list);
  1081. });
  1082. $this->assertEquals([1, 2, 3, 1, 2, 3], $collection->toList());
  1083. }
  1084. }