SupportCollectionTest.php 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use ArrayAccess;
  4. use ArrayIterator;
  5. use ArrayObject;
  6. use CachingIterator;
  7. use Exception;
  8. use Illuminate\Contracts\Support\Arrayable;
  9. use Illuminate\Contracts\Support\Jsonable;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\HtmlString;
  12. use Illuminate\Support\ItemNotFoundException;
  13. use Illuminate\Support\LazyCollection;
  14. use Illuminate\Support\MultipleItemsFoundException;
  15. use Illuminate\Support\Str;
  16. use InvalidArgumentException;
  17. use JsonSerializable;
  18. use Mockery as m;
  19. use PHPUnit\Framework\TestCase;
  20. use ReflectionClass;
  21. use stdClass;
  22. use Symfony\Component\VarDumper\VarDumper;
  23. use UnexpectedValueException;
  24. if (PHP_VERSION_ID >= 80100) {
  25. include_once 'Enums.php';
  26. }
  27. class SupportCollectionTest extends TestCase
  28. {
  29. /**
  30. * @dataProvider collectionClassProvider
  31. */
  32. public function testFirstReturnsFirstItemInCollection($collection)
  33. {
  34. $c = new $collection(['foo', 'bar']);
  35. $this->assertSame('foo', $c->first());
  36. }
  37. /**
  38. * @dataProvider collectionClassProvider
  39. */
  40. public function testFirstWithCallback($collection)
  41. {
  42. $data = new $collection(['foo', 'bar', 'baz']);
  43. $result = $data->first(function ($value) {
  44. return $value === 'bar';
  45. });
  46. $this->assertSame('bar', $result);
  47. }
  48. /**
  49. * @dataProvider collectionClassProvider
  50. */
  51. public function testFirstWithCallbackAndDefault($collection)
  52. {
  53. $data = new $collection(['foo', 'bar']);
  54. $result = $data->first(function ($value) {
  55. return $value === 'baz';
  56. }, 'default');
  57. $this->assertSame('default', $result);
  58. }
  59. /**
  60. * @dataProvider collectionClassProvider
  61. */
  62. public function testFirstWithDefaultAndWithoutCallback($collection)
  63. {
  64. $data = new $collection;
  65. $result = $data->first(null, 'default');
  66. $this->assertSame('default', $result);
  67. }
  68. /**
  69. * @dataProvider collectionClassProvider
  70. */
  71. public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists($collection)
  72. {
  73. $collection = new $collection([
  74. ['name' => 'foo'],
  75. ['name' => 'bar'],
  76. ]);
  77. $this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->sole());
  78. $this->assertSame(['name' => 'foo'], $collection->sole('name', '=', 'foo'));
  79. $this->assertSame(['name' => 'foo'], $collection->sole('name', 'foo'));
  80. }
  81. /**
  82. * @dataProvider collectionClassProvider
  83. */
  84. public function testSoleThrowsExceptionIfNoItemsExist($collection)
  85. {
  86. $this->expectException(ItemNotFoundException::class);
  87. $collection = new $collection([
  88. ['name' => 'foo'],
  89. ['name' => 'bar'],
  90. ]);
  91. $collection->where('name', 'INVALID')->sole();
  92. }
  93. /**
  94. * @dataProvider collectionClassProvider
  95. */
  96. public function testSoleThrowsExceptionIfMoreThanOneItemExists($collection)
  97. {
  98. $this->expectException(MultipleItemsFoundException::class);
  99. $collection = new $collection([
  100. ['name' => 'foo'],
  101. ['name' => 'foo'],
  102. ['name' => 'bar'],
  103. ]);
  104. $collection->where('name', 'foo')->sole();
  105. }
  106. /**
  107. * @dataProvider collectionClassProvider
  108. */
  109. public function testSoleReturnsFirstItemInCollectionIfOnlyOneExistsWithCallback($collection)
  110. {
  111. $data = new $collection(['foo', 'bar', 'baz']);
  112. $result = $data->sole(function ($value) {
  113. return $value === 'bar';
  114. });
  115. $this->assertSame('bar', $result);
  116. }
  117. /**
  118. * @dataProvider collectionClassProvider
  119. */
  120. public function testSoleThrowsExceptionIfNoItemsExistWithCallback($collection)
  121. {
  122. $this->expectException(ItemNotFoundException::class);
  123. $data = new $collection(['foo', 'bar', 'baz']);
  124. $data->sole(function ($value) {
  125. return $value === 'invalid';
  126. });
  127. }
  128. /**
  129. * @dataProvider collectionClassProvider
  130. */
  131. public function testSoleThrowsExceptionIfMoreThanOneItemExistsWithCallback($collection)
  132. {
  133. $this->expectException(MultipleItemsFoundException::class);
  134. $data = new $collection(['foo', 'bar', 'bar']);
  135. $data->sole(function ($value) {
  136. return $value === 'bar';
  137. });
  138. }
  139. /**
  140. * @dataProvider collectionClassProvider
  141. */
  142. public function testFirstOrFailReturnsFirstItemInCollection($collection)
  143. {
  144. $collection = new $collection([
  145. ['name' => 'foo'],
  146. ['name' => 'bar'],
  147. ]);
  148. $this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->firstOrFail());
  149. $this->assertSame(['name' => 'foo'], $collection->firstOrFail('name', '=', 'foo'));
  150. $this->assertSame(['name' => 'foo'], $collection->firstOrFail('name', 'foo'));
  151. }
  152. /**
  153. * @dataProvider collectionClassProvider
  154. */
  155. public function testFirstOrFailThrowsExceptionIfNoItemsExist($collection)
  156. {
  157. $this->expectException(ItemNotFoundException::class);
  158. $collection = new $collection([
  159. ['name' => 'foo'],
  160. ['name' => 'bar'],
  161. ]);
  162. $collection->where('name', 'INVALID')->firstOrFail();
  163. }
  164. /**
  165. * @dataProvider collectionClassProvider
  166. */
  167. public function testFirstOrFailDoesntThrowExceptionIfMoreThanOneItemExists($collection)
  168. {
  169. $collection = new $collection([
  170. ['name' => 'foo'],
  171. ['name' => 'foo'],
  172. ['name' => 'bar'],
  173. ]);
  174. $this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->firstOrFail());
  175. }
  176. /**
  177. * @dataProvider collectionClassProvider
  178. */
  179. public function testFirstOrFailReturnsFirstItemInCollectionIfOnlyOneExistsWithCallback($collection)
  180. {
  181. $data = new $collection(['foo', 'bar', 'baz']);
  182. $result = $data->firstOrFail(function ($value) {
  183. return $value === 'bar';
  184. });
  185. $this->assertSame('bar', $result);
  186. }
  187. /**
  188. * @dataProvider collectionClassProvider
  189. */
  190. public function testFirstOrFailThrowsExceptionIfNoItemsExistWithCallback($collection)
  191. {
  192. $this->expectException(ItemNotFoundException::class);
  193. $data = new $collection(['foo', 'bar', 'baz']);
  194. $data->firstOrFail(function ($value) {
  195. return $value === 'invalid';
  196. });
  197. }
  198. /**
  199. * @dataProvider collectionClassProvider
  200. */
  201. public function testFirstOrFailDoesntThrowExceptionIfMoreThanOneItemExistsWithCallback($collection)
  202. {
  203. $data = new $collection(['foo', 'bar', 'bar']);
  204. $this->assertSame(
  205. 'bar',
  206. $data->firstOrFail(function ($value) {
  207. return $value === 'bar';
  208. })
  209. );
  210. }
  211. /**
  212. * @dataProvider collectionClassProvider
  213. */
  214. public function testFirstOrFailStopsIteratingAtFirstMatch($collection)
  215. {
  216. $data = new $collection([
  217. function () {
  218. return false;
  219. },
  220. function () {
  221. return true;
  222. },
  223. function () {
  224. throw new Exception();
  225. },
  226. ]);
  227. $this->assertNotNull($data->firstOrFail(function ($callback) {
  228. return $callback();
  229. }));
  230. }
  231. /**
  232. * @dataProvider collectionClassProvider
  233. */
  234. public function testFirstWhere($collection)
  235. {
  236. $data = new $collection([
  237. ['material' => 'paper', 'type' => 'book'],
  238. ['material' => 'rubber', 'type' => 'gasket'],
  239. ]);
  240. $this->assertSame('book', $data->firstWhere('material', 'paper')['type']);
  241. $this->assertSame('gasket', $data->firstWhere('material', 'rubber')['type']);
  242. $this->assertNull($data->firstWhere('material', 'nonexistent'));
  243. $this->assertNull($data->firstWhere('nonexistent', 'key'));
  244. }
  245. /**
  246. * @dataProvider collectionClassProvider
  247. */
  248. public function testLastReturnsLastItemInCollection($collection)
  249. {
  250. $c = new $collection(['foo', 'bar']);
  251. $this->assertSame('bar', $c->last());
  252. }
  253. /**
  254. * @dataProvider collectionClassProvider
  255. */
  256. public function testLastWithCallback($collection)
  257. {
  258. $data = new $collection([100, 200, 300]);
  259. $result = $data->last(function ($value) {
  260. return $value < 250;
  261. });
  262. $this->assertEquals(200, $result);
  263. $result = $data->last(function ($value, $key) {
  264. return $key < 2;
  265. });
  266. $this->assertEquals(200, $result);
  267. }
  268. /**
  269. * @dataProvider collectionClassProvider
  270. */
  271. public function testLastWithCallbackAndDefault($collection)
  272. {
  273. $data = new $collection(['foo', 'bar']);
  274. $result = $data->last(function ($value) {
  275. return $value === 'baz';
  276. }, 'default');
  277. $this->assertSame('default', $result);
  278. }
  279. /**
  280. * @dataProvider collectionClassProvider
  281. */
  282. public function testLastWithDefaultAndWithoutCallback($collection)
  283. {
  284. $data = new $collection;
  285. $result = $data->last(null, 'default');
  286. $this->assertSame('default', $result);
  287. }
  288. public function testPopReturnsAndRemovesLastItemInCollection()
  289. {
  290. $c = new Collection(['foo', 'bar']);
  291. $this->assertSame('bar', $c->pop());
  292. $this->assertSame('foo', $c->first());
  293. }
  294. public function testPopReturnsAndRemovesLastXItemsInCollection()
  295. {
  296. $c = new Collection(['foo', 'bar', 'baz']);
  297. $this->assertEquals(new Collection(['baz', 'bar']), $c->pop(2));
  298. $this->assertSame('foo', $c->first());
  299. $this->assertEquals(new Collection(['baz', 'bar', 'foo']), (new Collection(['foo', 'bar', 'baz']))->pop(6));
  300. }
  301. public function testShiftReturnsAndRemovesFirstItemInCollection()
  302. {
  303. $data = new Collection(['Taylor', 'Otwell']);
  304. $this->assertSame('Taylor', $data->shift());
  305. $this->assertSame('Otwell', $data->first());
  306. $this->assertSame('Otwell', $data->shift());
  307. $this->assertNull($data->first());
  308. }
  309. public function testShiftReturnsAndRemovesFirstXItemsInCollection()
  310. {
  311. $data = new Collection(['foo', 'bar', 'baz']);
  312. $this->assertEquals(new Collection(['foo', 'bar']), $data->shift(2));
  313. $this->assertSame('baz', $data->first());
  314. $this->assertEquals(new Collection(['foo', 'bar', 'baz']), (new Collection(['foo', 'bar', 'baz']))->shift(6));
  315. }
  316. /**
  317. * @dataProvider collectionClassProvider
  318. */
  319. public function testSliding($collection)
  320. {
  321. // Default parameters: $size = 2, $step = 1
  322. $this->assertSame([], $collection::times(0)->sliding()->toArray());
  323. $this->assertSame([], $collection::times(1)->sliding()->toArray());
  324. $this->assertSame([[1, 2]], $collection::times(2)->sliding()->toArray());
  325. $this->assertSame(
  326. [[1, 2], [2, 3]],
  327. $collection::times(3)->sliding()->map->values()->toArray()
  328. );
  329. // Custom step: $size = 2, $step = 3
  330. $this->assertSame([], $collection::times(1)->sliding(2, 3)->toArray());
  331. $this->assertSame([[1, 2]], $collection::times(2)->sliding(2, 3)->toArray());
  332. $this->assertSame([[1, 2]], $collection::times(3)->sliding(2, 3)->toArray());
  333. $this->assertSame([[1, 2]], $collection::times(4)->sliding(2, 3)->toArray());
  334. $this->assertSame(
  335. [[1, 2], [4, 5]],
  336. $collection::times(5)->sliding(2, 3)->map->values()->toArray()
  337. );
  338. // Custom size: $size = 3, $step = 1
  339. $this->assertSame([], $collection::times(2)->sliding(3)->toArray());
  340. $this->assertSame([[1, 2, 3]], $collection::times(3)->sliding(3)->toArray());
  341. $this->assertSame(
  342. [[1, 2, 3], [2, 3, 4]],
  343. $collection::times(4)->sliding(3)->map->values()->toArray()
  344. );
  345. $this->assertSame(
  346. [[1, 2, 3], [2, 3, 4]],
  347. $collection::times(4)->sliding(3)->map->values()->toArray()
  348. );
  349. // Custom size and custom step: $size = 3, $step = 2
  350. $this->assertSame([], $collection::times(2)->sliding(3, 2)->toArray());
  351. $this->assertSame([[1, 2, 3]], $collection::times(3)->sliding(3, 2)->toArray());
  352. $this->assertSame([[1, 2, 3]], $collection::times(4)->sliding(3, 2)->toArray());
  353. $this->assertSame(
  354. [[1, 2, 3], [3, 4, 5]],
  355. $collection::times(5)->sliding(3, 2)->map->values()->toArray()
  356. );
  357. $this->assertSame(
  358. [[1, 2, 3], [3, 4, 5]],
  359. $collection::times(6)->sliding(3, 2)->map->values()->toArray()
  360. );
  361. // Ensure keys are preserved, and inner chunks are also collections
  362. $chunks = $collection::times(3)->sliding();
  363. $this->assertSame([[0 => 1, 1 => 2], [1 => 2, 2 => 3]], $chunks->toArray());
  364. $this->assertInstanceOf($collection, $chunks);
  365. $this->assertInstanceOf($collection, $chunks->first());
  366. $this->assertInstanceOf($collection, $chunks->skip(1)->first());
  367. }
  368. /**
  369. * @dataProvider collectionClassProvider
  370. */
  371. public function testEmptyCollectionIsEmpty($collection)
  372. {
  373. $c = new $collection;
  374. $this->assertTrue($c->isEmpty());
  375. }
  376. /**
  377. * @dataProvider collectionClassProvider
  378. */
  379. public function testEmptyCollectionIsNotEmpty($collection)
  380. {
  381. $c = new $collection(['foo', 'bar']);
  382. $this->assertFalse($c->isEmpty());
  383. $this->assertTrue($c->isNotEmpty());
  384. }
  385. /**
  386. * @dataProvider collectionClassProvider
  387. */
  388. public function testCollectionIsConstructed($collection)
  389. {
  390. $data = new $collection('foo');
  391. $this->assertSame(['foo'], $data->all());
  392. $data = new $collection(2);
  393. $this->assertSame([2], $data->all());
  394. $data = new $collection(false);
  395. $this->assertSame([false], $data->all());
  396. $data = new $collection(null);
  397. $this->assertEmpty($data->all());
  398. $data = new $collection;
  399. $this->assertEmpty($data->all());
  400. }
  401. /**
  402. * @dataProvider collectionClassProvider
  403. */
  404. public function testCollectionShuffleWithSeed($collection)
  405. {
  406. $data = new $collection(range(0, 100, 10));
  407. $firstRandom = $data->shuffle(1234);
  408. $secondRandom = $data->shuffle(1234);
  409. $this->assertEquals($firstRandom, $secondRandom);
  410. }
  411. /**
  412. * @dataProvider collectionClassProvider
  413. */
  414. public function testSkipMethod($collection)
  415. {
  416. $data = new $collection([1, 2, 3, 4, 5, 6]);
  417. // Total items to skip is smaller than collection length
  418. $this->assertSame([5, 6], $data->skip(4)->values()->all());
  419. // Total items to skip is more than collection length
  420. $this->assertSame([], $data->skip(10)->values()->all());
  421. }
  422. /**
  423. * @dataProvider collectionClassProvider
  424. */
  425. public function testSkipUntil($collection)
  426. {
  427. $data = new $collection([1, 1, 2, 2, 3, 3, 4, 4]);
  428. // Item at the beginning of the collection
  429. $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipUntil(1)->values()->all());
  430. // Item at the middle of the collection
  431. $this->assertSame([3, 3, 4, 4], $data->skipUntil(3)->values()->all());
  432. // Item not in the collection
  433. $this->assertSame([], $data->skipUntil(5)->values()->all());
  434. // Item at the beginning of the collection
  435. $data = $data->skipUntil(function ($value, $key) {
  436. return $value <= 1;
  437. })->values();
  438. $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
  439. // Item at the middle of the collection
  440. $data = $data->skipUntil(function ($value, $key) {
  441. return $value >= 3;
  442. })->values();
  443. $this->assertSame([3, 3, 4, 4], $data->all());
  444. // Item not in the collection
  445. $data = $data->skipUntil(function ($value, $key) {
  446. return $value >= 5;
  447. })->values();
  448. $this->assertSame([], $data->all());
  449. }
  450. /**
  451. * @dataProvider collectionClassProvider
  452. */
  453. public function testSkipWhile($collection)
  454. {
  455. $data = new $collection([1, 1, 2, 2, 3, 3, 4, 4]);
  456. // Item at the beginning of the collection
  457. $this->assertSame([2, 2, 3, 3, 4, 4], $data->skipWhile(1)->values()->all());
  458. // Item not in the collection
  459. $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipWhile(5)->values()->all());
  460. // Item in the collection but not at the beginning
  461. $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipWhile(2)->values()->all());
  462. // Item not in the collection
  463. $data = $data->skipWhile(function ($value, $key) {
  464. return $value >= 5;
  465. })->values();
  466. $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
  467. // Item in the collection but not at the beginning
  468. $data = $data->skipWhile(function ($value, $key) {
  469. return $value >= 2;
  470. })->values();
  471. $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
  472. // Item at the beginning of the collection
  473. $data = $data->skipWhile(function ($value, $key) {
  474. return $value < 3;
  475. })->values();
  476. $this->assertSame([3, 3, 4, 4], $data->all());
  477. }
  478. /**
  479. * @dataProvider collectionClassProvider
  480. */
  481. public function testGetArrayableItems($collection)
  482. {
  483. $data = new $collection;
  484. $class = new ReflectionClass($collection);
  485. $method = $class->getMethod('getArrayableItems');
  486. $method->setAccessible(true);
  487. $items = new TestArrayableObject;
  488. $array = $method->invokeArgs($data, [$items]);
  489. $this->assertSame(['foo' => 'bar'], $array);
  490. $items = new TestJsonableObject;
  491. $array = $method->invokeArgs($data, [$items]);
  492. $this->assertSame(['foo' => 'bar'], $array);
  493. $items = new TestJsonSerializeObject;
  494. $array = $method->invokeArgs($data, [$items]);
  495. $this->assertSame(['foo' => 'bar'], $array);
  496. $items = new TestJsonSerializeWithScalarValueObject;
  497. $array = $method->invokeArgs($data, [$items]);
  498. $this->assertSame(['foo'], $array);
  499. $items = new $collection(['foo' => 'bar']);
  500. $array = $method->invokeArgs($data, [$items]);
  501. $this->assertSame(['foo' => 'bar'], $array);
  502. $items = ['foo' => 'bar'];
  503. $array = $method->invokeArgs($data, [$items]);
  504. $this->assertSame(['foo' => 'bar'], $array);
  505. }
  506. /**
  507. * @dataProvider collectionClassProvider
  508. */
  509. public function testToArrayCallsToArrayOnEachItemInCollection($collection)
  510. {
  511. $item1 = m::mock(Arrayable::class);
  512. $item1->shouldReceive('toArray')->once()->andReturn('foo.array');
  513. $item2 = m::mock(Arrayable::class);
  514. $item2->shouldReceive('toArray')->once()->andReturn('bar.array');
  515. $c = new $collection([$item1, $item2]);
  516. $results = $c->toArray();
  517. $this->assertEquals(['foo.array', 'bar.array'], $results);
  518. }
  519. public function testLazyReturnsLazyCollection()
  520. {
  521. $data = new Collection([1, 2, 3, 4, 5]);
  522. $lazy = $data->lazy();
  523. $data->add(6);
  524. $this->assertInstanceOf(LazyCollection::class, $lazy);
  525. $this->assertSame([1, 2, 3, 4, 5], $lazy->all());
  526. }
  527. /**
  528. * @dataProvider collectionClassProvider
  529. */
  530. public function testJsonSerializeCallsToArrayOrJsonSerializeOnEachItemInCollection($collection)
  531. {
  532. $item1 = m::mock(JsonSerializable::class);
  533. $item1->shouldReceive('jsonSerialize')->once()->andReturn('foo.json');
  534. $item2 = m::mock(Arrayable::class);
  535. $item2->shouldReceive('toArray')->once()->andReturn('bar.array');
  536. $c = new $collection([$item1, $item2]);
  537. $results = $c->jsonSerialize();
  538. $this->assertEquals(['foo.json', 'bar.array'], $results);
  539. }
  540. /**
  541. * @dataProvider collectionClassProvider
  542. */
  543. public function testToJsonEncodesTheJsonSerializeResult($collection)
  544. {
  545. $c = $this->getMockBuilder($collection)->onlyMethods(['jsonSerialize'])->getMock();
  546. $c->expects($this->once())->method('jsonSerialize')->willReturn('foo');
  547. $results = $c->toJson();
  548. $this->assertJsonStringEqualsJsonString(json_encode('foo'), $results);
  549. }
  550. /**
  551. * @dataProvider collectionClassProvider
  552. */
  553. public function testCastingToStringJsonEncodesTheToArrayResult($collection)
  554. {
  555. $c = $this->getMockBuilder($collection)->onlyMethods(['jsonSerialize'])->getMock();
  556. $c->expects($this->once())->method('jsonSerialize')->willReturn('foo');
  557. $this->assertJsonStringEqualsJsonString(json_encode('foo'), (string) $c);
  558. }
  559. public function testOffsetAccess()
  560. {
  561. $c = new Collection(['name' => 'taylor']);
  562. $this->assertSame('taylor', $c['name']);
  563. $c['name'] = 'dayle';
  564. $this->assertSame('dayle', $c['name']);
  565. $this->assertTrue(isset($c['name']));
  566. unset($c['name']);
  567. $this->assertFalse(isset($c['name']));
  568. $c[] = 'jason';
  569. $this->assertSame('jason', $c[0]);
  570. }
  571. public function testArrayAccessOffsetExists()
  572. {
  573. $c = new Collection(['foo', 'bar', null]);
  574. $this->assertTrue($c->offsetExists(0));
  575. $this->assertTrue($c->offsetExists(1));
  576. $this->assertFalse($c->offsetExists(2));
  577. }
  578. public function testBehavesLikeAnArrayWithArrayAccess()
  579. {
  580. // indexed array
  581. $input = ['foo', null];
  582. $c = new Collection($input);
  583. $this->assertEquals(isset($input[0]), isset($c[0])); // existing value
  584. $this->assertEquals(isset($input[1]), isset($c[1])); // existing but null value
  585. $this->assertEquals(isset($input[1000]), isset($c[1000])); // non-existing value
  586. $this->assertEquals($input[0], $c[0]);
  587. $this->assertEquals($input[1], $c[1]);
  588. // associative array
  589. $input = ['k1' => 'foo', 'k2' => null];
  590. $c = new Collection($input);
  591. $this->assertEquals(isset($input['k1']), isset($c['k1'])); // existing value
  592. $this->assertEquals(isset($input['k2']), isset($c['k2'])); // existing but null value
  593. $this->assertEquals(isset($input['k3']), isset($c['k3'])); // non-existing value
  594. $this->assertEquals($input['k1'], $c['k1']);
  595. $this->assertEquals($input['k2'], $c['k2']);
  596. }
  597. public function testArrayAccessOffsetGet()
  598. {
  599. $c = new Collection(['foo', 'bar']);
  600. $this->assertSame('foo', $c->offsetGet(0));
  601. $this->assertSame('bar', $c->offsetGet(1));
  602. }
  603. public function testArrayAccessOffsetSet()
  604. {
  605. $c = new Collection(['foo', 'foo']);
  606. $c->offsetSet(1, 'bar');
  607. $this->assertSame('bar', $c[1]);
  608. $c->offsetSet(null, 'qux');
  609. $this->assertSame('qux', $c[2]);
  610. }
  611. public function testArrayAccessOffsetUnset()
  612. {
  613. $c = new Collection(['foo', 'bar']);
  614. $c->offsetUnset(1);
  615. $this->assertFalse(isset($c[1]));
  616. }
  617. public function testForgetSingleKey()
  618. {
  619. $c = new Collection(['foo', 'bar']);
  620. $c = $c->forget(0)->all();
  621. $this->assertFalse(isset($c['foo']));
  622. $this->assertFalse(isset($c[0]));
  623. $this->assertTrue(isset($c[1]));
  624. $c = new Collection(['foo' => 'bar', 'baz' => 'qux']);
  625. $c = $c->forget('foo')->all();
  626. $this->assertFalse(isset($c['foo']));
  627. $this->assertTrue(isset($c['baz']));
  628. }
  629. public function testForgetArrayOfKeys()
  630. {
  631. $c = new Collection(['foo', 'bar', 'baz']);
  632. $c = $c->forget([0, 2])->all();
  633. $this->assertFalse(isset($c[0]));
  634. $this->assertFalse(isset($c[2]));
  635. $this->assertTrue(isset($c[1]));
  636. $c = new Collection(['name' => 'taylor', 'foo' => 'bar', 'baz' => 'qux']);
  637. $c = $c->forget(['foo', 'baz'])->all();
  638. $this->assertFalse(isset($c['foo']));
  639. $this->assertFalse(isset($c['baz']));
  640. $this->assertTrue(isset($c['name']));
  641. }
  642. /**
  643. * @dataProvider collectionClassProvider
  644. */
  645. public function testCountable($collection)
  646. {
  647. $c = new $collection(['foo', 'bar']);
  648. $this->assertCount(2, $c);
  649. }
  650. /**
  651. * @dataProvider collectionClassProvider
  652. */
  653. public function testCountByStandalone($collection)
  654. {
  655. $c = new $collection(['foo', 'foo', 'foo', 'bar', 'bar', 'foobar']);
  656. $this->assertEquals(['foo' => 3, 'bar' => 2, 'foobar' => 1], $c->countBy()->all());
  657. $c = new $collection([true, true, false, false, false]);
  658. $this->assertEquals([true => 2, false => 3], $c->countBy()->all());
  659. $c = new $collection([1, 5, 1, 5, 5, 1]);
  660. $this->assertEquals([1 => 3, 5 => 3], $c->countBy()->all());
  661. }
  662. /**
  663. * @dataProvider collectionClassProvider
  664. */
  665. public function testCountByWithKey($collection)
  666. {
  667. $c = new $collection([
  668. ['key' => 'a'], ['key' => 'a'], ['key' => 'a'], ['key' => 'a'],
  669. ['key' => 'b'], ['key' => 'b'], ['key' => 'b'],
  670. ]);
  671. $this->assertEquals(['a' => 4, 'b' => 3], $c->countBy('key')->all());
  672. }
  673. /**
  674. * @dataProvider collectionClassProvider
  675. */
  676. public function testCountableByWithCallback($collection)
  677. {
  678. $c = new $collection(['alice', 'aaron', 'bob', 'carla']);
  679. $this->assertEquals(['a' => 2, 'b' => 1, 'c' => 1], $c->countBy(function ($name) {
  680. return substr($name, 0, 1);
  681. })->all());
  682. $c = new $collection([1, 2, 3, 4, 5]);
  683. $this->assertEquals([true => 2, false => 3], $c->countBy(function ($i) {
  684. return $i % 2 === 0;
  685. })->all());
  686. }
  687. /**
  688. * @dataProvider collectionClassProvider
  689. */
  690. public function testContainsOneItem($collection)
  691. {
  692. $this->assertFalse((new $collection([]))->containsOneItem());
  693. $this->assertTrue((new $collection([1]))->containsOneItem());
  694. $this->assertFalse((new $collection([1, 2]))->containsOneItem());
  695. }
  696. public function testIterable()
  697. {
  698. $c = new Collection(['foo']);
  699. $this->assertInstanceOf(ArrayIterator::class, $c->getIterator());
  700. $this->assertEquals(['foo'], $c->getIterator()->getArrayCopy());
  701. }
  702. /**
  703. * @dataProvider collectionClassProvider
  704. */
  705. public function testCachingIterator($collection)
  706. {
  707. $c = new $collection(['foo']);
  708. $this->assertInstanceOf(CachingIterator::class, $c->getCachingIterator());
  709. }
  710. /**
  711. * @dataProvider collectionClassProvider
  712. */
  713. public function testFilter($collection)
  714. {
  715. $c = new $collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
  716. $this->assertEquals([1 => ['id' => 2, 'name' => 'World']], $c->filter(function ($item) {
  717. return $item['id'] == 2;
  718. })->all());
  719. $c = new $collection(['', 'Hello', '', 'World']);
  720. $this->assertEquals(['Hello', 'World'], $c->filter()->values()->toArray());
  721. $c = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
  722. $this->assertEquals(['first' => 'Hello', 'second' => 'World'], $c->filter(function ($item, $key) {
  723. return $key !== 'id';
  724. })->all());
  725. }
  726. /**
  727. * @dataProvider collectionClassProvider
  728. */
  729. public function testHigherOrderKeyBy($collection)
  730. {
  731. $c = new $collection([
  732. ['id' => 'id1', 'name' => 'first'],
  733. ['id' => 'id2', 'name' => 'second'],
  734. ]);
  735. $this->assertEquals(['id1' => 'first', 'id2' => 'second'], $c->keyBy->id->map->name->all());
  736. }
  737. /**
  738. * @dataProvider collectionClassProvider
  739. */
  740. public function testHigherOrderUnique($collection)
  741. {
  742. $c = new $collection([
  743. ['id' => '1', 'name' => 'first'],
  744. ['id' => '1', 'name' => 'second'],
  745. ]);
  746. $this->assertCount(1, $c->unique->id);
  747. }
  748. /**
  749. * @dataProvider collectionClassProvider
  750. */
  751. public function testHigherOrderFilter($collection)
  752. {
  753. $c = new $collection([
  754. new class
  755. {
  756. public $name = 'Alex';
  757. public function active()
  758. {
  759. return true;
  760. }
  761. },
  762. new class
  763. {
  764. public $name = 'John';
  765. public function active()
  766. {
  767. return false;
  768. }
  769. },
  770. ]);
  771. $this->assertCount(1, $c->filter->active());
  772. }
  773. /**
  774. * @dataProvider collectionClassProvider
  775. */
  776. public function testWhere($collection)
  777. {
  778. $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  779. $this->assertEquals(
  780. [['v' => 3], ['v' => '3']],
  781. $c->where('v', 3)->values()->all()
  782. );
  783. $this->assertEquals(
  784. [['v' => 3], ['v' => '3']],
  785. $c->where('v', '=', 3)->values()->all()
  786. );
  787. $this->assertEquals(
  788. [['v' => 3], ['v' => '3']],
  789. $c->where('v', '==', 3)->values()->all()
  790. );
  791. $this->assertEquals(
  792. [['v' => 3], ['v' => '3']],
  793. $c->where('v', 'garbage', 3)->values()->all()
  794. );
  795. $this->assertEquals(
  796. [['v' => 3]],
  797. $c->where('v', '===', 3)->values()->all()
  798. );
  799. $this->assertEquals(
  800. [['v' => 1], ['v' => 2], ['v' => 4]],
  801. $c->where('v', '<>', 3)->values()->all()
  802. );
  803. $this->assertEquals(
  804. [['v' => 1], ['v' => 2], ['v' => 4]],
  805. $c->where('v', '!=', 3)->values()->all()
  806. );
  807. $this->assertEquals(
  808. [['v' => 1], ['v' => 2], ['v' => '3'], ['v' => 4]],
  809. $c->where('v', '!==', 3)->values()->all()
  810. );
  811. $this->assertEquals(
  812. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3']],
  813. $c->where('v', '<=', 3)->values()->all()
  814. );
  815. $this->assertEquals(
  816. [['v' => 3], ['v' => '3'], ['v' => 4]],
  817. $c->where('v', '>=', 3)->values()->all()
  818. );
  819. $this->assertEquals(
  820. [['v' => 1], ['v' => 2]],
  821. $c->where('v', '<', 3)->values()->all()
  822. );
  823. $this->assertEquals(
  824. [['v' => 4]],
  825. $c->where('v', '>', 3)->values()->all()
  826. );
  827. $object = (object) ['foo' => 'bar'];
  828. $this->assertEquals(
  829. [],
  830. $c->where('v', $object)->values()->all()
  831. );
  832. $this->assertEquals(
  833. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  834. $c->where('v', '<>', $object)->values()->all()
  835. );
  836. $this->assertEquals(
  837. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  838. $c->where('v', '!=', $object)->values()->all()
  839. );
  840. $this->assertEquals(
  841. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  842. $c->where('v', '!==', $object)->values()->all()
  843. );
  844. $this->assertEquals(
  845. [],
  846. $c->where('v', '>', $object)->values()->all()
  847. );
  848. $c = new $collection([['v' => 1], ['v' => $object]]);
  849. $this->assertEquals(
  850. [['v' => $object]],
  851. $c->where('v', $object)->values()->all()
  852. );
  853. $this->assertEquals(
  854. [['v' => 1], ['v' => $object]],
  855. $c->where('v', '<>', null)->values()->all()
  856. );
  857. $this->assertEquals(
  858. [],
  859. $c->where('v', '<', null)->values()->all()
  860. );
  861. $c = new $collection([['v' => 1], ['v' => new HtmlString('hello')]]);
  862. $this->assertEquals(
  863. [['v' => new HtmlString('hello')]],
  864. $c->where('v', 'hello')->values()->all()
  865. );
  866. $c = new $collection([['v' => 1], ['v' => 'hello']]);
  867. $this->assertEquals(
  868. [['v' => 'hello']],
  869. $c->where('v', new HtmlString('hello'))->values()->all()
  870. );
  871. $c = new $collection([['v' => 1], ['v' => 2], ['v' => null]]);
  872. $this->assertEquals(
  873. [['v' => 1], ['v' => 2]],
  874. $c->where('v')->values()->all()
  875. );
  876. }
  877. /**
  878. * @dataProvider collectionClassProvider
  879. */
  880. public function testWhereStrict($collection)
  881. {
  882. $c = new $collection([['v' => 3], ['v' => '3']]);
  883. $this->assertEquals(
  884. [['v' => 3]],
  885. $c->whereStrict('v', 3)->values()->all()
  886. );
  887. }
  888. /**
  889. * @dataProvider collectionClassProvider
  890. */
  891. public function testWhereInstanceOf($collection)
  892. {
  893. $c = new $collection([new stdClass, new stdClass, new $collection, new stdClass, new Str]);
  894. $this->assertCount(3, $c->whereInstanceOf(stdClass::class));
  895. $this->assertCount(4, $c->whereInstanceOf([stdClass::class, Str::class]));
  896. }
  897. /**
  898. * @dataProvider collectionClassProvider
  899. */
  900. public function testWhereIn($collection)
  901. {
  902. $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  903. $this->assertEquals([['v' => 1], ['v' => 3], ['v' => '3']], $c->whereIn('v', [1, 3])->values()->all());
  904. }
  905. /**
  906. * @dataProvider collectionClassProvider
  907. */
  908. public function testWhereInStrict($collection)
  909. {
  910. $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  911. $this->assertEquals([['v' => 1], ['v' => 3]], $c->whereInStrict('v', [1, 3])->values()->all());
  912. }
  913. /**
  914. * @dataProvider collectionClassProvider
  915. */
  916. public function testWhereNotIn($collection)
  917. {
  918. $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  919. $this->assertEquals([['v' => 2], ['v' => 4]], $c->whereNotIn('v', [1, 3])->values()->all());
  920. }
  921. /**
  922. * @dataProvider collectionClassProvider
  923. */
  924. public function testWhereNotInStrict($collection)
  925. {
  926. $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  927. $this->assertEquals([['v' => 2], ['v' => '3'], ['v' => 4]], $c->whereNotInStrict('v', [1, 3])->values()->all());
  928. }
  929. /**
  930. * @dataProvider collectionClassProvider
  931. */
  932. public function testValues($collection)
  933. {
  934. $c = new $collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
  935. $this->assertEquals([['id' => 2, 'name' => 'World']], $c->filter(function ($item) {
  936. return $item['id'] == 2;
  937. })->values()->all());
  938. }
  939. /**
  940. * @dataProvider collectionClassProvider
  941. */
  942. public function testBetween($collection)
  943. {
  944. $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  945. $this->assertEquals([['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  946. $c->whereBetween('v', [2, 4])->values()->all());
  947. $this->assertEquals([['v' => 1]], $c->whereBetween('v', [-1, 1])->all());
  948. $this->assertEquals([['v' => 3], ['v' => '3']], $c->whereBetween('v', [3, 3])->values()->all());
  949. }
  950. /**
  951. * @dataProvider collectionClassProvider
  952. */
  953. public function testWhereNotBetween($collection)
  954. {
  955. $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  956. $this->assertEquals([['v' => 1]], $c->whereNotBetween('v', [2, 4])->values()->all());
  957. $this->assertEquals([['v' => 2], ['v' => 3], ['v' => 3], ['v' => 4]], $c->whereNotBetween('v', [-1, 1])->values()->all());
  958. $this->assertEquals([['v' => 1], ['v' => '2'], ['v' => '4']], $c->whereNotBetween('v', [3, 3])->values()->all());
  959. }
  960. /**
  961. * @dataProvider collectionClassProvider
  962. */
  963. public function testFlatten($collection)
  964. {
  965. // Flat arrays are unaffected
  966. $c = new $collection(['#foo', '#bar', '#baz']);
  967. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  968. // Nested arrays are flattened with existing flat items
  969. $c = new $collection([['#foo', '#bar'], '#baz']);
  970. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  971. // Sets of nested arrays are flattened
  972. $c = new $collection([['#foo', '#bar'], ['#baz']]);
  973. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  974. // Deeply nested arrays are flattened
  975. $c = new $collection([['#foo', ['#bar']], ['#baz']]);
  976. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  977. // Nested collections are flattened alongside arrays
  978. $c = new $collection([new $collection(['#foo', '#bar']), ['#baz']]);
  979. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  980. // Nested collections containing plain arrays are flattened
  981. $c = new $collection([new $collection(['#foo', ['#bar']]), ['#baz']]);
  982. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  983. // Nested arrays containing collections are flattened
  984. $c = new $collection([['#foo', new $collection(['#bar'])], ['#baz']]);
  985. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  986. // Nested arrays containing collections containing arrays are flattened
  987. $c = new $collection([['#foo', new $collection(['#bar', ['#zap']])], ['#baz']]);
  988. $this->assertEquals(['#foo', '#bar', '#zap', '#baz'], $c->flatten()->all());
  989. }
  990. /**
  991. * @dataProvider collectionClassProvider
  992. */
  993. public function testFlattenWithDepth($collection)
  994. {
  995. // No depth flattens recursively
  996. $c = new $collection([['#foo', ['#bar', ['#baz']]], '#zap']);
  997. $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
  998. // Specifying a depth only flattens to that depth
  999. $c = new $collection([['#foo', ['#bar', ['#baz']]], '#zap']);
  1000. $this->assertEquals(['#foo', ['#bar', ['#baz']], '#zap'], $c->flatten(1)->all());
  1001. $c = new $collection([['#foo', ['#bar', ['#baz']]], '#zap']);
  1002. $this->assertEquals(['#foo', '#bar', ['#baz'], '#zap'], $c->flatten(2)->all());
  1003. }
  1004. /**
  1005. * @dataProvider collectionClassProvider
  1006. */
  1007. public function testFlattenIgnoresKeys($collection)
  1008. {
  1009. // No depth ignores keys
  1010. $c = new $collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
  1011. $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
  1012. // Depth of 1 ignores keys
  1013. $c = new $collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
  1014. $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten(1)->all());
  1015. }
  1016. /**
  1017. * @dataProvider collectionClassProvider
  1018. */
  1019. public function testMergeNull($collection)
  1020. {
  1021. $c = new $collection(['name' => 'Hello']);
  1022. $this->assertEquals(['name' => 'Hello'], $c->merge(null)->all());
  1023. }
  1024. /**
  1025. * @dataProvider collectionClassProvider
  1026. */
  1027. public function testMergeArray($collection)
  1028. {
  1029. $c = new $collection(['name' => 'Hello']);
  1030. $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->merge(['id' => 1])->all());
  1031. }
  1032. /**
  1033. * @dataProvider collectionClassProvider
  1034. */
  1035. public function testMergeCollection($collection)
  1036. {
  1037. $c = new $collection(['name' => 'Hello']);
  1038. $this->assertEquals(['name' => 'World', 'id' => 1], $c->merge(new $collection(['name' => 'World', 'id' => 1]))->all());
  1039. }
  1040. /**
  1041. * @dataProvider collectionClassProvider
  1042. */
  1043. public function testMergeRecursiveNull($collection)
  1044. {
  1045. $c = new $collection(['name' => 'Hello']);
  1046. $this->assertEquals(['name' => 'Hello'], $c->mergeRecursive(null)->all());
  1047. }
  1048. /**
  1049. * @dataProvider collectionClassProvider
  1050. */
  1051. public function testMergeRecursiveArray($collection)
  1052. {
  1053. $c = new $collection(['name' => 'Hello', 'id' => 1]);
  1054. $this->assertEquals(['name' => 'Hello', 'id' => [1, 2]], $c->mergeRecursive(['id' => 2])->all());
  1055. }
  1056. /**
  1057. * @dataProvider collectionClassProvider
  1058. */
  1059. public function testMergeRecursiveCollection($collection)
  1060. {
  1061. $c = new $collection(['name' => 'Hello', 'id' => 1, 'meta' => ['tags' => ['a', 'b'], 'roles' => 'admin']]);
  1062. $this->assertEquals(
  1063. ['name' => 'Hello', 'id' => 1, 'meta' => ['tags' => ['a', 'b', 'c'], 'roles' => ['admin', 'editor']]],
  1064. $c->mergeRecursive(new $collection(['meta' => ['tags' => ['c'], 'roles' => 'editor']]))->all()
  1065. );
  1066. }
  1067. /**
  1068. * @dataProvider collectionClassProvider
  1069. */
  1070. public function testReplaceNull($collection)
  1071. {
  1072. $c = new $collection(['a', 'b', 'c']);
  1073. $this->assertEquals(['a', 'b', 'c'], $c->replace(null)->all());
  1074. }
  1075. /**
  1076. * @dataProvider collectionClassProvider
  1077. */
  1078. public function testReplaceArray($collection)
  1079. {
  1080. $c = new $collection(['a', 'b', 'c']);
  1081. $this->assertEquals(['a', 'd', 'e'], $c->replace([1 => 'd', 2 => 'e'])->all());
  1082. }
  1083. /**
  1084. * @dataProvider collectionClassProvider
  1085. */
  1086. public function testReplaceCollection($collection)
  1087. {
  1088. $c = new $collection(['a', 'b', 'c']);
  1089. $this->assertEquals(
  1090. ['a', 'd', 'e'],
  1091. $c->replace(new $collection([1 => 'd', 2 => 'e']))->all()
  1092. );
  1093. }
  1094. /**
  1095. * @dataProvider collectionClassProvider
  1096. */
  1097. public function testReplaceRecursiveNull($collection)
  1098. {
  1099. $c = new $collection(['a', 'b', ['c', 'd']]);
  1100. $this->assertEquals(['a', 'b', ['c', 'd']], $c->replaceRecursive(null)->all());
  1101. }
  1102. /**
  1103. * @dataProvider collectionClassProvider
  1104. */
  1105. public function testReplaceRecursiveArray($collection)
  1106. {
  1107. $c = new $collection(['a', 'b', ['c', 'd']]);
  1108. $this->assertEquals(['z', 'b', ['c', 'e']], $c->replaceRecursive(['z', 2 => [1 => 'e']])->all());
  1109. }
  1110. /**
  1111. * @dataProvider collectionClassProvider
  1112. */
  1113. public function testReplaceRecursiveCollection($collection)
  1114. {
  1115. $c = new $collection(['a', 'b', ['c', 'd']]);
  1116. $this->assertEquals(
  1117. ['z', 'b', ['c', 'e']],
  1118. $c->replaceRecursive(new $collection(['z', 2 => [1 => 'e']]))->all()
  1119. );
  1120. }
  1121. /**
  1122. * @dataProvider collectionClassProvider
  1123. */
  1124. public function testUnionNull($collection)
  1125. {
  1126. $c = new $collection(['name' => 'Hello']);
  1127. $this->assertEquals(['name' => 'Hello'], $c->union(null)->all());
  1128. }
  1129. /**
  1130. * @dataProvider collectionClassProvider
  1131. */
  1132. public function testUnionArray($collection)
  1133. {
  1134. $c = new $collection(['name' => 'Hello']);
  1135. $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(['id' => 1])->all());
  1136. }
  1137. /**
  1138. * @dataProvider collectionClassProvider
  1139. */
  1140. public function testUnionCollection($collection)
  1141. {
  1142. $c = new $collection(['name' => 'Hello']);
  1143. $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(new $collection(['name' => 'World', 'id' => 1]))->all());
  1144. }
  1145. /**
  1146. * @dataProvider collectionClassProvider
  1147. */
  1148. public function testDiffCollection($collection)
  1149. {
  1150. $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
  1151. $this->assertEquals(['id' => 1], $c->diff(new $collection(['first_word' => 'Hello', 'last_word' => 'World']))->all());
  1152. }
  1153. /**
  1154. * @dataProvider collectionClassProvider
  1155. */
  1156. public function testDiffUsingWithCollection($collection)
  1157. {
  1158. $c = new $collection(['en_GB', 'fr', 'HR']);
  1159. // demonstrate that diffKeys wont support case insensitivity
  1160. $this->assertEquals(['en_GB', 'fr', 'HR'], $c->diff(new $collection(['en_gb', 'hr']))->values()->toArray());
  1161. // allow for case insensitive difference
  1162. $this->assertEquals(['fr'], $c->diffUsing(new $collection(['en_gb', 'hr']), 'strcasecmp')->values()->toArray());
  1163. }
  1164. /**
  1165. * @dataProvider collectionClassProvider
  1166. */
  1167. public function testDiffUsingWithNull($collection)
  1168. {
  1169. $c = new $collection(['en_GB', 'fr', 'HR']);
  1170. $this->assertEquals(['en_GB', 'fr', 'HR'], $c->diffUsing(null, 'strcasecmp')->values()->toArray());
  1171. }
  1172. /**
  1173. * @dataProvider collectionClassProvider
  1174. */
  1175. public function testDiffNull($collection)
  1176. {
  1177. $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
  1178. $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c->diff(null)->all());
  1179. }
  1180. /**
  1181. * @dataProvider collectionClassProvider
  1182. */
  1183. public function testDiffKeys($collection)
  1184. {
  1185. $c1 = new $collection(['id' => 1, 'first_word' => 'Hello']);
  1186. $c2 = new $collection(['id' => 123, 'foo_bar' => 'Hello']);
  1187. $this->assertEquals(['first_word' => 'Hello'], $c1->diffKeys($c2)->all());
  1188. }
  1189. /**
  1190. * @dataProvider collectionClassProvider
  1191. */
  1192. public function testDiffKeysUsing($collection)
  1193. {
  1194. $c1 = new $collection(['id' => 1, 'first_word' => 'Hello']);
  1195. $c2 = new $collection(['ID' => 123, 'foo_bar' => 'Hello']);
  1196. // demonstrate that diffKeys wont support case insensitivity
  1197. $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c1->diffKeys($c2)->all());
  1198. // allow for case insensitive difference
  1199. $this->assertEquals(['first_word' => 'Hello'], $c1->diffKeysUsing($c2, 'strcasecmp')->all());
  1200. }
  1201. /**
  1202. * @dataProvider collectionClassProvider
  1203. */
  1204. public function testDiffAssoc($collection)
  1205. {
  1206. $c1 = new $collection(['id' => 1, 'first_word' => 'Hello', 'not_affected' => 'value']);
  1207. $c2 = new $collection(['id' => 123, 'foo_bar' => 'Hello', 'not_affected' => 'value']);
  1208. $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c1->diffAssoc($c2)->all());
  1209. }
  1210. /**
  1211. * @dataProvider collectionClassProvider
  1212. */
  1213. public function testDiffAssocUsing($collection)
  1214. {
  1215. $c1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']);
  1216. $c2 = new $collection(['A' => 'green', 'yellow', 'red']);
  1217. // demonstrate that the case of the keys will affect the output when diffAssoc is used
  1218. $this->assertEquals(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssoc($c2)->all());
  1219. // allow for case insensitive difference
  1220. $this->assertEquals(['b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssocUsing($c2, 'strcasecmp')->all());
  1221. }
  1222. /**
  1223. * @dataProvider collectionClassProvider
  1224. */
  1225. public function testDuplicates($collection)
  1226. {
  1227. $duplicates = $collection::make([1, 2, 1, 'laravel', null, 'laravel', 'php', null])->duplicates()->all();
  1228. $this->assertSame([2 => 1, 5 => 'laravel', 7 => null], $duplicates);
  1229. // does loose comparison
  1230. $duplicates = $collection::make([2, '2', [], null])->duplicates()->all();
  1231. $this->assertSame([1 => '2', 3 => null], $duplicates);
  1232. // works with mix of primitives
  1233. $duplicates = $collection::make([1, '2', ['laravel'], ['laravel'], null, '2'])->duplicates()->all();
  1234. $this->assertSame([3 => ['laravel'], 5 => '2'], $duplicates);
  1235. // works with mix of objects and primitives **excepts numbers**.
  1236. $expected = new Collection(['laravel']);
  1237. $duplicates = $collection::make([new Collection(['laravel']), $expected, $expected, [], '2', '2'])->duplicates()->all();
  1238. $this->assertSame([1 => $expected, 2 => $expected, 5 => '2'], $duplicates);
  1239. }
  1240. /**
  1241. * @dataProvider collectionClassProvider
  1242. */
  1243. public function testDuplicatesWithKey($collection)
  1244. {
  1245. $items = [['framework' => 'vue'], ['framework' => 'laravel'], ['framework' => 'laravel']];
  1246. $duplicates = $collection::make($items)->duplicates('framework')->all();
  1247. $this->assertSame([2 => 'laravel'], $duplicates);
  1248. }
  1249. /**
  1250. * @dataProvider collectionClassProvider
  1251. */
  1252. public function testDuplicatesWithCallback($collection)
  1253. {
  1254. $items = [['framework' => 'vue'], ['framework' => 'laravel'], ['framework' => 'laravel']];
  1255. $duplicates = $collection::make($items)->duplicates(function ($item) {
  1256. return $item['framework'];
  1257. })->all();
  1258. $this->assertSame([2 => 'laravel'], $duplicates);
  1259. }
  1260. /**
  1261. * @dataProvider collectionClassProvider
  1262. */
  1263. public function testDuplicatesWithStrict($collection)
  1264. {
  1265. $duplicates = $collection::make([1, 2, 1, 'laravel', null, 'laravel', 'php', null])->duplicatesStrict()->all();
  1266. $this->assertSame([2 => 1, 5 => 'laravel', 7 => null], $duplicates);
  1267. // does strict comparison
  1268. $duplicates = $collection::make([2, '2', [], null])->duplicatesStrict()->all();
  1269. $this->assertSame([], $duplicates);
  1270. // works with mix of primitives
  1271. $duplicates = $collection::make([1, '2', ['laravel'], ['laravel'], null, '2'])->duplicatesStrict()->all();
  1272. $this->assertSame([3 => ['laravel'], 5 => '2'], $duplicates);
  1273. // works with mix of primitives, objects, and numbers
  1274. $expected = new $collection(['laravel']);
  1275. $duplicates = $collection::make([new $collection(['laravel']), $expected, $expected, [], '2', '2'])->duplicatesStrict()->all();
  1276. $this->assertSame([2 => $expected, 5 => '2'], $duplicates);
  1277. }
  1278. /**
  1279. * @dataProvider collectionClassProvider
  1280. */
  1281. public function testEach($collection)
  1282. {
  1283. $c = new $collection($original = [1, 2, 'foo' => 'bar', 'bam' => 'baz']);
  1284. $result = [];
  1285. $c->each(function ($item, $key) use (&$result) {
  1286. $result[$key] = $item;
  1287. });
  1288. $this->assertEquals($original, $result);
  1289. $result = [];
  1290. $c->each(function ($item, $key) use (&$result) {
  1291. $result[$key] = $item;
  1292. if (is_string($key)) {
  1293. return false;
  1294. }
  1295. });
  1296. $this->assertEquals([1, 2, 'foo' => 'bar'], $result);
  1297. }
  1298. /**
  1299. * @dataProvider collectionClassProvider
  1300. */
  1301. public function testEachSpread($collection)
  1302. {
  1303. $c = new $collection([[1, 'a'], [2, 'b']]);
  1304. $result = [];
  1305. $c->eachSpread(function ($number, $character) use (&$result) {
  1306. $result[] = [$number, $character];
  1307. });
  1308. $this->assertEquals($c->all(), $result);
  1309. $result = [];
  1310. $c->eachSpread(function ($number, $character) use (&$result) {
  1311. $result[] = [$number, $character];
  1312. return false;
  1313. });
  1314. $this->assertEquals([[1, 'a']], $result);
  1315. $result = [];
  1316. $c->eachSpread(function ($number, $character, $key) use (&$result) {
  1317. $result[] = [$number, $character, $key];
  1318. });
  1319. $this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
  1320. $c = new $collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
  1321. $result = [];
  1322. $c->eachSpread(function ($number, $character, $key) use (&$result) {
  1323. $result[] = [$number, $character, $key];
  1324. });
  1325. $this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
  1326. }
  1327. /**
  1328. * @dataProvider collectionClassProvider
  1329. */
  1330. public function testIntersectNull($collection)
  1331. {
  1332. $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
  1333. $this->assertEquals([], $c->intersect(null)->all());
  1334. }
  1335. /**
  1336. * @dataProvider collectionClassProvider
  1337. */
  1338. public function testIntersectCollection($collection)
  1339. {
  1340. $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
  1341. $this->assertEquals(['first_word' => 'Hello'], $c->intersect(new $collection(['first_world' => 'Hello', 'last_word' => 'World']))->all());
  1342. }
  1343. /**
  1344. * @dataProvider collectionClassProvider
  1345. */
  1346. public function testIntersectByKeysNull($collection)
  1347. {
  1348. $c = new $collection(['name' => 'Mateus', 'age' => 18]);
  1349. $this->assertEquals([], $c->intersectByKeys(null)->all());
  1350. }
  1351. /**
  1352. * @dataProvider collectionClassProvider
  1353. */
  1354. public function testIntersectByKeys($collection)
  1355. {
  1356. $c = new $collection(['name' => 'Mateus', 'age' => 18]);
  1357. $this->assertEquals(['name' => 'Mateus'], $c->intersectByKeys(new $collection(['name' => 'Mateus', 'surname' => 'Guimaraes']))->all());
  1358. }
  1359. /**
  1360. * @dataProvider collectionClassProvider
  1361. */
  1362. public function testUnique($collection)
  1363. {
  1364. $c = new $collection(['Hello', 'World', 'World']);
  1365. $this->assertEquals(['Hello', 'World'], $c->unique()->all());
  1366. $c = new $collection([[1, 2], [1, 2], [2, 3], [3, 4], [2, 3]]);
  1367. $this->assertEquals([[1, 2], [2, 3], [3, 4]], $c->unique()->values()->all());
  1368. }
  1369. /**
  1370. * @dataProvider collectionClassProvider
  1371. */
  1372. public function testUniqueWithCallback($collection)
  1373. {
  1374. $c = new $collection([
  1375. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  1376. 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'],
  1377. 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
  1378. 4 => ['id' => 4, 'first' => 'Abigail', 'last' => 'Otwell'],
  1379. 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'],
  1380. 6 => ['id' => 6, 'first' => 'Taylor', 'last' => 'Swift'],
  1381. ]);
  1382. $this->assertEquals([
  1383. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  1384. 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
  1385. ], $c->unique('first')->all());
  1386. $this->assertEquals([
  1387. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  1388. 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
  1389. 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'],
  1390. ], $c->unique(function ($item) {
  1391. return $item['first'].$item['last'];
  1392. })->all());
  1393. $this->assertEquals([
  1394. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  1395. 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'],
  1396. ], $c->unique(function ($item, $key) {
  1397. return $key % 2;
  1398. })->all());
  1399. }
  1400. /**
  1401. * @dataProvider collectionClassProvider
  1402. */
  1403. public function testUniqueStrict($collection)
  1404. {
  1405. $c = new $collection([
  1406. [
  1407. 'id' => '0',
  1408. 'name' => 'zero',
  1409. ],
  1410. [
  1411. 'id' => '00',
  1412. 'name' => 'double zero',
  1413. ],
  1414. [
  1415. 'id' => '0',
  1416. 'name' => 'again zero',
  1417. ],
  1418. ]);
  1419. $this->assertEquals([
  1420. [
  1421. 'id' => '0',
  1422. 'name' => 'zero',
  1423. ],
  1424. [
  1425. 'id' => '00',
  1426. 'name' => 'double zero',
  1427. ],
  1428. ], $c->uniqueStrict('id')->all());
  1429. }
  1430. /**
  1431. * @dataProvider collectionClassProvider
  1432. */
  1433. public function testCollapse($collection)
  1434. {
  1435. $data = new $collection([[$object1 = new stdClass], [$object2 = new stdClass]]);
  1436. $this->assertEquals([$object1, $object2], $data->collapse()->all());
  1437. }
  1438. /**
  1439. * @dataProvider collectionClassProvider
  1440. */
  1441. public function testCollapseWithNestedCollections($collection)
  1442. {
  1443. $data = new $collection([new $collection([1, 2, 3]), new $collection([4, 5, 6])]);
  1444. $this->assertEquals([1, 2, 3, 4, 5, 6], $data->collapse()->all());
  1445. }
  1446. /**
  1447. * @dataProvider collectionClassProvider
  1448. */
  1449. public function testJoin($collection)
  1450. {
  1451. $this->assertSame('a, b, c', (new $collection(['a', 'b', 'c']))->join(', '));
  1452. $this->assertSame('a, b and c', (new $collection(['a', 'b', 'c']))->join(', ', ' and '));
  1453. $this->assertSame('a and b', (new $collection(['a', 'b']))->join(', ', ' and '));
  1454. $this->assertSame('a', (new $collection(['a']))->join(', ', ' and '));
  1455. $this->assertSame('', (new $collection([]))->join(', ', ' and '));
  1456. }
  1457. /**
  1458. * @dataProvider collectionClassProvider
  1459. */
  1460. public function testCrossJoin($collection)
  1461. {
  1462. // Cross join with an array
  1463. $this->assertEquals(
  1464. [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
  1465. (new $collection([1, 2]))->crossJoin(['a', 'b'])->all()
  1466. );
  1467. // Cross join with a collection
  1468. $this->assertEquals(
  1469. [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
  1470. (new $collection([1, 2]))->crossJoin(new $collection(['a', 'b']))->all()
  1471. );
  1472. // Cross join with 2 collections
  1473. $this->assertEquals(
  1474. [
  1475. [1, 'a', 'I'], [1, 'a', 'II'],
  1476. [1, 'b', 'I'], [1, 'b', 'II'],
  1477. [2, 'a', 'I'], [2, 'a', 'II'],
  1478. [2, 'b', 'I'], [2, 'b', 'II'],
  1479. ],
  1480. (new $collection([1, 2]))->crossJoin(
  1481. new $collection(['a', 'b']),
  1482. new $collection(['I', 'II'])
  1483. )->all()
  1484. );
  1485. }
  1486. /**
  1487. * @dataProvider collectionClassProvider
  1488. */
  1489. public function testSort($collection)
  1490. {
  1491. $data = (new $collection([5, 3, 1, 2, 4]))->sort();
  1492. $this->assertEquals([1, 2, 3, 4, 5], $data->values()->all());
  1493. $data = (new $collection([-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4]))->sort();
  1494. $this->assertEquals([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], $data->values()->all());
  1495. $data = (new $collection(['foo', 'bar-10', 'bar-1']))->sort();
  1496. $this->assertEquals(['bar-1', 'bar-10', 'foo'], $data->values()->all());
  1497. $data = (new $collection(['T2', 'T1', 'T10']))->sort();
  1498. $this->assertEquals(['T1', 'T10', 'T2'], $data->values()->all());
  1499. $data = (new $collection(['T2', 'T1', 'T10']))->sort(SORT_NATURAL);
  1500. $this->assertEquals(['T1', 'T2', 'T10'], $data->values()->all());
  1501. }
  1502. /**
  1503. * @dataProvider collectionClassProvider
  1504. */
  1505. public function testSortDesc($collection)
  1506. {
  1507. $data = (new $collection([5, 3, 1, 2, 4]))->sortDesc();
  1508. $this->assertEquals([5, 4, 3, 2, 1], $data->values()->all());
  1509. $data = (new $collection([-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4]))->sortDesc();
  1510. $this->assertEquals([5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5], $data->values()->all());
  1511. $data = (new $collection(['bar-1', 'foo', 'bar-10']))->sortDesc();
  1512. $this->assertEquals(['foo', 'bar-10', 'bar-1'], $data->values()->all());
  1513. $data = (new $collection(['T2', 'T1', 'T10']))->sortDesc();
  1514. $this->assertEquals(['T2', 'T10', 'T1'], $data->values()->all());
  1515. $data = (new $collection(['T2', 'T1', 'T10']))->sortDesc(SORT_NATURAL);
  1516. $this->assertEquals(['T10', 'T2', 'T1'], $data->values()->all());
  1517. }
  1518. /**
  1519. * @dataProvider collectionClassProvider
  1520. */
  1521. public function testSortWithCallback($collection)
  1522. {
  1523. $data = (new $collection([5, 3, 1, 2, 4]))->sort(function ($a, $b) {
  1524. if ($a === $b) {
  1525. return 0;
  1526. }
  1527. return ($a < $b) ? -1 : 1;
  1528. });
  1529. $this->assertEquals(range(1, 5), array_values($data->all()));
  1530. }
  1531. /**
  1532. * @dataProvider collectionClassProvider
  1533. */
  1534. public function testSortBy($collection)
  1535. {
  1536. $data = new $collection(['taylor', 'dayle']);
  1537. $data = $data->sortBy(function ($x) {
  1538. return $x;
  1539. });
  1540. $this->assertEquals(['dayle', 'taylor'], array_values($data->all()));
  1541. $data = new $collection(['dayle', 'taylor']);
  1542. $data = $data->sortByDesc(function ($x) {
  1543. return $x;
  1544. });
  1545. $this->assertEquals(['taylor', 'dayle'], array_values($data->all()));
  1546. }
  1547. /**
  1548. * @dataProvider collectionClassProvider
  1549. */
  1550. public function testSortByString($collection)
  1551. {
  1552. $data = new $collection([['name' => 'taylor'], ['name' => 'dayle']]);
  1553. $data = $data->sortBy('name', SORT_STRING);
  1554. $this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
  1555. $data = new $collection([['name' => 'taylor'], ['name' => 'dayle']]);
  1556. $data = $data->sortBy('name', SORT_STRING);
  1557. $this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
  1558. }
  1559. /**
  1560. * @dataProvider collectionClassProvider
  1561. */
  1562. public function testSortByCallableString($collection)
  1563. {
  1564. $data = new $collection([['sort' => 2], ['sort' => 1]]);
  1565. $data = $data->sortBy([['sort', 'asc']]);
  1566. $this->assertEquals([['sort' => 1], ['sort' => 2]], array_values($data->all()));
  1567. }
  1568. /**
  1569. * @dataProvider collectionClassProvider
  1570. */
  1571. public function testSortByAlwaysReturnsAssoc($collection)
  1572. {
  1573. $data = new $collection(['a' => 'taylor', 'b' => 'dayle']);
  1574. $data = $data->sortBy(function ($x) {
  1575. return $x;
  1576. });
  1577. $this->assertEquals(['b' => 'dayle', 'a' => 'taylor'], $data->all());
  1578. $data = new $collection(['taylor', 'dayle']);
  1579. $data = $data->sortBy(function ($x) {
  1580. return $x;
  1581. });
  1582. $this->assertEquals([1 => 'dayle', 0 => 'taylor'], $data->all());
  1583. }
  1584. /**
  1585. * @dataProvider collectionClassProvider
  1586. */
  1587. public function testSortKeys($collection)
  1588. {
  1589. $data = new $collection(['b' => 'dayle', 'a' => 'taylor']);
  1590. $this->assertSame(['a' => 'taylor', 'b' => 'dayle'], $data->sortKeys()->all());
  1591. }
  1592. /**
  1593. * @dataProvider collectionClassProvider
  1594. */
  1595. public function testSortKeysDesc($collection)
  1596. {
  1597. $data = new $collection(['a' => 'taylor', 'b' => 'dayle']);
  1598. $this->assertSame(['b' => 'dayle', 'a' => 'taylor'], $data->sortKeysDesc()->all());
  1599. }
  1600. /**
  1601. * @dataProvider collectionClassProvider
  1602. */
  1603. public function testSortKeysUsing($collection)
  1604. {
  1605. $data = new $collection(['B' => 'dayle', 'a' => 'taylor']);
  1606. $this->assertSame(['a' => 'taylor', 'B' => 'dayle'], $data->sortKeysUsing('strnatcasecmp')->all());
  1607. }
  1608. /**
  1609. * @dataProvider collectionClassProvider
  1610. */
  1611. public function testReverse($collection)
  1612. {
  1613. $data = new $collection(['zaeed', 'alan']);
  1614. $reversed = $data->reverse();
  1615. $this->assertSame([1 => 'alan', 0 => 'zaeed'], $reversed->all());
  1616. $data = new $collection(['name' => 'taylor', 'framework' => 'laravel']);
  1617. $reversed = $data->reverse();
  1618. $this->assertSame(['framework' => 'laravel', 'name' => 'taylor'], $reversed->all());
  1619. }
  1620. /**
  1621. * @dataProvider collectionClassProvider
  1622. */
  1623. public function testFlip($collection)
  1624. {
  1625. $data = new $collection(['name' => 'taylor', 'framework' => 'laravel']);
  1626. $this->assertEquals(['taylor' => 'name', 'laravel' => 'framework'], $data->flip()->toArray());
  1627. }
  1628. /**
  1629. * @dataProvider collectionClassProvider
  1630. */
  1631. public function testChunk($collection)
  1632. {
  1633. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  1634. $data = $data->chunk(3);
  1635. $this->assertInstanceOf($collection, $data);
  1636. $this->assertInstanceOf($collection, $data->first());
  1637. $this->assertCount(4, $data);
  1638. $this->assertEquals([1, 2, 3], $data->first()->toArray());
  1639. $this->assertEquals([9 => 10], $data->get(3)->toArray());
  1640. }
  1641. /**
  1642. * @dataProvider collectionClassProvider
  1643. */
  1644. public function testChunkWhenGivenZeroAsSize($collection)
  1645. {
  1646. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  1647. $this->assertEquals(
  1648. [],
  1649. $data->chunk(0)->toArray()
  1650. );
  1651. }
  1652. /**
  1653. * @dataProvider collectionClassProvider
  1654. */
  1655. public function testChunkWhenGivenLessThanZero($collection)
  1656. {
  1657. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  1658. $this->assertEquals(
  1659. [],
  1660. $data->chunk(-1)->toArray()
  1661. );
  1662. }
  1663. /**
  1664. * @dataProvider collectionClassProvider
  1665. */
  1666. public function testSplitIn($collection)
  1667. {
  1668. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  1669. $data = $data->splitIn(3);
  1670. $this->assertInstanceOf($collection, $data);
  1671. $this->assertInstanceOf($collection, $data->first());
  1672. $this->assertCount(3, $data);
  1673. $this->assertEquals([1, 2, 3, 4], $data->get(0)->values()->toArray());
  1674. $this->assertEquals([5, 6, 7, 8], $data->get(1)->values()->toArray());
  1675. $this->assertEquals([9, 10], $data->get(2)->values()->toArray());
  1676. }
  1677. /**
  1678. * @dataProvider collectionClassProvider
  1679. */
  1680. public function testChunkWhileOnEqualElements($collection)
  1681. {
  1682. $data = (new $collection(['A', 'A', 'B', 'B', 'C', 'C', 'C']))
  1683. ->chunkWhile(function ($current, $key, $chunk) {
  1684. return $chunk->last() === $current;
  1685. });
  1686. $this->assertInstanceOf($collection, $data);
  1687. $this->assertInstanceOf($collection, $data->first());
  1688. $this->assertEquals([0 => 'A', 1 => 'A'], $data->first()->toArray());
  1689. $this->assertEquals([2 => 'B', 3 => 'B'], $data->get(1)->toArray());
  1690. $this->assertEquals([4 => 'C', 5 => 'C', 6 => 'C'], $data->last()->toArray());
  1691. }
  1692. /**
  1693. * @dataProvider collectionClassProvider
  1694. */
  1695. public function testChunkWhileOnContiguouslyIncreasingIntegers($collection)
  1696. {
  1697. $data = (new $collection([1, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]))
  1698. ->chunkWhile(function ($current, $key, $chunk) {
  1699. return $chunk->last() + 1 == $current;
  1700. });
  1701. $this->assertInstanceOf($collection, $data);
  1702. $this->assertInstanceOf($collection, $data->first());
  1703. $this->assertEquals([0 => 1], $data->first()->toArray());
  1704. $this->assertEquals([1 => 4], $data->get(1)->toArray());
  1705. $this->assertEquals([2 => 9, 3 => 10, 4 => 11, 5 => 12], $data->get(2)->toArray());
  1706. $this->assertEquals([6 => 15, 7 => 16], $data->get(3)->toArray());
  1707. $this->assertEquals([8 => 19, 9 => 20, 10 => 21], $data->last()->toArray());
  1708. }
  1709. /**
  1710. * @dataProvider collectionClassProvider
  1711. */
  1712. public function testEvery($collection)
  1713. {
  1714. $c = new $collection([]);
  1715. $this->assertTrue($c->every('key', 'value'));
  1716. $this->assertTrue($c->every(function () {
  1717. return false;
  1718. }));
  1719. $c = new $collection([['age' => 18], ['age' => 20], ['age' => 20]]);
  1720. $this->assertFalse($c->every('age', 18));
  1721. $this->assertTrue($c->every('age', '>=', 18));
  1722. $this->assertTrue($c->every(function ($item) {
  1723. return $item['age'] >= 18;
  1724. }));
  1725. $this->assertFalse($c->every(function ($item) {
  1726. return $item['age'] >= 20;
  1727. }));
  1728. $c = new $collection([null, null]);
  1729. $this->assertTrue($c->every(function ($item) {
  1730. return $item === null;
  1731. }));
  1732. $c = new $collection([['active' => true], ['active' => true]]);
  1733. $this->assertTrue($c->every('active'));
  1734. $this->assertTrue($c->every->active);
  1735. $this->assertFalse($c->concat([['active' => false]])->every->active);
  1736. }
  1737. /**
  1738. * @dataProvider collectionClassProvider
  1739. */
  1740. public function testExcept($collection)
  1741. {
  1742. $data = new $collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']);
  1743. $this->assertEquals(['first' => 'Taylor'], $data->except(['last', 'email', 'missing'])->all());
  1744. $this->assertEquals(['first' => 'Taylor'], $data->except('last', 'email', 'missing')->all());
  1745. $this->assertEquals(['first' => 'Taylor'], $data->except(collect(['last', 'email', 'missing']))->all());
  1746. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(['last'])->all());
  1747. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except('last')->all());
  1748. }
  1749. /**
  1750. * @dataProvider collectionClassProvider
  1751. */
  1752. public function testExceptSelf($collection)
  1753. {
  1754. $data = new $collection(['first' => 'Taylor', 'last' => 'Otwell']);
  1755. $this->assertEquals(['first' => 'Taylor', 'last' => 'Otwell'], $data->except($data)->all());
  1756. }
  1757. /**
  1758. * @dataProvider collectionClassProvider
  1759. */
  1760. public function testPluckWithArrayAndObjectValues($collection)
  1761. {
  1762. $data = new $collection([(object) ['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]);
  1763. $this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], $data->pluck('email', 'name')->all());
  1764. $this->assertEquals(['foo', 'bar'], $data->pluck('email')->all());
  1765. }
  1766. /**
  1767. * @dataProvider collectionClassProvider
  1768. */
  1769. public function testPluckWithArrayAccessValues($collection)
  1770. {
  1771. $data = new $collection([
  1772. new TestArrayAccessImplementation(['name' => 'taylor', 'email' => 'foo']),
  1773. new TestArrayAccessImplementation(['name' => 'dayle', 'email' => 'bar']),
  1774. ]);
  1775. $this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], $data->pluck('email', 'name')->all());
  1776. $this->assertEquals(['foo', 'bar'], $data->pluck('email')->all());
  1777. }
  1778. /**
  1779. * @dataProvider collectionClassProvider
  1780. */
  1781. public function testHas($collection)
  1782. {
  1783. $data = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
  1784. $this->assertTrue($data->has('first'));
  1785. $this->assertFalse($data->has('third'));
  1786. $this->assertTrue($data->has(['first', 'second']));
  1787. $this->assertFalse($data->has(['third', 'first']));
  1788. }
  1789. /**
  1790. * @dataProvider collectionClassProvider
  1791. */
  1792. public function testHasAny($collection)
  1793. {
  1794. $data = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
  1795. $this->assertTrue($data->hasAny('first'));
  1796. $this->assertFalse($data->hasAny('third'));
  1797. $this->assertTrue($data->hasAny(['first', 'second']));
  1798. $this->assertTrue($data->hasAny(['first', 'fourth']));
  1799. $this->assertFalse($data->hasAny(['third', 'fourth']));
  1800. }
  1801. /**
  1802. * @dataProvider collectionClassProvider
  1803. */
  1804. public function testImplode($collection)
  1805. {
  1806. $data = new $collection([['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]);
  1807. $this->assertSame('foobar', $data->implode('email'));
  1808. $this->assertSame('foo,bar', $data->implode('email', ','));
  1809. $data = new $collection(['taylor', 'dayle']);
  1810. $this->assertSame('taylordayle', $data->implode(''));
  1811. $this->assertSame('taylor,dayle', $data->implode(','));
  1812. $data = new $collection([
  1813. ['name' => Str::of('taylor'), 'email' => Str::of('foo')],
  1814. ['name' => Str::of('dayle'), 'email' => Str::of('bar')],
  1815. ]);
  1816. $this->assertSame('foobar', $data->implode('email'));
  1817. $this->assertSame('foo,bar', $data->implode('email', ','));
  1818. $data = new $collection([Str::of('taylor'), Str::of('dayle')]);
  1819. $this->assertSame('taylordayle', $data->implode(''));
  1820. $this->assertSame('taylor,dayle', $data->implode(','));
  1821. }
  1822. /**
  1823. * @dataProvider collectionClassProvider
  1824. */
  1825. public function testTake($collection)
  1826. {
  1827. $data = new $collection(['taylor', 'dayle', 'shawn']);
  1828. $data = $data->take(2);
  1829. $this->assertEquals(['taylor', 'dayle'], $data->all());
  1830. }
  1831. public function testGetOrPut()
  1832. {
  1833. $data = new Collection(['name' => 'taylor', 'email' => 'foo']);
  1834. $this->assertEquals('taylor', $data->getOrPut('name', null));
  1835. $this->assertEquals('foo', $data->getOrPut('email', null));
  1836. $this->assertEquals('male', $data->getOrPut('gender', 'male'));
  1837. $this->assertEquals('taylor', $data->get('name'));
  1838. $this->assertEquals('foo', $data->get('email'));
  1839. $this->assertEquals('male', $data->get('gender'));
  1840. $data = new Collection(['name' => 'taylor', 'email' => 'foo']);
  1841. $this->assertEquals('taylor', $data->getOrPut('name', function () {
  1842. return null;
  1843. }));
  1844. $this->assertEquals('foo', $data->getOrPut('email', function () {
  1845. return null;
  1846. }));
  1847. $this->assertEquals('male', $data->getOrPut('gender', function () {
  1848. return 'male';
  1849. }));
  1850. $this->assertEquals('taylor', $data->get('name'));
  1851. $this->assertEquals('foo', $data->get('email'));
  1852. $this->assertEquals('male', $data->get('gender'));
  1853. }
  1854. public function testPut()
  1855. {
  1856. $data = new Collection(['name' => 'taylor', 'email' => 'foo']);
  1857. $data = $data->put('name', 'dayle');
  1858. $this->assertEquals(['name' => 'dayle', 'email' => 'foo'], $data->all());
  1859. }
  1860. public function testPutWithNoKey()
  1861. {
  1862. $data = new Collection(['taylor', 'shawn']);
  1863. $data = $data->put(null, 'dayle');
  1864. $this->assertEquals(['taylor', 'shawn', 'dayle'], $data->all());
  1865. }
  1866. /**
  1867. * @dataProvider collectionClassProvider
  1868. */
  1869. public function testRandom($collection)
  1870. {
  1871. $data = new $collection([1, 2, 3, 4, 5, 6]);
  1872. $random = $data->random();
  1873. $this->assertIsInt($random);
  1874. $this->assertContains($random, $data->all());
  1875. $random = $data->random(0);
  1876. $this->assertInstanceOf($collection, $random);
  1877. $this->assertCount(0, $random);
  1878. $random = $data->random(1);
  1879. $this->assertInstanceOf($collection, $random);
  1880. $this->assertCount(1, $random);
  1881. $random = $data->random(2);
  1882. $this->assertInstanceOf($collection, $random);
  1883. $this->assertCount(2, $random);
  1884. $random = $data->random('0');
  1885. $this->assertInstanceOf($collection, $random);
  1886. $this->assertCount(0, $random);
  1887. $random = $data->random('1');
  1888. $this->assertInstanceOf($collection, $random);
  1889. $this->assertCount(1, $random);
  1890. $random = $data->random('2');
  1891. $this->assertInstanceOf($collection, $random);
  1892. $this->assertCount(2, $random);
  1893. }
  1894. /**
  1895. * @dataProvider collectionClassProvider
  1896. */
  1897. public function testRandomOnEmptyCollection($collection)
  1898. {
  1899. $data = new $collection;
  1900. $random = $data->random(0);
  1901. $this->assertInstanceOf($collection, $random);
  1902. $this->assertCount(0, $random);
  1903. $random = $data->random('0');
  1904. $this->assertInstanceOf($collection, $random);
  1905. $this->assertCount(0, $random);
  1906. }
  1907. /**
  1908. * @dataProvider collectionClassProvider
  1909. */
  1910. public function testTakeLast($collection)
  1911. {
  1912. $data = new $collection(['taylor', 'dayle', 'shawn']);
  1913. $data = $data->take(-2);
  1914. $this->assertEquals([1 => 'dayle', 2 => 'shawn'], $data->all());
  1915. }
  1916. /**
  1917. * @dataProvider collectionClassProvider
  1918. */
  1919. public function testTakeUntilUsingValue($collection)
  1920. {
  1921. $data = new $collection([1, 2, 3, 4]);
  1922. $data = $data->takeUntil(3);
  1923. $this->assertSame([1, 2], $data->toArray());
  1924. }
  1925. /**
  1926. * @dataProvider collectionClassProvider
  1927. */
  1928. public function testTakeUntilUsingCallback($collection)
  1929. {
  1930. $data = new $collection([1, 2, 3, 4]);
  1931. $data = $data->takeUntil(function ($item) {
  1932. return $item >= 3;
  1933. });
  1934. $this->assertSame([1, 2], $data->toArray());
  1935. }
  1936. /**
  1937. * @dataProvider collectionClassProvider
  1938. */
  1939. public function testTakeUntilReturnsAllItemsForUnmetValue($collection)
  1940. {
  1941. $data = new $collection([1, 2, 3, 4]);
  1942. $actual = $data->takeUntil(99);
  1943. $this->assertSame($data->toArray(), $actual->toArray());
  1944. $actual = $data->takeUntil(function ($item) {
  1945. return $item >= 99;
  1946. });
  1947. $this->assertSame($data->toArray(), $actual->toArray());
  1948. }
  1949. /**
  1950. * @dataProvider collectionClassProvider
  1951. */
  1952. public function testTakeUntilCanBeProxied($collection)
  1953. {
  1954. $data = new $collection([
  1955. new TestSupportCollectionHigherOrderItem('Adam'),
  1956. new TestSupportCollectionHigherOrderItem('Taylor'),
  1957. new TestSupportCollectionHigherOrderItem('Jason'),
  1958. ]);
  1959. $actual = $data->takeUntil->is('Jason');
  1960. $this->assertCount(2, $actual);
  1961. $this->assertSame('Adam', $actual->get(0)->name);
  1962. $this->assertSame('Taylor', $actual->get(1)->name);
  1963. }
  1964. /**
  1965. * @dataProvider collectionClassProvider
  1966. */
  1967. public function testTakeWhileUsingValue($collection)
  1968. {
  1969. $data = new $collection([1, 1, 2, 2, 3, 3]);
  1970. $data = $data->takeWhile(1);
  1971. $this->assertSame([1, 1], $data->toArray());
  1972. }
  1973. /**
  1974. * @dataProvider collectionClassProvider
  1975. */
  1976. public function testTakeWhileUsingCallback($collection)
  1977. {
  1978. $data = new $collection([1, 2, 3, 4]);
  1979. $data = $data->takeWhile(function ($item) {
  1980. return $item < 3;
  1981. });
  1982. $this->assertSame([1, 2], $data->toArray());
  1983. }
  1984. /**
  1985. * @dataProvider collectionClassProvider
  1986. */
  1987. public function testTakeWhileReturnsNoItemsForUnmetValue($collection)
  1988. {
  1989. $data = new $collection([1, 2, 3, 4]);
  1990. $actual = $data->takeWhile(2);
  1991. $this->assertSame([], $actual->toArray());
  1992. $actual = $data->takeWhile(function ($item) {
  1993. return $item == 99;
  1994. });
  1995. $this->assertSame([], $actual->toArray());
  1996. }
  1997. /**
  1998. * @dataProvider collectionClassProvider
  1999. */
  2000. public function testTakeWhileCanBeProxied($collection)
  2001. {
  2002. $data = new $collection([
  2003. new TestSupportCollectionHigherOrderItem('Adam'),
  2004. new TestSupportCollectionHigherOrderItem('Adam'),
  2005. new TestSupportCollectionHigherOrderItem('Taylor'),
  2006. new TestSupportCollectionHigherOrderItem('Taylor'),
  2007. ]);
  2008. $actual = $data->takeWhile->is('Adam');
  2009. $this->assertCount(2, $actual);
  2010. $this->assertSame('Adam', $actual->get(0)->name);
  2011. $this->assertSame('Adam', $actual->get(1)->name);
  2012. }
  2013. /**
  2014. * @dataProvider collectionClassProvider
  2015. */
  2016. public function testMacroable($collection)
  2017. {
  2018. // Foo() macro : unique values starting with A
  2019. $collection::macro('foo', function () {
  2020. return $this->filter(function ($item) {
  2021. return strpos($item, 'a') === 0;
  2022. })
  2023. ->unique()
  2024. ->values();
  2025. });
  2026. $c = new $collection(['a', 'a', 'aa', 'aaa', 'bar']);
  2027. $this->assertSame(['a', 'aa', 'aaa'], $c->foo()->all());
  2028. }
  2029. /**
  2030. * @dataProvider collectionClassProvider
  2031. */
  2032. public function testCanAddMethodsToProxy($collection)
  2033. {
  2034. $collection::macro('adults', function ($callback) {
  2035. return $this->filter(function ($item) use ($callback) {
  2036. return $callback($item) >= 18;
  2037. });
  2038. });
  2039. $collection::proxy('adults');
  2040. $c = new $collection([['age' => 3], ['age' => 12], ['age' => 18], ['age' => 56]]);
  2041. $this->assertSame([['age' => 18], ['age' => 56]], $c->adults->age->values()->all());
  2042. }
  2043. /**
  2044. * @dataProvider collectionClassProvider
  2045. */
  2046. public function testMakeMethod($collection)
  2047. {
  2048. $data = $collection::make('foo');
  2049. $this->assertEquals(['foo'], $data->all());
  2050. }
  2051. /**
  2052. * @dataProvider collectionClassProvider
  2053. */
  2054. public function testMakeMethodFromNull($collection)
  2055. {
  2056. $data = $collection::make(null);
  2057. $this->assertEquals([], $data->all());
  2058. $data = $collection::make();
  2059. $this->assertEquals([], $data->all());
  2060. }
  2061. /**
  2062. * @dataProvider collectionClassProvider
  2063. */
  2064. public function testMakeMethodFromCollection($collection)
  2065. {
  2066. $firstCollection = $collection::make(['foo' => 'bar']);
  2067. $secondCollection = $collection::make($firstCollection);
  2068. $this->assertEquals(['foo' => 'bar'], $secondCollection->all());
  2069. }
  2070. /**
  2071. * @dataProvider collectionClassProvider
  2072. */
  2073. public function testMakeMethodFromArray($collection)
  2074. {
  2075. $data = $collection::make(['foo' => 'bar']);
  2076. $this->assertEquals(['foo' => 'bar'], $data->all());
  2077. }
  2078. /**
  2079. * @dataProvider collectionClassProvider
  2080. */
  2081. public function testWrapWithScalar($collection)
  2082. {
  2083. $data = $collection::wrap('foo');
  2084. $this->assertEquals(['foo'], $data->all());
  2085. }
  2086. /**
  2087. * @dataProvider collectionClassProvider
  2088. */
  2089. public function testWrapWithArray($collection)
  2090. {
  2091. $data = $collection::wrap(['foo']);
  2092. $this->assertEquals(['foo'], $data->all());
  2093. }
  2094. /**
  2095. * @dataProvider collectionClassProvider
  2096. */
  2097. public function testWrapWithArrayable($collection)
  2098. {
  2099. $data = $collection::wrap($o = new TestArrayableObject);
  2100. $this->assertEquals([$o], $data->all());
  2101. }
  2102. /**
  2103. * @dataProvider collectionClassProvider
  2104. */
  2105. public function testWrapWithJsonable($collection)
  2106. {
  2107. $data = $collection::wrap($o = new TestJsonableObject);
  2108. $this->assertEquals([$o], $data->all());
  2109. }
  2110. /**
  2111. * @dataProvider collectionClassProvider
  2112. */
  2113. public function testWrapWithJsonSerialize($collection)
  2114. {
  2115. $data = $collection::wrap($o = new TestJsonSerializeObject);
  2116. $this->assertEquals([$o], $data->all());
  2117. }
  2118. /**
  2119. * @dataProvider collectionClassProvider
  2120. */
  2121. public function testWrapWithCollectionClass($collection)
  2122. {
  2123. $data = $collection::wrap($collection::make(['foo']));
  2124. $this->assertEquals(['foo'], $data->all());
  2125. }
  2126. /**
  2127. * @dataProvider collectionClassProvider
  2128. */
  2129. public function testWrapWithCollectionSubclass($collection)
  2130. {
  2131. $data = TestCollectionSubclass::wrap($collection::make(['foo']));
  2132. $this->assertEquals(['foo'], $data->all());
  2133. $this->assertInstanceOf(TestCollectionSubclass::class, $data);
  2134. }
  2135. /**
  2136. * @dataProvider collectionClassProvider
  2137. */
  2138. public function testUnwrapCollection($collection)
  2139. {
  2140. $data = new $collection(['foo']);
  2141. $this->assertEquals(['foo'], $collection::unwrap($data));
  2142. }
  2143. /**
  2144. * @dataProvider collectionClassProvider
  2145. */
  2146. public function testUnwrapCollectionWithArray($collection)
  2147. {
  2148. $this->assertEquals(['foo'], $collection::unwrap(['foo']));
  2149. }
  2150. /**
  2151. * @dataProvider collectionClassProvider
  2152. */
  2153. public function testUnwrapCollectionWithScalar($collection)
  2154. {
  2155. $this->assertSame('foo', $collection::unwrap('foo'));
  2156. }
  2157. /**
  2158. * @dataProvider collectionClassProvider
  2159. */
  2160. public function testEmptyMethod($collection)
  2161. {
  2162. $collection = $collection::empty();
  2163. $this->assertCount(0, $collection->all());
  2164. }
  2165. /**
  2166. * @dataProvider collectionClassProvider
  2167. */
  2168. public function testTimesMethod($collection)
  2169. {
  2170. $two = $collection::times(2, function ($number) {
  2171. return 'slug-'.$number;
  2172. });
  2173. $zero = $collection::times(0, function ($number) {
  2174. return 'slug-'.$number;
  2175. });
  2176. $negative = $collection::times(-4, function ($number) {
  2177. return 'slug-'.$number;
  2178. });
  2179. $range = $collection::times(5);
  2180. $this->assertEquals(['slug-1', 'slug-2'], $two->all());
  2181. $this->assertTrue($zero->isEmpty());
  2182. $this->assertTrue($negative->isEmpty());
  2183. $this->assertEquals(range(1, 5), $range->all());
  2184. }
  2185. /**
  2186. * @dataProvider collectionClassProvider
  2187. */
  2188. public function testRangeMethod($collection)
  2189. {
  2190. $this->assertSame(
  2191. [1, 2, 3, 4, 5],
  2192. $collection::range(1, 5)->all()
  2193. );
  2194. $this->assertSame(
  2195. [-2, -1, 0, 1, 2],
  2196. $collection::range(-2, 2)->all()
  2197. );
  2198. $this->assertSame(
  2199. [-4, -3, -2],
  2200. $collection::range(-4, -2)->all()
  2201. );
  2202. $this->assertSame(
  2203. [5, 4, 3, 2, 1],
  2204. $collection::range(5, 1)->all()
  2205. );
  2206. $this->assertSame(
  2207. [2, 1, 0, -1, -2],
  2208. $collection::range(2, -2)->all()
  2209. );
  2210. $this->assertSame(
  2211. [-2, -3, -4],
  2212. $collection::range(-2, -4)->all()
  2213. );
  2214. }
  2215. /**
  2216. * @dataProvider collectionClassProvider
  2217. */
  2218. public function testConstructMakeFromObject($collection)
  2219. {
  2220. $object = new stdClass;
  2221. $object->foo = 'bar';
  2222. $data = $collection::make($object);
  2223. $this->assertEquals(['foo' => 'bar'], $data->all());
  2224. }
  2225. /**
  2226. * @dataProvider collectionClassProvider
  2227. */
  2228. public function testConstructMethod($collection)
  2229. {
  2230. $data = new $collection('foo');
  2231. $this->assertEquals(['foo'], $data->all());
  2232. }
  2233. /**
  2234. * @dataProvider collectionClassProvider
  2235. */
  2236. public function testConstructMethodFromNull($collection)
  2237. {
  2238. $data = new $collection(null);
  2239. $this->assertEquals([], $data->all());
  2240. $data = new $collection;
  2241. $this->assertEquals([], $data->all());
  2242. }
  2243. /**
  2244. * @dataProvider collectionClassProvider
  2245. */
  2246. public function testConstructMethodFromCollection($collection)
  2247. {
  2248. $firstCollection = new $collection(['foo' => 'bar']);
  2249. $secondCollection = new $collection($firstCollection);
  2250. $this->assertEquals(['foo' => 'bar'], $secondCollection->all());
  2251. }
  2252. /**
  2253. * @dataProvider collectionClassProvider
  2254. */
  2255. public function testConstructMethodFromArray($collection)
  2256. {
  2257. $data = new $collection(['foo' => 'bar']);
  2258. $this->assertEquals(['foo' => 'bar'], $data->all());
  2259. }
  2260. /**
  2261. * @dataProvider collectionClassProvider
  2262. */
  2263. public function testConstructMethodFromObject($collection)
  2264. {
  2265. $object = new stdClass;
  2266. $object->foo = 'bar';
  2267. $data = new $collection($object);
  2268. $this->assertEquals(['foo' => 'bar'], $data->all());
  2269. }
  2270. public function testSplice()
  2271. {
  2272. $data = new Collection(['foo', 'baz']);
  2273. $data->splice(1);
  2274. $this->assertEquals(['foo'], $data->all());
  2275. $data = new Collection(['foo', 'baz']);
  2276. $data->splice(1, 0, 'bar');
  2277. $this->assertEquals(['foo', 'bar', 'baz'], $data->all());
  2278. $data = new Collection(['foo', 'baz']);
  2279. $data->splice(1, 1);
  2280. $this->assertEquals(['foo'], $data->all());
  2281. $data = new Collection(['foo', 'baz']);
  2282. $cut = $data->splice(1, 1, 'bar');
  2283. $this->assertEquals(['foo', 'bar'], $data->all());
  2284. $this->assertEquals(['baz'], $cut->all());
  2285. $data = new Collection(['foo', 'baz']);
  2286. $data->splice(1, 0, ['bar']);
  2287. $this->assertEquals(['foo', 'bar', 'baz'], $data->all());
  2288. $data = new Collection(['foo', 'baz']);
  2289. $data->splice(1, 0, new Collection(['bar']));
  2290. $this->assertEquals(['foo', 'bar', 'baz'], $data->all());
  2291. }
  2292. /**
  2293. * @dataProvider collectionClassProvider
  2294. */
  2295. public function testGetPluckValueWithAccessors($collection)
  2296. {
  2297. $model = new TestAccessorEloquentTestStub(['some' => 'foo']);
  2298. $modelTwo = new TestAccessorEloquentTestStub(['some' => 'bar']);
  2299. $data = new $collection([$model, $modelTwo]);
  2300. $this->assertEquals(['foo', 'bar'], $data->pluck('some')->all());
  2301. }
  2302. /**
  2303. * @dataProvider collectionClassProvider
  2304. */
  2305. public function testMap($collection)
  2306. {
  2307. $data = new $collection(['first' => 'taylor', 'last' => 'otwell']);
  2308. $data = $data->map(function ($item, $key) {
  2309. return $key.'-'.strrev($item);
  2310. });
  2311. $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
  2312. }
  2313. /**
  2314. * @dataProvider collectionClassProvider
  2315. */
  2316. public function testMapSpread($collection)
  2317. {
  2318. $c = new $collection([[1, 'a'], [2, 'b']]);
  2319. $result = $c->mapSpread(function ($number, $character) {
  2320. return "{$number}-{$character}";
  2321. });
  2322. $this->assertEquals(['1-a', '2-b'], $result->all());
  2323. $result = $c->mapSpread(function ($number, $character, $key) {
  2324. return "{$number}-{$character}-{$key}";
  2325. });
  2326. $this->assertEquals(['1-a-0', '2-b-1'], $result->all());
  2327. $c = new $collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
  2328. $result = $c->mapSpread(function ($number, $character, $key) {
  2329. return "{$number}-{$character}-{$key}";
  2330. });
  2331. $this->assertEquals(['1-a-0', '2-b-1'], $result->all());
  2332. }
  2333. /**
  2334. * @dataProvider collectionClassProvider
  2335. */
  2336. public function testFlatMap($collection)
  2337. {
  2338. $data = new $collection([
  2339. ['name' => 'taylor', 'hobbies' => ['programming', 'basketball']],
  2340. ['name' => 'adam', 'hobbies' => ['music', 'powerlifting']],
  2341. ]);
  2342. $data = $data->flatMap(function ($person) {
  2343. return $person['hobbies'];
  2344. });
  2345. $this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all());
  2346. }
  2347. /**
  2348. * @dataProvider collectionClassProvider
  2349. */
  2350. public function testMapToDictionary($collection)
  2351. {
  2352. $data = new $collection([
  2353. ['id' => 1, 'name' => 'A'],
  2354. ['id' => 2, 'name' => 'B'],
  2355. ['id' => 3, 'name' => 'C'],
  2356. ['id' => 4, 'name' => 'B'],
  2357. ]);
  2358. $groups = $data->mapToDictionary(function ($item, $key) {
  2359. return [$item['name'] => $item['id']];
  2360. });
  2361. $this->assertInstanceOf($collection, $groups);
  2362. $this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
  2363. $this->assertIsArray($groups->get('A'));
  2364. }
  2365. /**
  2366. * @dataProvider collectionClassProvider
  2367. */
  2368. public function testMapToDictionaryWithNumericKeys($collection)
  2369. {
  2370. $data = new $collection([1, 2, 3, 2, 1]);
  2371. $groups = $data->mapToDictionary(function ($item, $key) {
  2372. return [$item => $key];
  2373. });
  2374. $this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
  2375. }
  2376. /**
  2377. * @dataProvider collectionClassProvider
  2378. */
  2379. public function testMapToGroups($collection)
  2380. {
  2381. $data = new $collection([
  2382. ['id' => 1, 'name' => 'A'],
  2383. ['id' => 2, 'name' => 'B'],
  2384. ['id' => 3, 'name' => 'C'],
  2385. ['id' => 4, 'name' => 'B'],
  2386. ]);
  2387. $groups = $data->mapToGroups(function ($item, $key) {
  2388. return [$item['name'] => $item['id']];
  2389. });
  2390. $this->assertInstanceOf($collection, $groups);
  2391. $this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
  2392. $this->assertInstanceOf($collection, $groups->get('A'));
  2393. }
  2394. /**
  2395. * @dataProvider collectionClassProvider
  2396. */
  2397. public function testMapToGroupsWithNumericKeys($collection)
  2398. {
  2399. $data = new $collection([1, 2, 3, 2, 1]);
  2400. $groups = $data->mapToGroups(function ($item, $key) {
  2401. return [$item => $key];
  2402. });
  2403. $this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
  2404. }
  2405. /**
  2406. * @dataProvider collectionClassProvider
  2407. */
  2408. public function testMapWithKeys($collection)
  2409. {
  2410. $data = new $collection([
  2411. ['name' => 'Blastoise', 'type' => 'Water', 'idx' => 9],
  2412. ['name' => 'Charmander', 'type' => 'Fire', 'idx' => 4],
  2413. ['name' => 'Dragonair', 'type' => 'Dragon', 'idx' => 148],
  2414. ]);
  2415. $data = $data->mapWithKeys(function ($pokemon) {
  2416. return [$pokemon['name'] => $pokemon['type']];
  2417. });
  2418. $this->assertEquals(
  2419. ['Blastoise' => 'Water', 'Charmander' => 'Fire', 'Dragonair' => 'Dragon'],
  2420. $data->all()
  2421. );
  2422. }
  2423. /**
  2424. * @dataProvider collectionClassProvider
  2425. */
  2426. public function testMapWithKeysIntegerKeys($collection)
  2427. {
  2428. $data = new $collection([
  2429. ['id' => 1, 'name' => 'A'],
  2430. ['id' => 3, 'name' => 'B'],
  2431. ['id' => 2, 'name' => 'C'],
  2432. ]);
  2433. $data = $data->mapWithKeys(function ($item) {
  2434. return [$item['id'] => $item];
  2435. });
  2436. $this->assertSame(
  2437. [1, 3, 2],
  2438. $data->keys()->all()
  2439. );
  2440. }
  2441. /**
  2442. * @dataProvider collectionClassProvider
  2443. */
  2444. public function testMapWithKeysMultipleRows($collection)
  2445. {
  2446. $data = new $collection([
  2447. ['id' => 1, 'name' => 'A'],
  2448. ['id' => 2, 'name' => 'B'],
  2449. ['id' => 3, 'name' => 'C'],
  2450. ]);
  2451. $data = $data->mapWithKeys(function ($item) {
  2452. return [$item['id'] => $item['name'], $item['name'] => $item['id']];
  2453. });
  2454. $this->assertSame(
  2455. [
  2456. 1 => 'A',
  2457. 'A' => 1,
  2458. 2 => 'B',
  2459. 'B' => 2,
  2460. 3 => 'C',
  2461. 'C' => 3,
  2462. ],
  2463. $data->all()
  2464. );
  2465. }
  2466. /**
  2467. * @dataProvider collectionClassProvider
  2468. */
  2469. public function testMapWithKeysCallbackKey($collection)
  2470. {
  2471. $data = new $collection([
  2472. 3 => ['id' => 1, 'name' => 'A'],
  2473. 5 => ['id' => 3, 'name' => 'B'],
  2474. 4 => ['id' => 2, 'name' => 'C'],
  2475. ]);
  2476. $data = $data->mapWithKeys(function ($item, $key) {
  2477. return [$key => $item['id']];
  2478. });
  2479. $this->assertSame(
  2480. [3, 5, 4],
  2481. $data->keys()->all()
  2482. );
  2483. }
  2484. /**
  2485. * @dataProvider collectionClassProvider
  2486. */
  2487. public function testMapInto($collection)
  2488. {
  2489. $data = new $collection([
  2490. 'first', 'second',
  2491. ]);
  2492. $data = $data->mapInto(TestCollectionMapIntoObject::class);
  2493. $this->assertSame('first', $data->get(0)->value);
  2494. $this->assertSame('second', $data->get(1)->value);
  2495. }
  2496. /**
  2497. * @dataProvider collectionClassProvider
  2498. */
  2499. public function testNth($collection)
  2500. {
  2501. $data = new $collection([
  2502. 6 => 'a',
  2503. 4 => 'b',
  2504. 7 => 'c',
  2505. 1 => 'd',
  2506. 5 => 'e',
  2507. 3 => 'f',
  2508. ]);
  2509. $this->assertEquals(['a', 'e'], $data->nth(4)->all());
  2510. $this->assertEquals(['b', 'f'], $data->nth(4, 1)->all());
  2511. $this->assertEquals(['c'], $data->nth(4, 2)->all());
  2512. $this->assertEquals(['d'], $data->nth(4, 3)->all());
  2513. $this->assertEquals(['c', 'e'], $data->nth(2, 2)->all());
  2514. $this->assertEquals(['c', 'd', 'e', 'f'], $data->nth(1, 2)->all());
  2515. }
  2516. /**
  2517. * @dataProvider collectionClassProvider
  2518. */
  2519. public function testMapWithKeysOverwritingKeys($collection)
  2520. {
  2521. $data = new $collection([
  2522. ['id' => 1, 'name' => 'A'],
  2523. ['id' => 2, 'name' => 'B'],
  2524. ['id' => 1, 'name' => 'C'],
  2525. ]);
  2526. $data = $data->mapWithKeys(function ($item) {
  2527. return [$item['id'] => $item['name']];
  2528. });
  2529. $this->assertSame(
  2530. [
  2531. 1 => 'C',
  2532. 2 => 'B',
  2533. ],
  2534. $data->all()
  2535. );
  2536. }
  2537. public function testTransform()
  2538. {
  2539. $data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
  2540. $data->transform(function ($item, $key) {
  2541. return $key.'-'.strrev($item);
  2542. });
  2543. $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
  2544. }
  2545. /**
  2546. * @dataProvider collectionClassProvider
  2547. */
  2548. public function testGroupByAttribute($collection)
  2549. {
  2550. $data = new $collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
  2551. $result = $data->groupBy('rating');
  2552. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  2553. $result = $data->groupBy('url');
  2554. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  2555. }
  2556. /**
  2557. * @dataProvider collectionClassProvider
  2558. */
  2559. public function testGroupByCallable($collection)
  2560. {
  2561. $data = new $collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
  2562. $result = $data->groupBy([$this, 'sortByRating']);
  2563. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  2564. $result = $data->groupBy([$this, 'sortByUrl']);
  2565. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  2566. }
  2567. public function sortByRating(array $value)
  2568. {
  2569. return $value['rating'];
  2570. }
  2571. public function sortByUrl(array $value)
  2572. {
  2573. return $value['url'];
  2574. }
  2575. /**
  2576. * @dataProvider collectionClassProvider
  2577. */
  2578. public function testGroupByAttributePreservingKeys($collection)
  2579. {
  2580. $data = new $collection([10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1'], 30 => ['rating' => 2, 'url' => '2']]);
  2581. $result = $data->groupBy('rating', true);
  2582. $expected_result = [
  2583. 1 => [10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1']],
  2584. 2 => [30 => ['rating' => 2, 'url' => '2']],
  2585. ];
  2586. $this->assertEquals($expected_result, $result->toArray());
  2587. }
  2588. /**
  2589. * @dataProvider collectionClassProvider
  2590. */
  2591. public function testGroupByClosureWhereItemsHaveSingleGroup($collection)
  2592. {
  2593. $data = new $collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
  2594. $result = $data->groupBy(function ($item) {
  2595. return $item['rating'];
  2596. });
  2597. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  2598. }
  2599. /**
  2600. * @dataProvider collectionClassProvider
  2601. */
  2602. public function testGroupByClosureWhereItemsHaveSingleGroupPreservingKeys($collection)
  2603. {
  2604. $data = new $collection([10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1'], 30 => ['rating' => 2, 'url' => '2']]);
  2605. $result = $data->groupBy(function ($item) {
  2606. return $item['rating'];
  2607. }, true);
  2608. $expected_result = [
  2609. 1 => [10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1']],
  2610. 2 => [30 => ['rating' => 2, 'url' => '2']],
  2611. ];
  2612. $this->assertEquals($expected_result, $result->toArray());
  2613. }
  2614. /**
  2615. * @dataProvider collectionClassProvider
  2616. */
  2617. public function testGroupByClosureWhereItemsHaveMultipleGroups($collection)
  2618. {
  2619. $data = new $collection([
  2620. ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  2621. ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  2622. ['user' => 3, 'roles' => ['Role_1']],
  2623. ]);
  2624. $result = $data->groupBy(function ($item) {
  2625. return $item['roles'];
  2626. });
  2627. $expected_result = [
  2628. 'Role_1' => [
  2629. ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  2630. ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  2631. ['user' => 3, 'roles' => ['Role_1']],
  2632. ],
  2633. 'Role_2' => [
  2634. ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  2635. ],
  2636. 'Role_3' => [
  2637. ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  2638. ],
  2639. ];
  2640. $this->assertEquals($expected_result, $result->toArray());
  2641. }
  2642. /**
  2643. * @dataProvider collectionClassProvider
  2644. */
  2645. public function testGroupByClosureWhereItemsHaveMultipleGroupsPreservingKeys($collection)
  2646. {
  2647. $data = new $collection([
  2648. 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  2649. 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  2650. 30 => ['user' => 3, 'roles' => ['Role_1']],
  2651. ]);
  2652. $result = $data->groupBy(function ($item) {
  2653. return $item['roles'];
  2654. }, true);
  2655. $expected_result = [
  2656. 'Role_1' => [
  2657. 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  2658. 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  2659. 30 => ['user' => 3, 'roles' => ['Role_1']],
  2660. ],
  2661. 'Role_2' => [
  2662. 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  2663. ],
  2664. 'Role_3' => [
  2665. 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  2666. ],
  2667. ];
  2668. $this->assertEquals($expected_result, $result->toArray());
  2669. }
  2670. /**
  2671. * @dataProvider collectionClassProvider
  2672. */
  2673. public function testGroupByMultiLevelAndClosurePreservingKeys($collection)
  2674. {
  2675. $data = new $collection([
  2676. 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
  2677. 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
  2678. 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']],
  2679. 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']],
  2680. ]);
  2681. $result = $data->groupBy([
  2682. 'skilllevel',
  2683. function ($item) {
  2684. return $item['roles'];
  2685. },
  2686. ], true);
  2687. $expected_result = [
  2688. 1 => [
  2689. 'Role_1' => [
  2690. 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
  2691. 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
  2692. ],
  2693. 'Role_3' => [
  2694. 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
  2695. ],
  2696. 'Role_2' => [
  2697. 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
  2698. ],
  2699. ],
  2700. 2 => [
  2701. 'Role_1' => [
  2702. 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']],
  2703. ],
  2704. 'Role_2' => [
  2705. 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']],
  2706. ],
  2707. ],
  2708. ];
  2709. $this->assertEquals($expected_result, $result->toArray());
  2710. }
  2711. /**
  2712. * @dataProvider collectionClassProvider
  2713. */
  2714. public function testKeyByAttribute($collection)
  2715. {
  2716. $data = new $collection([['rating' => 1, 'name' => '1'], ['rating' => 2, 'name' => '2'], ['rating' => 3, 'name' => '3']]);
  2717. $result = $data->keyBy('rating');
  2718. $this->assertEquals([1 => ['rating' => 1, 'name' => '1'], 2 => ['rating' => 2, 'name' => '2'], 3 => ['rating' => 3, 'name' => '3']], $result->all());
  2719. $result = $data->keyBy(function ($item) {
  2720. return $item['rating'] * 2;
  2721. });
  2722. $this->assertEquals([2 => ['rating' => 1, 'name' => '1'], 4 => ['rating' => 2, 'name' => '2'], 6 => ['rating' => 3, 'name' => '3']], $result->all());
  2723. }
  2724. /**
  2725. * @dataProvider collectionClassProvider
  2726. */
  2727. public function testKeyByClosure($collection)
  2728. {
  2729. $data = new $collection([
  2730. ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
  2731. ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
  2732. ]);
  2733. $result = $data->keyBy(function ($item, $key) {
  2734. return strtolower($key.'-'.$item['firstname'].$item['lastname']);
  2735. });
  2736. $this->assertEquals([
  2737. '0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
  2738. '1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
  2739. ], $result->all());
  2740. }
  2741. /**
  2742. * @dataProvider collectionClassProvider
  2743. */
  2744. public function testKeyByObject($collection)
  2745. {
  2746. $data = new $collection([
  2747. ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
  2748. ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
  2749. ]);
  2750. $result = $data->keyBy(function ($item, $key) use ($collection) {
  2751. return new $collection([$key, $item['firstname'], $item['lastname']]);
  2752. });
  2753. $this->assertEquals([
  2754. '[0,"Taylor","Otwell"]' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
  2755. '[1,"Lucas","Michot"]' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
  2756. ], $result->all());
  2757. }
  2758. /**
  2759. * @dataProvider collectionClassProvider
  2760. */
  2761. public function testContains($collection)
  2762. {
  2763. $c = new $collection([1, 3, 5]);
  2764. $this->assertTrue($c->contains(1));
  2765. $this->assertTrue($c->contains('1'));
  2766. $this->assertFalse($c->contains(2));
  2767. $this->assertFalse($c->contains('2'));
  2768. $c = new $collection(['1']);
  2769. $this->assertTrue($c->contains('1'));
  2770. $this->assertTrue($c->contains(1));
  2771. $c = new $collection([null]);
  2772. $this->assertTrue($c->contains(false));
  2773. $this->assertTrue($c->contains(null));
  2774. $this->assertTrue($c->contains([]));
  2775. $this->assertTrue($c->contains(0));
  2776. $this->assertTrue($c->contains(''));
  2777. $c = new $collection([0]);
  2778. $this->assertTrue($c->contains(0));
  2779. $this->assertTrue($c->contains('0'));
  2780. $this->assertTrue($c->contains(false));
  2781. $this->assertTrue($c->contains(null));
  2782. $this->assertTrue($c->contains(function ($value) {
  2783. return $value < 5;
  2784. }));
  2785. $this->assertFalse($c->contains(function ($value) {
  2786. return $value > 5;
  2787. }));
  2788. $c = new $collection([['v' => 1], ['v' => 3], ['v' => 5]]);
  2789. $this->assertTrue($c->contains('v', 1));
  2790. $this->assertFalse($c->contains('v', 2));
  2791. $c = new $collection(['date', 'class', (object) ['foo' => 50]]);
  2792. $this->assertTrue($c->contains('date'));
  2793. $this->assertTrue($c->contains('class'));
  2794. $this->assertFalse($c->contains('foo'));
  2795. $c = new $collection([['a' => false, 'b' => false], ['a' => true, 'b' => false]]);
  2796. $this->assertTrue($c->contains->a);
  2797. $this->assertFalse($c->contains->b);
  2798. $c = new $collection([
  2799. null, 1, 2,
  2800. ]);
  2801. $this->assertTrue($c->contains(function ($value) {
  2802. return is_null($value);
  2803. }));
  2804. }
  2805. /**
  2806. * @dataProvider collectionClassProvider
  2807. */
  2808. public function testSome($collection)
  2809. {
  2810. $c = new $collection([1, 3, 5]);
  2811. $this->assertTrue($c->some(1));
  2812. $this->assertFalse($c->some(2));
  2813. $this->assertTrue($c->some(function ($value) {
  2814. return $value < 5;
  2815. }));
  2816. $this->assertFalse($c->some(function ($value) {
  2817. return $value > 5;
  2818. }));
  2819. $c = new $collection([['v' => 1], ['v' => 3], ['v' => 5]]);
  2820. $this->assertTrue($c->some('v', 1));
  2821. $this->assertFalse($c->some('v', 2));
  2822. $c = new $collection(['date', 'class', (object) ['foo' => 50]]);
  2823. $this->assertTrue($c->some('date'));
  2824. $this->assertTrue($c->some('class'));
  2825. $this->assertFalse($c->some('foo'));
  2826. $c = new $collection([['a' => false, 'b' => false], ['a' => true, 'b' => false]]);
  2827. $this->assertTrue($c->some->a);
  2828. $this->assertFalse($c->some->b);
  2829. $c = new $collection([
  2830. null, 1, 2,
  2831. ]);
  2832. $this->assertTrue($c->some(function ($value) {
  2833. return is_null($value);
  2834. }));
  2835. }
  2836. /**
  2837. * @dataProvider collectionClassProvider
  2838. */
  2839. public function testContainsStrict($collection)
  2840. {
  2841. $c = new $collection([1, 3, 5, '02']);
  2842. $this->assertTrue($c->containsStrict(1));
  2843. $this->assertFalse($c->containsStrict('1'));
  2844. $this->assertFalse($c->containsStrict(2));
  2845. $this->assertTrue($c->containsStrict('02'));
  2846. $this->assertFalse($c->containsStrict(true));
  2847. $this->assertTrue($c->containsStrict(function ($value) {
  2848. return $value < 5;
  2849. }));
  2850. $this->assertFalse($c->containsStrict(function ($value) {
  2851. return $value > 5;
  2852. }));
  2853. $c = new $collection([0]);
  2854. $this->assertTrue($c->containsStrict(0));
  2855. $this->assertFalse($c->containsStrict('0'));
  2856. $this->assertFalse($c->containsStrict(false));
  2857. $this->assertFalse($c->containsStrict(null));
  2858. $c = new $collection([1, null]);
  2859. $this->assertTrue($c->containsStrict(null));
  2860. $this->assertFalse($c->containsStrict(0));
  2861. $this->assertFalse($c->containsStrict(false));
  2862. $c = new $collection([['v' => 1], ['v' => 3], ['v' => '04'], ['v' => 5]]);
  2863. $this->assertTrue($c->containsStrict('v', 1));
  2864. $this->assertFalse($c->containsStrict('v', 2));
  2865. $this->assertFalse($c->containsStrict('v', '1'));
  2866. $this->assertFalse($c->containsStrict('v', 4));
  2867. $this->assertTrue($c->containsStrict('v', '04'));
  2868. $c = new $collection(['date', 'class', (object) ['foo' => 50], '']);
  2869. $this->assertTrue($c->containsStrict('date'));
  2870. $this->assertTrue($c->containsStrict('class'));
  2871. $this->assertFalse($c->containsStrict('foo'));
  2872. $this->assertFalse($c->containsStrict(null));
  2873. $this->assertTrue($c->containsStrict(''));
  2874. }
  2875. /**
  2876. * @dataProvider collectionClassProvider
  2877. */
  2878. public function testContainsWithOperator($collection)
  2879. {
  2880. $c = new $collection([['v' => 1], ['v' => 3], ['v' => '4'], ['v' => 5]]);
  2881. $this->assertTrue($c->contains('v', '=', 4));
  2882. $this->assertTrue($c->contains('v', '==', 4));
  2883. $this->assertFalse($c->contains('v', '===', 4));
  2884. $this->assertTrue($c->contains('v', '>', 4));
  2885. }
  2886. /**
  2887. * @dataProvider collectionClassProvider
  2888. */
  2889. public function testGettingSumFromCollection($collection)
  2890. {
  2891. $c = new $collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
  2892. $this->assertEquals(100, $c->sum('foo'));
  2893. $c = new $collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
  2894. $this->assertEquals(100, $c->sum(function ($i) {
  2895. return $i->foo;
  2896. }));
  2897. }
  2898. /**
  2899. * @dataProvider collectionClassProvider
  2900. */
  2901. public function testCanSumValuesWithoutACallback($collection)
  2902. {
  2903. $c = new $collection([1, 2, 3, 4, 5]);
  2904. $this->assertEquals(15, $c->sum());
  2905. }
  2906. /**
  2907. * @dataProvider collectionClassProvider
  2908. */
  2909. public function testGettingSumFromEmptyCollection($collection)
  2910. {
  2911. $c = new $collection;
  2912. $this->assertEquals(0, $c->sum('foo'));
  2913. }
  2914. /**
  2915. * @dataProvider collectionClassProvider
  2916. */
  2917. public function testValueRetrieverAcceptsDotNotation($collection)
  2918. {
  2919. $c = new $collection([
  2920. (object) ['id' => 1, 'foo' => ['bar' => 'B']], (object) ['id' => 2, 'foo' => ['bar' => 'A']],
  2921. ]);
  2922. $c = $c->sortBy('foo.bar');
  2923. $this->assertEquals([2, 1], $c->pluck('id')->all());
  2924. }
  2925. public function testPullRetrievesItemFromCollection()
  2926. {
  2927. $c = new Collection(['foo', 'bar']);
  2928. $this->assertSame('foo', $c->pull(0));
  2929. }
  2930. public function testPullRemovesItemFromCollection()
  2931. {
  2932. $c = new Collection(['foo', 'bar']);
  2933. $c->pull(0);
  2934. $this->assertEquals([1 => 'bar'], $c->all());
  2935. }
  2936. public function testPullReturnsDefault()
  2937. {
  2938. $c = new Collection([]);
  2939. $value = $c->pull(0, 'foo');
  2940. $this->assertSame('foo', $value);
  2941. }
  2942. /**
  2943. * @dataProvider collectionClassProvider
  2944. */
  2945. public function testRejectRemovesElementsPassingTruthTest($collection)
  2946. {
  2947. $c = new $collection(['foo', 'bar']);
  2948. $this->assertEquals(['foo'], $c->reject('bar')->values()->all());
  2949. $c = new $collection(['foo', 'bar']);
  2950. $this->assertEquals(['foo'], $c->reject(function ($v) {
  2951. return $v === 'bar';
  2952. })->values()->all());
  2953. $c = new $collection(['foo', null]);
  2954. $this->assertEquals(['foo'], $c->reject(null)->values()->all());
  2955. $c = new $collection(['foo', 'bar']);
  2956. $this->assertEquals(['foo', 'bar'], $c->reject('baz')->values()->all());
  2957. $c = new $collection(['foo', 'bar']);
  2958. $this->assertEquals(['foo', 'bar'], $c->reject(function ($v) {
  2959. return $v === 'baz';
  2960. })->values()->all());
  2961. $c = new $collection(['id' => 1, 'primary' => 'foo', 'secondary' => 'bar']);
  2962. $this->assertEquals(['primary' => 'foo', 'secondary' => 'bar'], $c->reject(function ($item, $key) {
  2963. return $key === 'id';
  2964. })->all());
  2965. }
  2966. /**
  2967. * @dataProvider collectionClassProvider
  2968. */
  2969. public function testRejectWithoutAnArgumentRemovesTruthyValues($collection)
  2970. {
  2971. $data1 = new $collection([
  2972. false,
  2973. true,
  2974. new $collection(),
  2975. 0,
  2976. ]);
  2977. $this->assertSame([0 => false, 3 => 0], $data1->reject()->all());
  2978. $data2 = new $collection([
  2979. 'a' => true,
  2980. 'b' => true,
  2981. 'c' => true,
  2982. ]);
  2983. $this->assertTrue(
  2984. $data2->reject()->isEmpty()
  2985. );
  2986. }
  2987. /**
  2988. * @dataProvider collectionClassProvider
  2989. */
  2990. public function testSearchReturnsIndexOfFirstFoundItem($collection)
  2991. {
  2992. $c = new $collection([1, 2, 3, 4, 5, 2, 5, 'foo' => 'bar']);
  2993. $this->assertEquals(1, $c->search(2));
  2994. $this->assertEquals(1, $c->search('2'));
  2995. $this->assertSame('foo', $c->search('bar'));
  2996. $this->assertEquals(4, $c->search(function ($value) {
  2997. return $value > 4;
  2998. }));
  2999. $this->assertSame('foo', $c->search(function ($value) {
  3000. return ! is_numeric($value);
  3001. }));
  3002. }
  3003. /**
  3004. * @dataProvider collectionClassProvider
  3005. */
  3006. public function testSearchInStrictMode($collection)
  3007. {
  3008. $c = new $collection([false, 0, 1, [], '']);
  3009. $this->assertFalse($c->search('false', true));
  3010. $this->assertFalse($c->search('1', true));
  3011. $this->assertEquals(0, $c->search(false, true));
  3012. $this->assertEquals(1, $c->search(0, true));
  3013. $this->assertEquals(2, $c->search(1, true));
  3014. $this->assertEquals(3, $c->search([], true));
  3015. $this->assertEquals(4, $c->search('', true));
  3016. }
  3017. /**
  3018. * @dataProvider collectionClassProvider
  3019. */
  3020. public function testSearchReturnsFalseWhenItemIsNotFound($collection)
  3021. {
  3022. $c = new $collection([1, 2, 3, 4, 5, 'foo' => 'bar']);
  3023. $this->assertFalse($c->search(6));
  3024. $this->assertFalse($c->search('foo'));
  3025. $this->assertFalse($c->search(function ($value) {
  3026. return $value < 1 && is_numeric($value);
  3027. }));
  3028. $this->assertFalse($c->search(function ($value) {
  3029. return $value === 'nope';
  3030. }));
  3031. }
  3032. /**
  3033. * @dataProvider collectionClassProvider
  3034. */
  3035. public function testKeys($collection)
  3036. {
  3037. $c = new $collection(['name' => 'taylor', 'framework' => 'laravel']);
  3038. $this->assertEquals(['name', 'framework'], $c->keys()->all());
  3039. }
  3040. /**
  3041. * @dataProvider collectionClassProvider
  3042. */
  3043. public function testPaginate($collection)
  3044. {
  3045. $c = new $collection(['one', 'two', 'three', 'four']);
  3046. $this->assertEquals(['one', 'two'], $c->forPage(0, 2)->all());
  3047. $this->assertEquals(['one', 'two'], $c->forPage(1, 2)->all());
  3048. $this->assertEquals([2 => 'three', 3 => 'four'], $c->forPage(2, 2)->all());
  3049. $this->assertEquals([], $c->forPage(3, 2)->all());
  3050. }
  3051. public function testPrepend()
  3052. {
  3053. $c = new Collection(['one', 'two', 'three', 'four']);
  3054. $this->assertEquals(
  3055. ['zero', 'one', 'two', 'three', 'four'],
  3056. $c->prepend('zero')->all()
  3057. );
  3058. $c = new Collection(['one' => 1, 'two' => 2]);
  3059. $this->assertEquals(
  3060. ['zero' => 0, 'one' => 1, 'two' => 2],
  3061. $c->prepend(0, 'zero')->all()
  3062. );
  3063. $c = new Collection(['one' => 1, 'two' => 2]);
  3064. $this->assertEquals(
  3065. [null => 0, 'one' => 1, 'two' => 2],
  3066. $c->prepend(0, null)->all()
  3067. );
  3068. }
  3069. public function testPushWithOneItem()
  3070. {
  3071. $expected = [
  3072. 0 => 4,
  3073. 1 => 5,
  3074. 2 => 6,
  3075. 3 => ['a', 'b', 'c'],
  3076. 4 => ['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe'],
  3077. 5 => 'Jonny from Laroe',
  3078. ];
  3079. $data = new Collection([4, 5, 6]);
  3080. $data->push(['a', 'b', 'c']);
  3081. $data->push(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
  3082. $actual = $data->push('Jonny from Laroe')->toArray();
  3083. $this->assertSame($expected, $actual);
  3084. }
  3085. public function testPushWithMultipleItems()
  3086. {
  3087. $expected = [
  3088. 0 => 4,
  3089. 1 => 5,
  3090. 2 => 6,
  3091. 3 => 'Jonny',
  3092. 4 => 'from',
  3093. 5 => 'Laroe',
  3094. 6 => 'Jonny',
  3095. 7 => 'from',
  3096. 8 => 'Laroe',
  3097. 9 => 'a',
  3098. 10 => 'b',
  3099. 11 => 'c',
  3100. ];
  3101. $data = new Collection([4, 5, 6]);
  3102. $data->push('Jonny', 'from', 'Laroe');
  3103. $data->push(...[11 => 'Jonny', 12 => 'from', 13 => 'Laroe']);
  3104. $data->push(...collect(['a', 'b', 'c']));
  3105. $actual = $data->push(...[])->toArray();
  3106. $this->assertSame($expected, $actual);
  3107. }
  3108. /**
  3109. * @dataProvider collectionClassProvider
  3110. */
  3111. public function testZip($collection)
  3112. {
  3113. $c = new $collection([1, 2, 3]);
  3114. $c = $c->zip(new $collection([4, 5, 6]));
  3115. $this->assertInstanceOf($collection, $c);
  3116. $this->assertInstanceOf($collection, $c->get(0));
  3117. $this->assertInstanceOf($collection, $c->get(1));
  3118. $this->assertInstanceOf($collection, $c->get(2));
  3119. $this->assertCount(3, $c);
  3120. $this->assertEquals([1, 4], $c->get(0)->all());
  3121. $this->assertEquals([2, 5], $c->get(1)->all());
  3122. $this->assertEquals([3, 6], $c->get(2)->all());
  3123. $c = new $collection([1, 2, 3]);
  3124. $c = $c->zip([4, 5, 6], [7, 8, 9]);
  3125. $this->assertCount(3, $c);
  3126. $this->assertEquals([1, 4, 7], $c->get(0)->all());
  3127. $this->assertEquals([2, 5, 8], $c->get(1)->all());
  3128. $this->assertEquals([3, 6, 9], $c->get(2)->all());
  3129. $c = new $collection([1, 2, 3]);
  3130. $c = $c->zip([4, 5, 6], [7]);
  3131. $this->assertCount(3, $c);
  3132. $this->assertEquals([1, 4, 7], $c->get(0)->all());
  3133. $this->assertEquals([2, 5, null], $c->get(1)->all());
  3134. $this->assertEquals([3, 6, null], $c->get(2)->all());
  3135. }
  3136. /**
  3137. * @dataProvider collectionClassProvider
  3138. */
  3139. public function testPadPadsArrayWithValue($collection)
  3140. {
  3141. $c = new $collection([1, 2, 3]);
  3142. $c = $c->pad(4, 0);
  3143. $this->assertEquals([1, 2, 3, 0], $c->all());
  3144. $c = new $collection([1, 2, 3, 4, 5]);
  3145. $c = $c->pad(4, 0);
  3146. $this->assertEquals([1, 2, 3, 4, 5], $c->all());
  3147. $c = new $collection([1, 2, 3]);
  3148. $c = $c->pad(-4, 0);
  3149. $this->assertEquals([0, 1, 2, 3], $c->all());
  3150. $c = new $collection([1, 2, 3, 4, 5]);
  3151. $c = $c->pad(-4, 0);
  3152. $this->assertEquals([1, 2, 3, 4, 5], $c->all());
  3153. }
  3154. /**
  3155. * @dataProvider collectionClassProvider
  3156. */
  3157. public function testGettingMaxItemsFromCollection($collection)
  3158. {
  3159. $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
  3160. $this->assertEquals(20, $c->max(function ($item) {
  3161. return $item->foo;
  3162. }));
  3163. $this->assertEquals(20, $c->max('foo'));
  3164. $this->assertEquals(20, $c->max->foo);
  3165. $c = new $collection([['foo' => 10], ['foo' => 20]]);
  3166. $this->assertEquals(20, $c->max('foo'));
  3167. $this->assertEquals(20, $c->max->foo);
  3168. $c = new $collection([1, 2, 3, 4, 5]);
  3169. $this->assertEquals(5, $c->max());
  3170. $c = new $collection;
  3171. $this->assertNull($c->max());
  3172. }
  3173. /**
  3174. * @dataProvider collectionClassProvider
  3175. */
  3176. public function testGettingMinItemsFromCollection($collection)
  3177. {
  3178. $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
  3179. $this->assertEquals(10, $c->min(function ($item) {
  3180. return $item->foo;
  3181. }));
  3182. $this->assertEquals(10, $c->min('foo'));
  3183. $this->assertEquals(10, $c->min->foo);
  3184. $c = new $collection([['foo' => 10], ['foo' => 20]]);
  3185. $this->assertEquals(10, $c->min('foo'));
  3186. $this->assertEquals(10, $c->min->foo);
  3187. $c = new $collection([['foo' => 10], ['foo' => 20], ['foo' => null]]);
  3188. $this->assertEquals(10, $c->min('foo'));
  3189. $this->assertEquals(10, $c->min->foo);
  3190. $c = new $collection([1, 2, 3, 4, 5]);
  3191. $this->assertEquals(1, $c->min());
  3192. $c = new $collection([1, null, 3, 4, 5]);
  3193. $this->assertEquals(1, $c->min());
  3194. $c = new $collection([0, 1, 2, 3, 4]);
  3195. $this->assertEquals(0, $c->min());
  3196. $c = new $collection;
  3197. $this->assertNull($c->min());
  3198. }
  3199. /**
  3200. * @dataProvider collectionClassProvider
  3201. */
  3202. public function testOnly($collection)
  3203. {
  3204. $data = new $collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']);
  3205. $this->assertEquals($data->all(), $data->only(null)->all());
  3206. $this->assertEquals(['first' => 'Taylor'], $data->only(['first', 'missing'])->all());
  3207. $this->assertEquals(['first' => 'Taylor'], $data->only('first', 'missing')->all());
  3208. $this->assertEquals(['first' => 'Taylor'], $data->only(collect(['first', 'missing']))->all());
  3209. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(['first', 'email'])->all());
  3210. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only('first', 'email')->all());
  3211. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(collect(['first', 'email']))->all());
  3212. }
  3213. /**
  3214. * @dataProvider collectionClassProvider
  3215. */
  3216. public function testGettingAvgItemsFromCollection($collection)
  3217. {
  3218. $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
  3219. $this->assertEquals(15, $c->avg(function ($item) {
  3220. return $item->foo;
  3221. }));
  3222. $this->assertEquals(15, $c->avg('foo'));
  3223. $this->assertEquals(15, $c->avg->foo);
  3224. $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20], (object) ['foo' => null]]);
  3225. $this->assertEquals(15, $c->avg(function ($item) {
  3226. return $item->foo;
  3227. }));
  3228. $this->assertEquals(15, $c->avg('foo'));
  3229. $this->assertEquals(15, $c->avg->foo);
  3230. $c = new $collection([['foo' => 10], ['foo' => 20]]);
  3231. $this->assertEquals(15, $c->avg('foo'));
  3232. $this->assertEquals(15, $c->avg->foo);
  3233. $c = new $collection([1, 2, 3, 4, 5]);
  3234. $this->assertEquals(3, $c->avg());
  3235. $c = new $collection;
  3236. $this->assertNull($c->avg());
  3237. }
  3238. /**
  3239. * @dataProvider collectionClassProvider
  3240. */
  3241. public function testJsonSerialize($collection)
  3242. {
  3243. $c = new $collection([
  3244. new TestArrayableObject,
  3245. new TestJsonableObject,
  3246. new TestJsonSerializeObject,
  3247. 'baz',
  3248. ]);
  3249. $this->assertSame([
  3250. ['foo' => 'bar'],
  3251. ['foo' => 'bar'],
  3252. ['foo' => 'bar'],
  3253. 'baz',
  3254. ], $c->jsonSerialize());
  3255. }
  3256. /**
  3257. * @dataProvider collectionClassProvider
  3258. */
  3259. public function testCombineWithArray($collection)
  3260. {
  3261. $expected = [
  3262. 1 => 4,
  3263. 2 => 5,
  3264. 3 => 6,
  3265. ];
  3266. $c = new $collection(array_keys($expected));
  3267. $actual = $c->combine(array_values($expected))->toArray();
  3268. $this->assertSame($expected, $actual);
  3269. }
  3270. /**
  3271. * @dataProvider collectionClassProvider
  3272. */
  3273. public function testCombineWithCollection($collection)
  3274. {
  3275. $expected = [
  3276. 1 => 4,
  3277. 2 => 5,
  3278. 3 => 6,
  3279. ];
  3280. $keyCollection = new $collection(array_keys($expected));
  3281. $valueCollection = new $collection(array_values($expected));
  3282. $actual = $keyCollection->combine($valueCollection)->toArray();
  3283. $this->assertSame($expected, $actual);
  3284. }
  3285. /**
  3286. * @dataProvider collectionClassProvider
  3287. */
  3288. public function testConcatWithArray($collection)
  3289. {
  3290. $expected = [
  3291. 0 => 4,
  3292. 1 => 5,
  3293. 2 => 6,
  3294. 3 => 'a',
  3295. 4 => 'b',
  3296. 5 => 'c',
  3297. 6 => 'Jonny',
  3298. 7 => 'from',
  3299. 8 => 'Laroe',
  3300. 9 => 'Jonny',
  3301. 10 => 'from',
  3302. 11 => 'Laroe',
  3303. ];
  3304. $data = new $collection([4, 5, 6]);
  3305. $data = $data->concat(['a', 'b', 'c']);
  3306. $data = $data->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
  3307. $actual = $data->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe'])->toArray();
  3308. $this->assertSame($expected, $actual);
  3309. }
  3310. /**
  3311. * @dataProvider collectionClassProvider
  3312. */
  3313. public function testConcatWithCollection($collection)
  3314. {
  3315. $expected = [
  3316. 0 => 4,
  3317. 1 => 5,
  3318. 2 => 6,
  3319. 3 => 'a',
  3320. 4 => 'b',
  3321. 5 => 'c',
  3322. 6 => 'Jonny',
  3323. 7 => 'from',
  3324. 8 => 'Laroe',
  3325. 9 => 'Jonny',
  3326. 10 => 'from',
  3327. 11 => 'Laroe',
  3328. ];
  3329. $firstCollection = new $collection([4, 5, 6]);
  3330. $secondCollection = new $collection(['a', 'b', 'c']);
  3331. $thirdCollection = new $collection(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
  3332. $firstCollection = $firstCollection->concat($secondCollection);
  3333. $firstCollection = $firstCollection->concat($thirdCollection);
  3334. $actual = $firstCollection->concat($thirdCollection)->toArray();
  3335. $this->assertSame($expected, $actual);
  3336. }
  3337. /**
  3338. * @dataProvider collectionClassProvider
  3339. */
  3340. public function testDump($collection)
  3341. {
  3342. $log = new Collection;
  3343. VarDumper::setHandler(function ($value) use ($log) {
  3344. $log->add($value);
  3345. });
  3346. (new $collection([1, 2, 3]))->dump('one', 'two');
  3347. $this->assertSame(['one', 'two', [1, 2, 3]], $log->all());
  3348. VarDumper::setHandler(null);
  3349. }
  3350. /**
  3351. * @dataProvider collectionClassProvider
  3352. */
  3353. public function testReduce($collection)
  3354. {
  3355. $data = new $collection([1, 2, 3]);
  3356. $this->assertEquals(6, $data->reduce(function ($carry, $element) {
  3357. return $carry += $element;
  3358. }));
  3359. $data = new $collection([
  3360. 'foo' => 'bar',
  3361. 'baz' => 'qux',
  3362. ]);
  3363. $this->assertSame('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
  3364. return $carry .= $key.$element;
  3365. }));
  3366. }
  3367. /**
  3368. * @dataProvider collectionClassProvider
  3369. */
  3370. public function testReduceWithKeys($collection)
  3371. {
  3372. $data = new $collection([
  3373. 'foo' => 'bar',
  3374. 'baz' => 'qux',
  3375. ]);
  3376. $this->assertSame('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) {
  3377. return $carry .= $key.$element;
  3378. }));
  3379. }
  3380. /**
  3381. * @dataProvider collectionClassProvider
  3382. */
  3383. public function testReduceSpread($collection)
  3384. {
  3385. $data = new $collection([-1, 0, 1, 2, 3, 4, 5]);
  3386. [$sum, $max, $min] = $data->reduceSpread(function ($sum, $max, $min, $value) {
  3387. $sum += $value;
  3388. $max = max($max, $value);
  3389. $min = min($min, $value);
  3390. return [$sum, $max, $min];
  3391. }, 0, PHP_INT_MIN, PHP_INT_MAX);
  3392. $this->assertEquals(14, $sum);
  3393. $this->assertEquals(5, $max);
  3394. $this->assertEquals(-1, $min);
  3395. }
  3396. /**
  3397. * @dataProvider collectionClassProvider
  3398. */
  3399. public function testReduceSpreadThrowsAnExceptionIfReducerDoesNotReturnAnArray($collection)
  3400. {
  3401. $data = new $collection([1]);
  3402. $this->expectException(UnexpectedValueException::class);
  3403. $data->reduceSpread(function () {
  3404. return false;
  3405. }, null);
  3406. }
  3407. /**
  3408. * @dataProvider collectionClassProvider
  3409. */
  3410. public function testRandomThrowsAnExceptionUsingAmountBiggerThanCollectionSize($collection)
  3411. {
  3412. $this->expectException(InvalidArgumentException::class);
  3413. $data = new $collection([1, 2, 3]);
  3414. $data->random(4);
  3415. }
  3416. /**
  3417. * @dataProvider collectionClassProvider
  3418. */
  3419. public function testPipe($collection)
  3420. {
  3421. $data = new $collection([1, 2, 3]);
  3422. $this->assertEquals(6, $data->pipe(function ($data) {
  3423. return $data->sum();
  3424. }));
  3425. }
  3426. /**
  3427. * @dataProvider collectionClassProvider
  3428. */
  3429. public function testPipeInto($collection)
  3430. {
  3431. $data = new $collection([
  3432. 'first', 'second',
  3433. ]);
  3434. $instance = $data->pipeInto(TestCollectionMapIntoObject::class);
  3435. $this->assertSame($data, $instance->value);
  3436. }
  3437. /**
  3438. * @dataProvider collectionClassProvider
  3439. */
  3440. public function testPipeThrough($collection)
  3441. {
  3442. $data = new $collection([1, 2, 3]);
  3443. $result = $data->pipeThrough([
  3444. function ($data) {
  3445. return $data->merge([4, 5]);
  3446. },
  3447. function ($data) {
  3448. return $data->sum();
  3449. },
  3450. ]);
  3451. $this->assertEquals(15, $result);
  3452. }
  3453. /**
  3454. * @dataProvider collectionClassProvider
  3455. */
  3456. public function testMedianValueWithArrayCollection($collection)
  3457. {
  3458. $data = new $collection([1, 2, 2, 4]);
  3459. $this->assertEquals(2, $data->median());
  3460. }
  3461. /**
  3462. * @dataProvider collectionClassProvider
  3463. */
  3464. public function testMedianValueByKey($collection)
  3465. {
  3466. $data = new $collection([
  3467. (object) ['foo' => 1],
  3468. (object) ['foo' => 2],
  3469. (object) ['foo' => 2],
  3470. (object) ['foo' => 4],
  3471. ]);
  3472. $this->assertEquals(2, $data->median('foo'));
  3473. }
  3474. /**
  3475. * @dataProvider collectionClassProvider
  3476. */
  3477. public function testMedianOnCollectionWithNull($collection)
  3478. {
  3479. $data = new $collection([
  3480. (object) ['foo' => 1],
  3481. (object) ['foo' => 2],
  3482. (object) ['foo' => 4],
  3483. (object) ['foo' => null],
  3484. ]);
  3485. $this->assertEquals(2, $data->median('foo'));
  3486. }
  3487. /**
  3488. * @dataProvider collectionClassProvider
  3489. */
  3490. public function testEvenMedianCollection($collection)
  3491. {
  3492. $data = new $collection([
  3493. (object) ['foo' => 0],
  3494. (object) ['foo' => 3],
  3495. ]);
  3496. $this->assertEquals(1.5, $data->median('foo'));
  3497. }
  3498. /**
  3499. * @dataProvider collectionClassProvider
  3500. */
  3501. public function testMedianOutOfOrderCollection($collection)
  3502. {
  3503. $data = new $collection([
  3504. (object) ['foo' => 0],
  3505. (object) ['foo' => 5],
  3506. (object) ['foo' => 3],
  3507. ]);
  3508. $this->assertEquals(3, $data->median('foo'));
  3509. }
  3510. /**
  3511. * @dataProvider collectionClassProvider
  3512. */
  3513. public function testMedianOnEmptyCollectionReturnsNull($collection)
  3514. {
  3515. $data = new $collection;
  3516. $this->assertNull($data->median());
  3517. }
  3518. /**
  3519. * @dataProvider collectionClassProvider
  3520. */
  3521. public function testModeOnNullCollection($collection)
  3522. {
  3523. $data = new $collection;
  3524. $this->assertNull($data->mode());
  3525. }
  3526. /**
  3527. * @dataProvider collectionClassProvider
  3528. */
  3529. public function testMode($collection)
  3530. {
  3531. $data = new $collection([1, 2, 3, 4, 4, 5]);
  3532. $this->assertEquals([4], $data->mode());
  3533. }
  3534. /**
  3535. * @dataProvider collectionClassProvider
  3536. */
  3537. public function testModeValueByKey($collection)
  3538. {
  3539. $data = new $collection([
  3540. (object) ['foo' => 1],
  3541. (object) ['foo' => 1],
  3542. (object) ['foo' => 2],
  3543. (object) ['foo' => 4],
  3544. ]);
  3545. $this->assertEquals([1], $data->mode('foo'));
  3546. }
  3547. /**
  3548. * @dataProvider collectionClassProvider
  3549. */
  3550. public function testWithMultipleModeValues($collection)
  3551. {
  3552. $data = new $collection([1, 2, 2, 1]);
  3553. $this->assertEquals([1, 2], $data->mode());
  3554. }
  3555. /**
  3556. * @dataProvider collectionClassProvider
  3557. */
  3558. public function testSliceOffset($collection)
  3559. {
  3560. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
  3561. $this->assertEquals([4, 5, 6, 7, 8], $data->slice(3)->values()->toArray());
  3562. }
  3563. /**
  3564. * @dataProvider collectionClassProvider
  3565. */
  3566. public function testSliceNegativeOffset($collection)
  3567. {
  3568. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
  3569. $this->assertEquals([6, 7, 8], $data->slice(-3)->values()->toArray());
  3570. }
  3571. /**
  3572. * @dataProvider collectionClassProvider
  3573. */
  3574. public function testSliceOffsetAndLength($collection)
  3575. {
  3576. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
  3577. $this->assertEquals([4, 5, 6], $data->slice(3, 3)->values()->toArray());
  3578. }
  3579. /**
  3580. * @dataProvider collectionClassProvider
  3581. */
  3582. public function testSliceOffsetAndNegativeLength($collection)
  3583. {
  3584. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
  3585. $this->assertEquals([4, 5, 6, 7], $data->slice(3, -1)->values()->toArray());
  3586. }
  3587. /**
  3588. * @dataProvider collectionClassProvider
  3589. */
  3590. public function testSliceNegativeOffsetAndLength($collection)
  3591. {
  3592. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
  3593. $this->assertEquals([4, 5, 6], $data->slice(-5, 3)->values()->toArray());
  3594. }
  3595. /**
  3596. * @dataProvider collectionClassProvider
  3597. */
  3598. public function testSliceNegativeOffsetAndNegativeLength($collection)
  3599. {
  3600. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
  3601. $this->assertEquals([3, 4, 5, 6], $data->slice(-6, -2)->values()->toArray());
  3602. }
  3603. /**
  3604. * @dataProvider collectionClassProvider
  3605. */
  3606. public function testCollectionFromTraversable($collection)
  3607. {
  3608. $data = new $collection(new ArrayObject([1, 2, 3]));
  3609. $this->assertEquals([1, 2, 3], $data->toArray());
  3610. }
  3611. /**
  3612. * @dataProvider collectionClassProvider
  3613. */
  3614. public function testCollectionFromTraversableWithKeys($collection)
  3615. {
  3616. $data = new $collection(new ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3]));
  3617. $this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $data->toArray());
  3618. }
  3619. /**
  3620. * @dataProvider collectionClassProvider
  3621. * @requires PHP >= 8.1
  3622. */
  3623. public function testCollectionFromEnum($collection)
  3624. {
  3625. $data = new $collection(TestEnum::A);
  3626. $this->assertEquals([TestEnum::A], $data->toArray());
  3627. }
  3628. /**
  3629. * @dataProvider collectionClassProvider
  3630. * @requires PHP >= 8.1
  3631. */
  3632. public function testCollectionFromBackedEnum($collection)
  3633. {
  3634. $data = new $collection(TestBackedEnum::A);
  3635. $this->assertEquals([TestBackedEnum::A], $data->toArray());
  3636. }
  3637. /**
  3638. * @dataProvider collectionClassProvider
  3639. */
  3640. public function testSplitCollectionWithADivisableCount($collection)
  3641. {
  3642. $data = new $collection(['a', 'b', 'c', 'd']);
  3643. $this->assertEquals(
  3644. [['a', 'b'], ['c', 'd']],
  3645. $data->split(2)->map(function (Collection $chunk) {
  3646. return $chunk->values()->toArray();
  3647. })->toArray()
  3648. );
  3649. $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  3650. $this->assertEquals(
  3651. [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
  3652. $data->split(2)->map(function (Collection $chunk) {
  3653. return $chunk->values()->toArray();
  3654. })->toArray()
  3655. );
  3656. }
  3657. /**
  3658. * @dataProvider collectionClassProvider
  3659. */
  3660. public function testSplitCollectionWithAnUndivisableCount($collection)
  3661. {
  3662. $data = new $collection(['a', 'b', 'c']);
  3663. $this->assertEquals(
  3664. [['a', 'b'], ['c']],
  3665. $data->split(2)->map(function (Collection $chunk) {
  3666. return $chunk->values()->toArray();
  3667. })->toArray()
  3668. );
  3669. }
  3670. /**
  3671. * @dataProvider collectionClassProvider
  3672. */
  3673. public function testSplitCollectionWithCountLessThenDivisor($collection)
  3674. {
  3675. $data = new $collection(['a']);
  3676. $this->assertEquals(
  3677. [['a']],
  3678. $data->split(2)->map(function (Collection $chunk) {
  3679. return $chunk->values()->toArray();
  3680. })->toArray()
  3681. );
  3682. }
  3683. /**
  3684. * @dataProvider collectionClassProvider
  3685. */
  3686. public function testSplitCollectionIntoThreeWithCountOfFour($collection)
  3687. {
  3688. $data = new $collection(['a', 'b', 'c', 'd']);
  3689. $this->assertEquals(
  3690. [['a', 'b'], ['c'], ['d']],
  3691. $data->split(3)->map(function (Collection $chunk) {
  3692. return $chunk->values()->toArray();
  3693. })->toArray()
  3694. );
  3695. }
  3696. /**
  3697. * @dataProvider collectionClassProvider
  3698. */
  3699. public function testSplitCollectionIntoThreeWithCountOfFive($collection)
  3700. {
  3701. $data = new $collection(['a', 'b', 'c', 'd', 'e']);
  3702. $this->assertEquals(
  3703. [['a', 'b'], ['c', 'd'], ['e']],
  3704. $data->split(3)->map(function (Collection $chunk) {
  3705. return $chunk->values()->toArray();
  3706. })->toArray()
  3707. );
  3708. }
  3709. /**
  3710. * @dataProvider collectionClassProvider
  3711. */
  3712. public function testSplitCollectionIntoSixWithCountOfTen($collection)
  3713. {
  3714. $data = new $collection(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']);
  3715. $this->assertEquals(
  3716. [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i'], ['j']],
  3717. $data->split(6)->map(function (Collection $chunk) {
  3718. return $chunk->values()->toArray();
  3719. })->toArray()
  3720. );
  3721. }
  3722. /**
  3723. * @dataProvider collectionClassProvider
  3724. */
  3725. public function testSplitEmptyCollection($collection)
  3726. {
  3727. $data = new $collection;
  3728. $this->assertEquals(
  3729. [],
  3730. $data->split(2)->map(function (Collection $chunk) {
  3731. return $chunk->values()->toArray();
  3732. })->toArray()
  3733. );
  3734. }
  3735. /**
  3736. * @dataProvider collectionClassProvider
  3737. */
  3738. public function testHigherOrderCollectionGroupBy($collection)
  3739. {
  3740. $data = new $collection([
  3741. new TestSupportCollectionHigherOrderItem,
  3742. new TestSupportCollectionHigherOrderItem('TAYLOR'),
  3743. new TestSupportCollectionHigherOrderItem('foo'),
  3744. ]);
  3745. $this->assertEquals([
  3746. 'taylor' => [$data->get(0)],
  3747. 'TAYLOR' => [$data->get(1)],
  3748. 'foo' => [$data->get(2)],
  3749. ], $data->groupBy->name->toArray());
  3750. $this->assertEquals([
  3751. 'TAYLOR' => [$data->get(0), $data->get(1)],
  3752. 'FOO' => [$data->get(2)],
  3753. ], $data->groupBy->uppercase()->toArray());
  3754. }
  3755. /**
  3756. * @dataProvider collectionClassProvider
  3757. */
  3758. public function testHigherOrderCollectionMap($collection)
  3759. {
  3760. $person1 = (object) ['name' => 'Taylor'];
  3761. $person2 = (object) ['name' => 'Yaz'];
  3762. $data = new $collection([$person1, $person2]);
  3763. $this->assertEquals(['Taylor', 'Yaz'], $data->map->name->toArray());
  3764. $data = new $collection([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);
  3765. $this->assertEquals(['TAYLOR', 'TAYLOR'], $data->each->uppercase()->map->name->toArray());
  3766. }
  3767. /**
  3768. * @dataProvider collectionClassProvider
  3769. */
  3770. public function testHigherOrderCollectionMapFromArrays($collection)
  3771. {
  3772. $person1 = ['name' => 'Taylor'];
  3773. $person2 = ['name' => 'Yaz'];
  3774. $data = new $collection([$person1, $person2]);
  3775. $this->assertEquals(['Taylor', 'Yaz'], $data->map->name->toArray());
  3776. $data = new $collection([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);
  3777. $this->assertEquals(['TAYLOR', 'TAYLOR'], $data->each->uppercase()->map->name->toArray());
  3778. }
  3779. /**
  3780. * @dataProvider collectionClassProvider
  3781. */
  3782. public function testPartition($collection)
  3783. {
  3784. $data = new $collection(range(1, 10));
  3785. [$firstPartition, $secondPartition] = $data->partition(function ($i) {
  3786. return $i <= 5;
  3787. })->all();
  3788. $this->assertEquals([1, 2, 3, 4, 5], $firstPartition->values()->toArray());
  3789. $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray());
  3790. }
  3791. /**
  3792. * @dataProvider collectionClassProvider
  3793. */
  3794. public function testPartitionCallbackWithKey($collection)
  3795. {
  3796. $data = new $collection(['zero', 'one', 'two', 'three']);
  3797. [$even, $odd] = $data->partition(function ($item, $index) {
  3798. return $index % 2 === 0;
  3799. })->all();
  3800. $this->assertEquals(['zero', 'two'], $even->values()->toArray());
  3801. $this->assertEquals(['one', 'three'], $odd->values()->toArray());
  3802. }
  3803. /**
  3804. * @dataProvider collectionClassProvider
  3805. */
  3806. public function testPartitionByKey($collection)
  3807. {
  3808. $courses = new $collection([
  3809. ['free' => true, 'title' => 'Basic'], ['free' => false, 'title' => 'Premium'],
  3810. ]);
  3811. [$free, $premium] = $courses->partition('free')->all();
  3812. $this->assertSame([['free' => true, 'title' => 'Basic']], $free->values()->toArray());
  3813. $this->assertSame([['free' => false, 'title' => 'Premium']], $premium->values()->toArray());
  3814. }
  3815. /**
  3816. * @dataProvider collectionClassProvider
  3817. */
  3818. public function testPartitionWithOperators($collection)
  3819. {
  3820. $data = new $collection([
  3821. ['name' => 'Tim', 'age' => 17],
  3822. ['name' => 'Agatha', 'age' => 62],
  3823. ['name' => 'Kristina', 'age' => 33],
  3824. ['name' => 'Tim', 'age' => 41],
  3825. ]);
  3826. [$tims, $others] = $data->partition('name', 'Tim')->all();
  3827. $this->assertEquals([
  3828. ['name' => 'Tim', 'age' => 17],
  3829. ['name' => 'Tim', 'age' => 41],
  3830. ], $tims->values()->all());
  3831. $this->assertEquals([
  3832. ['name' => 'Agatha', 'age' => 62],
  3833. ['name' => 'Kristina', 'age' => 33],
  3834. ], $others->values()->all());
  3835. [$adults, $minors] = $data->partition('age', '>=', 18)->all();
  3836. $this->assertEquals([
  3837. ['name' => 'Agatha', 'age' => 62],
  3838. ['name' => 'Kristina', 'age' => 33],
  3839. ['name' => 'Tim', 'age' => 41],
  3840. ], $adults->values()->all());
  3841. $this->assertEquals([
  3842. ['name' => 'Tim', 'age' => 17],
  3843. ], $minors->values()->all());
  3844. }
  3845. /**
  3846. * @dataProvider collectionClassProvider
  3847. */
  3848. public function testPartitionPreservesKeys($collection)
  3849. {
  3850. $courses = new $collection([
  3851. 'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
  3852. ]);
  3853. [$free, $premium] = $courses->partition('free')->all();
  3854. $this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());
  3855. $this->assertSame(['b' => ['free' => false]], $premium->toArray());
  3856. }
  3857. /**
  3858. * @dataProvider collectionClassProvider
  3859. */
  3860. public function testPartitionEmptyCollection($collection)
  3861. {
  3862. $data = new $collection;
  3863. $this->assertCount(2, $data->partition(function () {
  3864. return true;
  3865. }));
  3866. }
  3867. /**
  3868. * @dataProvider collectionClassProvider
  3869. */
  3870. public function testHigherOrderPartition($collection)
  3871. {
  3872. $courses = new $collection([
  3873. 'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
  3874. ]);
  3875. [$free, $premium] = $courses->partition->free->all();
  3876. $this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());
  3877. $this->assertSame(['b' => ['free' => false]], $premium->toArray());
  3878. }
  3879. /**
  3880. * @dataProvider collectionClassProvider
  3881. */
  3882. public function testTap($collection)
  3883. {
  3884. $data = new $collection([1, 2, 3]);
  3885. $fromTap = [];
  3886. $data = $data->tap(function ($data) use (&$fromTap) {
  3887. $fromTap = $data->slice(0, 1)->toArray();
  3888. });
  3889. $this->assertSame([1], $fromTap);
  3890. $this->assertSame([1, 2, 3], $data->toArray());
  3891. }
  3892. /**
  3893. * @dataProvider collectionClassProvider
  3894. */
  3895. public function testWhen($collection)
  3896. {
  3897. $data = new $collection(['michael', 'tom']);
  3898. $data = $data->when('adam', function ($data, $newName) {
  3899. return $data->concat([$newName]);
  3900. });
  3901. $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
  3902. $data = new $collection(['michael', 'tom']);
  3903. $data = $data->when(false, function ($data) {
  3904. return $data->concat(['adam']);
  3905. });
  3906. $this->assertSame(['michael', 'tom'], $data->toArray());
  3907. }
  3908. /**
  3909. * @dataProvider collectionClassProvider
  3910. */
  3911. public function testWhenDefault($collection)
  3912. {
  3913. $data = new $collection(['michael', 'tom']);
  3914. $data = $data->when(false, function ($data) {
  3915. return $data->concat(['adam']);
  3916. }, function ($data) {
  3917. return $data->concat(['taylor']);
  3918. });
  3919. $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
  3920. }
  3921. /**
  3922. * @dataProvider collectionClassProvider
  3923. */
  3924. public function testWhenEmpty($collection)
  3925. {
  3926. $data = new $collection(['michael', 'tom']);
  3927. $data = $data->whenEmpty(function ($collection) {
  3928. return $data->concat(['adam']);
  3929. });
  3930. $this->assertSame(['michael', 'tom'], $data->toArray());
  3931. $data = new $collection;
  3932. $data = $data->whenEmpty(function ($data) {
  3933. return $data->concat(['adam']);
  3934. });
  3935. $this->assertSame(['adam'], $data->toArray());
  3936. }
  3937. /**
  3938. * @dataProvider collectionClassProvider
  3939. */
  3940. public function testWhenEmptyDefault($collection)
  3941. {
  3942. $data = new $collection(['michael', 'tom']);
  3943. $data = $data->whenEmpty(function ($data) {
  3944. return $data->concat(['adam']);
  3945. }, function ($data) {
  3946. return $data->concat(['taylor']);
  3947. });
  3948. $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
  3949. }
  3950. /**
  3951. * @dataProvider collectionClassProvider
  3952. */
  3953. public function testWhenNotEmpty($collection)
  3954. {
  3955. $data = new $collection(['michael', 'tom']);
  3956. $data = $data->whenNotEmpty(function ($data) {
  3957. return $data->concat(['adam']);
  3958. });
  3959. $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
  3960. $data = new $collection;
  3961. $data = $data->whenNotEmpty(function ($data) {
  3962. return $data->concat(['adam']);
  3963. });
  3964. $this->assertSame([], $data->toArray());
  3965. }
  3966. /**
  3967. * @dataProvider collectionClassProvider
  3968. */
  3969. public function testWhenNotEmptyDefault($collection)
  3970. {
  3971. $data = new $collection(['michael', 'tom']);
  3972. $data = $data->whenNotEmpty(function ($data) {
  3973. return $data->concat(['adam']);
  3974. }, function ($data) {
  3975. return $data->concat(['taylor']);
  3976. });
  3977. $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
  3978. }
  3979. /**
  3980. * @dataProvider collectionClassProvider
  3981. */
  3982. public function testUnless($collection)
  3983. {
  3984. $data = new $collection(['michael', 'tom']);
  3985. $data = $data->unless(false, function ($data) {
  3986. return $data->concat(['caleb']);
  3987. });
  3988. $this->assertSame(['michael', 'tom', 'caleb'], $data->toArray());
  3989. $data = new $collection(['michael', 'tom']);
  3990. $data = $data->unless(true, function ($data) {
  3991. return $data->concat(['caleb']);
  3992. });
  3993. $this->assertSame(['michael', 'tom'], $data->toArray());
  3994. }
  3995. /**
  3996. * @dataProvider collectionClassProvider
  3997. */
  3998. public function testUnlessDefault($collection)
  3999. {
  4000. $data = new $collection(['michael', 'tom']);
  4001. $data = $data->unless(true, function ($data) {
  4002. return $data->concat(['caleb']);
  4003. }, function ($data) {
  4004. return $data->concat(['taylor']);
  4005. });
  4006. $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
  4007. }
  4008. /**
  4009. * @dataProvider collectionClassProvider
  4010. */
  4011. public function testUnlessEmpty($collection)
  4012. {
  4013. $data = new $collection(['michael', 'tom']);
  4014. $data = $data->unlessEmpty(function ($data) {
  4015. return $data->concat(['adam']);
  4016. });
  4017. $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
  4018. $data = new $collection;
  4019. $data = $data->unlessEmpty(function ($data) {
  4020. return $data->concat(['adam']);
  4021. });
  4022. $this->assertSame([], $data->toArray());
  4023. }
  4024. /**
  4025. * @dataProvider collectionClassProvider
  4026. */
  4027. public function testUnlessEmptyDefault($collection)
  4028. {
  4029. $data = new $collection(['michael', 'tom']);
  4030. $data = $data->unlessEmpty(function ($data) {
  4031. return $data->concat(['adam']);
  4032. }, function ($data) {
  4033. return $data->concat(['taylor']);
  4034. });
  4035. $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
  4036. }
  4037. /**
  4038. * @dataProvider collectionClassProvider
  4039. */
  4040. public function testUnlessNotEmpty($collection)
  4041. {
  4042. $data = new $collection(['michael', 'tom']);
  4043. $data = $data->unlessNotEmpty(function ($data) {
  4044. return $data->concat(['adam']);
  4045. });
  4046. $this->assertSame(['michael', 'tom'], $data->toArray());
  4047. $data = new $collection;
  4048. $data = $data->unlessNotEmpty(function ($data) {
  4049. return $data->concat(['adam']);
  4050. });
  4051. $this->assertSame(['adam'], $data->toArray());
  4052. }
  4053. /**
  4054. * @dataProvider collectionClassProvider
  4055. */
  4056. public function testUnlessNotEmptyDefault($collection)
  4057. {
  4058. $data = new $collection(['michael', 'tom']);
  4059. $data = $data->unlessNotEmpty(function ($data) {
  4060. return $data->concat(['adam']);
  4061. }, function ($data) {
  4062. return $data->concat(['taylor']);
  4063. });
  4064. $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
  4065. }
  4066. /**
  4067. * @dataProvider collectionClassProvider
  4068. */
  4069. public function testHasReturnsValidResults($collection)
  4070. {
  4071. $data = new $collection(['foo' => 'one', 'bar' => 'two', 1 => 'three']);
  4072. $this->assertTrue($data->has('foo'));
  4073. $this->assertTrue($data->has('foo', 'bar', 1));
  4074. $this->assertFalse($data->has('foo', 'bar', 1, 'baz'));
  4075. $this->assertFalse($data->has('baz'));
  4076. }
  4077. public function testPutAddsItemToCollection()
  4078. {
  4079. $data = new Collection;
  4080. $this->assertSame([], $data->toArray());
  4081. $data->put('foo', 1);
  4082. $this->assertSame(['foo' => 1], $data->toArray());
  4083. $data->put('bar', ['nested' => 'two']);
  4084. $this->assertSame(['foo' => 1, 'bar' => ['nested' => 'two']], $data->toArray());
  4085. $data->put('foo', 3);
  4086. $this->assertSame(['foo' => 3, 'bar' => ['nested' => 'two']], $data->toArray());
  4087. }
  4088. /**
  4089. * @dataProvider collectionClassProvider
  4090. */
  4091. public function testItThrowsExceptionWhenTryingToAccessNoProxyProperty($collection)
  4092. {
  4093. $data = new $collection;
  4094. $this->expectException(Exception::class);
  4095. $this->expectExceptionMessage('Property [foo] does not exist on this collection instance.');
  4096. $data->foo;
  4097. }
  4098. /**
  4099. * @dataProvider collectionClassProvider
  4100. */
  4101. public function testGetWithNullReturnsNull($collection)
  4102. {
  4103. $data = new $collection([1, 2, 3]);
  4104. $this->assertNull($data->get(null));
  4105. }
  4106. /**
  4107. * @dataProvider collectionClassProvider
  4108. */
  4109. public function testWhereNull($collection)
  4110. {
  4111. $data = new $collection([
  4112. ['name' => 'Taylor'],
  4113. ['name' => null],
  4114. ['name' => 'Bert'],
  4115. ['name' => false],
  4116. ['name' => ''],
  4117. ]);
  4118. $this->assertSame([
  4119. 1 => ['name' => null],
  4120. ], $data->whereNull('name')->all());
  4121. $this->assertSame([], $data->whereNull()->all());
  4122. }
  4123. /**
  4124. * @dataProvider collectionClassProvider
  4125. */
  4126. public function testWhereNullWithoutKey($collection)
  4127. {
  4128. $collection = new $collection([1, null, 3, 'null', false, true]);
  4129. $this->assertSame([
  4130. 1 => null,
  4131. ], $collection->whereNull()->all());
  4132. }
  4133. /**
  4134. * @dataProvider collectionClassProvider
  4135. */
  4136. public function testWhereNotNull($collection)
  4137. {
  4138. $data = new $collection($originalData = [
  4139. ['name' => 'Taylor'],
  4140. ['name' => null],
  4141. ['name' => 'Bert'],
  4142. ['name' => false],
  4143. ['name' => ''],
  4144. ]);
  4145. $this->assertSame([
  4146. 0 => ['name' => 'Taylor'],
  4147. 2 => ['name' => 'Bert'],
  4148. 3 => ['name' => false],
  4149. 4 => ['name' => ''],
  4150. ], $data->whereNotNull('name')->all());
  4151. $this->assertSame($originalData, $data->whereNotNull()->all());
  4152. }
  4153. /**
  4154. * @dataProvider collectionClassProvider
  4155. */
  4156. public function testWhereNotNullWithoutKey($collection)
  4157. {
  4158. $data = new $collection([1, null, 3, 'null', false, true]);
  4159. $this->assertSame([
  4160. 0 => 1,
  4161. 2 => 3,
  4162. 3 => 'null',
  4163. 4 => false,
  4164. 5 => true,
  4165. ], $data->whereNotNull()->all());
  4166. }
  4167. /**
  4168. * @dataProvider collectionClassProvider
  4169. */
  4170. public function testCollect($collection)
  4171. {
  4172. $data = $collection::make([
  4173. 'a' => 1,
  4174. 'b' => 2,
  4175. 'c' => 3,
  4176. ])->collect();
  4177. $this->assertInstanceOf(Collection::class, $data);
  4178. $this->assertSame([
  4179. 'a' => 1,
  4180. 'b' => 2,
  4181. 'c' => 3,
  4182. ], $data->all());
  4183. }
  4184. /**
  4185. * @dataProvider collectionClassProvider
  4186. */
  4187. public function testUndot($collection)
  4188. {
  4189. $data = $collection::make([
  4190. 'name' => 'Taylor',
  4191. 'meta.foo' => 'bar',
  4192. 'meta.baz' => 'boom',
  4193. 'meta.bam.boom' => 'bip',
  4194. ])->undot();
  4195. $this->assertSame([
  4196. 'name' => 'Taylor',
  4197. 'meta' => [
  4198. 'foo' => 'bar',
  4199. 'baz' => 'boom',
  4200. 'bam' => [
  4201. 'boom' => 'bip',
  4202. ],
  4203. ],
  4204. ], $data->all());
  4205. $data = $collection::make([
  4206. 'foo.0' => 'bar',
  4207. 'foo.1' => 'baz',
  4208. 'foo.baz' => 'boom',
  4209. ])->undot();
  4210. $this->assertSame([
  4211. 'foo' => [
  4212. 'bar',
  4213. 'baz',
  4214. 'baz' => 'boom',
  4215. ],
  4216. ], $data->all());
  4217. }
  4218. /**
  4219. * Provides each collection class, respectively.
  4220. *
  4221. * @return array
  4222. */
  4223. public function collectionClassProvider()
  4224. {
  4225. return [
  4226. [Collection::class],
  4227. [LazyCollection::class],
  4228. ];
  4229. }
  4230. }
  4231. class TestSupportCollectionHigherOrderItem
  4232. {
  4233. public $name;
  4234. public function __construct($name = 'taylor')
  4235. {
  4236. $this->name = $name;
  4237. }
  4238. public function uppercase()
  4239. {
  4240. return $this->name = strtoupper($this->name);
  4241. }
  4242. public function is($name)
  4243. {
  4244. return $this->name === $name;
  4245. }
  4246. }
  4247. class TestAccessorEloquentTestStub
  4248. {
  4249. protected $attributes = [];
  4250. public function __construct($attributes)
  4251. {
  4252. $this->attributes = $attributes;
  4253. }
  4254. public function __get($attribute)
  4255. {
  4256. $accessor = 'get'.lcfirst($attribute).'Attribute';
  4257. if (method_exists($this, $accessor)) {
  4258. return $this->$accessor();
  4259. }
  4260. return $this->$attribute;
  4261. }
  4262. public function __isset($attribute)
  4263. {
  4264. $accessor = 'get'.lcfirst($attribute).'Attribute';
  4265. if (method_exists($this, $accessor)) {
  4266. return ! is_null($this->$accessor());
  4267. }
  4268. return isset($this->$attribute);
  4269. }
  4270. public function getSomeAttribute()
  4271. {
  4272. return $this->attributes['some'];
  4273. }
  4274. }
  4275. class TestArrayAccessImplementation implements ArrayAccess
  4276. {
  4277. private $arr;
  4278. public function __construct($arr)
  4279. {
  4280. $this->arr = $arr;
  4281. }
  4282. #[\ReturnTypeWillChange]
  4283. public function offsetExists($offset)
  4284. {
  4285. return isset($this->arr[$offset]);
  4286. }
  4287. #[\ReturnTypeWillChange]
  4288. public function offsetGet($offset)
  4289. {
  4290. return $this->arr[$offset];
  4291. }
  4292. #[\ReturnTypeWillChange]
  4293. public function offsetSet($offset, $value)
  4294. {
  4295. $this->arr[$offset] = $value;
  4296. }
  4297. #[\ReturnTypeWillChange]
  4298. public function offsetUnset($offset)
  4299. {
  4300. unset($this->arr[$offset]);
  4301. }
  4302. }
  4303. class TestArrayableObject implements Arrayable
  4304. {
  4305. public function toArray()
  4306. {
  4307. return ['foo' => 'bar'];
  4308. }
  4309. }
  4310. class TestJsonableObject implements Jsonable
  4311. {
  4312. public function toJson($options = 0)
  4313. {
  4314. return '{"foo":"bar"}';
  4315. }
  4316. }
  4317. class TestJsonSerializeObject implements JsonSerializable
  4318. {
  4319. public function jsonSerialize(): array
  4320. {
  4321. return ['foo' => 'bar'];
  4322. }
  4323. }
  4324. class TestJsonSerializeWithScalarValueObject implements JsonSerializable
  4325. {
  4326. public function jsonSerialize(): string
  4327. {
  4328. return 'foo';
  4329. }
  4330. }
  4331. class TestCollectionMapIntoObject
  4332. {
  4333. public $value;
  4334. public function __construct($value)
  4335. {
  4336. $this->value = $value;
  4337. }
  4338. }
  4339. class TestCollectionSubclass extends Collection
  4340. {
  4341. //
  4342. }