CollectionTest.php 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Collection;
  17. use ArrayIterator;
  18. use ArrayObject;
  19. use Cake\Collection\Collection;
  20. use Cake\ORM\Entity;
  21. use Cake\TestSuite\TestCase;
  22. use InvalidArgumentException;
  23. use NoRewindIterator;
  24. use stdClass;
  25. use TestApp\Collection\CountableIterator;
  26. use TestApp\Collection\TestCollection;
  27. use TestApp\Collection\TestIterator;
  28. /**
  29. * Collection Test
  30. *
  31. * @coversDefaultClass \Cake\Collection\Collection
  32. */
  33. class CollectionTest extends TestCase
  34. {
  35. /**
  36. * Tests that it is possible to convert an array into a collection
  37. *
  38. * @return void
  39. */
  40. public function testArrayIsWrapped()
  41. {
  42. $items = [1, 2, 3];
  43. $collection = new Collection($items);
  44. $this->assertEquals($items, iterator_to_array($collection));
  45. }
  46. /**
  47. * Provider for average tests
  48. *
  49. * @return array
  50. */
  51. public function avgProvider()
  52. {
  53. $items = [1, 2, 3];
  54. return [
  55. 'array' => [$items],
  56. 'iterator' => [$this->yieldItems($items)],
  57. ];
  58. }
  59. /**
  60. * Tests the avg method
  61. *
  62. * @dataProvider avgProvider
  63. * @return void
  64. */
  65. public function testAvg($items)
  66. {
  67. $collection = new Collection($items);
  68. $this->assertSame(2, $collection->avg());
  69. $items = [['foo' => 1], ['foo' => 2], ['foo' => 3]];
  70. $collection = new Collection($items);
  71. $this->assertSame(2, $collection->avg('foo'));
  72. }
  73. /**
  74. * Tests the avg method when on an empty collection
  75. *
  76. * @return void
  77. */
  78. public function testAvgWithEmptyCollection()
  79. {
  80. $collection = new Collection([]);
  81. $this->assertNull($collection->avg());
  82. $collection = new Collection([null, null]);
  83. $this->assertSame(0, $collection->avg());
  84. }
  85. /**
  86. * Provider for average tests with use of a matcher
  87. *
  88. * @return array
  89. */
  90. public function avgWithMatcherProvider()
  91. {
  92. $items = [['foo' => 1], ['foo' => 2], ['foo' => 3]];
  93. return [
  94. 'array' => [$items],
  95. 'iterator' => [$this->yieldItems($items)],
  96. ];
  97. }
  98. /**
  99. * ests the avg method
  100. *
  101. * @dataProvider avgWithMatcherProvider
  102. * @return void
  103. */
  104. public function testAvgWithMatcher($items)
  105. {
  106. $collection = new Collection($items);
  107. $this->assertSame(2, $collection->avg('foo'));
  108. }
  109. /**
  110. * Provider for some median tests
  111. *
  112. * @return array
  113. */
  114. public function medianProvider()
  115. {
  116. $items = [5, 2, 4];
  117. return [
  118. 'array' => [$items],
  119. 'iterator' => [$this->yieldItems($items)],
  120. ];
  121. }
  122. /**
  123. * Tests the median method
  124. *
  125. * @dataProvider medianProvider
  126. * @return void
  127. */
  128. public function testMedian($items)
  129. {
  130. $collection = new Collection($items);
  131. $this->assertSame(4, $collection->median());
  132. }
  133. /**
  134. * Tests the median method when on an empty collection
  135. *
  136. * @return void
  137. */
  138. public function testMedianWithEmptyCollection()
  139. {
  140. $collection = new Collection([]);
  141. $this->assertNull($collection->median());
  142. $collection = new Collection([null, null]);
  143. $this->assertSame(0, $collection->median());
  144. }
  145. /**
  146. * Tests the median method
  147. *
  148. * @dataProvider simpleProvider
  149. * @return void
  150. */
  151. public function testMedianEven($items)
  152. {
  153. $collection = new Collection($items);
  154. $this->assertSame(2.5, $collection->median());
  155. }
  156. /**
  157. * Provider for median tests with use of a matcher
  158. *
  159. * @return array
  160. */
  161. public function medianWithMatcherProvider()
  162. {
  163. $items = [
  164. ['invoice' => ['total' => 400]],
  165. ['invoice' => ['total' => 500]],
  166. ['invoice' => ['total' => 200]],
  167. ['invoice' => ['total' => 100]],
  168. ['invoice' => ['total' => 333]],
  169. ];
  170. return [
  171. 'array' => [$items],
  172. 'iterator' => [$this->yieldItems($items)],
  173. ];
  174. }
  175. /**
  176. * Tests the median method
  177. *
  178. * @dataProvider medianWithMatcherProvider
  179. * @return void
  180. */
  181. public function testMedianWithMatcher($items)
  182. {
  183. $this->assertSame(333, (new Collection($items))->median('invoice.total'));
  184. }
  185. /**
  186. * Tests that it is possible to convert an iterator into a collection
  187. *
  188. * @return void
  189. */
  190. public function testIteratorIsWrapped()
  191. {
  192. $items = new \ArrayObject([1, 2, 3]);
  193. $collection = new Collection($items);
  194. $this->assertEquals(iterator_to_array($items), iterator_to_array($collection));
  195. }
  196. /**
  197. * Test running a method over all elements in the collection
  198. *
  199. * @return void
  200. */
  201. public function testEach()
  202. {
  203. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  204. $collection = new Collection($items);
  205. $results = [];
  206. $collection->each(function ($value, $key) use (&$results) {
  207. $results[] = [$key => $value];
  208. });
  209. $this->assertSame([['a' => 1], ['b' => 2], ['c' => 3]], $results);
  210. }
  211. public function filterProvider()
  212. {
  213. $items = [1, 2, 0, 3, false, 4, null, 5, ''];
  214. return [
  215. 'array' => [$items],
  216. 'iterator' => [$this->yieldItems($items)],
  217. ];
  218. }
  219. /**
  220. * Test filter() with no callback.
  221. *
  222. * @dataProvider filterProvider
  223. * @return void
  224. */
  225. public function testFilterNoCallback($items)
  226. {
  227. $collection = new Collection($items);
  228. $result = $collection->filter()->toArray();
  229. $expected = [1, 2, 3, 4, 5];
  230. $this->assertSame($expected, array_values($result));
  231. }
  232. /**
  233. * Tests that it is possible to chain filter() as it returns a collection object
  234. *
  235. * @return void
  236. */
  237. public function testFilterChaining()
  238. {
  239. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  240. $collection = new Collection($items);
  241. $filtered = $collection->filter(function ($value, $key, $iterator) {
  242. return $value > 2;
  243. });
  244. $this->assertInstanceOf(Collection::class, $filtered);
  245. $results = [];
  246. $filtered->each(function ($value, $key) use (&$results) {
  247. $results[] = [$key => $value];
  248. });
  249. $this->assertSame([['c' => 3]], $results);
  250. }
  251. /**
  252. * Tests reject
  253. *
  254. * @return void
  255. */
  256. public function testReject()
  257. {
  258. $collection = new Collection([]);
  259. $result = $collection->reject(function ($v) {
  260. return false;
  261. });
  262. $this->assertSame([], iterator_to_array($result));
  263. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  264. $collection = new Collection($items);
  265. $result = $collection->reject(function ($v, $k, $items) use ($collection) {
  266. $this->assertSame($collection->getInnerIterator(), $items);
  267. return $v > 2;
  268. });
  269. $this->assertEquals(['a' => 1, 'b' => 2], iterator_to_array($result));
  270. $this->assertInstanceOf('Cake\Collection\Collection', $result);
  271. }
  272. /**
  273. * Tests every when the callback returns true for all elements
  274. *
  275. * @return void
  276. */
  277. public function testEveryReturnTrue()
  278. {
  279. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  280. $collection = new Collection($items);
  281. $results = [];
  282. $this->assertTrue($collection->every(function ($value, $key) use (&$results) {
  283. $results[] = [$key => $value];
  284. return true;
  285. }));
  286. $this->assertSame([['a' => 1], ['b' => 2], ['c' => 3]], $results);
  287. }
  288. /**
  289. * Tests every when the callback returns false for one of the elements
  290. *
  291. * @return void
  292. */
  293. public function testEveryReturnFalse()
  294. {
  295. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  296. $collection = new Collection($items);
  297. $results = [];
  298. $this->assertFalse($collection->every(function ($value, $key) use (&$results) {
  299. $results[] = [$key => $value];
  300. return $key !== 'b';
  301. }));
  302. $this->assertSame([['a' => 1], ['b' => 2]], $results);
  303. }
  304. /**
  305. * Tests some() when one of the calls return true
  306. *
  307. * @return void
  308. */
  309. public function testSomeReturnTrue()
  310. {
  311. $collection = new Collection([]);
  312. $result = $collection->some(function ($v) {
  313. return true;
  314. });
  315. $this->assertFalse($result);
  316. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  317. $collection = new Collection($items);
  318. $results = [];
  319. $this->assertTrue($collection->some(function ($value, $key) use (&$results) {
  320. $results[] = [$key => $value];
  321. return $key === 'b';
  322. }));
  323. $this->assertSame([['a' => 1], ['b' => 2]], $results);
  324. }
  325. /**
  326. * Tests some() when none of the calls return true
  327. *
  328. * @return void
  329. */
  330. public function testSomeReturnFalse()
  331. {
  332. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  333. $collection = new Collection($items);
  334. $results = [];
  335. $this->assertFalse($collection->some(function ($value, $key) use (&$results) {
  336. $results[] = [$key => $value];
  337. return false;
  338. }));
  339. $this->assertSame([['a' => 1], ['b' => 2], ['c' => 3]], $results);
  340. }
  341. /**
  342. * Tests contains
  343. *
  344. * @return void
  345. */
  346. public function testContains()
  347. {
  348. $collection = new Collection([]);
  349. $this->assertFalse($collection->contains('a'));
  350. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  351. $collection = new Collection($items);
  352. $this->assertTrue($collection->contains(2));
  353. $this->assertTrue($collection->contains(1));
  354. $this->assertFalse($collection->contains(10));
  355. $this->assertFalse($collection->contains('2'));
  356. }
  357. /**
  358. * Provider for some simple tests
  359. *
  360. * @return array
  361. */
  362. public function simpleProvider()
  363. {
  364. $items = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
  365. return [
  366. 'array' => [$items],
  367. 'iterator' => [$this->yieldItems($items)],
  368. ];
  369. }
  370. /**
  371. * Tests map
  372. *
  373. * @dataProvider simpleProvider
  374. * @return void
  375. */
  376. public function testMap($items)
  377. {
  378. $collection = new Collection($items);
  379. $map = $collection->map(function ($v, $k, $it) use ($collection) {
  380. $this->assertSame($collection->getInnerIterator(), $it);
  381. return $v * $v;
  382. });
  383. $this->assertInstanceOf('Cake\Collection\Iterator\ReplaceIterator', $map);
  384. $this->assertEquals(['a' => 1, 'b' => 4, 'c' => 9, 'd' => 16], iterator_to_array($map));
  385. }
  386. /**
  387. * Tests reduce with initial value
  388. *
  389. * @dataProvider simpleProvider
  390. * @return void
  391. */
  392. public function testReduceWithInitialValue($items)
  393. {
  394. $collection = new Collection($items);
  395. $this->assertSame(20, $collection->reduce(function ($reduction, $value, $key) {
  396. return $value + $reduction;
  397. }, 10));
  398. }
  399. /**
  400. * Tests reduce without initial value
  401. *
  402. * @dataProvider simpleProvider
  403. * @return void
  404. */
  405. public function testReduceWithoutInitialValue($items)
  406. {
  407. $collection = new Collection($items);
  408. $this->assertSame(10, $collection->reduce(function ($reduction, $value, $key) {
  409. return $value + $reduction;
  410. }));
  411. }
  412. /**
  413. * Provider for some extract tests
  414. *
  415. * @return array
  416. */
  417. public function extractProvider()
  418. {
  419. $items = [['a' => ['b' => ['c' => 1]]], 2];
  420. return [
  421. 'array' => [$items],
  422. 'iterator' => [$this->yieldItems($items)],
  423. ];
  424. }
  425. /**
  426. * Tests extract
  427. *
  428. * @dataProvider extractProvider
  429. * @return void
  430. */
  431. public function testExtract($items)
  432. {
  433. $collection = new Collection($items);
  434. $map = $collection->extract('a.b.c');
  435. $this->assertInstanceOf('Cake\Collection\Iterator\ExtractIterator', $map);
  436. $this->assertEquals([1, null], iterator_to_array($map));
  437. }
  438. /**
  439. * Provider for some sort tests
  440. *
  441. * @return array
  442. */
  443. public function sortProvider()
  444. {
  445. $items = [
  446. ['a' => ['b' => ['c' => 4]]],
  447. ['a' => ['b' => ['c' => 10]]],
  448. ['a' => ['b' => ['c' => 6]]],
  449. ];
  450. return [
  451. 'array' => [$items],
  452. 'iterator' => [$this->yieldItems($items)],
  453. ];
  454. }
  455. /**
  456. * Tests sort
  457. *
  458. * @dataProvider sortProvider
  459. * @return void
  460. */
  461. public function testSortString($items)
  462. {
  463. $collection = new Collection($items);
  464. $map = $collection->sortBy('a.b.c');
  465. $this->assertInstanceOf('Cake\Collection\Collection', $map);
  466. $expected = [
  467. ['a' => ['b' => ['c' => 10]]],
  468. ['a' => ['b' => ['c' => 6]]],
  469. ['a' => ['b' => ['c' => 4]]],
  470. ];
  471. $this->assertEquals($expected, $map->toList());
  472. }
  473. /**
  474. * Tests max
  475. *
  476. * @dataProvider sortProvider
  477. * @return void
  478. */
  479. public function testMax($items)
  480. {
  481. $collection = new Collection($items);
  482. $this->assertEquals(['a' => ['b' => ['c' => 10]]], $collection->max('a.b.c'));
  483. }
  484. /**
  485. * Tests max
  486. *
  487. * @dataProvider sortProvider
  488. * @return void
  489. */
  490. public function testMaxCallback($items)
  491. {
  492. $collection = new Collection($items);
  493. $callback = function ($e) {
  494. return $e['a']['b']['c'] * - 1;
  495. };
  496. $this->assertEquals(['a' => ['b' => ['c' => 4]]], $collection->max($callback));
  497. }
  498. /**
  499. * Tests max
  500. *
  501. * @dataProvider sortProvider
  502. * @return void
  503. */
  504. public function testMaxCallable($items)
  505. {
  506. $collection = new Collection($items);
  507. $this->assertEquals(['a' => ['b' => ['c' => 4]]], $collection->max(function ($e) {
  508. return $e['a']['b']['c'] * - 1;
  509. }));
  510. }
  511. /**
  512. * Test max with a collection of Entities
  513. *
  514. * @return void
  515. */
  516. public function testMaxWithEntities()
  517. {
  518. $collection = new Collection([
  519. new Entity(['id' => 1, 'count' => 18]),
  520. new Entity(['id' => 2, 'count' => 9]),
  521. new Entity(['id' => 3, 'count' => 42]),
  522. new Entity(['id' => 4, 'count' => 4]),
  523. new Entity(['id' => 5, 'count' => 22]),
  524. ]);
  525. $expected = new Entity(['id' => 3, 'count' => 42]);
  526. $this->assertEquals($expected, $collection->max('count'));
  527. }
  528. /**
  529. * Tests min
  530. *
  531. * @dataProvider sortProvider
  532. * @return void
  533. */
  534. public function testMin($items)
  535. {
  536. $collection = new Collection($items);
  537. $this->assertEquals(['a' => ['b' => ['c' => 4]]], $collection->min('a.b.c'));
  538. }
  539. /**
  540. * Test min with a collection of Entities
  541. *
  542. * @return void
  543. */
  544. public function testMinWithEntities()
  545. {
  546. $collection = new Collection([
  547. new Entity(['id' => 1, 'count' => 18]),
  548. new Entity(['id' => 2, 'count' => 9]),
  549. new Entity(['id' => 3, 'count' => 42]),
  550. new Entity(['id' => 4, 'count' => 4]),
  551. new Entity(['id' => 5, 'count' => 22]),
  552. ]);
  553. $expected = new Entity(['id' => 4, 'count' => 4]);
  554. $this->assertEquals($expected, $collection->min('count'));
  555. }
  556. /**
  557. * Provider for some groupBy tests
  558. *
  559. * @return array
  560. */
  561. public function groupByProvider()
  562. {
  563. $items = [
  564. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  565. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  566. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  567. ];
  568. return [
  569. 'array' => [$items],
  570. 'iterator' => [$this->yieldItems($items)],
  571. ];
  572. }
  573. /**
  574. * Tests groupBy
  575. *
  576. * @dataProvider groupByProvider
  577. * @return void
  578. */
  579. public function testGroupBy($items)
  580. {
  581. $collection = new Collection($items);
  582. $grouped = $collection->groupBy('parent_id');
  583. $expected = [
  584. 10 => [
  585. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  586. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  587. ],
  588. 11 => [
  589. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  590. ],
  591. ];
  592. $this->assertEquals($expected, iterator_to_array($grouped));
  593. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  594. }
  595. /**
  596. * Tests groupBy
  597. *
  598. * @dataProvider groupByProvider
  599. * @return void
  600. */
  601. public function testGroupByCallback($items)
  602. {
  603. $collection = new Collection($items);
  604. $expected = [
  605. 10 => [
  606. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  607. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  608. ],
  609. 11 => [
  610. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  611. ],
  612. ];
  613. $grouped = $collection->groupBy(function ($element) {
  614. return $element['parent_id'];
  615. });
  616. $this->assertEquals($expected, iterator_to_array($grouped));
  617. }
  618. /**
  619. * Tests grouping by a deep key
  620. *
  621. * @return void
  622. */
  623. public function testGroupByDeepKey()
  624. {
  625. $items = [
  626. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  627. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  628. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  629. ];
  630. $collection = new Collection($items);
  631. $grouped = $collection->groupBy('thing.parent_id');
  632. $expected = [
  633. 10 => [
  634. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  635. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  636. ],
  637. 11 => [
  638. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  639. ],
  640. ];
  641. $this->assertEquals($expected, iterator_to_array($grouped));
  642. }
  643. /**
  644. * Tests passing an invalid path to groupBy.
  645. *
  646. * @return void
  647. */
  648. public function testGroupByInvalidPath()
  649. {
  650. $items = [
  651. ['id' => 1, 'name' => 'foo'],
  652. ['id' => 2, 'name' => 'bar'],
  653. ['id' => 3, 'name' => 'baz'],
  654. ];
  655. $collection = new Collection($items);
  656. $this->expectException(InvalidArgumentException::class);
  657. $this->expectExceptionMessage('Cannot use a nonexistent path or null value.');
  658. $collection->groupBy('missing');
  659. }
  660. /**
  661. * Provider for some indexBy tests
  662. *
  663. * @return array
  664. */
  665. public function indexByProvider()
  666. {
  667. $items = [
  668. ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  669. ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  670. ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  671. ];
  672. return [
  673. 'array' => [$items],
  674. 'iterator' => [$this->yieldItems($items)],
  675. ];
  676. }
  677. /**
  678. * Tests indexBy
  679. *
  680. * @dataProvider indexByProvider
  681. * @return void
  682. */
  683. public function testIndexBy($items)
  684. {
  685. $collection = new Collection($items);
  686. $grouped = $collection->indexBy('id');
  687. $expected = [
  688. 1 => ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  689. 3 => ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  690. 2 => ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  691. ];
  692. $this->assertEquals($expected, iterator_to_array($grouped));
  693. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  694. }
  695. /**
  696. * Tests indexBy
  697. *
  698. * @dataProvider indexByProvider
  699. * @return void
  700. */
  701. public function testIndexByCallback($items)
  702. {
  703. $collection = new Collection($items);
  704. $grouped = $collection->indexBy(function ($element) {
  705. return $element['id'];
  706. });
  707. $expected = [
  708. 1 => ['id' => 1, 'name' => 'foo', 'parent_id' => 10],
  709. 3 => ['id' => 3, 'name' => 'baz', 'parent_id' => 10],
  710. 2 => ['id' => 2, 'name' => 'bar', 'parent_id' => 11],
  711. ];
  712. $this->assertEquals($expected, iterator_to_array($grouped));
  713. }
  714. /**
  715. * Tests indexBy with a deep property
  716. *
  717. * @return void
  718. */
  719. public function testIndexByDeep()
  720. {
  721. $items = [
  722. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  723. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  724. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  725. ];
  726. $collection = new Collection($items);
  727. $grouped = $collection->indexBy('thing.parent_id');
  728. $expected = [
  729. 10 => ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  730. 11 => ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  731. ];
  732. $this->assertEquals($expected, iterator_to_array($grouped));
  733. }
  734. /**
  735. * Tests passing an invalid path to indexBy.
  736. *
  737. * @return void
  738. */
  739. public function testIndexByInvalidPath()
  740. {
  741. $items = [
  742. ['id' => 1, 'name' => 'foo'],
  743. ['id' => 2, 'name' => 'bar'],
  744. ['id' => 3, 'name' => 'baz'],
  745. ];
  746. $collection = new Collection($items);
  747. $this->expectException(InvalidArgumentException::class);
  748. $this->expectExceptionMessage('Cannot use a nonexistent path or null value.');
  749. $collection->indexBy('missing');
  750. }
  751. /**
  752. * Tests countBy
  753. *
  754. * @dataProvider groupByProvider
  755. * @return void
  756. */
  757. public function testCountBy($items)
  758. {
  759. $collection = new Collection($items);
  760. $grouped = $collection->countBy('parent_id');
  761. $expected = [
  762. 10 => 2,
  763. 11 => 1,
  764. ];
  765. $result = iterator_to_array($grouped);
  766. $this->assertInstanceOf('Cake\Collection\Collection', $grouped);
  767. $this->assertEquals($expected, $result);
  768. }
  769. /**
  770. * Tests countBy
  771. *
  772. * @dataProvider groupByProvider
  773. * @return void
  774. */
  775. public function testCountByCallback($items)
  776. {
  777. $expected = [
  778. 10 => 2,
  779. 11 => 1,
  780. ];
  781. $collection = new Collection($items);
  782. $grouped = $collection->countBy(function ($element) {
  783. return $element['parent_id'];
  784. });
  785. $this->assertEquals($expected, iterator_to_array($grouped));
  786. }
  787. /**
  788. * Tests shuffle
  789. *
  790. * @dataProvider simpleProvider
  791. * @return void
  792. */
  793. public function testShuffle($data)
  794. {
  795. $collection = (new Collection($data))->shuffle();
  796. $result = $collection->toArray();
  797. $this->assertCount(4, $result);
  798. $this->assertContains(1, $result);
  799. $this->assertContains(2, $result);
  800. $this->assertContains(3, $result);
  801. $this->assertContains(4, $result);
  802. }
  803. /**
  804. * Tests shuffle with duplicate keys.
  805. *
  806. * @return void
  807. */
  808. public function testShuffleDuplicateKeys()
  809. {
  810. $collection = (new Collection(['a' => 1]))->append(['a' => 2])->shuffle();
  811. $result = $collection->toArray();
  812. $this->assertCount(2, $result);
  813. $this->assertEquals([0, 1], array_keys($result));
  814. $this->assertContainsEquals(1, $result);
  815. $this->assertContainsEquals(2, $result);
  816. }
  817. /**
  818. * Tests sample
  819. *
  820. * @dataProvider simpleProvider
  821. * @return void
  822. */
  823. public function testSample($data)
  824. {
  825. $result = (new Collection($data))->sample(2)->toArray();
  826. $this->assertCount(2, $result);
  827. foreach ($result as $number) {
  828. $this->assertContains($number, [1, 2, 3, 4]);
  829. }
  830. }
  831. /**
  832. * Tests the sample() method with a traversable non-iterator
  833. *
  834. * @return void
  835. */
  836. public function testSampleWithTraversableNonIterator()
  837. {
  838. $collection = new Collection($this->datePeriod('2017-01-01', '2017-01-07'));
  839. $result = $collection->sample(3)->toList();
  840. $list = [
  841. '2017-01-01',
  842. '2017-01-02',
  843. '2017-01-03',
  844. '2017-01-04',
  845. '2017-01-05',
  846. '2017-01-06',
  847. ];
  848. $this->assertCount(3, $result);
  849. foreach ($result as $date) {
  850. $this->assertContains($date->format('Y-m-d'), $list);
  851. }
  852. }
  853. /**
  854. * Test toArray method
  855. *
  856. * @return void
  857. */
  858. public function testToArray()
  859. {
  860. $data = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
  861. $collection = new Collection($data);
  862. $this->assertEquals($data, $collection->toArray());
  863. }
  864. /**
  865. * Test toList method
  866. *
  867. * @dataProvider simpleProvider
  868. * @return void
  869. */
  870. public function testToList($data)
  871. {
  872. $collection = new Collection($data);
  873. $this->assertEquals([1, 2, 3, 4], $collection->toList());
  874. }
  875. /**
  876. * Test JSON encoding
  877. *
  878. * @return void
  879. */
  880. public function testToJson()
  881. {
  882. $data = [1, 2, 3, 4];
  883. $collection = new Collection($data);
  884. $this->assertEquals(json_encode($data), json_encode($collection));
  885. }
  886. /**
  887. * Tests that Count returns the number of elements
  888. *
  889. * @dataProvider simpleProvider
  890. * @return void
  891. */
  892. public function testCollectionCount($list)
  893. {
  894. $list = (new Collection($list))->buffered();
  895. $collection = new Collection($list);
  896. $this->assertSame(8, $collection->append($list)->count());
  897. }
  898. /**
  899. * Tests that countKeys returns the number of unique keys
  900. *
  901. * @dataProvider simpleProvider
  902. * @return void
  903. */
  904. public function testCollectionCountKeys($list)
  905. {
  906. $list = (new Collection($list))->buffered();
  907. $collection = new Collection($list);
  908. $this->assertSame(4, $collection->append($list)->countKeys());
  909. }
  910. /**
  911. * Tests take method
  912. *
  913. * @return void
  914. */
  915. public function testTake()
  916. {
  917. $data = [1, 2, 3, 4];
  918. $collection = new Collection($data);
  919. $taken = $collection->take(2);
  920. $this->assertEquals([1, 2], $taken->toArray());
  921. $taken = $collection->take(3);
  922. $this->assertEquals([1, 2, 3], $taken->toArray());
  923. $taken = $collection->take(500);
  924. $this->assertEquals([1, 2, 3, 4], $taken->toArray());
  925. $taken = $collection->take(1);
  926. $this->assertEquals([1], $taken->toArray());
  927. $taken = $collection->take();
  928. $this->assertEquals([1], $taken->toArray());
  929. $taken = $collection->take(2, 2);
  930. $this->assertEquals([2 => 3, 3 => 4], $taken->toArray());
  931. }
  932. /**
  933. * Tests the take() method with a traversable non-iterator
  934. *
  935. * @return void
  936. */
  937. public function testTakeWithTraversableNonIterator()
  938. {
  939. $collection = new Collection($this->datePeriod('2017-01-01', '2017-01-07'));
  940. $result = $collection->take(3, 1)->toList();
  941. $expected = [
  942. new \DateTime('2017-01-02'),
  943. new \DateTime('2017-01-03'),
  944. new \DateTime('2017-01-04'),
  945. ];
  946. $this->assertEquals($expected, $result);
  947. }
  948. /**
  949. * Tests match
  950. *
  951. * @return void
  952. */
  953. public function testMatch()
  954. {
  955. $items = [
  956. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  957. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  958. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  959. ];
  960. $collection = new Collection($items);
  961. $matched = $collection->match(['thing.parent_id' => 10, 'name' => 'baz']);
  962. $this->assertEquals([2 => $items[2]], $matched->toArray());
  963. $matched = $collection->match(['thing.parent_id' => 10]);
  964. $this->assertEquals(
  965. [0 => $items[0], 2 => $items[2]],
  966. $matched->toArray()
  967. );
  968. $matched = $collection->match(['thing.parent_id' => 500]);
  969. $this->assertEquals([], $matched->toArray());
  970. $matched = $collection->match(['parent_id' => 10, 'name' => 'baz']);
  971. $this->assertEquals([], $matched->toArray());
  972. }
  973. /**
  974. * Tests firstMatch
  975. *
  976. * @return void
  977. */
  978. public function testFirstMatch()
  979. {
  980. $items = [
  981. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  982. ['id' => 2, 'name' => 'bar', 'thing' => ['parent_id' => 11]],
  983. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  984. ];
  985. $collection = new Collection($items);
  986. $matched = $collection->firstMatch(['thing.parent_id' => 10]);
  987. $this->assertEquals(
  988. ['id' => 1, 'name' => 'foo', 'thing' => ['parent_id' => 10]],
  989. $matched
  990. );
  991. $matched = $collection->firstMatch(['thing.parent_id' => 10, 'name' => 'baz']);
  992. $this->assertEquals(
  993. ['id' => 3, 'name' => 'baz', 'thing' => ['parent_id' => 10]],
  994. $matched
  995. );
  996. }
  997. /**
  998. * Tests the append method
  999. *
  1000. * @return void
  1001. */
  1002. public function testAppend()
  1003. {
  1004. $collection = new Collection([1, 2, 3]);
  1005. $combined = $collection->append([4, 5, 6]);
  1006. $this->assertEquals([1, 2, 3, 4, 5, 6], $combined->toArray(false));
  1007. $collection = new Collection(['a' => 1, 'b' => 2]);
  1008. $combined = $collection->append(['c' => 3, 'a' => 4]);
  1009. $this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray());
  1010. }
  1011. /**
  1012. * Tests the appendItem method
  1013. */
  1014. public function testAppendItem()
  1015. {
  1016. $collection = new Collection([1, 2, 3]);
  1017. $combined = $collection->appendItem(4);
  1018. $this->assertEquals([1, 2, 3, 4], $combined->toArray(false));
  1019. $collection = new Collection(['a' => 1, 'b' => 2]);
  1020. $combined = $collection->appendItem(3, 'c');
  1021. $combined = $combined->appendItem(4, 'a');
  1022. $this->assertEquals(['a' => 4, 'b' => 2, 'c' => 3], $combined->toArray());
  1023. }
  1024. /**
  1025. * Tests the prepend method
  1026. */
  1027. public function testPrepend()
  1028. {
  1029. $collection = new Collection([1, 2, 3]);
  1030. $combined = $collection->prepend(['a']);
  1031. $this->assertEquals(['a', 1, 2, 3], $combined->toList());
  1032. $collection = new Collection(['c' => 3, 'd' => 4]);
  1033. $combined = $collection->prepend(['a' => 1, 'b' => 2]);
  1034. $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray());
  1035. }
  1036. /**
  1037. * Tests prependItem method
  1038. */
  1039. public function testPrependItem()
  1040. {
  1041. $collection = new Collection([1, 2, 3]);
  1042. $combined = $collection->prependItem('a');
  1043. $this->assertEquals(['a', 1, 2, 3], $combined->toList());
  1044. $collection = new Collection(['c' => 3, 'd' => 4]);
  1045. $combined = $collection->prependItem(2, 'b');
  1046. $combined = $combined->prependItem(1, 'a');
  1047. $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray());
  1048. }
  1049. /**
  1050. * Tests prependItem method
  1051. */
  1052. public function testPrependItemPreserveKeys()
  1053. {
  1054. $collection = new Collection([1, 2, 3]);
  1055. $combined = $collection->prependItem('a');
  1056. $this->assertEquals(['a', 1, 2, 3], $combined->toList());
  1057. $collection = new Collection(['c' => 3, 'd' => 4]);
  1058. $combined = $collection->prependItem(2, 'b');
  1059. $combined = $combined->prependItem(1, 'a');
  1060. $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $combined->toArray());
  1061. }
  1062. /**
  1063. * Tests the append method with iterator
  1064. */
  1065. public function testAppendIterator()
  1066. {
  1067. $collection = new Collection([1, 2, 3]);
  1068. $iterator = new ArrayIterator([4, 5, 6]);
  1069. $combined = $collection->append($iterator);
  1070. $this->assertEquals([1, 2, 3, 4, 5, 6], $combined->toList());
  1071. }
  1072. public function testAppendNotCollectionInstance()
  1073. {
  1074. $collection = new TestCollection([1, 2, 3]);
  1075. $combined = $collection->append([4, 5, 6]);
  1076. $this->assertEquals([1, 2, 3, 4, 5, 6], $combined->toList());
  1077. }
  1078. /**
  1079. * Tests that by calling compile internal iteration operations are not done
  1080. * more than once
  1081. *
  1082. * @return void
  1083. */
  1084. public function testCompile()
  1085. {
  1086. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  1087. $collection = new Collection($items);
  1088. $results = [];
  1089. $compiled = $collection
  1090. ->map(function ($value, $key) use (&$results) {
  1091. $results[] = [$key => $value];
  1092. return $value + 3;
  1093. })
  1094. ->compile();
  1095. $this->assertSame(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
  1096. $this->assertSame(['a' => 4, 'b' => 5, 'c' => 6], $compiled->toArray());
  1097. $this->assertSame([['a' => 1], ['b' => 2], ['c' => 3]], $results);
  1098. }
  1099. /**
  1100. * Tests converting a non rewindable iterator into a rewindable one using
  1101. * the buffered method.
  1102. *
  1103. * @return void
  1104. */
  1105. public function testBuffered()
  1106. {
  1107. $items = new NoRewindIterator(new ArrayIterator(['a' => 4, 'b' => 5, 'c' => 6]));
  1108. $buffered = (new Collection($items))->buffered();
  1109. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
  1110. $this->assertEquals(['a' => 4, 'b' => 5, 'c' => 6], $buffered->toArray());
  1111. }
  1112. /**
  1113. * Tests the combine method
  1114. *
  1115. * @return void
  1116. */
  1117. public function testCombine()
  1118. {
  1119. $items = [
  1120. ['id' => 1, 'name' => 'foo', 'parent' => 'a'],
  1121. ['id' => 2, 'name' => 'bar', 'parent' => 'b'],
  1122. ['id' => 3, 'name' => 'baz', 'parent' => 'a'],
  1123. ];
  1124. $collection = (new Collection($items))->combine('id', 'name');
  1125. $expected = [1 => 'foo', 2 => 'bar', 3 => 'baz'];
  1126. $this->assertEquals($expected, $collection->toArray());
  1127. $expected = ['foo' => 1, 'bar' => 2, 'baz' => 3];
  1128. $collection = (new Collection($items))->combine('name', 'id');
  1129. $this->assertEquals($expected, $collection->toArray());
  1130. $collection = (new Collection($items))->combine('id', 'name', 'parent');
  1131. $expected = ['a' => [1 => 'foo', 3 => 'baz'], 'b' => [2 => 'bar']];
  1132. $this->assertEquals($expected, $collection->toArray());
  1133. $expected = [
  1134. '0-1' => ['foo-0-1' => '0-1-foo'],
  1135. '1-2' => ['bar-1-2' => '1-2-bar'],
  1136. '2-3' => ['baz-2-3' => '2-3-baz'],
  1137. ];
  1138. $collection = (new Collection($items))->combine(
  1139. function ($value, $key) {
  1140. return $value['name'] . '-' . $key;
  1141. },
  1142. function ($value, $key) {
  1143. return $key . '-' . $value['name'];
  1144. },
  1145. function ($value, $key) {
  1146. return $key . '-' . $value['id'];
  1147. }
  1148. );
  1149. $this->assertEquals($expected, $collection->toArray());
  1150. $collection = (new Collection($items))->combine('id', 'crazy');
  1151. $this->assertEquals([1 => null, 2 => null, 3 => null], $collection->toArray());
  1152. }
  1153. /**
  1154. * Tests the nest method with only one level
  1155. *
  1156. * @return void
  1157. */
  1158. public function testNest()
  1159. {
  1160. $items = [
  1161. ['id' => 1, 'parent_id' => null],
  1162. ['id' => 2, 'parent_id' => 1],
  1163. ['id' => 3, 'parent_id' => 1],
  1164. ['id' => 4, 'parent_id' => 1],
  1165. ['id' => 5, 'parent_id' => 6],
  1166. ['id' => 6, 'parent_id' => null],
  1167. ['id' => 7, 'parent_id' => 1],
  1168. ['id' => 8, 'parent_id' => 6],
  1169. ['id' => 9, 'parent_id' => 6],
  1170. ['id' => 10, 'parent_id' => 6],
  1171. ];
  1172. $collection = (new Collection($items))->nest('id', 'parent_id');
  1173. $expected = [
  1174. [
  1175. 'id' => 1,
  1176. 'parent_id' => null,
  1177. 'children' => [
  1178. ['id' => 2, 'parent_id' => 1, 'children' => []],
  1179. ['id' => 3, 'parent_id' => 1, 'children' => []],
  1180. ['id' => 4, 'parent_id' => 1, 'children' => []],
  1181. ['id' => 7, 'parent_id' => 1, 'children' => []],
  1182. ],
  1183. ],
  1184. [
  1185. 'id' => 6,
  1186. 'parent_id' => null,
  1187. 'children' => [
  1188. ['id' => 5, 'parent_id' => 6, 'children' => []],
  1189. ['id' => 8, 'parent_id' => 6, 'children' => []],
  1190. ['id' => 9, 'parent_id' => 6, 'children' => []],
  1191. ['id' => 10, 'parent_id' => 6, 'children' => []],
  1192. ],
  1193. ],
  1194. ];
  1195. $this->assertEquals($expected, $collection->toArray());
  1196. }
  1197. /**
  1198. * Tests the nest method with alternate nesting key
  1199. *
  1200. * @return void
  1201. */
  1202. public function testNestAlternateNestingKey()
  1203. {
  1204. $items = [
  1205. ['id' => 1, 'parent_id' => null],
  1206. ['id' => 2, 'parent_id' => 1],
  1207. ['id' => 3, 'parent_id' => 1],
  1208. ['id' => 4, 'parent_id' => 1],
  1209. ['id' => 5, 'parent_id' => 6],
  1210. ['id' => 6, 'parent_id' => null],
  1211. ['id' => 7, 'parent_id' => 1],
  1212. ['id' => 8, 'parent_id' => 6],
  1213. ['id' => 9, 'parent_id' => 6],
  1214. ['id' => 10, 'parent_id' => 6],
  1215. ];
  1216. $collection = (new Collection($items))->nest('id', 'parent_id', 'nodes');
  1217. $expected = [
  1218. [
  1219. 'id' => 1,
  1220. 'parent_id' => null,
  1221. 'nodes' => [
  1222. ['id' => 2, 'parent_id' => 1, 'nodes' => []],
  1223. ['id' => 3, 'parent_id' => 1, 'nodes' => []],
  1224. ['id' => 4, 'parent_id' => 1, 'nodes' => []],
  1225. ['id' => 7, 'parent_id' => 1, 'nodes' => []],
  1226. ],
  1227. ],
  1228. [
  1229. 'id' => 6,
  1230. 'parent_id' => null,
  1231. 'nodes' => [
  1232. ['id' => 5, 'parent_id' => 6, 'nodes' => []],
  1233. ['id' => 8, 'parent_id' => 6, 'nodes' => []],
  1234. ['id' => 9, 'parent_id' => 6, 'nodes' => []],
  1235. ['id' => 10, 'parent_id' => 6, 'nodes' => []],
  1236. ],
  1237. ],
  1238. ];
  1239. $this->assertEquals($expected, $collection->toArray());
  1240. }
  1241. /**
  1242. * Tests the nest method with more than one level
  1243. *
  1244. * @return void
  1245. */
  1246. public function testNestMultiLevel()
  1247. {
  1248. $items = [
  1249. ['id' => 1, 'parent_id' => null],
  1250. ['id' => 2, 'parent_id' => 1],
  1251. ['id' => 3, 'parent_id' => 2],
  1252. ['id' => 4, 'parent_id' => 2],
  1253. ['id' => 5, 'parent_id' => 3],
  1254. ['id' => 6, 'parent_id' => null],
  1255. ['id' => 7, 'parent_id' => 3],
  1256. ['id' => 8, 'parent_id' => 4],
  1257. ['id' => 9, 'parent_id' => 6],
  1258. ['id' => 10, 'parent_id' => 6],
  1259. ];
  1260. $collection = (new Collection($items))->nest('id', 'parent_id', 'nodes');
  1261. $expected = [
  1262. [
  1263. 'id' => 1,
  1264. 'parent_id' => null,
  1265. 'nodes' => [
  1266. [
  1267. 'id' => 2,
  1268. 'parent_id' => 1,
  1269. 'nodes' => [
  1270. [
  1271. 'id' => 3,
  1272. 'parent_id' => 2,
  1273. 'nodes' => [
  1274. ['id' => 5, 'parent_id' => 3, 'nodes' => []],
  1275. ['id' => 7, 'parent_id' => 3, 'nodes' => []],
  1276. ],
  1277. ],
  1278. [
  1279. 'id' => 4,
  1280. 'parent_id' => 2,
  1281. 'nodes' => [
  1282. ['id' => 8, 'parent_id' => 4, 'nodes' => []],
  1283. ],
  1284. ],
  1285. ],
  1286. ],
  1287. ],
  1288. ],
  1289. [
  1290. 'id' => 6,
  1291. 'parent_id' => null,
  1292. 'nodes' => [
  1293. ['id' => 9, 'parent_id' => 6, 'nodes' => []],
  1294. ['id' => 10, 'parent_id' => 6, 'nodes' => []],
  1295. ],
  1296. ],
  1297. ];
  1298. $this->assertEquals($expected, $collection->toArray());
  1299. }
  1300. /**
  1301. * Tests the nest method with more than one level
  1302. *
  1303. * @return void
  1304. */
  1305. public function testNestMultiLevelAlternateNestingKey()
  1306. {
  1307. $items = [
  1308. ['id' => 1, 'parent_id' => null],
  1309. ['id' => 2, 'parent_id' => 1],
  1310. ['id' => 3, 'parent_id' => 2],
  1311. ['id' => 4, 'parent_id' => 2],
  1312. ['id' => 5, 'parent_id' => 3],
  1313. ['id' => 6, 'parent_id' => null],
  1314. ['id' => 7, 'parent_id' => 3],
  1315. ['id' => 8, 'parent_id' => 4],
  1316. ['id' => 9, 'parent_id' => 6],
  1317. ['id' => 10, 'parent_id' => 6],
  1318. ];
  1319. $collection = (new Collection($items))->nest('id', 'parent_id');
  1320. $expected = [
  1321. [
  1322. 'id' => 1,
  1323. 'parent_id' => null,
  1324. 'children' => [
  1325. [
  1326. 'id' => 2,
  1327. 'parent_id' => 1,
  1328. 'children' => [
  1329. [
  1330. 'id' => 3,
  1331. 'parent_id' => 2,
  1332. 'children' => [
  1333. ['id' => 5, 'parent_id' => 3, 'children' => []],
  1334. ['id' => 7, 'parent_id' => 3, 'children' => []],
  1335. ],
  1336. ],
  1337. [
  1338. 'id' => 4,
  1339. 'parent_id' => 2,
  1340. 'children' => [
  1341. ['id' => 8, 'parent_id' => 4, 'children' => []],
  1342. ],
  1343. ],
  1344. ],
  1345. ],
  1346. ],
  1347. ],
  1348. [
  1349. 'id' => 6,
  1350. 'parent_id' => null,
  1351. 'children' => [
  1352. ['id' => 9, 'parent_id' => 6, 'children' => []],
  1353. ['id' => 10, 'parent_id' => 6, 'children' => []],
  1354. ],
  1355. ],
  1356. ];
  1357. $this->assertEquals($expected, $collection->toArray());
  1358. }
  1359. /**
  1360. * Tests the nest method with more than one level
  1361. *
  1362. * @return void
  1363. */
  1364. public function testNestObjects()
  1365. {
  1366. $items = [
  1367. new ArrayObject(['id' => 1, 'parent_id' => null]),
  1368. new ArrayObject(['id' => 2, 'parent_id' => 1]),
  1369. new ArrayObject(['id' => 3, 'parent_id' => 2]),
  1370. new ArrayObject(['id' => 4, 'parent_id' => 2]),
  1371. new ArrayObject(['id' => 5, 'parent_id' => 3]),
  1372. new ArrayObject(['id' => 6, 'parent_id' => null]),
  1373. new ArrayObject(['id' => 7, 'parent_id' => 3]),
  1374. new ArrayObject(['id' => 8, 'parent_id' => 4]),
  1375. new ArrayObject(['id' => 9, 'parent_id' => 6]),
  1376. new ArrayObject(['id' => 10, 'parent_id' => 6]),
  1377. ];
  1378. $collection = (new Collection($items))->nest('id', 'parent_id');
  1379. $expected = [
  1380. new ArrayObject([
  1381. 'id' => 1,
  1382. 'parent_id' => null,
  1383. 'children' => [
  1384. new ArrayObject([
  1385. 'id' => 2,
  1386. 'parent_id' => 1,
  1387. 'children' => [
  1388. new ArrayObject([
  1389. 'id' => 3,
  1390. 'parent_id' => 2,
  1391. 'children' => [
  1392. new ArrayObject(['id' => 5, 'parent_id' => 3, 'children' => []]),
  1393. new ArrayObject(['id' => 7, 'parent_id' => 3, 'children' => []]),
  1394. ],
  1395. ]),
  1396. new ArrayObject([
  1397. 'id' => 4,
  1398. 'parent_id' => 2,
  1399. 'children' => [
  1400. new ArrayObject(['id' => 8, 'parent_id' => 4, 'children' => []]),
  1401. ],
  1402. ]),
  1403. ],
  1404. ]),
  1405. ],
  1406. ]),
  1407. new ArrayObject([
  1408. 'id' => 6,
  1409. 'parent_id' => null,
  1410. 'children' => [
  1411. new ArrayObject(['id' => 9, 'parent_id' => 6, 'children' => []]),
  1412. new ArrayObject(['id' => 10, 'parent_id' => 6, 'children' => []]),
  1413. ],
  1414. ]),
  1415. ];
  1416. $this->assertEquals($expected, $collection->toArray());
  1417. }
  1418. /**
  1419. * Tests the nest method with more than one level
  1420. *
  1421. * @return void
  1422. */
  1423. public function testNestObjectsAlternateNestingKey()
  1424. {
  1425. $items = [
  1426. new ArrayObject(['id' => 1, 'parent_id' => null]),
  1427. new ArrayObject(['id' => 2, 'parent_id' => 1]),
  1428. new ArrayObject(['id' => 3, 'parent_id' => 2]),
  1429. new ArrayObject(['id' => 4, 'parent_id' => 2]),
  1430. new ArrayObject(['id' => 5, 'parent_id' => 3]),
  1431. new ArrayObject(['id' => 6, 'parent_id' => null]),
  1432. new ArrayObject(['id' => 7, 'parent_id' => 3]),
  1433. new ArrayObject(['id' => 8, 'parent_id' => 4]),
  1434. new ArrayObject(['id' => 9, 'parent_id' => 6]),
  1435. new ArrayObject(['id' => 10, 'parent_id' => 6]),
  1436. ];
  1437. $collection = (new Collection($items))->nest('id', 'parent_id', 'nodes');
  1438. $expected = [
  1439. new ArrayObject([
  1440. 'id' => 1,
  1441. 'parent_id' => null,
  1442. 'nodes' => [
  1443. new ArrayObject([
  1444. 'id' => 2,
  1445. 'parent_id' => 1,
  1446. 'nodes' => [
  1447. new ArrayObject([
  1448. 'id' => 3,
  1449. 'parent_id' => 2,
  1450. 'nodes' => [
  1451. new ArrayObject(['id' => 5, 'parent_id' => 3, 'nodes' => []]),
  1452. new ArrayObject(['id' => 7, 'parent_id' => 3, 'nodes' => []]),
  1453. ],
  1454. ]),
  1455. new ArrayObject([
  1456. 'id' => 4,
  1457. 'parent_id' => 2,
  1458. 'nodes' => [
  1459. new ArrayObject(['id' => 8, 'parent_id' => 4, 'nodes' => []]),
  1460. ],
  1461. ]),
  1462. ],
  1463. ]),
  1464. ],
  1465. ]),
  1466. new ArrayObject([
  1467. 'id' => 6,
  1468. 'parent_id' => null,
  1469. 'nodes' => [
  1470. new ArrayObject(['id' => 9, 'parent_id' => 6, 'nodes' => []]),
  1471. new ArrayObject(['id' => 10, 'parent_id' => 6, 'nodes' => []]),
  1472. ],
  1473. ]),
  1474. ];
  1475. $this->assertEquals($expected, $collection->toArray());
  1476. }
  1477. /**
  1478. * Tests insert
  1479. *
  1480. * @return void
  1481. */
  1482. public function testInsert()
  1483. {
  1484. $items = [['a' => 1], ['b' => 2]];
  1485. $collection = new Collection($items);
  1486. $iterator = $collection->insert('c', [3, 4]);
  1487. $this->assertInstanceOf('Cake\Collection\Iterator\InsertIterator', $iterator);
  1488. $this->assertEquals(
  1489. [['a' => 1, 'c' => 3], ['b' => 2, 'c' => 4]],
  1490. iterator_to_array($iterator)
  1491. );
  1492. }
  1493. /**
  1494. * Provider for testing each of the directions for listNested
  1495. *
  1496. * @return array
  1497. */
  1498. public function nestedListProvider()
  1499. {
  1500. return [
  1501. ['desc', [1, 2, 3, 5, 7, 4, 8, 6, 9, 10]],
  1502. ['asc', [5, 7, 3, 8, 4, 2, 1, 9, 10, 6]],
  1503. ['leaves', [5, 7, 8, 9, 10]],
  1504. ];
  1505. }
  1506. /**
  1507. * Tests the listNested method with the default 'children' nesting key
  1508. *
  1509. * @dataProvider nestedListProvider
  1510. * @return void
  1511. */
  1512. public function testListNested($dir, $expected)
  1513. {
  1514. $items = [
  1515. ['id' => 1, 'parent_id' => null],
  1516. ['id' => 2, 'parent_id' => 1],
  1517. ['id' => 3, 'parent_id' => 2],
  1518. ['id' => 4, 'parent_id' => 2],
  1519. ['id' => 5, 'parent_id' => 3],
  1520. ['id' => 6, 'parent_id' => null],
  1521. ['id' => 7, 'parent_id' => 3],
  1522. ['id' => 8, 'parent_id' => 4],
  1523. ['id' => 9, 'parent_id' => 6],
  1524. ['id' => 10, 'parent_id' => 6],
  1525. ];
  1526. $collection = (new Collection($items))->nest('id', 'parent_id')->listNested($dir);
  1527. $this->assertEquals($expected, $collection->extract('id')->toArray(false));
  1528. }
  1529. /**
  1530. * Tests the listNested spacer output.
  1531. *
  1532. * @return void
  1533. */
  1534. public function testListNestedSpacer()
  1535. {
  1536. $items = [
  1537. ['id' => 1, 'parent_id' => null, 'name' => 'Birds'],
  1538. ['id' => 2, 'parent_id' => 1, 'name' => 'Land Birds'],
  1539. ['id' => 3, 'parent_id' => 1, 'name' => 'Eagle'],
  1540. ['id' => 4, 'parent_id' => 1, 'name' => 'Seagull'],
  1541. ['id' => 5, 'parent_id' => 6, 'name' => 'Clown Fish'],
  1542. ['id' => 6, 'parent_id' => null, 'name' => 'Fish'],
  1543. ];
  1544. $collection = (new Collection($items))->nest('id', 'parent_id')->listNested();
  1545. $expected = [
  1546. 'Birds',
  1547. '---Land Birds',
  1548. '---Eagle',
  1549. '---Seagull',
  1550. 'Fish',
  1551. '---Clown Fish',
  1552. ];
  1553. $this->assertSame($expected, $collection->printer('name', 'id', '---')->toList());
  1554. }
  1555. /**
  1556. * Tests using listNested with a different nesting key
  1557. *
  1558. * @return void
  1559. */
  1560. public function testListNestedCustomKey()
  1561. {
  1562. $items = [
  1563. ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
  1564. ['id' => 4, 'stuff' => [['id' => 5]]],
  1565. ];
  1566. $collection = (new Collection($items))->listNested('desc', 'stuff');
  1567. $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
  1568. }
  1569. /**
  1570. * Tests flattening the collection using a custom callable function
  1571. *
  1572. * @return void
  1573. */
  1574. public function testListNestedWithCallable()
  1575. {
  1576. $items = [
  1577. ['id' => 1, 'stuff' => [['id' => 2, 'stuff' => [['id' => 3]]]]],
  1578. ['id' => 4, 'stuff' => [['id' => 5]]],
  1579. ];
  1580. $collection = (new Collection($items))->listNested('desc', function ($item) {
  1581. return $item['stuff'] ?? [];
  1582. });
  1583. $this->assertEquals(range(1, 5), $collection->extract('id')->toArray(false));
  1584. }
  1585. /**
  1586. * Provider for sumOf tests
  1587. *
  1588. * @return array
  1589. */
  1590. public function sumOfProvider()
  1591. {
  1592. $items = [
  1593. ['invoice' => ['total' => 100]],
  1594. ['invoice' => ['total' => 200]],
  1595. ];
  1596. $floatItems = [
  1597. ['invoice' => ['total' => 100.0]],
  1598. ['invoice' => ['total' => 200.0]],
  1599. ];
  1600. return [
  1601. 'array' => [$items, 300],
  1602. 'iterator' => [$this->yieldItems($items), 300],
  1603. 'floatArray' => [$floatItems, 300.0],
  1604. 'floatIterator' => [$this->yieldItems($floatItems), 300.0],
  1605. ];
  1606. }
  1607. /**
  1608. * Tests the sumOf method
  1609. *
  1610. * @dataProvider sumOfProvider
  1611. * @return void
  1612. */
  1613. public function testSumOf($items, $expected)
  1614. {
  1615. $this->assertEquals($expected, (new Collection($items))->sumOf('invoice.total'));
  1616. }
  1617. /**
  1618. * Tests the sumOf method
  1619. *
  1620. * @dataProvider sumOfProvider
  1621. * @return void
  1622. */
  1623. public function testSumOfCallable($items, $expected)
  1624. {
  1625. $sum = (new Collection($items))->sumOf(function ($v) {
  1626. return $v['invoice']['total'];
  1627. });
  1628. $this->assertEquals($expected, $sum);
  1629. }
  1630. /**
  1631. * Tests the stopWhen method with a callable
  1632. *
  1633. * @dataProvider simpleProvider
  1634. * @return void
  1635. */
  1636. public function testStopWhenCallable($items)
  1637. {
  1638. $collection = (new Collection($items))->stopWhen(function ($v) {
  1639. return $v > 3;
  1640. });
  1641. $this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3], $collection->toArray());
  1642. }
  1643. /**
  1644. * Tests the stopWhen method with a matching array
  1645. *
  1646. * @return void
  1647. */
  1648. public function testStopWhenWithArray()
  1649. {
  1650. $items = [
  1651. ['foo' => 'bar'],
  1652. ['foo' => 'baz'],
  1653. ['foo' => 'foo'],
  1654. ];
  1655. $collection = (new Collection($items))->stopWhen(['foo' => 'baz']);
  1656. $this->assertEquals([['foo' => 'bar']], $collection->toArray());
  1657. }
  1658. /**
  1659. * Tests the unfold method
  1660. *
  1661. * @return void
  1662. */
  1663. public function testUnfold()
  1664. {
  1665. $items = [
  1666. [1, 2, 3, 4],
  1667. [5, 6],
  1668. [7, 8],
  1669. ];
  1670. $collection = (new Collection($items))->unfold();
  1671. $this->assertEquals(range(1, 8), $collection->toArray(false));
  1672. $items = [
  1673. [1, 2],
  1674. new Collection([3, 4]),
  1675. ];
  1676. $collection = (new Collection($items))->unfold();
  1677. $this->assertEquals(range(1, 4), $collection->toArray(false));
  1678. }
  1679. /**
  1680. * Tests the unfold method with empty levels
  1681. *
  1682. * @return void
  1683. */
  1684. public function testUnfoldEmptyLevels()
  1685. {
  1686. $items = [[], [1, 2], []];
  1687. $collection = (new Collection($items))->unfold();
  1688. $this->assertEquals(range(1, 2), $collection->toArray(false));
  1689. $items = [];
  1690. $collection = (new Collection($items))->unfold();
  1691. $this->assertEmpty($collection->toArray(false));
  1692. }
  1693. /**
  1694. * Tests the unfold when passing a callable
  1695. *
  1696. * @return void
  1697. */
  1698. public function testUnfoldWithCallable()
  1699. {
  1700. $items = [1, 2, 3];
  1701. $collection = (new Collection($items))->unfold(function ($item) {
  1702. return range($item, $item * 2);
  1703. });
  1704. $expected = [1, 2, 2, 3, 4, 3, 4, 5, 6];
  1705. $this->assertEquals($expected, $collection->toArray(false));
  1706. }
  1707. /**
  1708. * Tests the through() method
  1709. *
  1710. * @return void
  1711. */
  1712. public function testThrough()
  1713. {
  1714. $items = [1, 2, 3];
  1715. $collection = (new Collection($items))->through(function ($collection) {
  1716. return $collection->append($collection->toList());
  1717. });
  1718. $this->assertEquals([1, 2, 3, 1, 2, 3], $collection->toList());
  1719. }
  1720. /**
  1721. * Tests the through method when it returns an array
  1722. *
  1723. * @return void
  1724. */
  1725. public function testThroughReturnArray()
  1726. {
  1727. $items = [1, 2, 3];
  1728. $collection = (new Collection($items))->through(function ($collection) {
  1729. $list = $collection->toList();
  1730. return array_merge($list, $list);
  1731. });
  1732. $this->assertEquals([1, 2, 3, 1, 2, 3], $collection->toList());
  1733. }
  1734. /**
  1735. * Tests that the sortBy method does not die when something that is not a
  1736. * collection is passed
  1737. *
  1738. * @return void
  1739. */
  1740. public function testComplexSortBy()
  1741. {
  1742. $results = collection([3, 7])
  1743. ->unfold(function ($value) {
  1744. return [
  1745. ['sorting' => $value * 2],
  1746. ['sorting' => $value * 2],
  1747. ];
  1748. })
  1749. ->sortBy('sorting')
  1750. ->extract('sorting')
  1751. ->toList();
  1752. $this->assertEquals([14, 14, 6, 6], $results);
  1753. }
  1754. /**
  1755. * Tests __debugInfo() or debug() usage
  1756. *
  1757. * @return void
  1758. */
  1759. public function testDebug()
  1760. {
  1761. $items = [1, 2, 3];
  1762. $collection = new Collection($items);
  1763. $result = $collection->__debugInfo();
  1764. $expected = [
  1765. 'count' => 3,
  1766. ];
  1767. $this->assertSame($expected, $result);
  1768. // Calling it again will rewind
  1769. $result = $collection->__debugInfo();
  1770. $expected = [
  1771. 'count' => 3,
  1772. ];
  1773. $this->assertSame($expected, $result);
  1774. // Make sure it also works with non rewindable iterators
  1775. $iterator = new NoRewindIterator(new ArrayIterator($items));
  1776. $collection = new Collection($iterator);
  1777. $result = $collection->__debugInfo();
  1778. $expected = [
  1779. 'count' => 3,
  1780. ];
  1781. $this->assertSame($expected, $result);
  1782. // Calling it again will in this case not rewind
  1783. $result = $collection->__debugInfo();
  1784. $expected = [
  1785. 'count' => 0,
  1786. ];
  1787. $this->assertSame($expected, $result);
  1788. }
  1789. /**
  1790. * Tests the isEmpty() method
  1791. *
  1792. * @return void
  1793. */
  1794. public function testIsEmpty()
  1795. {
  1796. $collection = new Collection([1, 2, 3]);
  1797. $this->assertFalse($collection->isEmpty());
  1798. $collection = $collection->map(function () {
  1799. return null;
  1800. });
  1801. $this->assertFalse($collection->isEmpty());
  1802. $collection = $collection->filter();
  1803. $this->assertTrue($collection->isEmpty());
  1804. }
  1805. /**
  1806. * Tests the isEmpty() method does not consume data
  1807. * from buffered iterators.
  1808. *
  1809. * @return void
  1810. */
  1811. public function testIsEmptyDoesNotConsume()
  1812. {
  1813. $array = new \ArrayIterator([1, 2, 3]);
  1814. $inner = new \Cake\Collection\Iterator\BufferedIterator($array);
  1815. $collection = new Collection($inner);
  1816. $this->assertFalse($collection->isEmpty());
  1817. $this->assertCount(3, $collection->toArray());
  1818. }
  1819. /**
  1820. * Tests the zip() method
  1821. *
  1822. * @return void
  1823. */
  1824. public function testZip()
  1825. {
  1826. $collection = new Collection([1, 2]);
  1827. $zipped = $collection->zip([3, 4]);
  1828. $this->assertEquals([[1, 3], [2, 4]], $zipped->toList());
  1829. $collection = new Collection([1, 2]);
  1830. $zipped = $collection->zip([3]);
  1831. $this->assertEquals([[1, 3]], $zipped->toList());
  1832. $collection = new Collection([1, 2]);
  1833. $zipped = $collection->zip([3, 4], [5, 6], [7, 8], [9, 10, 11]);
  1834. $this->assertEquals([
  1835. [1, 3, 5, 7, 9],
  1836. [2, 4, 6, 8, 10],
  1837. ], $zipped->toList());
  1838. }
  1839. /**
  1840. * Tests the zipWith() method
  1841. *
  1842. * @return void
  1843. */
  1844. public function testZipWith()
  1845. {
  1846. $collection = new Collection([1, 2]);
  1847. $zipped = $collection->zipWith([3, 4], function ($a, $b) {
  1848. return $a * $b;
  1849. });
  1850. $this->assertEquals([3, 8], $zipped->toList());
  1851. $zipped = $collection->zipWith([3, 4], [5, 6, 7], function (...$args) {
  1852. return array_sum($args);
  1853. });
  1854. $this->assertEquals([9, 12], $zipped->toList());
  1855. }
  1856. /**
  1857. * Tests the skip() method
  1858. *
  1859. * @return void
  1860. */
  1861. public function testSkip()
  1862. {
  1863. $collection = new Collection([1, 2, 3, 4, 5]);
  1864. $this->assertEquals([3, 4, 5], $collection->skip(2)->toList());
  1865. $this->assertEquals([1, 2, 3, 4, 5], $collection->skip(0)->toList());
  1866. $this->assertEquals([4, 5], $collection->skip(3)->toList());
  1867. $this->assertEquals([5], $collection->skip(4)->toList());
  1868. }
  1869. /**
  1870. * Test skip() with an overflow
  1871. *
  1872. * @return void
  1873. */
  1874. public function testSkipOverflow()
  1875. {
  1876. $collection = new Collection([1, 2, 3]);
  1877. $this->assertEquals([], $collection->skip(3)->toArray());
  1878. $this->assertEquals([], $collection->skip(4)->toArray());
  1879. }
  1880. /**
  1881. * Tests the skip() method with a traversable non-iterator
  1882. *
  1883. * @return void
  1884. */
  1885. public function testSkipWithTraversableNonIterator()
  1886. {
  1887. $collection = new Collection($this->datePeriod('2017-01-01', '2017-01-07'));
  1888. $result = $collection->skip(3)->toList();
  1889. $expected = [
  1890. new \DateTime('2017-01-04'),
  1891. new \DateTime('2017-01-05'),
  1892. new \DateTime('2017-01-06'),
  1893. ];
  1894. $this->assertEquals($expected, $result);
  1895. }
  1896. /**
  1897. * Tests the first() method with a traversable non-iterator
  1898. *
  1899. * @return void
  1900. */
  1901. public function testFirstWithTraversableNonIterator()
  1902. {
  1903. $collection = new Collection($this->datePeriod('2017-01-01', '2017-01-07'));
  1904. $date = $collection->first();
  1905. $this->assertInstanceOf('DateTime', $date);
  1906. $this->assertSame('2017-01-01', $date->format('Y-m-d'));
  1907. }
  1908. /**
  1909. * Tests the last() method
  1910. *
  1911. * @return void
  1912. */
  1913. public function testLast()
  1914. {
  1915. $collection = new Collection([1, 2, 3]);
  1916. $this->assertSame(3, $collection->last());
  1917. $collection = $collection->map(function ($e) {
  1918. return $e * 2;
  1919. });
  1920. $this->assertSame(6, $collection->last());
  1921. }
  1922. /**
  1923. * Tests the last() method when on an empty collection
  1924. *
  1925. * @return void
  1926. */
  1927. public function testLastWithEmptyCollection()
  1928. {
  1929. $collection = new Collection([]);
  1930. $this->assertNull($collection->last());
  1931. }
  1932. /**
  1933. * Tests the last() method with a countable object
  1934. *
  1935. * @return void
  1936. */
  1937. public function testLastWithCountable()
  1938. {
  1939. $collection = new Collection(new ArrayObject([1, 2, 3]));
  1940. $this->assertSame(3, $collection->last());
  1941. }
  1942. /**
  1943. * Tests the last() method with an empty countable object
  1944. *
  1945. * @return void
  1946. */
  1947. public function testLastWithEmptyCountable()
  1948. {
  1949. $collection = new Collection(new ArrayObject([]));
  1950. $this->assertNull($collection->last());
  1951. }
  1952. /**
  1953. * Tests the last() method with a non-rewindable iterator
  1954. *
  1955. * @return void
  1956. */
  1957. public function testLastWithNonRewindableIterator()
  1958. {
  1959. $iterator = new NoRewindIterator(new ArrayIterator([1, 2, 3]));
  1960. $collection = new Collection($iterator);
  1961. $this->assertSame(3, $collection->last());
  1962. }
  1963. /**
  1964. * Tests the last() method with a traversable non-iterator
  1965. *
  1966. * @return void
  1967. */
  1968. public function testLastWithTraversableNonIterator()
  1969. {
  1970. $collection = new Collection($this->datePeriod('2017-01-01', '2017-01-07'));
  1971. $date = $collection->last();
  1972. $this->assertInstanceOf('DateTime', $date);
  1973. $this->assertSame('2017-01-06', $date->format('Y-m-d'));
  1974. }
  1975. /**
  1976. * Tests the takeLast() method
  1977. *
  1978. * @dataProvider simpleProvider
  1979. * @param array $data The data to test with.
  1980. * @return void
  1981. * @covers ::takeLast
  1982. */
  1983. public function testLastN($data)
  1984. {
  1985. $collection = new Collection($data);
  1986. $result = $collection->takeLast(3)->toArray();
  1987. $expected = ['b' => 2, 'c' => 3, 'd' => 4];
  1988. $this->assertEquals($expected, $result);
  1989. }
  1990. /**
  1991. * Tests the takeLast() method with overflow
  1992. *
  1993. * @dataProvider simpleProvider
  1994. * @param array $data The data to test with.
  1995. * @return void
  1996. * @covers ::takeLast
  1997. */
  1998. public function testLastNtWithOverflow($data)
  1999. {
  2000. $collection = new Collection($data);
  2001. $result = $collection->takeLast(10)->toArray();
  2002. $expected = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
  2003. $this->assertEquals($expected, $result);
  2004. }
  2005. /**
  2006. * Tests the takeLast() with an odd numbers collection
  2007. *
  2008. * @dataProvider simpleProvider
  2009. * @param array $data The data to test with.
  2010. * @return void
  2011. * @covers ::takeLast
  2012. */
  2013. public function testLastNtWithOddData($data)
  2014. {
  2015. $collection = new Collection($data);
  2016. $result = $collection->take(3)->takeLast(2)->toArray();
  2017. $expected = ['b' => 2, 'c' => 3];
  2018. $this->assertEquals($expected, $result);
  2019. }
  2020. /**
  2021. * Tests the takeLast() with countable collection
  2022. *
  2023. * @return void
  2024. * @covers ::takeLast
  2025. */
  2026. public function testLastNtWithCountable()
  2027. {
  2028. $rangeZeroToFive = range(0, 5);
  2029. $collection = new Collection(new CountableIterator($rangeZeroToFive));
  2030. $result = $collection->takeLast(2)->toList();
  2031. $this->assertEquals([4, 5], $result);
  2032. $collection = new Collection(new CountableIterator($rangeZeroToFive));
  2033. $result = $collection->takeLast(1)->toList();
  2034. $this->assertEquals([5], $result);
  2035. }
  2036. /**
  2037. * Tests the takeLast() with countable collection
  2038. *
  2039. * @dataProvider simpleProvider
  2040. * @param array $data The data to test with.
  2041. * @return void
  2042. * @covers ::takeLast
  2043. */
  2044. public function testLastNtWithNegative($data)
  2045. {
  2046. $collection = new Collection($data);
  2047. $this->expectException(\InvalidArgumentException::class);
  2048. $this->expectExceptionMessage('The takeLast method requires a number greater than 0.');
  2049. $collection->takeLast(-1)->toArray();
  2050. }
  2051. /**
  2052. * Tests sumOf with no parameters
  2053. *
  2054. * @return void
  2055. */
  2056. public function testSumOfWithIdentity()
  2057. {
  2058. $collection = new Collection([1, 2, 3]);
  2059. $this->assertSame(6, $collection->sumOf());
  2060. $collection = new Collection(['a' => 1, 'b' => 4, 'c' => 6]);
  2061. $this->assertSame(11, $collection->sumOf());
  2062. }
  2063. /**
  2064. * Tests using extract with the {*} notation
  2065. *
  2066. * @return void
  2067. */
  2068. public function testUnfoldedExtract()
  2069. {
  2070. $items = [
  2071. ['comments' => [['id' => 1], ['id' => 2]]],
  2072. ['comments' => [['id' => 3], ['id' => 4]]],
  2073. ['comments' => [['id' => 7], ['nope' => 8]]],
  2074. ];
  2075. $extracted = (new Collection($items))->extract('comments.{*}.id');
  2076. $this->assertEquals([1, 2, 3, 4, 7, null], $extracted->toArray());
  2077. $items = [
  2078. [
  2079. 'comments' => [
  2080. [
  2081. 'voters' => [['id' => 1], ['id' => 2]],
  2082. ],
  2083. ],
  2084. ],
  2085. [
  2086. 'comments' => [
  2087. [
  2088. 'voters' => [['id' => 3], ['id' => 4]],
  2089. ],
  2090. ],
  2091. ],
  2092. [
  2093. 'comments' => [
  2094. [
  2095. 'voters' => [['id' => 5], ['nope' => 'fail'], ['id' => 6]],
  2096. ],
  2097. ],
  2098. ],
  2099. [
  2100. 'comments' => [
  2101. [
  2102. 'not_voters' => [['id' => 5]],
  2103. ],
  2104. ],
  2105. ],
  2106. ['not_comments' => []],
  2107. ];
  2108. $extracted = (new Collection($items))->extract('comments.{*}.voters.{*}.id');
  2109. $expected = [1, 2, 3, 4, 5, null, 6];
  2110. $this->assertEquals($expected, $extracted->toArray());
  2111. $this->assertEquals($expected, $extracted->toList());
  2112. }
  2113. /**
  2114. * Tests serializing a simple collection
  2115. *
  2116. * @return void
  2117. */
  2118. public function testSerializeSimpleCollection()
  2119. {
  2120. $collection = new Collection([1, 2, 3]);
  2121. $serialized = serialize($collection);
  2122. $unserialized = unserialize($serialized);
  2123. $this->assertEquals($collection->toList(), $unserialized->toList());
  2124. $this->assertEquals($collection->toArray(), $unserialized->toArray());
  2125. }
  2126. /**
  2127. * Tests serialization when using append
  2128. *
  2129. * @return void
  2130. */
  2131. public function testSerializeWithAppendIterators()
  2132. {
  2133. $collection = new Collection([1, 2, 3]);
  2134. $collection = $collection->append(['a' => 4, 'b' => 5, 'c' => 6]);
  2135. $serialized = serialize($collection);
  2136. $unserialized = unserialize($serialized);
  2137. $this->assertEquals($collection->toList(), $unserialized->toList());
  2138. $this->assertEquals($collection->toArray(), $unserialized->toArray());
  2139. }
  2140. /**
  2141. * Tests serialization when using nested iterators
  2142. *
  2143. * @return void
  2144. */
  2145. public function testSerializeWithNestedIterators()
  2146. {
  2147. $collection = new Collection([1, 2, 3]);
  2148. $collection = $collection->map(function ($e) {
  2149. return $e * 3;
  2150. });
  2151. $collection = $collection->groupBy(function ($e) {
  2152. return $e % 2;
  2153. });
  2154. $serialized = serialize($collection);
  2155. $unserialized = unserialize($serialized);
  2156. $this->assertEquals($collection->toList(), $unserialized->toList());
  2157. $this->assertEquals($collection->toArray(), $unserialized->toArray());
  2158. }
  2159. /**
  2160. * Tests serializing a zip() call
  2161. *
  2162. * @return void
  2163. */
  2164. public function testSerializeWithZipIterator()
  2165. {
  2166. $collection = new Collection([4, 5]);
  2167. $collection = $collection->zip([1, 2]);
  2168. $serialized = serialize($collection);
  2169. $unserialized = unserialize($serialized);
  2170. $this->assertEquals($collection->toList(), $unserialized->toList());
  2171. }
  2172. /**
  2173. * Provider for some chunk tests
  2174. *
  2175. * @return array
  2176. */
  2177. public function chunkProvider()
  2178. {
  2179. $items = range(1, 10);
  2180. return [
  2181. 'array' => [$items],
  2182. 'iterator' => [$this->yieldItems($items)],
  2183. ];
  2184. }
  2185. /**
  2186. * Tests the chunk method with exact chunks
  2187. *
  2188. * @dataProvider chunkProvider
  2189. * @return void
  2190. */
  2191. public function testChunk($items)
  2192. {
  2193. $collection = new Collection($items);
  2194. $chunked = $collection->chunk(2)->toList();
  2195. $expected = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]];
  2196. $this->assertEquals($expected, $chunked);
  2197. }
  2198. /**
  2199. * Tests the chunk method with overflowing chunk size
  2200. *
  2201. * @return void
  2202. */
  2203. public function testChunkOverflow()
  2204. {
  2205. $collection = new Collection(range(1, 11));
  2206. $chunked = $collection->chunk(2)->toList();
  2207. $expected = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11]];
  2208. $this->assertEquals($expected, $chunked);
  2209. }
  2210. /**
  2211. * Tests the chunk method with non-scalar items
  2212. *
  2213. * @return void
  2214. */
  2215. public function testChunkNested()
  2216. {
  2217. $collection = new Collection([1, 2, 3, [4, 5], 6, [7, [8, 9], 10], 11]);
  2218. $chunked = $collection->chunk(2)->toList();
  2219. $expected = [[1, 2], [3, [4, 5]], [6, [7, [8, 9], 10]], [11]];
  2220. $this->assertEquals($expected, $chunked);
  2221. }
  2222. /**
  2223. * Tests the chunkWithKeys method with exact chunks
  2224. *
  2225. * @return void
  2226. */
  2227. public function testChunkWithKeys()
  2228. {
  2229. $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6]);
  2230. $chunked = $collection->chunkWithKeys(2)->toList();
  2231. $expected = [['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['e' => 5, 'f' => 6]];
  2232. $this->assertEquals($expected, $chunked);
  2233. }
  2234. /**
  2235. * Tests the chunkWithKeys method with overflowing chunk size
  2236. *
  2237. * @return void
  2238. */
  2239. public function testChunkWithKeysOverflow()
  2240. {
  2241. $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7]);
  2242. $chunked = $collection->chunkWithKeys(2)->toList();
  2243. $expected = [['a' => 1, 'b' => 2], ['c' => 3, 'd' => 4], ['e' => 5, 'f' => 6], ['g' => 7]];
  2244. $this->assertEquals($expected, $chunked);
  2245. }
  2246. /**
  2247. * Tests the chunkWithKeys method with non-scalar items
  2248. *
  2249. * @return void
  2250. */
  2251. public function testChunkWithKeysNested()
  2252. {
  2253. $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => [4, 5], 'e' => 6, 'f' => [7, [8, 9], 10], 'g' => 11]);
  2254. $chunked = $collection->chunkWithKeys(2)->toList();
  2255. $expected = [['a' => 1, 'b' => 2], ['c' => 3, 'd' => [4, 5]], ['e' => 6, 'f' => [7, [8, 9], 10]], ['g' => 11]];
  2256. $this->assertEquals($expected, $chunked);
  2257. }
  2258. /**
  2259. * Tests the chunkWithKeys method without preserving keys
  2260. *
  2261. * @return void
  2262. */
  2263. public function testChunkWithKeysNoPreserveKeys()
  2264. {
  2265. $collection = new Collection(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7]);
  2266. $chunked = $collection->chunkWithKeys(2, false)->toList();
  2267. $expected = [[0 => 1, 1 => 2], [0 => 3, 1 => 4], [0 => 5, 1 => 6], [0 => 7]];
  2268. $this->assertEquals($expected, $chunked);
  2269. }
  2270. /**
  2271. * Tests cartesianProduct
  2272. *
  2273. * @return void
  2274. */
  2275. public function testCartesianProduct()
  2276. {
  2277. $collection = new Collection([]);
  2278. $result = $collection->cartesianProduct();
  2279. $expected = [];
  2280. $this->assertEquals($expected, $result->toList());
  2281. $collection = new Collection([['A', 'B', 'C'], [1, 2, 3]]);
  2282. $result = $collection->cartesianProduct();
  2283. $expected = [
  2284. ['A', 1],
  2285. ['A', 2],
  2286. ['A', 3],
  2287. ['B', 1],
  2288. ['B', 2],
  2289. ['B', 3],
  2290. ['C', 1],
  2291. ['C', 2],
  2292. ['C', 3],
  2293. ];
  2294. $this->assertEquals($expected, $result->toList());
  2295. $collection = new Collection([[1, 2, 3], ['A', 'B', 'C'], ['a', 'b', 'c']]);
  2296. $result = $collection->cartesianProduct(function ($value) {
  2297. return [strval($value[0]) . $value[1] . $value[2]];
  2298. }, function ($value) {
  2299. return $value[0] >= 2;
  2300. });
  2301. $expected = [
  2302. ['2Aa'],
  2303. ['2Ab'],
  2304. ['2Ac'],
  2305. ['2Ba'],
  2306. ['2Bb'],
  2307. ['2Bc'],
  2308. ['2Ca'],
  2309. ['2Cb'],
  2310. ['2Cc'],
  2311. ['3Aa'],
  2312. ['3Ab'],
  2313. ['3Ac'],
  2314. ['3Ba'],
  2315. ['3Bb'],
  2316. ['3Bc'],
  2317. ['3Ca'],
  2318. ['3Cb'],
  2319. ['3Cc'],
  2320. ];
  2321. $this->assertEquals($expected, $result->toList());
  2322. $collection = new Collection([['1', '2', '3', '4'], ['A', 'B', 'C'], ['name', 'surname', 'telephone']]);
  2323. $result = $collection->cartesianProduct(function ($value) {
  2324. return [$value[0] => [$value[1] => $value[2]]];
  2325. }, function ($value) {
  2326. return $value[2] !== 'surname';
  2327. });
  2328. $expected = [
  2329. [1 => ['A' => 'name']],
  2330. [1 => ['A' => 'telephone']],
  2331. [1 => ['B' => 'name']],
  2332. [1 => ['B' => 'telephone']],
  2333. [1 => ['C' => 'name']],
  2334. [1 => ['C' => 'telephone']],
  2335. [2 => ['A' => 'name']],
  2336. [2 => ['A' => 'telephone']],
  2337. [2 => ['B' => 'name']],
  2338. [2 => ['B' => 'telephone']],
  2339. [2 => ['C' => 'name']],
  2340. [2 => ['C' => 'telephone']],
  2341. [3 => ['A' => 'name']],
  2342. [3 => ['A' => 'telephone']],
  2343. [3 => ['B' => 'name']],
  2344. [3 => ['B' => 'telephone']],
  2345. [3 => ['C' => 'name']],
  2346. [3 => ['C' => 'telephone']],
  2347. [4 => ['A' => 'name']],
  2348. [4 => ['A' => 'telephone']],
  2349. [4 => ['B' => 'name']],
  2350. [4 => ['B' => 'telephone']],
  2351. [4 => ['C' => 'name']],
  2352. [4 => ['C' => 'telephone']],
  2353. ];
  2354. $this->assertEquals($expected, $result->toList());
  2355. $collection = new Collection([
  2356. [
  2357. 'name1' => 'alex',
  2358. 'name2' => 'kostas',
  2359. 0 => 'leon',
  2360. ],
  2361. [
  2362. 'val1' => 'alex@example.com',
  2363. 24 => 'kostas@example.com',
  2364. 'val2' => 'leon@example.com',
  2365. ],
  2366. ]);
  2367. $result = $collection->cartesianProduct();
  2368. $expected = [
  2369. ['alex', 'alex@example.com'],
  2370. ['alex', 'kostas@example.com'],
  2371. ['alex', 'leon@example.com'],
  2372. ['kostas', 'alex@example.com'],
  2373. ['kostas', 'kostas@example.com'],
  2374. ['kostas', 'leon@example.com'],
  2375. ['leon', 'alex@example.com'],
  2376. ['leon', 'kostas@example.com'],
  2377. ['leon', 'leon@example.com'],
  2378. ];
  2379. $this->assertEquals($expected, $result->toList());
  2380. }
  2381. /**
  2382. * Tests that an exception is thrown if the cartesian product is called with multidimensional arrays
  2383. *
  2384. * @return void
  2385. */
  2386. public function testCartesianProductMultidimensionalArray()
  2387. {
  2388. $this->expectException(\LogicException::class);
  2389. $collection = new Collection([
  2390. [
  2391. 'names' => [
  2392. 'alex', 'kostas', 'leon',
  2393. ],
  2394. ],
  2395. [
  2396. 'locations' => [
  2397. 'crete', 'london', 'paris',
  2398. ],
  2399. ],
  2400. ]);
  2401. $result = $collection->cartesianProduct();
  2402. }
  2403. public function testTranspose()
  2404. {
  2405. $collection = new Collection([
  2406. ['Products', '2012', '2013', '2014'],
  2407. ['Product A', '200', '100', '50'],
  2408. ['Product B', '300', '200', '100'],
  2409. ['Product C', '400', '300', '200'],
  2410. ['Product D', '500', '400', '300'],
  2411. ]);
  2412. $transposed = $collection->transpose();
  2413. $expected = [
  2414. ['Products', 'Product A', 'Product B', 'Product C', 'Product D'],
  2415. ['2012', '200', '300', '400', '500'],
  2416. ['2013', '100', '200', '300', '400'],
  2417. ['2014', '50', '100', '200', '300'],
  2418. ];
  2419. $this->assertEquals($expected, $transposed->toList());
  2420. }
  2421. /**
  2422. * Tests that provided arrays do not have even length
  2423. *
  2424. * @return void
  2425. */
  2426. public function testTransposeUnEvenLengthShouldThrowException()
  2427. {
  2428. $this->expectException(\LogicException::class);
  2429. $collection = new Collection([
  2430. ['Products', '2012', '2013', '2014'],
  2431. ['Product A', '200', '100', '50'],
  2432. ['Product B', '300'],
  2433. ['Product C', '400', '300'],
  2434. ]);
  2435. $collection->transpose();
  2436. }
  2437. /**
  2438. * Yields all the elements as passed
  2439. *
  2440. * @param array $items the elements to be yielded
  2441. * @return \Generator<array>
  2442. */
  2443. protected function yieldItems(array $items)
  2444. {
  2445. foreach ($items as $k => $v) {
  2446. yield $k => $v;
  2447. }
  2448. }
  2449. /**
  2450. * Create a DatePeriod object.
  2451. *
  2452. * @param string $start Start date
  2453. * @param string $end End date
  2454. * @return \DatePeriod
  2455. */
  2456. protected function datePeriod($start, $end)
  2457. {
  2458. return new \DatePeriod(new \DateTime($start), new \DateInterval('P1D'), new \DateTime($end));
  2459. }
  2460. /**
  2461. * Tests to ensure that collection classes extending ArrayIterator work as expected.
  2462. *
  2463. * @return void
  2464. */
  2465. public function testArrayIteratorExtend()
  2466. {
  2467. $iterator = new TestIterator(range(0, 10));
  2468. $this->assertTrue(method_exists($iterator, 'checkValues'));
  2469. $this->assertTrue($iterator->checkValues());
  2470. //We need to perform at least two collection operation to trigger the issue.
  2471. $newIterator = $iterator
  2472. ->filter(function ($item) {
  2473. return $item < 5;
  2474. })
  2475. ->reject(function ($item) {
  2476. return $item > 2;
  2477. });
  2478. $this->assertTrue(method_exists($newIterator, 'checkValues'), 'Our method has gone missing!');
  2479. $this->assertTrue($newIterator->checkValues());
  2480. $this->assertCount(3, $newIterator->toArray());
  2481. }
  2482. /**
  2483. * Tests that elements in a lazy collection are not fetched immediately.
  2484. *
  2485. * @return void
  2486. */
  2487. public function testLazy()
  2488. {
  2489. $items = ['a' => 1, 'b' => 2, 'c' => 3];
  2490. $collection = (new Collection($items))->lazy();
  2491. $callable = $this->getMockBuilder(stdClass::class)
  2492. ->addMethods(['__invoke'])
  2493. ->getMock();
  2494. $callable->expects($this->never())->method('__invoke');
  2495. $collection->filter($callable)->filter($callable);
  2496. }
  2497. }