SupportLazyCollectionIsLazyTest.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Exception;
  4. use Illuminate\Support\ItemNotFoundException;
  5. use Illuminate\Support\LazyCollection;
  6. use Illuminate\Support\MultipleItemsFoundException;
  7. use PHPUnit\Framework\TestCase;
  8. use stdClass;
  9. class SupportLazyCollectionIsLazyTest extends TestCase
  10. {
  11. use Concerns\CountsEnumerations;
  12. public function testMakeWithClosureIsLazy()
  13. {
  14. [$closure, $recorder] = $this->makeGeneratorFunctionWithRecorder();
  15. LazyCollection::make($closure);
  16. $this->assertEquals([], $recorder->all());
  17. }
  18. public function testMakeWithLazyCollectionIsLazy()
  19. {
  20. $this->assertDoesNotEnumerate(function ($collection) {
  21. LazyCollection::make($collection);
  22. });
  23. }
  24. public function testMakeWithGeneratorIsNotLazy()
  25. {
  26. [$closure, $recorder] = $this->makeGeneratorFunctionWithRecorder(5);
  27. LazyCollection::make($closure());
  28. $this->assertEquals([1, 2, 3, 4, 5], $recorder->all());
  29. }
  30. public function testEagerEnumeratesOnce()
  31. {
  32. $this->assertEnumeratesOnce(function ($collection) {
  33. $collection = $collection->eager();
  34. $collection->count();
  35. $collection->all();
  36. });
  37. }
  38. public function testChunkIsLazy()
  39. {
  40. $this->assertDoesNotEnumerate(function ($collection) {
  41. $collection->chunk(3);
  42. });
  43. $this->assertEnumerates(15, function ($collection) {
  44. $collection->chunk(5)->take(3)->all();
  45. });
  46. }
  47. public function testChunkWhileIsLazy()
  48. {
  49. $collection = LazyCollection::make(['A', 'A', 'B', 'B', 'C', 'C', 'C']);
  50. $this->assertDoesNotEnumerateCollection($collection, function ($collection) {
  51. $collection->chunkWhile(function ($current, $key, $chunk) {
  52. return $current === $chunk->last();
  53. });
  54. });
  55. $this->assertEnumeratesCollection($collection, 3, function ($collection) {
  56. $collection->chunkWhile(function ($current, $key, $chunk) {
  57. return $current === $chunk->last();
  58. })->first();
  59. });
  60. $this->assertEnumeratesCollectionOnce($collection, function ($collection) {
  61. $collection->chunkWhile(function ($current, $key, $chunk) {
  62. return $current === $chunk->last();
  63. })->all();
  64. });
  65. }
  66. public function testCollapseIsLazy()
  67. {
  68. $collection = LazyCollection::make([
  69. [1, 2, 3],
  70. [4, 5, 6],
  71. [7, 8, 9],
  72. ]);
  73. $this->assertDoesNotEnumerateCollection($collection, function ($collection) {
  74. $collection->collapse();
  75. });
  76. $this->assertEnumeratesCollection($collection, 1, function ($collection) {
  77. $collection->collapse()->take(3)->all();
  78. });
  79. }
  80. public function testCombineIsLazy()
  81. {
  82. $firstEnumerations = 0;
  83. $secondEnumerations = 0;
  84. $first = $this->countEnumerations($this->make([1, 2]), $firstEnumerations);
  85. $second = $this->countEnumerations($this->make([1, 2]), $secondEnumerations);
  86. $first->combine($second);
  87. $this->assertEnumerations(0, $firstEnumerations);
  88. $this->assertEnumerations(0, $secondEnumerations);
  89. $first->combine($second)->take(1)->all();
  90. $this->assertEnumerations(1, $firstEnumerations);
  91. $this->assertEnumerations(1, $secondEnumerations);
  92. }
  93. public function testConcatIsLazy()
  94. {
  95. $firstEnumerations = 0;
  96. $secondEnumerations = 0;
  97. $first = $this->countEnumerations($this->make([1, 2]), $firstEnumerations);
  98. $second = $this->countEnumerations($this->make([1, 2]), $secondEnumerations);
  99. $first->concat($second);
  100. $this->assertEnumerations(0, $firstEnumerations);
  101. $this->assertEnumerations(0, $secondEnumerations);
  102. $first->concat($second)->take(2)->all();
  103. $this->assertEnumerations(2, $firstEnumerations);
  104. $this->assertEnumerations(0, $secondEnumerations);
  105. $firstEnumerations = 0;
  106. $secondEnumerations = 0;
  107. $first->concat($second)->take(3)->all();
  108. $this->assertEnumerations(2, $firstEnumerations);
  109. $this->assertEnumerations(1, $secondEnumerations);
  110. }
  111. public function testContainsIsLazy()
  112. {
  113. $this->assertEnumerates(5, function ($collection) {
  114. $collection->contains(5);
  115. });
  116. }
  117. public function testContainsStrictIsLazy()
  118. {
  119. $this->assertEnumerates(5, function ($collection) {
  120. $collection->containsStrict(5);
  121. });
  122. }
  123. public function testCountEnumeratesOnce()
  124. {
  125. $this->assertEnumeratesOnce(function ($collection) {
  126. $collection->count();
  127. });
  128. }
  129. public function testCountByIsLazy()
  130. {
  131. $this->assertDoesNotEnumerate(function ($collection) {
  132. $collection->countBy();
  133. });
  134. $this->assertEnumeratesCollectionOnce(
  135. $this->make([1, 2, 2, 3]),
  136. function ($collection) {
  137. $collection->countBy()->all();
  138. }
  139. );
  140. }
  141. public function testCrossJoinIsLazy()
  142. {
  143. $this->assertDoesNotEnumerate(function ($collection) {
  144. $collection->crossJoin([1]);
  145. });
  146. $this->assertEnumeratesOnce(function ($collection) {
  147. $collection->crossJoin([1], [2])->all();
  148. });
  149. }
  150. public function testDiffIsLazy()
  151. {
  152. $this->assertDoesNotEnumerate(function ($collection) {
  153. $collection->diff([1, 2]);
  154. });
  155. $this->assertEnumeratesOnce(function ($collection) {
  156. $collection->diff([1, 2])->all();
  157. });
  158. }
  159. public function testDiffAssocIsLazy()
  160. {
  161. $this->assertDoesNotEnumerate(function ($collection) {
  162. $collection->diffAssoc([1, 2]);
  163. });
  164. $this->assertEnumeratesOnce(function ($collection) {
  165. $collection->diffAssoc([1, 2])->all();
  166. });
  167. }
  168. public function testDiffAssocUsingIsLazy()
  169. {
  170. $this->assertDoesNotEnumerate(function ($collection) {
  171. $collection->diffAssocUsing([1, 2], 'strcasecmp');
  172. });
  173. $this->assertEnumeratesOnce(function ($collection) {
  174. $collection->diffAssocUsing([1, 2], 'strcasecmp')->all();
  175. });
  176. }
  177. public function testDiffKeysIsLazy()
  178. {
  179. $this->assertDoesNotEnumerate(function ($collection) {
  180. $collection->diffKeys([1, 2]);
  181. });
  182. $this->assertEnumeratesOnce(function ($collection) {
  183. $collection->diffKeys([1, 2])->all();
  184. });
  185. }
  186. public function testDiffKeysUsingIsLazy()
  187. {
  188. $this->assertDoesNotEnumerate(function ($collection) {
  189. $collection->diffKeysUsing([1, 2], 'strcasecmp');
  190. });
  191. $this->assertEnumeratesOnce(function ($collection) {
  192. $collection->diffKeysUsing([1, 2], 'strcasecmp')->all();
  193. });
  194. }
  195. public function testDiffUsingIsLazy()
  196. {
  197. $this->assertDoesNotEnumerate(function ($collection) {
  198. $collection->diffUsing([1, 2], 'strcasecmp');
  199. });
  200. $this->assertEnumeratesOnce(function ($collection) {
  201. $collection->diffUsing([1, 2], 'strcasecmp')->all();
  202. });
  203. }
  204. public function testDuplicatesIsLazy()
  205. {
  206. $this->assertDoesNotEnumerate(function ($collection) {
  207. $collection->duplicates();
  208. });
  209. $this->assertEnumeratesOnce(function ($collection) {
  210. $collection->duplicates()->all();
  211. });
  212. }
  213. public function testDuplicatesStrictIsLazy()
  214. {
  215. $this->assertDoesNotEnumerate(function ($collection) {
  216. $collection->duplicatesStrict();
  217. });
  218. $this->assertEnumeratesOnce(function ($collection) {
  219. $collection->duplicatesStrict()->all();
  220. });
  221. }
  222. public function testEachIsLazy()
  223. {
  224. $this->assertEnumerates(5, function ($collection) {
  225. $collection->each(function ($value, $key) {
  226. if ($value == 5) {
  227. return false;
  228. }
  229. });
  230. });
  231. $this->assertEnumeratesOnce(function ($collection) {
  232. $collection->each(function ($value, $key) {
  233. // Silence is golden!
  234. });
  235. });
  236. $this->assertEnumerates(5, function ($collection) {
  237. foreach ($collection as $key => $value) {
  238. if ($value == 5) {
  239. return false;
  240. }
  241. }
  242. });
  243. $this->assertEnumeratesOnce(function ($collection) {
  244. foreach ($collection as $key => $value) {
  245. // Silence is golden!
  246. }
  247. });
  248. }
  249. public function testEachSpreadIsLazy()
  250. {
  251. $data = $this->make([[1, 2], [3, 4], [5, 6], [7, 8]]);
  252. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  253. $collection->eachSpread(function ($first, $second, $key) {
  254. if ($first == 3) {
  255. return false;
  256. }
  257. });
  258. });
  259. $this->assertEnumeratesCollectionOnce($data, function ($collection) {
  260. $collection->eachSpread(function ($first, $second, $key) {
  261. // Silence is golden!
  262. });
  263. });
  264. }
  265. public function testEveryIsLazy()
  266. {
  267. $this->assertEnumerates(2, function ($collection) {
  268. $collection->every(function ($value) {
  269. return $value == 1;
  270. });
  271. });
  272. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3]]);
  273. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  274. $collection->every('a', 1);
  275. });
  276. }
  277. public function testExceptIsLazy()
  278. {
  279. $this->assertDoesNotEnumerate(function ($collection) {
  280. $collection->except([1, 2]);
  281. });
  282. $this->assertEnumeratesOnce(function ($collection) {
  283. $collection->except([1, 2])->all();
  284. });
  285. }
  286. public function testFilterIsLazy()
  287. {
  288. $this->assertDoesNotEnumerate(function ($collection) {
  289. $collection->filter(function ($value) {
  290. return $value > 5;
  291. });
  292. });
  293. $this->assertEnumeratesOnce(function ($collection) {
  294. $collection->filter(function ($value) {
  295. return $value > 5;
  296. })->all();
  297. });
  298. }
  299. public function testFirstIsLazy()
  300. {
  301. $this->assertEnumerates(1, function ($collection) {
  302. $collection->first();
  303. });
  304. $this->assertEnumerates(2, function ($collection) {
  305. $collection->first(function ($value) {
  306. return $value == 2;
  307. });
  308. });
  309. }
  310. public function testFirstWhereIsLazy()
  311. {
  312. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3]]);
  313. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  314. $collection->firstWhere('a', 2);
  315. });
  316. }
  317. public function testFlatMapIsLazy()
  318. {
  319. $data = $this->make([1, 2, 3, 4, 5]);
  320. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  321. $collection->flatMap(function ($values) {
  322. return array_sum($values);
  323. });
  324. });
  325. $this->assertEnumeratesCollection($data, 3, function ($collection) {
  326. $collection->flatMap(function ($value) {
  327. return range(1, $value);
  328. })->take(5)->all();
  329. });
  330. }
  331. public function testFlattenIsLazy()
  332. {
  333. $data = $this->make([1, [2, 3], [4, 5], [6, 7]]);
  334. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  335. $collection->flatten();
  336. });
  337. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  338. $collection->flatten()->take(3)->all();
  339. });
  340. }
  341. public function testFlipIsLazy()
  342. {
  343. $this->assertDoesNotEnumerate(function ($collection) {
  344. $collection->flip();
  345. });
  346. $this->assertEnumerates(2, function ($collection) {
  347. $collection->flip()->take(2)->all();
  348. });
  349. }
  350. public function testForPageIsLazy()
  351. {
  352. $this->assertDoesNotEnumerate(function ($collection) {
  353. $collection->forPage(2, 10);
  354. });
  355. $this->assertEnumerates(20, function ($collection) {
  356. $collection->forPage(2, 10)->all();
  357. });
  358. }
  359. public function testGetIsLazy()
  360. {
  361. $this->assertEnumerates(5, function ($collection) {
  362. $collection->get(4);
  363. });
  364. }
  365. public function testGroupByIsLazy()
  366. {
  367. $this->assertDoesNotEnumerate(function ($collection) {
  368. $collection->groupBy(function ($value) {
  369. return $value % 5;
  370. });
  371. });
  372. $this->assertEnumeratesOnce(function ($collection) {
  373. $collection->groupBy(function ($value) {
  374. return $value % 5;
  375. })->all();
  376. });
  377. }
  378. public function testHasIsLazy()
  379. {
  380. $this->assertEnumerates(5, function ($collection) {
  381. $collection->has(4);
  382. });
  383. $this->assertEnumeratesOnce(function ($collection) {
  384. $collection->has('non-existent');
  385. });
  386. }
  387. public function testHasAnyIsLazy()
  388. {
  389. $this->assertEnumerates(5, function ($collection) {
  390. $collection->hasAny(4);
  391. });
  392. $this->assertEnumerates(2, function ($collection) {
  393. $collection->hasAny([1, 4]);
  394. });
  395. $this->assertEnumeratesOnce(function ($collection) {
  396. $collection->hasAny(['non', 'existent']);
  397. });
  398. }
  399. public function testImplodeEnumeratesOnce()
  400. {
  401. $this->assertEnumeratesOnce(function ($collection) {
  402. $collection->implode(', ');
  403. });
  404. }
  405. public function testIntersectIsLazy()
  406. {
  407. $this->assertDoesNotEnumerate(function ($collection) {
  408. $collection->intersect([1, 2, 3]);
  409. });
  410. $this->assertEnumeratesOnce(function ($collection) {
  411. $collection->intersect([1, 2, 3])->all();
  412. });
  413. }
  414. public function testIntersectByKeysIsLazy()
  415. {
  416. $this->assertDoesNotEnumerate(function ($collection) {
  417. $collection->intersectByKeys([1, 2, 3]);
  418. });
  419. $this->assertEnumeratesOnce(function ($collection) {
  420. $collection->intersectByKeys([1, 2, 3])->all();
  421. });
  422. }
  423. public function testIsEmptyIsLazy()
  424. {
  425. $this->assertEnumerates(1, function ($collection) {
  426. $collection->isEmpty();
  427. });
  428. }
  429. public function testIsNotEmptyIsLazy()
  430. {
  431. $this->assertEnumerates(1, function ($collection) {
  432. $collection->isNotEmpty();
  433. });
  434. }
  435. public function testContainsOneItemIsLazy()
  436. {
  437. $this->assertEnumerates(2, function ($collection) {
  438. $collection->containsOneItem();
  439. });
  440. }
  441. public function testJoinIsLazy()
  442. {
  443. $this->assertEnumeratesOnce(function ($collection) {
  444. $collection->join(', ', ' and ');
  445. });
  446. }
  447. public function testJsonSerializeEnumeratesOnce()
  448. {
  449. $this->assertEnumeratesOnce(function ($collection) {
  450. $collection->jsonSerialize();
  451. });
  452. }
  453. public function testKeyByIsLazy()
  454. {
  455. $this->assertDoesNotEnumerate(function ($collection) {
  456. $collection->keyBy(function ($value) {
  457. return "key-of-{$value}";
  458. });
  459. });
  460. $this->assertEnumerates(2, function ($collection) {
  461. $collection->keyBy(function ($value) {
  462. return "key-of-{$value}";
  463. })->take(2)->all();
  464. });
  465. }
  466. public function testKeysIsLazy()
  467. {
  468. $this->assertDoesNotEnumerate(function ($collection) {
  469. $collection->keys();
  470. });
  471. $this->assertEnumerates(2, function ($collection) {
  472. $collection->keys()->take(2)->all();
  473. });
  474. }
  475. public function testLastEnumeratesOnce()
  476. {
  477. $this->assertEnumeratesOnce(function ($collection) {
  478. $collection->last();
  479. });
  480. }
  481. public function testMapIsLazy()
  482. {
  483. $this->assertDoesNotEnumerate(function ($collection) {
  484. $collection->map(function ($value) {
  485. return $value + 1;
  486. });
  487. });
  488. $this->assertEnumerates(2, function ($collection) {
  489. $collection->map(function ($value) {
  490. return $value + 1;
  491. })->take(2)->all();
  492. });
  493. }
  494. public function testMapIntoIsLazy()
  495. {
  496. $this->assertDoesNotEnumerate(function ($collection) {
  497. $collection->mapInto(stdClass::class);
  498. });
  499. $this->assertEnumerates(2, function ($collection) {
  500. $collection->mapInto(stdClass::class)->take(2)->all();
  501. });
  502. }
  503. public function testMapSpreadIsLazy()
  504. {
  505. $data = $this->make([[1, 2], [3, 4], [5, 6], [7, 8]]);
  506. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  507. $collection->mapSpread(function ($first, $second, $key) {
  508. return $first + $second + $key;
  509. });
  510. });
  511. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  512. $collection->mapSpread(function ($first, $second, $key) {
  513. return $first + $second + $key;
  514. })->take(2)->all();
  515. });
  516. }
  517. public function testMapToDictionaryIsLazy()
  518. {
  519. $this->assertDoesNotEnumerate(function ($collection) {
  520. $collection->mapToDictionary(function ($value, $key) {
  521. return [$value => $key];
  522. });
  523. });
  524. $this->assertEnumeratesOnce(function ($collection) {
  525. $collection->mapToDictionary(function ($value, $key) {
  526. return [$value => $key];
  527. })->all();
  528. });
  529. }
  530. public function testMapToGroupsIsLazy()
  531. {
  532. $this->assertDoesNotEnumerate(function ($collection) {
  533. $collection->mapToGroups(function ($value, $key) {
  534. return [$value => $key];
  535. });
  536. });
  537. $this->assertEnumeratesOnce(function ($collection) {
  538. $collection->mapToGroups(function ($value, $key) {
  539. return [$value => $key];
  540. })->all();
  541. });
  542. }
  543. public function testMapWithKeysIsLazy()
  544. {
  545. $this->assertDoesNotEnumerate(function ($collection) {
  546. $collection->mapWithKeys(function ($value, $key) {
  547. return [$value => $key];
  548. });
  549. });
  550. $this->assertEnumerates(2, function ($collection) {
  551. $collection->mapWithKeys(function ($value, $key) {
  552. return [$value => $key];
  553. })->take(2)->all();
  554. });
  555. }
  556. public function testMaxEnumeratesOnce()
  557. {
  558. $this->assertEnumeratesOnce(function ($collection) {
  559. $collection->max();
  560. });
  561. }
  562. public function testMedianEnumeratesOnce()
  563. {
  564. $this->assertEnumeratesOnce(function ($collection) {
  565. $collection->median();
  566. });
  567. }
  568. public function testMergeIsLazy()
  569. {
  570. $this->assertDoesNotEnumerate(function ($collection) {
  571. $collection->merge([1, 2, 3]);
  572. });
  573. $this->assertEnumeratesOnce(function ($collection) {
  574. $collection->merge([1, 2, 3])->all();
  575. });
  576. }
  577. public function testMergeRecursiveIsLazy()
  578. {
  579. $this->assertDoesNotEnumerate(function ($collection) {
  580. $collection->mergeRecursive([1, 2, 3]);
  581. });
  582. $this->assertEnumeratesOnce(function ($collection) {
  583. $collection->mergeRecursive([1, 2, 3])->all();
  584. });
  585. }
  586. public function testMinEnumeratesOnce()
  587. {
  588. $this->assertEnumeratesOnce(function ($collection) {
  589. $collection->min();
  590. });
  591. }
  592. public function testModeEnumeratesOnce()
  593. {
  594. $this->assertEnumeratesOnce(function ($collection) {
  595. $collection->mode();
  596. });
  597. }
  598. public function testNthIsLazy()
  599. {
  600. $this->assertDoesNotEnumerate(function ($collection) {
  601. $collection->nth(5);
  602. });
  603. $this->assertEnumerates(11, function ($collection) {
  604. $collection->nth(5)->take(3)->all();
  605. });
  606. }
  607. public function testOnlyIsLazy()
  608. {
  609. $this->assertDoesNotEnumerate(function ($collection) {
  610. $collection->only(5, 6, 7);
  611. });
  612. $this->assertEnumerates(8, function ($collection) {
  613. $collection->only(5, 6, 7)->all();
  614. });
  615. }
  616. public function testPadIsLazy()
  617. {
  618. $this->assertDoesNotEnumerate(function ($collection) {
  619. $collection->pad(200, null);
  620. $collection->pad(-200, null);
  621. });
  622. $this->assertEnumeratesOnce(function ($collection) {
  623. $collection->pad(20, null)->all();
  624. });
  625. $this->assertEnumeratesOnce(function ($collection) {
  626. $collection->pad(-20, null)->all();
  627. });
  628. }
  629. public function testPartitionEnumeratesOnce()
  630. {
  631. $this->assertEnumeratesOnce(function ($collection) {
  632. $collection->partition(function ($value) {
  633. return $value > 10;
  634. });
  635. });
  636. }
  637. public function testPipeDoesNotEnumerate()
  638. {
  639. $this->assertDoesNotEnumerate(function ($collection) {
  640. $collection->pipe(function () {
  641. // Silence is golden!
  642. });
  643. });
  644. }
  645. public function testPluckIsLazy()
  646. {
  647. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  648. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  649. $collection->pluck('a');
  650. });
  651. $this->assertEnumeratesCollectionOnce($data, function ($collection) {
  652. $collection->pluck('a')->all();
  653. });
  654. }
  655. public function testRandomEnumeratesOnce()
  656. {
  657. $this->assertEnumeratesOnce(function ($collection) {
  658. $collection->random();
  659. });
  660. $this->assertEnumeratesOnce(function ($collection) {
  661. $collection->random(5);
  662. });
  663. }
  664. public function testRangeIsLazy()
  665. {
  666. $data = LazyCollection::range(10, 1000);
  667. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  668. $collection->take(50);
  669. });
  670. $this->assertEnumeratesCollection($data, 5, function ($collection) {
  671. $collection->take(5)->all();
  672. });
  673. }
  674. public function testReduceIsLazy()
  675. {
  676. $this->assertEnumerates(1, function ($collection) {
  677. $this->rescue(function () use ($collection) {
  678. $collection->reduce(function ($total, $value) {
  679. throw new Exception('Short-circuit');
  680. }, 0);
  681. });
  682. });
  683. $this->assertEnumeratesOnce(function ($collection) {
  684. $collection->reduce(function ($total, $value) {
  685. return $total + $value;
  686. }, 0);
  687. });
  688. }
  689. public function testReduceSpreadIsLazy()
  690. {
  691. $this->assertEnumerates(1, function ($collection) {
  692. $this->rescue(function () use ($collection) {
  693. $collection->reduceSpread(function ($one, $two, $value) {
  694. throw new Exception('Short-circuit');
  695. }, 0, 0);
  696. });
  697. });
  698. $this->assertEnumeratesOnce(function ($collection) {
  699. $collection->reduceSpread(function ($total, $max, $value) {
  700. return [$total + $value, max($max, $value)];
  701. }, 0, 0);
  702. });
  703. }
  704. public function testRejectIsLazy()
  705. {
  706. $this->assertDoesNotEnumerate(function ($collection) {
  707. $collection->reject(function ($value) {
  708. return $value % 2;
  709. });
  710. });
  711. $this->assertEnumeratesOnce(function ($collection) {
  712. $collection->reject(function ($value) {
  713. return $value % 2;
  714. })->all();
  715. });
  716. }
  717. public function testRememberIsLazy()
  718. {
  719. $this->assertDoesNotEnumerate(function ($collection) {
  720. $collection->remember();
  721. });
  722. $this->assertEnumeratesOnce(function ($collection) {
  723. $collection = $collection->remember();
  724. $collection->all();
  725. $collection->all();
  726. });
  727. $this->assertEnumerates(5, function ($collection) {
  728. $collection = $collection->remember();
  729. $collection->take(5)->all();
  730. $collection->take(5)->all();
  731. });
  732. }
  733. public function testReplaceIsLazy()
  734. {
  735. $this->assertDoesNotEnumerate(function ($collection) {
  736. $collection->replace([5 => 'a', 10 => 'b']);
  737. });
  738. $this->assertEnumeratesOnce(function ($collection) {
  739. $collection->replace([5 => 'a', 10 => 'b'])->all();
  740. });
  741. }
  742. public function testReplaceRecursiveIsLazy()
  743. {
  744. $this->assertDoesNotEnumerate(function ($collection) {
  745. $collection->replaceRecursive([5 => 'a', 10 => 'b']);
  746. });
  747. $this->assertEnumeratesOnce(function ($collection) {
  748. $collection->replaceRecursive([5 => 'a', 10 => 'b'])->all();
  749. });
  750. }
  751. public function testReverseIsLazy()
  752. {
  753. $this->assertDoesNotEnumerate(function ($collection) {
  754. $collection->reverse();
  755. });
  756. $this->assertEnumeratesOnce(function ($collection) {
  757. $collection->reverse()->all();
  758. });
  759. }
  760. public function testSearchIsLazy()
  761. {
  762. $this->assertEnumerates(5, function ($collection) {
  763. $collection->search(5);
  764. });
  765. $this->assertEnumeratesOnce(function ($collection) {
  766. $collection->search('missing');
  767. });
  768. }
  769. public function testShuffleIsLazy()
  770. {
  771. $this->assertDoesNotEnumerate(function ($collection) {
  772. $collection->shuffle();
  773. });
  774. $this->assertEnumeratesOnce(function ($collection) {
  775. $collection->shuffle()->all();
  776. });
  777. }
  778. public function testSlidingIsLazy()
  779. {
  780. $this->assertDoesNotEnumerate(function ($collection) {
  781. $collection->sliding();
  782. });
  783. $this->assertEnumerates(2, function ($collection) {
  784. $collection->sliding()->take(1)->all();
  785. });
  786. $this->assertEnumerates(3, function ($collection) {
  787. $collection->sliding()->take(2)->all();
  788. });
  789. $this->assertEnumerates(13, function ($collection) {
  790. $collection->sliding(3, 5)->take(3)->all();
  791. });
  792. $this->assertEnumeratesOnce(function ($collection) {
  793. $collection->sliding()->all();
  794. });
  795. }
  796. public function testSkipIsLazy()
  797. {
  798. $this->assertDoesNotEnumerate(function ($collection) {
  799. $collection->skip(10);
  800. });
  801. $this->assertEnumerates(12, function ($collection) {
  802. $collection->skip(10)->take(2)->all();
  803. });
  804. }
  805. public function testSkipUntilIsLazy()
  806. {
  807. $this->assertDoesNotEnumerate(function ($collection) {
  808. $collection->skipUntil(INF);
  809. });
  810. $this->assertEnumerates(10, function ($collection) {
  811. $collection->skipUntil(10)->first();
  812. });
  813. $this->assertEnumerates(10, function ($collection) {
  814. $collection->skipUntil(function ($item) {
  815. return $item === 10;
  816. })->first();
  817. });
  818. }
  819. public function testSkipWhileIsLazy()
  820. {
  821. $this->assertDoesNotEnumerate(function ($collection) {
  822. $collection->skipWhile(1);
  823. });
  824. $this->assertEnumerates(2, function ($collection) {
  825. $collection->skipWhile(1)->first();
  826. });
  827. $this->assertEnumerates(10, function ($collection) {
  828. $collection->skipWhile(function ($item) {
  829. return $item < 10;
  830. })->first();
  831. });
  832. }
  833. public function testSliceIsLazy()
  834. {
  835. $this->assertDoesNotEnumerate(function ($collection) {
  836. $collection->slice(2);
  837. $collection->slice(2, 2);
  838. $collection->slice(-2, 2);
  839. });
  840. $this->assertEnumerates(4, function ($collection) {
  841. $collection->slice(2)->take(2)->all();
  842. });
  843. $this->assertEnumerates(4, function ($collection) {
  844. $collection->slice(2, 2)->all();
  845. });
  846. $this->assertEnumeratesOnce(function ($collection) {
  847. $collection->slice(-2, 2)->all();
  848. });
  849. }
  850. public function testFindFirstOrFailIsLazy()
  851. {
  852. $this->assertEnumerates(1, function ($collection) {
  853. $collection->firstOrFail();
  854. });
  855. $this->assertEnumerates(1, function ($collection) {
  856. $collection->firstOrFail(function ($item) {
  857. return $item === 1;
  858. });
  859. });
  860. $this->assertEnumerates(100, function ($collection) {
  861. try {
  862. $collection->firstOrFail(function ($item) {
  863. return $item === 101;
  864. });
  865. } catch (ItemNotFoundException $e) {
  866. //
  867. }
  868. });
  869. $this->assertEnumerates(2, function ($collection) {
  870. $collection->firstOrFail(function ($item) {
  871. return $item % 2 === 0;
  872. });
  873. });
  874. }
  875. public function testSomeIsLazy()
  876. {
  877. $this->assertEnumerates(5, function ($collection) {
  878. $collection->some(function ($value) {
  879. return $value == 5;
  880. });
  881. });
  882. $this->assertEnumeratesOnce(function ($collection) {
  883. $collection->some(function ($value) {
  884. return false;
  885. });
  886. });
  887. }
  888. public function testSoleIsLazy()
  889. {
  890. $this->assertEnumerates(2, function ($collection) {
  891. try {
  892. $collection->sole();
  893. } catch (MultipleItemsFoundException $e) {
  894. //
  895. }
  896. });
  897. $this->assertEnumeratesOnce(function ($collection) {
  898. $collection->sole(function ($item) {
  899. return $item === 1;
  900. });
  901. });
  902. $this->assertEnumerates(4, function ($collection) {
  903. try {
  904. $collection->sole(function ($item) {
  905. return $item % 2 === 0;
  906. });
  907. } catch (MultipleItemsFoundException $e) {
  908. //
  909. }
  910. });
  911. }
  912. public function testSortIsLazy()
  913. {
  914. $this->assertDoesNotEnumerate(function ($collection) {
  915. $collection->sort();
  916. });
  917. $this->assertEnumeratesOnce(function ($collection) {
  918. $collection->sort()->all();
  919. });
  920. }
  921. public function testSortDescIsLazy()
  922. {
  923. $this->assertDoesNotEnumerate(function ($collection) {
  924. $collection->sortDesc();
  925. });
  926. $this->assertEnumeratesOnce(function ($collection) {
  927. $collection->sortDesc()->all();
  928. });
  929. }
  930. public function testSortByIsLazy()
  931. {
  932. $this->assertDoesNotEnumerate(function ($collection) {
  933. $collection->sortBy(function ($value) {
  934. return $value;
  935. });
  936. });
  937. $this->assertEnumeratesOnce(function ($collection) {
  938. $collection->sortBy(function ($value) {
  939. return $value;
  940. })->all();
  941. });
  942. }
  943. public function testSortByDescIsLazy()
  944. {
  945. $this->assertDoesNotEnumerate(function ($collection) {
  946. $collection->sortByDesc(function ($value) {
  947. return $value;
  948. });
  949. });
  950. $this->assertEnumeratesOnce(function ($collection) {
  951. $collection->sortByDesc(function ($value) {
  952. return $value;
  953. })->all();
  954. });
  955. }
  956. public function testSortKeysIsLazy()
  957. {
  958. $this->assertDoesNotEnumerate(function ($collection) {
  959. $collection->sortKeys();
  960. });
  961. $this->assertEnumeratesOnce(function ($collection) {
  962. $collection->sortKeys()->all();
  963. });
  964. }
  965. public function testSortKeysDescIsLazy()
  966. {
  967. $this->assertDoesNotEnumerate(function ($collection) {
  968. $collection->sortKeysDesc();
  969. });
  970. $this->assertEnumeratesOnce(function ($collection) {
  971. $collection->sortKeysDesc()->all();
  972. });
  973. }
  974. public function testSplitIsLazy()
  975. {
  976. $this->assertDoesNotEnumerate(function ($collection) {
  977. $collection->split(4);
  978. });
  979. $this->assertEnumeratesOnce(function ($collection) {
  980. $collection->split(4)->all();
  981. });
  982. }
  983. public function testSumEnumeratesOnce()
  984. {
  985. $this->assertEnumeratesOnce(function ($collection) {
  986. $collection->sum();
  987. });
  988. }
  989. public function testTakeIsLazy()
  990. {
  991. $this->assertDoesNotEnumerate(function ($collection) {
  992. $collection->take(10);
  993. });
  994. $this->assertEnumerates(10, function ($collection) {
  995. $collection->take(10)->all();
  996. });
  997. }
  998. public function testTakeUntilIsLazy()
  999. {
  1000. $this->assertDoesNotEnumerate(function ($collection) {
  1001. $collection->takeUntil(INF);
  1002. });
  1003. $this->assertEnumerates(10, function ($collection) {
  1004. $collection->takeUntil(10)->all();
  1005. });
  1006. $this->assertEnumerates(10, function ($collection) {
  1007. $collection->takeUntil(function ($item) {
  1008. return $item === 10;
  1009. })->all();
  1010. });
  1011. }
  1012. public function testTakeWhileIsLazy()
  1013. {
  1014. $this->assertDoesNotEnumerate(function ($collection) {
  1015. $collection->takeWhile(0);
  1016. });
  1017. $this->assertEnumerates(1, function ($collection) {
  1018. $collection->takeWhile(0)->all();
  1019. });
  1020. $this->assertEnumerates(10, function ($collection) {
  1021. $collection->takeWhile(function ($item) {
  1022. return $item < 10;
  1023. })->all();
  1024. });
  1025. }
  1026. public function testTapDoesNotEnumerate()
  1027. {
  1028. $this->assertDoesNotEnumerate(function ($collection) {
  1029. $collection->tap(function ($collection) {
  1030. // Silence is golden!
  1031. });
  1032. });
  1033. }
  1034. public function testTapEachIsLazy()
  1035. {
  1036. $this->assertDoesNotEnumerate(function ($collection) {
  1037. $collection->tapEach(function ($value) {
  1038. // Silence is golden!
  1039. });
  1040. });
  1041. $this->assertEnumeratesOnce(function ($collection) {
  1042. $collection->tapEach(function ($value) {
  1043. // Silence is golden!
  1044. })->all();
  1045. });
  1046. }
  1047. public function testTimesIsLazy()
  1048. {
  1049. $data = LazyCollection::times(INF);
  1050. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  1051. $collection->take(2)->all();
  1052. });
  1053. }
  1054. public function testToArrayEnumeratesOnce()
  1055. {
  1056. $this->assertEnumeratesOnce(function ($collection) {
  1057. $collection->toArray();
  1058. });
  1059. }
  1060. public function testToJsonEnumeratesOnce()
  1061. {
  1062. $this->assertEnumeratesOnce(function ($collection) {
  1063. $collection->toJson();
  1064. });
  1065. }
  1066. public function testUnionIsLazy()
  1067. {
  1068. $this->assertDoesNotEnumerate(function ($collection) {
  1069. $collection->union([4, 5, 6]);
  1070. });
  1071. $this->assertEnumeratesOnce(function ($collection) {
  1072. $collection->union([4, 5, 6])->all();
  1073. });
  1074. }
  1075. public function testUniqueIsLazy()
  1076. {
  1077. $this->assertDoesNotEnumerate(function ($collection) {
  1078. $collection->unique();
  1079. });
  1080. $this->assertEnumeratesOnce(function ($collection) {
  1081. $collection->unique()->all();
  1082. });
  1083. }
  1084. public function testUniqueStrictIsLazy()
  1085. {
  1086. $this->assertDoesNotEnumerate(function ($collection) {
  1087. $collection->uniqueStrict();
  1088. });
  1089. $this->assertEnumeratesOnce(function ($collection) {
  1090. $collection->uniqueStrict()->all();
  1091. });
  1092. }
  1093. public function testUnlessDoesNotEnumerate()
  1094. {
  1095. $this->assertDoesNotEnumerate(function ($collection) {
  1096. $collection->unless(true, function ($collection) {
  1097. // Silence is golden!
  1098. });
  1099. $collection->unless(false, function ($collection) {
  1100. // Silence is golden!
  1101. });
  1102. });
  1103. }
  1104. public function testUnlessEmptyIsLazy()
  1105. {
  1106. $this->assertEnumerates(1, function ($collection) {
  1107. $collection->unlessEmpty(function ($collection) {
  1108. // Silence is golden!
  1109. });
  1110. });
  1111. }
  1112. public function testUnlessNotEmptyIsLazy()
  1113. {
  1114. $this->assertEnumerates(1, function ($collection) {
  1115. $collection->unlessNotEmpty(function ($collection) {
  1116. // Silence is golden!
  1117. });
  1118. });
  1119. }
  1120. public function testUnwrapEnumeratesOne()
  1121. {
  1122. $this->assertEnumeratesOnce(function ($collection) {
  1123. LazyCollection::unwrap($collection);
  1124. });
  1125. }
  1126. public function testValuesIsLazy()
  1127. {
  1128. $this->assertDoesNotEnumerate(function ($collection) {
  1129. $collection->values();
  1130. });
  1131. $this->assertEnumerates(2, function ($collection) {
  1132. $collection->values()->take(2)->all();
  1133. });
  1134. }
  1135. public function testWhenDoesNotEnumerate()
  1136. {
  1137. $this->assertDoesNotEnumerate(function ($collection) {
  1138. $collection->when(true, function ($collection) {
  1139. // Silence is golden!
  1140. });
  1141. $collection->when(false, function ($collection) {
  1142. // Silence is golden!
  1143. });
  1144. });
  1145. }
  1146. public function testWhenEmptyIsLazy()
  1147. {
  1148. $this->assertEnumerates(1, function ($collection) {
  1149. $collection->whenEmpty(function ($collection) {
  1150. // Silence is golden!
  1151. });
  1152. });
  1153. }
  1154. public function testWhenNotEmptyIsLazy()
  1155. {
  1156. $this->assertEnumerates(1, function ($collection) {
  1157. $collection->whenNotEmpty(function ($collection) {
  1158. // Silence is golden!
  1159. });
  1160. });
  1161. }
  1162. public function testWhereIsLazy()
  1163. {
  1164. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1165. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1166. $collection->where('a', '<', 3);
  1167. });
  1168. $this->assertEnumeratesCollection($data, 1, function ($collection) {
  1169. $collection->where('a', '<', 3)->take(1)->all();
  1170. });
  1171. }
  1172. public function testWhereBetweenIsLazy()
  1173. {
  1174. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1175. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1176. $collection->whereBetween('a', [2, 4]);
  1177. });
  1178. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  1179. $collection->whereBetween('a', [2, 4])->take(1)->all();
  1180. });
  1181. }
  1182. public function testWhereInIsLazy()
  1183. {
  1184. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1185. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1186. $collection->whereIn('a', [2, 3]);
  1187. });
  1188. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  1189. $collection->whereIn('a', [2, 3])->take(1)->all();
  1190. });
  1191. }
  1192. public function testWhereInstanceOfIsLazy()
  1193. {
  1194. $data = $this->make(['a' => 0])->concat(
  1195. $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]])
  1196. ->mapInto(stdClass::class)
  1197. );
  1198. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1199. $collection->whereInstanceOf(stdClass::class);
  1200. });
  1201. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  1202. $collection->whereInstanceOf(stdClass::class)->take(1)->all();
  1203. });
  1204. }
  1205. public function testWhereInStrictIsLazy()
  1206. {
  1207. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1208. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1209. $collection->whereInStrict('a', ['2', 3]);
  1210. });
  1211. $this->assertEnumeratesCollection($data, 3, function ($collection) {
  1212. $collection->whereInStrict('a', ['2', 3])->take(1)->all();
  1213. });
  1214. }
  1215. public function testWhereNotBetweenIsLazy()
  1216. {
  1217. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1218. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1219. $collection->whereNotBetween('a', [1, 2]);
  1220. });
  1221. $this->assertEnumeratesCollection($data, 3, function ($collection) {
  1222. $collection->whereNotBetween('a', [1, 2])->take(1)->all();
  1223. });
  1224. }
  1225. public function testWhereNotInIsLazy()
  1226. {
  1227. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1228. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1229. $collection->whereNotIn('a', [1, 2]);
  1230. });
  1231. $this->assertEnumeratesCollection($data, 3, function ($collection) {
  1232. $collection->whereNotIn('a', [1, 2])->take(1)->all();
  1233. });
  1234. }
  1235. public function testWhereNotInStrictIsLazy()
  1236. {
  1237. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1238. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1239. $collection->whereNotInStrict('a', ['1', 2]);
  1240. });
  1241. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  1242. $collection->whereNotInStrict('a', [1, '2'])->take(1)->all();
  1243. });
  1244. }
  1245. public function testWhereNotNullIsLazy()
  1246. {
  1247. $data = $this->make([['a' => 1], ['a' => null], ['a' => 2], ['a' => 3]]);
  1248. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1249. $collection->whereNotNull('a');
  1250. });
  1251. $this->assertEnumeratesCollectionOnce($data, function ($collection) {
  1252. $collection->whereNotNull('a')->all();
  1253. });
  1254. $data = $this->make([1, null, 2, null, 3]);
  1255. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1256. $collection->whereNotNull();
  1257. });
  1258. $this->assertEnumeratesCollectionOnce($data, function ($collection) {
  1259. $collection->whereNotNull()->all();
  1260. });
  1261. }
  1262. public function testWhereNullIsLazy()
  1263. {
  1264. $data = $this->make([['a' => 1], ['a' => null], ['a' => 2], ['a' => 3]]);
  1265. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1266. $collection->whereNull('a');
  1267. });
  1268. $this->assertEnumeratesCollectionOnce($data, function ($collection) {
  1269. $collection->whereNull('a')->all();
  1270. });
  1271. $data = $this->make([1, null, 2, null, 3]);
  1272. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1273. $collection->whereNull();
  1274. });
  1275. $this->assertEnumeratesCollectionOnce($data, function ($collection) {
  1276. $collection->whereNull()->all();
  1277. });
  1278. }
  1279. public function testWhereStrictIsLazy()
  1280. {
  1281. $data = $this->make([['a' => 1], ['a' => 2], ['a' => 3], ['a' => 4]]);
  1282. $this->assertDoesNotEnumerateCollection($data, function ($collection) {
  1283. $collection->whereStrict('a', 2);
  1284. });
  1285. $this->assertEnumeratesCollection($data, 2, function ($collection) {
  1286. $collection->whereStrict('a', 2)->take(1)->all();
  1287. });
  1288. }
  1289. public function testWrapIsLazy()
  1290. {
  1291. $this->assertDoesNotEnumerate(function ($collection) {
  1292. LazyCollection::wrap($collection);
  1293. });
  1294. $this->assertEnumeratesOnce(function ($collection) {
  1295. LazyCollection::wrap($collection)->all();
  1296. });
  1297. }
  1298. public function testZipIsLazy()
  1299. {
  1300. $firstEnumerations = 0;
  1301. $secondEnumerations = 0;
  1302. $first = $this->countEnumerations($this->make([1, 2]), $firstEnumerations);
  1303. $second = $this->countEnumerations($this->make([1, 2]), $secondEnumerations);
  1304. $first->zip($second);
  1305. $this->assertEnumerations(0, $firstEnumerations);
  1306. $this->assertEnumerations(0, $secondEnumerations);
  1307. $first->zip($second)->take(1)->all();
  1308. $this->assertEnumerations(1, $firstEnumerations);
  1309. $this->assertEnumerations(1, $secondEnumerations);
  1310. }
  1311. protected function make($source)
  1312. {
  1313. return new LazyCollection($source);
  1314. }
  1315. protected function rescue($callback)
  1316. {
  1317. try {
  1318. $callback();
  1319. } catch (Exception $e) {
  1320. // Silence is golden
  1321. }
  1322. }
  1323. }