| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140 |
- <?php
- namespace Illuminate\Tests\Support;
- use ArrayAccess;
- use ArrayIterator;
- use ArrayObject;
- use CachingIterator;
- use Exception;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Support\Jsonable;
- use Illuminate\Support\Collection;
- use Illuminate\Support\HtmlString;
- use Illuminate\Support\ItemNotFoundException;
- use Illuminate\Support\LazyCollection;
- use Illuminate\Support\MultipleItemsFoundException;
- use Illuminate\Support\Str;
- use InvalidArgumentException;
- use JsonSerializable;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use ReflectionClass;
- use stdClass;
- use Symfony\Component\VarDumper\VarDumper;
- use UnexpectedValueException;
- if (PHP_VERSION_ID >= 80100) {
- include_once 'Enums.php';
- }
- class SupportCollectionTest extends TestCase
- {
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstReturnsFirstItemInCollection($collection)
- {
- $c = new $collection(['foo', 'bar']);
- $this->assertSame('foo', $c->first());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstWithCallback($collection)
- {
- $data = new $collection(['foo', 'bar', 'baz']);
- $result = $data->first(function ($value) {
- return $value === 'bar';
- });
- $this->assertSame('bar', $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstWithCallbackAndDefault($collection)
- {
- $data = new $collection(['foo', 'bar']);
- $result = $data->first(function ($value) {
- return $value === 'baz';
- }, 'default');
- $this->assertSame('default', $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstWithDefaultAndWithoutCallback($collection)
- {
- $data = new $collection;
- $result = $data->first(null, 'default');
- $this->assertSame('default', $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists($collection)
- {
- $collection = new $collection([
- ['name' => 'foo'],
- ['name' => 'bar'],
- ]);
- $this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->sole());
- $this->assertSame(['name' => 'foo'], $collection->sole('name', '=', 'foo'));
- $this->assertSame(['name' => 'foo'], $collection->sole('name', 'foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSoleThrowsExceptionIfNoItemsExist($collection)
- {
- $this->expectException(ItemNotFoundException::class);
- $collection = new $collection([
- ['name' => 'foo'],
- ['name' => 'bar'],
- ]);
- $collection->where('name', 'INVALID')->sole();
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSoleThrowsExceptionIfMoreThanOneItemExists($collection)
- {
- $this->expectException(MultipleItemsFoundException::class);
- $collection = new $collection([
- ['name' => 'foo'],
- ['name' => 'foo'],
- ['name' => 'bar'],
- ]);
- $collection->where('name', 'foo')->sole();
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSoleReturnsFirstItemInCollectionIfOnlyOneExistsWithCallback($collection)
- {
- $data = new $collection(['foo', 'bar', 'baz']);
- $result = $data->sole(function ($value) {
- return $value === 'bar';
- });
- $this->assertSame('bar', $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSoleThrowsExceptionIfNoItemsExistWithCallback($collection)
- {
- $this->expectException(ItemNotFoundException::class);
- $data = new $collection(['foo', 'bar', 'baz']);
- $data->sole(function ($value) {
- return $value === 'invalid';
- });
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSoleThrowsExceptionIfMoreThanOneItemExistsWithCallback($collection)
- {
- $this->expectException(MultipleItemsFoundException::class);
- $data = new $collection(['foo', 'bar', 'bar']);
- $data->sole(function ($value) {
- return $value === 'bar';
- });
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstOrFailReturnsFirstItemInCollection($collection)
- {
- $collection = new $collection([
- ['name' => 'foo'],
- ['name' => 'bar'],
- ]);
- $this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->firstOrFail());
- $this->assertSame(['name' => 'foo'], $collection->firstOrFail('name', '=', 'foo'));
- $this->assertSame(['name' => 'foo'], $collection->firstOrFail('name', 'foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstOrFailThrowsExceptionIfNoItemsExist($collection)
- {
- $this->expectException(ItemNotFoundException::class);
- $collection = new $collection([
- ['name' => 'foo'],
- ['name' => 'bar'],
- ]);
- $collection->where('name', 'INVALID')->firstOrFail();
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstOrFailDoesntThrowExceptionIfMoreThanOneItemExists($collection)
- {
- $collection = new $collection([
- ['name' => 'foo'],
- ['name' => 'foo'],
- ['name' => 'bar'],
- ]);
- $this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->firstOrFail());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstOrFailReturnsFirstItemInCollectionIfOnlyOneExistsWithCallback($collection)
- {
- $data = new $collection(['foo', 'bar', 'baz']);
- $result = $data->firstOrFail(function ($value) {
- return $value === 'bar';
- });
- $this->assertSame('bar', $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstOrFailThrowsExceptionIfNoItemsExistWithCallback($collection)
- {
- $this->expectException(ItemNotFoundException::class);
- $data = new $collection(['foo', 'bar', 'baz']);
- $data->firstOrFail(function ($value) {
- return $value === 'invalid';
- });
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstOrFailDoesntThrowExceptionIfMoreThanOneItemExistsWithCallback($collection)
- {
- $data = new $collection(['foo', 'bar', 'bar']);
- $this->assertSame(
- 'bar',
- $data->firstOrFail(function ($value) {
- return $value === 'bar';
- })
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstOrFailStopsIteratingAtFirstMatch($collection)
- {
- $data = new $collection([
- function () {
- return false;
- },
- function () {
- return true;
- },
- function () {
- throw new Exception();
- },
- ]);
- $this->assertNotNull($data->firstOrFail(function ($callback) {
- return $callback();
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFirstWhere($collection)
- {
- $data = new $collection([
- ['material' => 'paper', 'type' => 'book'],
- ['material' => 'rubber', 'type' => 'gasket'],
- ]);
- $this->assertSame('book', $data->firstWhere('material', 'paper')['type']);
- $this->assertSame('gasket', $data->firstWhere('material', 'rubber')['type']);
- $this->assertNull($data->firstWhere('material', 'nonexistent'));
- $this->assertNull($data->firstWhere('nonexistent', 'key'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testLastReturnsLastItemInCollection($collection)
- {
- $c = new $collection(['foo', 'bar']);
- $this->assertSame('bar', $c->last());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testLastWithCallback($collection)
- {
- $data = new $collection([100, 200, 300]);
- $result = $data->last(function ($value) {
- return $value < 250;
- });
- $this->assertEquals(200, $result);
- $result = $data->last(function ($value, $key) {
- return $key < 2;
- });
- $this->assertEquals(200, $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testLastWithCallbackAndDefault($collection)
- {
- $data = new $collection(['foo', 'bar']);
- $result = $data->last(function ($value) {
- return $value === 'baz';
- }, 'default');
- $this->assertSame('default', $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testLastWithDefaultAndWithoutCallback($collection)
- {
- $data = new $collection;
- $result = $data->last(null, 'default');
- $this->assertSame('default', $result);
- }
- public function testPopReturnsAndRemovesLastItemInCollection()
- {
- $c = new Collection(['foo', 'bar']);
- $this->assertSame('bar', $c->pop());
- $this->assertSame('foo', $c->first());
- }
- public function testPopReturnsAndRemovesLastXItemsInCollection()
- {
- $c = new Collection(['foo', 'bar', 'baz']);
- $this->assertEquals(new Collection(['baz', 'bar']), $c->pop(2));
- $this->assertSame('foo', $c->first());
- $this->assertEquals(new Collection(['baz', 'bar', 'foo']), (new Collection(['foo', 'bar', 'baz']))->pop(6));
- }
- public function testShiftReturnsAndRemovesFirstItemInCollection()
- {
- $data = new Collection(['Taylor', 'Otwell']);
- $this->assertSame('Taylor', $data->shift());
- $this->assertSame('Otwell', $data->first());
- $this->assertSame('Otwell', $data->shift());
- $this->assertNull($data->first());
- }
- public function testShiftReturnsAndRemovesFirstXItemsInCollection()
- {
- $data = new Collection(['foo', 'bar', 'baz']);
- $this->assertEquals(new Collection(['foo', 'bar']), $data->shift(2));
- $this->assertSame('baz', $data->first());
- $this->assertEquals(new Collection(['foo', 'bar', 'baz']), (new Collection(['foo', 'bar', 'baz']))->shift(6));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSliding($collection)
- {
- // Default parameters: $size = 2, $step = 1
- $this->assertSame([], $collection::times(0)->sliding()->toArray());
- $this->assertSame([], $collection::times(1)->sliding()->toArray());
- $this->assertSame([[1, 2]], $collection::times(2)->sliding()->toArray());
- $this->assertSame(
- [[1, 2], [2, 3]],
- $collection::times(3)->sliding()->map->values()->toArray()
- );
- // Custom step: $size = 2, $step = 3
- $this->assertSame([], $collection::times(1)->sliding(2, 3)->toArray());
- $this->assertSame([[1, 2]], $collection::times(2)->sliding(2, 3)->toArray());
- $this->assertSame([[1, 2]], $collection::times(3)->sliding(2, 3)->toArray());
- $this->assertSame([[1, 2]], $collection::times(4)->sliding(2, 3)->toArray());
- $this->assertSame(
- [[1, 2], [4, 5]],
- $collection::times(5)->sliding(2, 3)->map->values()->toArray()
- );
- // Custom size: $size = 3, $step = 1
- $this->assertSame([], $collection::times(2)->sliding(3)->toArray());
- $this->assertSame([[1, 2, 3]], $collection::times(3)->sliding(3)->toArray());
- $this->assertSame(
- [[1, 2, 3], [2, 3, 4]],
- $collection::times(4)->sliding(3)->map->values()->toArray()
- );
- $this->assertSame(
- [[1, 2, 3], [2, 3, 4]],
- $collection::times(4)->sliding(3)->map->values()->toArray()
- );
- // Custom size and custom step: $size = 3, $step = 2
- $this->assertSame([], $collection::times(2)->sliding(3, 2)->toArray());
- $this->assertSame([[1, 2, 3]], $collection::times(3)->sliding(3, 2)->toArray());
- $this->assertSame([[1, 2, 3]], $collection::times(4)->sliding(3, 2)->toArray());
- $this->assertSame(
- [[1, 2, 3], [3, 4, 5]],
- $collection::times(5)->sliding(3, 2)->map->values()->toArray()
- );
- $this->assertSame(
- [[1, 2, 3], [3, 4, 5]],
- $collection::times(6)->sliding(3, 2)->map->values()->toArray()
- );
- // Ensure keys are preserved, and inner chunks are also collections
- $chunks = $collection::times(3)->sliding();
- $this->assertSame([[0 => 1, 1 => 2], [1 => 2, 2 => 3]], $chunks->toArray());
- $this->assertInstanceOf($collection, $chunks);
- $this->assertInstanceOf($collection, $chunks->first());
- $this->assertInstanceOf($collection, $chunks->skip(1)->first());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testEmptyCollectionIsEmpty($collection)
- {
- $c = new $collection;
- $this->assertTrue($c->isEmpty());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testEmptyCollectionIsNotEmpty($collection)
- {
- $c = new $collection(['foo', 'bar']);
- $this->assertFalse($c->isEmpty());
- $this->assertTrue($c->isNotEmpty());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCollectionIsConstructed($collection)
- {
- $data = new $collection('foo');
- $this->assertSame(['foo'], $data->all());
- $data = new $collection(2);
- $this->assertSame([2], $data->all());
- $data = new $collection(false);
- $this->assertSame([false], $data->all());
- $data = new $collection(null);
- $this->assertEmpty($data->all());
- $data = new $collection;
- $this->assertEmpty($data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCollectionShuffleWithSeed($collection)
- {
- $data = new $collection(range(0, 100, 10));
- $firstRandom = $data->shuffle(1234);
- $secondRandom = $data->shuffle(1234);
- $this->assertEquals($firstRandom, $secondRandom);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSkipMethod($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6]);
- // Total items to skip is smaller than collection length
- $this->assertSame([5, 6], $data->skip(4)->values()->all());
- // Total items to skip is more than collection length
- $this->assertSame([], $data->skip(10)->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSkipUntil($collection)
- {
- $data = new $collection([1, 1, 2, 2, 3, 3, 4, 4]);
- // Item at the beginning of the collection
- $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipUntil(1)->values()->all());
- // Item at the middle of the collection
- $this->assertSame([3, 3, 4, 4], $data->skipUntil(3)->values()->all());
- // Item not in the collection
- $this->assertSame([], $data->skipUntil(5)->values()->all());
- // Item at the beginning of the collection
- $data = $data->skipUntil(function ($value, $key) {
- return $value <= 1;
- })->values();
- $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
- // Item at the middle of the collection
- $data = $data->skipUntil(function ($value, $key) {
- return $value >= 3;
- })->values();
- $this->assertSame([3, 3, 4, 4], $data->all());
- // Item not in the collection
- $data = $data->skipUntil(function ($value, $key) {
- return $value >= 5;
- })->values();
- $this->assertSame([], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSkipWhile($collection)
- {
- $data = new $collection([1, 1, 2, 2, 3, 3, 4, 4]);
- // Item at the beginning of the collection
- $this->assertSame([2, 2, 3, 3, 4, 4], $data->skipWhile(1)->values()->all());
- // Item not in the collection
- $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipWhile(5)->values()->all());
- // Item in the collection but not at the beginning
- $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->skipWhile(2)->values()->all());
- // Item not in the collection
- $data = $data->skipWhile(function ($value, $key) {
- return $value >= 5;
- })->values();
- $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
- // Item in the collection but not at the beginning
- $data = $data->skipWhile(function ($value, $key) {
- return $value >= 2;
- })->values();
- $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $data->all());
- // Item at the beginning of the collection
- $data = $data->skipWhile(function ($value, $key) {
- return $value < 3;
- })->values();
- $this->assertSame([3, 3, 4, 4], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGetArrayableItems($collection)
- {
- $data = new $collection;
- $class = new ReflectionClass($collection);
- $method = $class->getMethod('getArrayableItems');
- $method->setAccessible(true);
- $items = new TestArrayableObject;
- $array = $method->invokeArgs($data, [$items]);
- $this->assertSame(['foo' => 'bar'], $array);
- $items = new TestJsonableObject;
- $array = $method->invokeArgs($data, [$items]);
- $this->assertSame(['foo' => 'bar'], $array);
- $items = new TestJsonSerializeObject;
- $array = $method->invokeArgs($data, [$items]);
- $this->assertSame(['foo' => 'bar'], $array);
- $items = new TestJsonSerializeWithScalarValueObject;
- $array = $method->invokeArgs($data, [$items]);
- $this->assertSame(['foo'], $array);
- $items = new $collection(['foo' => 'bar']);
- $array = $method->invokeArgs($data, [$items]);
- $this->assertSame(['foo' => 'bar'], $array);
- $items = ['foo' => 'bar'];
- $array = $method->invokeArgs($data, [$items]);
- $this->assertSame(['foo' => 'bar'], $array);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testToArrayCallsToArrayOnEachItemInCollection($collection)
- {
- $item1 = m::mock(Arrayable::class);
- $item1->shouldReceive('toArray')->once()->andReturn('foo.array');
- $item2 = m::mock(Arrayable::class);
- $item2->shouldReceive('toArray')->once()->andReturn('bar.array');
- $c = new $collection([$item1, $item2]);
- $results = $c->toArray();
- $this->assertEquals(['foo.array', 'bar.array'], $results);
- }
- public function testLazyReturnsLazyCollection()
- {
- $data = new Collection([1, 2, 3, 4, 5]);
- $lazy = $data->lazy();
- $data->add(6);
- $this->assertInstanceOf(LazyCollection::class, $lazy);
- $this->assertSame([1, 2, 3, 4, 5], $lazy->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testJsonSerializeCallsToArrayOrJsonSerializeOnEachItemInCollection($collection)
- {
- $item1 = m::mock(JsonSerializable::class);
- $item1->shouldReceive('jsonSerialize')->once()->andReturn('foo.json');
- $item2 = m::mock(Arrayable::class);
- $item2->shouldReceive('toArray')->once()->andReturn('bar.array');
- $c = new $collection([$item1, $item2]);
- $results = $c->jsonSerialize();
- $this->assertEquals(['foo.json', 'bar.array'], $results);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testToJsonEncodesTheJsonSerializeResult($collection)
- {
- $c = $this->getMockBuilder($collection)->onlyMethods(['jsonSerialize'])->getMock();
- $c->expects($this->once())->method('jsonSerialize')->willReturn('foo');
- $results = $c->toJson();
- $this->assertJsonStringEqualsJsonString(json_encode('foo'), $results);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCastingToStringJsonEncodesTheToArrayResult($collection)
- {
- $c = $this->getMockBuilder($collection)->onlyMethods(['jsonSerialize'])->getMock();
- $c->expects($this->once())->method('jsonSerialize')->willReturn('foo');
- $this->assertJsonStringEqualsJsonString(json_encode('foo'), (string) $c);
- }
- public function testOffsetAccess()
- {
- $c = new Collection(['name' => 'taylor']);
- $this->assertSame('taylor', $c['name']);
- $c['name'] = 'dayle';
- $this->assertSame('dayle', $c['name']);
- $this->assertTrue(isset($c['name']));
- unset($c['name']);
- $this->assertFalse(isset($c['name']));
- $c[] = 'jason';
- $this->assertSame('jason', $c[0]);
- }
- public function testArrayAccessOffsetExists()
- {
- $c = new Collection(['foo', 'bar', null]);
- $this->assertTrue($c->offsetExists(0));
- $this->assertTrue($c->offsetExists(1));
- $this->assertFalse($c->offsetExists(2));
- }
- public function testBehavesLikeAnArrayWithArrayAccess()
- {
- // indexed array
- $input = ['foo', null];
- $c = new Collection($input);
- $this->assertEquals(isset($input[0]), isset($c[0])); // existing value
- $this->assertEquals(isset($input[1]), isset($c[1])); // existing but null value
- $this->assertEquals(isset($input[1000]), isset($c[1000])); // non-existing value
- $this->assertEquals($input[0], $c[0]);
- $this->assertEquals($input[1], $c[1]);
- // associative array
- $input = ['k1' => 'foo', 'k2' => null];
- $c = new Collection($input);
- $this->assertEquals(isset($input['k1']), isset($c['k1'])); // existing value
- $this->assertEquals(isset($input['k2']), isset($c['k2'])); // existing but null value
- $this->assertEquals(isset($input['k3']), isset($c['k3'])); // non-existing value
- $this->assertEquals($input['k1'], $c['k1']);
- $this->assertEquals($input['k2'], $c['k2']);
- }
- public function testArrayAccessOffsetGet()
- {
- $c = new Collection(['foo', 'bar']);
- $this->assertSame('foo', $c->offsetGet(0));
- $this->assertSame('bar', $c->offsetGet(1));
- }
- public function testArrayAccessOffsetSet()
- {
- $c = new Collection(['foo', 'foo']);
- $c->offsetSet(1, 'bar');
- $this->assertSame('bar', $c[1]);
- $c->offsetSet(null, 'qux');
- $this->assertSame('qux', $c[2]);
- }
- public function testArrayAccessOffsetUnset()
- {
- $c = new Collection(['foo', 'bar']);
- $c->offsetUnset(1);
- $this->assertFalse(isset($c[1]));
- }
- public function testForgetSingleKey()
- {
- $c = new Collection(['foo', 'bar']);
- $c = $c->forget(0)->all();
- $this->assertFalse(isset($c['foo']));
- $this->assertFalse(isset($c[0]));
- $this->assertTrue(isset($c[1]));
- $c = new Collection(['foo' => 'bar', 'baz' => 'qux']);
- $c = $c->forget('foo')->all();
- $this->assertFalse(isset($c['foo']));
- $this->assertTrue(isset($c['baz']));
- }
- public function testForgetArrayOfKeys()
- {
- $c = new Collection(['foo', 'bar', 'baz']);
- $c = $c->forget([0, 2])->all();
- $this->assertFalse(isset($c[0]));
- $this->assertFalse(isset($c[2]));
- $this->assertTrue(isset($c[1]));
- $c = new Collection(['name' => 'taylor', 'foo' => 'bar', 'baz' => 'qux']);
- $c = $c->forget(['foo', 'baz'])->all();
- $this->assertFalse(isset($c['foo']));
- $this->assertFalse(isset($c['baz']));
- $this->assertTrue(isset($c['name']));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCountable($collection)
- {
- $c = new $collection(['foo', 'bar']);
- $this->assertCount(2, $c);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCountByStandalone($collection)
- {
- $c = new $collection(['foo', 'foo', 'foo', 'bar', 'bar', 'foobar']);
- $this->assertEquals(['foo' => 3, 'bar' => 2, 'foobar' => 1], $c->countBy()->all());
- $c = new $collection([true, true, false, false, false]);
- $this->assertEquals([true => 2, false => 3], $c->countBy()->all());
- $c = new $collection([1, 5, 1, 5, 5, 1]);
- $this->assertEquals([1 => 3, 5 => 3], $c->countBy()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCountByWithKey($collection)
- {
- $c = new $collection([
- ['key' => 'a'], ['key' => 'a'], ['key' => 'a'], ['key' => 'a'],
- ['key' => 'b'], ['key' => 'b'], ['key' => 'b'],
- ]);
- $this->assertEquals(['a' => 4, 'b' => 3], $c->countBy('key')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCountableByWithCallback($collection)
- {
- $c = new $collection(['alice', 'aaron', 'bob', 'carla']);
- $this->assertEquals(['a' => 2, 'b' => 1, 'c' => 1], $c->countBy(function ($name) {
- return substr($name, 0, 1);
- })->all());
- $c = new $collection([1, 2, 3, 4, 5]);
- $this->assertEquals([true => 2, false => 3], $c->countBy(function ($i) {
- return $i % 2 === 0;
- })->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testContainsOneItem($collection)
- {
- $this->assertFalse((new $collection([]))->containsOneItem());
- $this->assertTrue((new $collection([1]))->containsOneItem());
- $this->assertFalse((new $collection([1, 2]))->containsOneItem());
- }
- public function testIterable()
- {
- $c = new Collection(['foo']);
- $this->assertInstanceOf(ArrayIterator::class, $c->getIterator());
- $this->assertEquals(['foo'], $c->getIterator()->getArrayCopy());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCachingIterator($collection)
- {
- $c = new $collection(['foo']);
- $this->assertInstanceOf(CachingIterator::class, $c->getCachingIterator());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFilter($collection)
- {
- $c = new $collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
- $this->assertEquals([1 => ['id' => 2, 'name' => 'World']], $c->filter(function ($item) {
- return $item['id'] == 2;
- })->all());
- $c = new $collection(['', 'Hello', '', 'World']);
- $this->assertEquals(['Hello', 'World'], $c->filter()->values()->toArray());
- $c = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
- $this->assertEquals(['first' => 'Hello', 'second' => 'World'], $c->filter(function ($item, $key) {
- return $key !== 'id';
- })->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHigherOrderKeyBy($collection)
- {
- $c = new $collection([
- ['id' => 'id1', 'name' => 'first'],
- ['id' => 'id2', 'name' => 'second'],
- ]);
- $this->assertEquals(['id1' => 'first', 'id2' => 'second'], $c->keyBy->id->map->name->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHigherOrderUnique($collection)
- {
- $c = new $collection([
- ['id' => '1', 'name' => 'first'],
- ['id' => '1', 'name' => 'second'],
- ]);
- $this->assertCount(1, $c->unique->id);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHigherOrderFilter($collection)
- {
- $c = new $collection([
- new class
- {
- public $name = 'Alex';
- public function active()
- {
- return true;
- }
- },
- new class
- {
- public $name = 'John';
- public function active()
- {
- return false;
- }
- },
- ]);
- $this->assertCount(1, $c->filter->active());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhere($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
- $this->assertEquals(
- [['v' => 3], ['v' => '3']],
- $c->where('v', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 3], ['v' => '3']],
- $c->where('v', '=', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 3], ['v' => '3']],
- $c->where('v', '==', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 3], ['v' => '3']],
- $c->where('v', 'garbage', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 3]],
- $c->where('v', '===', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2], ['v' => 4]],
- $c->where('v', '<>', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2], ['v' => 4]],
- $c->where('v', '!=', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2], ['v' => '3'], ['v' => 4]],
- $c->where('v', '!==', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3']],
- $c->where('v', '<=', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 3], ['v' => '3'], ['v' => 4]],
- $c->where('v', '>=', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2]],
- $c->where('v', '<', 3)->values()->all()
- );
- $this->assertEquals(
- [['v' => 4]],
- $c->where('v', '>', 3)->values()->all()
- );
- $object = (object) ['foo' => 'bar'];
- $this->assertEquals(
- [],
- $c->where('v', $object)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
- $c->where('v', '<>', $object)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
- $c->where('v', '!=', $object)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
- $c->where('v', '!==', $object)->values()->all()
- );
- $this->assertEquals(
- [],
- $c->where('v', '>', $object)->values()->all()
- );
- $c = new $collection([['v' => 1], ['v' => $object]]);
- $this->assertEquals(
- [['v' => $object]],
- $c->where('v', $object)->values()->all()
- );
- $this->assertEquals(
- [['v' => 1], ['v' => $object]],
- $c->where('v', '<>', null)->values()->all()
- );
- $this->assertEquals(
- [],
- $c->where('v', '<', null)->values()->all()
- );
- $c = new $collection([['v' => 1], ['v' => new HtmlString('hello')]]);
- $this->assertEquals(
- [['v' => new HtmlString('hello')]],
- $c->where('v', 'hello')->values()->all()
- );
- $c = new $collection([['v' => 1], ['v' => 'hello']]);
- $this->assertEquals(
- [['v' => 'hello']],
- $c->where('v', new HtmlString('hello'))->values()->all()
- );
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => null]]);
- $this->assertEquals(
- [['v' => 1], ['v' => 2]],
- $c->where('v')->values()->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereStrict($collection)
- {
- $c = new $collection([['v' => 3], ['v' => '3']]);
- $this->assertEquals(
- [['v' => 3]],
- $c->whereStrict('v', 3)->values()->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereInstanceOf($collection)
- {
- $c = new $collection([new stdClass, new stdClass, new $collection, new stdClass, new Str]);
- $this->assertCount(3, $c->whereInstanceOf(stdClass::class));
- $this->assertCount(4, $c->whereInstanceOf([stdClass::class, Str::class]));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereIn($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
- $this->assertEquals([['v' => 1], ['v' => 3], ['v' => '3']], $c->whereIn('v', [1, 3])->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereInStrict($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
- $this->assertEquals([['v' => 1], ['v' => 3]], $c->whereInStrict('v', [1, 3])->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereNotIn($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
- $this->assertEquals([['v' => 2], ['v' => 4]], $c->whereNotIn('v', [1, 3])->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereNotInStrict($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
- $this->assertEquals([['v' => 2], ['v' => '3'], ['v' => 4]], $c->whereNotInStrict('v', [1, 3])->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testValues($collection)
- {
- $c = new $collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
- $this->assertEquals([['id' => 2, 'name' => 'World']], $c->filter(function ($item) {
- return $item['id'] == 2;
- })->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testBetween($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
- $this->assertEquals([['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
- $c->whereBetween('v', [2, 4])->values()->all());
- $this->assertEquals([['v' => 1]], $c->whereBetween('v', [-1, 1])->all());
- $this->assertEquals([['v' => 3], ['v' => '3']], $c->whereBetween('v', [3, 3])->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereNotBetween($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
- $this->assertEquals([['v' => 1]], $c->whereNotBetween('v', [2, 4])->values()->all());
- $this->assertEquals([['v' => 2], ['v' => 3], ['v' => 3], ['v' => 4]], $c->whereNotBetween('v', [-1, 1])->values()->all());
- $this->assertEquals([['v' => 1], ['v' => '2'], ['v' => '4']], $c->whereNotBetween('v', [3, 3])->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFlatten($collection)
- {
- // Flat arrays are unaffected
- $c = new $collection(['#foo', '#bar', '#baz']);
- $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
- // Nested arrays are flattened with existing flat items
- $c = new $collection([['#foo', '#bar'], '#baz']);
- $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
- // Sets of nested arrays are flattened
- $c = new $collection([['#foo', '#bar'], ['#baz']]);
- $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
- // Deeply nested arrays are flattened
- $c = new $collection([['#foo', ['#bar']], ['#baz']]);
- $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
- // Nested collections are flattened alongside arrays
- $c = new $collection([new $collection(['#foo', '#bar']), ['#baz']]);
- $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
- // Nested collections containing plain arrays are flattened
- $c = new $collection([new $collection(['#foo', ['#bar']]), ['#baz']]);
- $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
- // Nested arrays containing collections are flattened
- $c = new $collection([['#foo', new $collection(['#bar'])], ['#baz']]);
- $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
- // Nested arrays containing collections containing arrays are flattened
- $c = new $collection([['#foo', new $collection(['#bar', ['#zap']])], ['#baz']]);
- $this->assertEquals(['#foo', '#bar', '#zap', '#baz'], $c->flatten()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFlattenWithDepth($collection)
- {
- // No depth flattens recursively
- $c = new $collection([['#foo', ['#bar', ['#baz']]], '#zap']);
- $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
- // Specifying a depth only flattens to that depth
- $c = new $collection([['#foo', ['#bar', ['#baz']]], '#zap']);
- $this->assertEquals(['#foo', ['#bar', ['#baz']], '#zap'], $c->flatten(1)->all());
- $c = new $collection([['#foo', ['#bar', ['#baz']]], '#zap']);
- $this->assertEquals(['#foo', '#bar', ['#baz'], '#zap'], $c->flatten(2)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFlattenIgnoresKeys($collection)
- {
- // No depth ignores keys
- $c = new $collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
- $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
- // Depth of 1 ignores keys
- $c = new $collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
- $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten(1)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMergeNull($collection)
- {
- $c = new $collection(['name' => 'Hello']);
- $this->assertEquals(['name' => 'Hello'], $c->merge(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMergeArray($collection)
- {
- $c = new $collection(['name' => 'Hello']);
- $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->merge(['id' => 1])->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMergeCollection($collection)
- {
- $c = new $collection(['name' => 'Hello']);
- $this->assertEquals(['name' => 'World', 'id' => 1], $c->merge(new $collection(['name' => 'World', 'id' => 1]))->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMergeRecursiveNull($collection)
- {
- $c = new $collection(['name' => 'Hello']);
- $this->assertEquals(['name' => 'Hello'], $c->mergeRecursive(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMergeRecursiveArray($collection)
- {
- $c = new $collection(['name' => 'Hello', 'id' => 1]);
- $this->assertEquals(['name' => 'Hello', 'id' => [1, 2]], $c->mergeRecursive(['id' => 2])->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMergeRecursiveCollection($collection)
- {
- $c = new $collection(['name' => 'Hello', 'id' => 1, 'meta' => ['tags' => ['a', 'b'], 'roles' => 'admin']]);
- $this->assertEquals(
- ['name' => 'Hello', 'id' => 1, 'meta' => ['tags' => ['a', 'b', 'c'], 'roles' => ['admin', 'editor']]],
- $c->mergeRecursive(new $collection(['meta' => ['tags' => ['c'], 'roles' => 'editor']]))->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReplaceNull($collection)
- {
- $c = new $collection(['a', 'b', 'c']);
- $this->assertEquals(['a', 'b', 'c'], $c->replace(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReplaceArray($collection)
- {
- $c = new $collection(['a', 'b', 'c']);
- $this->assertEquals(['a', 'd', 'e'], $c->replace([1 => 'd', 2 => 'e'])->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReplaceCollection($collection)
- {
- $c = new $collection(['a', 'b', 'c']);
- $this->assertEquals(
- ['a', 'd', 'e'],
- $c->replace(new $collection([1 => 'd', 2 => 'e']))->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReplaceRecursiveNull($collection)
- {
- $c = new $collection(['a', 'b', ['c', 'd']]);
- $this->assertEquals(['a', 'b', ['c', 'd']], $c->replaceRecursive(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReplaceRecursiveArray($collection)
- {
- $c = new $collection(['a', 'b', ['c', 'd']]);
- $this->assertEquals(['z', 'b', ['c', 'e']], $c->replaceRecursive(['z', 2 => [1 => 'e']])->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReplaceRecursiveCollection($collection)
- {
- $c = new $collection(['a', 'b', ['c', 'd']]);
- $this->assertEquals(
- ['z', 'b', ['c', 'e']],
- $c->replaceRecursive(new $collection(['z', 2 => [1 => 'e']]))->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnionNull($collection)
- {
- $c = new $collection(['name' => 'Hello']);
- $this->assertEquals(['name' => 'Hello'], $c->union(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnionArray($collection)
- {
- $c = new $collection(['name' => 'Hello']);
- $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(['id' => 1])->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnionCollection($collection)
- {
- $c = new $collection(['name' => 'Hello']);
- $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(new $collection(['name' => 'World', 'id' => 1]))->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffCollection($collection)
- {
- $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
- $this->assertEquals(['id' => 1], $c->diff(new $collection(['first_word' => 'Hello', 'last_word' => 'World']))->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffUsingWithCollection($collection)
- {
- $c = new $collection(['en_GB', 'fr', 'HR']);
- // demonstrate that diffKeys wont support case insensitivity
- $this->assertEquals(['en_GB', 'fr', 'HR'], $c->diff(new $collection(['en_gb', 'hr']))->values()->toArray());
- // allow for case insensitive difference
- $this->assertEquals(['fr'], $c->diffUsing(new $collection(['en_gb', 'hr']), 'strcasecmp')->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffUsingWithNull($collection)
- {
- $c = new $collection(['en_GB', 'fr', 'HR']);
- $this->assertEquals(['en_GB', 'fr', 'HR'], $c->diffUsing(null, 'strcasecmp')->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffNull($collection)
- {
- $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
- $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c->diff(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffKeys($collection)
- {
- $c1 = new $collection(['id' => 1, 'first_word' => 'Hello']);
- $c2 = new $collection(['id' => 123, 'foo_bar' => 'Hello']);
- $this->assertEquals(['first_word' => 'Hello'], $c1->diffKeys($c2)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffKeysUsing($collection)
- {
- $c1 = new $collection(['id' => 1, 'first_word' => 'Hello']);
- $c2 = new $collection(['ID' => 123, 'foo_bar' => 'Hello']);
- // demonstrate that diffKeys wont support case insensitivity
- $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c1->diffKeys($c2)->all());
- // allow for case insensitive difference
- $this->assertEquals(['first_word' => 'Hello'], $c1->diffKeysUsing($c2, 'strcasecmp')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffAssoc($collection)
- {
- $c1 = new $collection(['id' => 1, 'first_word' => 'Hello', 'not_affected' => 'value']);
- $c2 = new $collection(['id' => 123, 'foo_bar' => 'Hello', 'not_affected' => 'value']);
- $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c1->diffAssoc($c2)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDiffAssocUsing($collection)
- {
- $c1 = new $collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']);
- $c2 = new $collection(['A' => 'green', 'yellow', 'red']);
- // demonstrate that the case of the keys will affect the output when diffAssoc is used
- $this->assertEquals(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssoc($c2)->all());
- // allow for case insensitive difference
- $this->assertEquals(['b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssocUsing($c2, 'strcasecmp')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDuplicates($collection)
- {
- $duplicates = $collection::make([1, 2, 1, 'laravel', null, 'laravel', 'php', null])->duplicates()->all();
- $this->assertSame([2 => 1, 5 => 'laravel', 7 => null], $duplicates);
- // does loose comparison
- $duplicates = $collection::make([2, '2', [], null])->duplicates()->all();
- $this->assertSame([1 => '2', 3 => null], $duplicates);
- // works with mix of primitives
- $duplicates = $collection::make([1, '2', ['laravel'], ['laravel'], null, '2'])->duplicates()->all();
- $this->assertSame([3 => ['laravel'], 5 => '2'], $duplicates);
- // works with mix of objects and primitives **excepts numbers**.
- $expected = new Collection(['laravel']);
- $duplicates = $collection::make([new Collection(['laravel']), $expected, $expected, [], '2', '2'])->duplicates()->all();
- $this->assertSame([1 => $expected, 2 => $expected, 5 => '2'], $duplicates);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDuplicatesWithKey($collection)
- {
- $items = [['framework' => 'vue'], ['framework' => 'laravel'], ['framework' => 'laravel']];
- $duplicates = $collection::make($items)->duplicates('framework')->all();
- $this->assertSame([2 => 'laravel'], $duplicates);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDuplicatesWithCallback($collection)
- {
- $items = [['framework' => 'vue'], ['framework' => 'laravel'], ['framework' => 'laravel']];
- $duplicates = $collection::make($items)->duplicates(function ($item) {
- return $item['framework'];
- })->all();
- $this->assertSame([2 => 'laravel'], $duplicates);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDuplicatesWithStrict($collection)
- {
- $duplicates = $collection::make([1, 2, 1, 'laravel', null, 'laravel', 'php', null])->duplicatesStrict()->all();
- $this->assertSame([2 => 1, 5 => 'laravel', 7 => null], $duplicates);
- // does strict comparison
- $duplicates = $collection::make([2, '2', [], null])->duplicatesStrict()->all();
- $this->assertSame([], $duplicates);
- // works with mix of primitives
- $duplicates = $collection::make([1, '2', ['laravel'], ['laravel'], null, '2'])->duplicatesStrict()->all();
- $this->assertSame([3 => ['laravel'], 5 => '2'], $duplicates);
- // works with mix of primitives, objects, and numbers
- $expected = new $collection(['laravel']);
- $duplicates = $collection::make([new $collection(['laravel']), $expected, $expected, [], '2', '2'])->duplicatesStrict()->all();
- $this->assertSame([2 => $expected, 5 => '2'], $duplicates);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testEach($collection)
- {
- $c = new $collection($original = [1, 2, 'foo' => 'bar', 'bam' => 'baz']);
- $result = [];
- $c->each(function ($item, $key) use (&$result) {
- $result[$key] = $item;
- });
- $this->assertEquals($original, $result);
- $result = [];
- $c->each(function ($item, $key) use (&$result) {
- $result[$key] = $item;
- if (is_string($key)) {
- return false;
- }
- });
- $this->assertEquals([1, 2, 'foo' => 'bar'], $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testEachSpread($collection)
- {
- $c = new $collection([[1, 'a'], [2, 'b']]);
- $result = [];
- $c->eachSpread(function ($number, $character) use (&$result) {
- $result[] = [$number, $character];
- });
- $this->assertEquals($c->all(), $result);
- $result = [];
- $c->eachSpread(function ($number, $character) use (&$result) {
- $result[] = [$number, $character];
- return false;
- });
- $this->assertEquals([[1, 'a']], $result);
- $result = [];
- $c->eachSpread(function ($number, $character, $key) use (&$result) {
- $result[] = [$number, $character, $key];
- });
- $this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
- $c = new $collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
- $result = [];
- $c->eachSpread(function ($number, $character, $key) use (&$result) {
- $result[] = [$number, $character, $key];
- });
- $this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testIntersectNull($collection)
- {
- $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
- $this->assertEquals([], $c->intersect(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testIntersectCollection($collection)
- {
- $c = new $collection(['id' => 1, 'first_word' => 'Hello']);
- $this->assertEquals(['first_word' => 'Hello'], $c->intersect(new $collection(['first_world' => 'Hello', 'last_word' => 'World']))->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testIntersectByKeysNull($collection)
- {
- $c = new $collection(['name' => 'Mateus', 'age' => 18]);
- $this->assertEquals([], $c->intersectByKeys(null)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testIntersectByKeys($collection)
- {
- $c = new $collection(['name' => 'Mateus', 'age' => 18]);
- $this->assertEquals(['name' => 'Mateus'], $c->intersectByKeys(new $collection(['name' => 'Mateus', 'surname' => 'Guimaraes']))->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnique($collection)
- {
- $c = new $collection(['Hello', 'World', 'World']);
- $this->assertEquals(['Hello', 'World'], $c->unique()->all());
- $c = new $collection([[1, 2], [1, 2], [2, 3], [3, 4], [2, 3]]);
- $this->assertEquals([[1, 2], [2, 3], [3, 4]], $c->unique()->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUniqueWithCallback($collection)
- {
- $c = new $collection([
- 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
- 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'],
- 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
- 4 => ['id' => 4, 'first' => 'Abigail', 'last' => 'Otwell'],
- 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'],
- 6 => ['id' => 6, 'first' => 'Taylor', 'last' => 'Swift'],
- ]);
- $this->assertEquals([
- 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
- 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
- ], $c->unique('first')->all());
- $this->assertEquals([
- 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
- 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
- 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'],
- ], $c->unique(function ($item) {
- return $item['first'].$item['last'];
- })->all());
- $this->assertEquals([
- 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
- 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'],
- ], $c->unique(function ($item, $key) {
- return $key % 2;
- })->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUniqueStrict($collection)
- {
- $c = new $collection([
- [
- 'id' => '0',
- 'name' => 'zero',
- ],
- [
- 'id' => '00',
- 'name' => 'double zero',
- ],
- [
- 'id' => '0',
- 'name' => 'again zero',
- ],
- ]);
- $this->assertEquals([
- [
- 'id' => '0',
- 'name' => 'zero',
- ],
- [
- 'id' => '00',
- 'name' => 'double zero',
- ],
- ], $c->uniqueStrict('id')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCollapse($collection)
- {
- $data = new $collection([[$object1 = new stdClass], [$object2 = new stdClass]]);
- $this->assertEquals([$object1, $object2], $data->collapse()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCollapseWithNestedCollections($collection)
- {
- $data = new $collection([new $collection([1, 2, 3]), new $collection([4, 5, 6])]);
- $this->assertEquals([1, 2, 3, 4, 5, 6], $data->collapse()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testJoin($collection)
- {
- $this->assertSame('a, b, c', (new $collection(['a', 'b', 'c']))->join(', '));
- $this->assertSame('a, b and c', (new $collection(['a', 'b', 'c']))->join(', ', ' and '));
- $this->assertSame('a and b', (new $collection(['a', 'b']))->join(', ', ' and '));
- $this->assertSame('a', (new $collection(['a']))->join(', ', ' and '));
- $this->assertSame('', (new $collection([]))->join(', ', ' and '));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCrossJoin($collection)
- {
- // Cross join with an array
- $this->assertEquals(
- [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
- (new $collection([1, 2]))->crossJoin(['a', 'b'])->all()
- );
- // Cross join with a collection
- $this->assertEquals(
- [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
- (new $collection([1, 2]))->crossJoin(new $collection(['a', 'b']))->all()
- );
- // Cross join with 2 collections
- $this->assertEquals(
- [
- [1, 'a', 'I'], [1, 'a', 'II'],
- [1, 'b', 'I'], [1, 'b', 'II'],
- [2, 'a', 'I'], [2, 'a', 'II'],
- [2, 'b', 'I'], [2, 'b', 'II'],
- ],
- (new $collection([1, 2]))->crossJoin(
- new $collection(['a', 'b']),
- new $collection(['I', 'II'])
- )->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSort($collection)
- {
- $data = (new $collection([5, 3, 1, 2, 4]))->sort();
- $this->assertEquals([1, 2, 3, 4, 5], $data->values()->all());
- $data = (new $collection([-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4]))->sort();
- $this->assertEquals([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], $data->values()->all());
- $data = (new $collection(['foo', 'bar-10', 'bar-1']))->sort();
- $this->assertEquals(['bar-1', 'bar-10', 'foo'], $data->values()->all());
- $data = (new $collection(['T2', 'T1', 'T10']))->sort();
- $this->assertEquals(['T1', 'T10', 'T2'], $data->values()->all());
- $data = (new $collection(['T2', 'T1', 'T10']))->sort(SORT_NATURAL);
- $this->assertEquals(['T1', 'T2', 'T10'], $data->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortDesc($collection)
- {
- $data = (new $collection([5, 3, 1, 2, 4]))->sortDesc();
- $this->assertEquals([5, 4, 3, 2, 1], $data->values()->all());
- $data = (new $collection([-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4]))->sortDesc();
- $this->assertEquals([5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5], $data->values()->all());
- $data = (new $collection(['bar-1', 'foo', 'bar-10']))->sortDesc();
- $this->assertEquals(['foo', 'bar-10', 'bar-1'], $data->values()->all());
- $data = (new $collection(['T2', 'T1', 'T10']))->sortDesc();
- $this->assertEquals(['T2', 'T10', 'T1'], $data->values()->all());
- $data = (new $collection(['T2', 'T1', 'T10']))->sortDesc(SORT_NATURAL);
- $this->assertEquals(['T10', 'T2', 'T1'], $data->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortWithCallback($collection)
- {
- $data = (new $collection([5, 3, 1, 2, 4]))->sort(function ($a, $b) {
- if ($a === $b) {
- return 0;
- }
- return ($a < $b) ? -1 : 1;
- });
- $this->assertEquals(range(1, 5), array_values($data->all()));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortBy($collection)
- {
- $data = new $collection(['taylor', 'dayle']);
- $data = $data->sortBy(function ($x) {
- return $x;
- });
- $this->assertEquals(['dayle', 'taylor'], array_values($data->all()));
- $data = new $collection(['dayle', 'taylor']);
- $data = $data->sortByDesc(function ($x) {
- return $x;
- });
- $this->assertEquals(['taylor', 'dayle'], array_values($data->all()));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortByString($collection)
- {
- $data = new $collection([['name' => 'taylor'], ['name' => 'dayle']]);
- $data = $data->sortBy('name', SORT_STRING);
- $this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
- $data = new $collection([['name' => 'taylor'], ['name' => 'dayle']]);
- $data = $data->sortBy('name', SORT_STRING);
- $this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortByCallableString($collection)
- {
- $data = new $collection([['sort' => 2], ['sort' => 1]]);
- $data = $data->sortBy([['sort', 'asc']]);
- $this->assertEquals([['sort' => 1], ['sort' => 2]], array_values($data->all()));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortByAlwaysReturnsAssoc($collection)
- {
- $data = new $collection(['a' => 'taylor', 'b' => 'dayle']);
- $data = $data->sortBy(function ($x) {
- return $x;
- });
- $this->assertEquals(['b' => 'dayle', 'a' => 'taylor'], $data->all());
- $data = new $collection(['taylor', 'dayle']);
- $data = $data->sortBy(function ($x) {
- return $x;
- });
- $this->assertEquals([1 => 'dayle', 0 => 'taylor'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortKeys($collection)
- {
- $data = new $collection(['b' => 'dayle', 'a' => 'taylor']);
- $this->assertSame(['a' => 'taylor', 'b' => 'dayle'], $data->sortKeys()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortKeysDesc($collection)
- {
- $data = new $collection(['a' => 'taylor', 'b' => 'dayle']);
- $this->assertSame(['b' => 'dayle', 'a' => 'taylor'], $data->sortKeysDesc()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSortKeysUsing($collection)
- {
- $data = new $collection(['B' => 'dayle', 'a' => 'taylor']);
- $this->assertSame(['a' => 'taylor', 'B' => 'dayle'], $data->sortKeysUsing('strnatcasecmp')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReverse($collection)
- {
- $data = new $collection(['zaeed', 'alan']);
- $reversed = $data->reverse();
- $this->assertSame([1 => 'alan', 0 => 'zaeed'], $reversed->all());
- $data = new $collection(['name' => 'taylor', 'framework' => 'laravel']);
- $reversed = $data->reverse();
- $this->assertSame(['framework' => 'laravel', 'name' => 'taylor'], $reversed->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFlip($collection)
- {
- $data = new $collection(['name' => 'taylor', 'framework' => 'laravel']);
- $this->assertEquals(['taylor' => 'name', 'laravel' => 'framework'], $data->flip()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testChunk($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
- $data = $data->chunk(3);
- $this->assertInstanceOf($collection, $data);
- $this->assertInstanceOf($collection, $data->first());
- $this->assertCount(4, $data);
- $this->assertEquals([1, 2, 3], $data->first()->toArray());
- $this->assertEquals([9 => 10], $data->get(3)->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testChunkWhenGivenZeroAsSize($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
- $this->assertEquals(
- [],
- $data->chunk(0)->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testChunkWhenGivenLessThanZero($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
- $this->assertEquals(
- [],
- $data->chunk(-1)->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitIn($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
- $data = $data->splitIn(3);
- $this->assertInstanceOf($collection, $data);
- $this->assertInstanceOf($collection, $data->first());
- $this->assertCount(3, $data);
- $this->assertEquals([1, 2, 3, 4], $data->get(0)->values()->toArray());
- $this->assertEquals([5, 6, 7, 8], $data->get(1)->values()->toArray());
- $this->assertEquals([9, 10], $data->get(2)->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testChunkWhileOnEqualElements($collection)
- {
- $data = (new $collection(['A', 'A', 'B', 'B', 'C', 'C', 'C']))
- ->chunkWhile(function ($current, $key, $chunk) {
- return $chunk->last() === $current;
- });
- $this->assertInstanceOf($collection, $data);
- $this->assertInstanceOf($collection, $data->first());
- $this->assertEquals([0 => 'A', 1 => 'A'], $data->first()->toArray());
- $this->assertEquals([2 => 'B', 3 => 'B'], $data->get(1)->toArray());
- $this->assertEquals([4 => 'C', 5 => 'C', 6 => 'C'], $data->last()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testChunkWhileOnContiguouslyIncreasingIntegers($collection)
- {
- $data = (new $collection([1, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]))
- ->chunkWhile(function ($current, $key, $chunk) {
- return $chunk->last() + 1 == $current;
- });
- $this->assertInstanceOf($collection, $data);
- $this->assertInstanceOf($collection, $data->first());
- $this->assertEquals([0 => 1], $data->first()->toArray());
- $this->assertEquals([1 => 4], $data->get(1)->toArray());
- $this->assertEquals([2 => 9, 3 => 10, 4 => 11, 5 => 12], $data->get(2)->toArray());
- $this->assertEquals([6 => 15, 7 => 16], $data->get(3)->toArray());
- $this->assertEquals([8 => 19, 9 => 20, 10 => 21], $data->last()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testEvery($collection)
- {
- $c = new $collection([]);
- $this->assertTrue($c->every('key', 'value'));
- $this->assertTrue($c->every(function () {
- return false;
- }));
- $c = new $collection([['age' => 18], ['age' => 20], ['age' => 20]]);
- $this->assertFalse($c->every('age', 18));
- $this->assertTrue($c->every('age', '>=', 18));
- $this->assertTrue($c->every(function ($item) {
- return $item['age'] >= 18;
- }));
- $this->assertFalse($c->every(function ($item) {
- return $item['age'] >= 20;
- }));
- $c = new $collection([null, null]);
- $this->assertTrue($c->every(function ($item) {
- return $item === null;
- }));
- $c = new $collection([['active' => true], ['active' => true]]);
- $this->assertTrue($c->every('active'));
- $this->assertTrue($c->every->active);
- $this->assertFalse($c->concat([['active' => false]])->every->active);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testExcept($collection)
- {
- $data = new $collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']);
- $this->assertEquals(['first' => 'Taylor'], $data->except(['last', 'email', 'missing'])->all());
- $this->assertEquals(['first' => 'Taylor'], $data->except('last', 'email', 'missing')->all());
- $this->assertEquals(['first' => 'Taylor'], $data->except(collect(['last', 'email', 'missing']))->all());
- $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(['last'])->all());
- $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except('last')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testExceptSelf($collection)
- {
- $data = new $collection(['first' => 'Taylor', 'last' => 'Otwell']);
- $this->assertEquals(['first' => 'Taylor', 'last' => 'Otwell'], $data->except($data)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPluckWithArrayAndObjectValues($collection)
- {
- $data = new $collection([(object) ['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]);
- $this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], $data->pluck('email', 'name')->all());
- $this->assertEquals(['foo', 'bar'], $data->pluck('email')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPluckWithArrayAccessValues($collection)
- {
- $data = new $collection([
- new TestArrayAccessImplementation(['name' => 'taylor', 'email' => 'foo']),
- new TestArrayAccessImplementation(['name' => 'dayle', 'email' => 'bar']),
- ]);
- $this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], $data->pluck('email', 'name')->all());
- $this->assertEquals(['foo', 'bar'], $data->pluck('email')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHas($collection)
- {
- $data = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
- $this->assertTrue($data->has('first'));
- $this->assertFalse($data->has('third'));
- $this->assertTrue($data->has(['first', 'second']));
- $this->assertFalse($data->has(['third', 'first']));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHasAny($collection)
- {
- $data = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
- $this->assertTrue($data->hasAny('first'));
- $this->assertFalse($data->hasAny('third'));
- $this->assertTrue($data->hasAny(['first', 'second']));
- $this->assertTrue($data->hasAny(['first', 'fourth']));
- $this->assertFalse($data->hasAny(['third', 'fourth']));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testImplode($collection)
- {
- $data = new $collection([['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]);
- $this->assertSame('foobar', $data->implode('email'));
- $this->assertSame('foo,bar', $data->implode('email', ','));
- $data = new $collection(['taylor', 'dayle']);
- $this->assertSame('taylordayle', $data->implode(''));
- $this->assertSame('taylor,dayle', $data->implode(','));
- $data = new $collection([
- ['name' => Str::of('taylor'), 'email' => Str::of('foo')],
- ['name' => Str::of('dayle'), 'email' => Str::of('bar')],
- ]);
- $this->assertSame('foobar', $data->implode('email'));
- $this->assertSame('foo,bar', $data->implode('email', ','));
- $data = new $collection([Str::of('taylor'), Str::of('dayle')]);
- $this->assertSame('taylordayle', $data->implode(''));
- $this->assertSame('taylor,dayle', $data->implode(','));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTake($collection)
- {
- $data = new $collection(['taylor', 'dayle', 'shawn']);
- $data = $data->take(2);
- $this->assertEquals(['taylor', 'dayle'], $data->all());
- }
- public function testGetOrPut()
- {
- $data = new Collection(['name' => 'taylor', 'email' => 'foo']);
- $this->assertEquals('taylor', $data->getOrPut('name', null));
- $this->assertEquals('foo', $data->getOrPut('email', null));
- $this->assertEquals('male', $data->getOrPut('gender', 'male'));
- $this->assertEquals('taylor', $data->get('name'));
- $this->assertEquals('foo', $data->get('email'));
- $this->assertEquals('male', $data->get('gender'));
- $data = new Collection(['name' => 'taylor', 'email' => 'foo']);
- $this->assertEquals('taylor', $data->getOrPut('name', function () {
- return null;
- }));
- $this->assertEquals('foo', $data->getOrPut('email', function () {
- return null;
- }));
- $this->assertEquals('male', $data->getOrPut('gender', function () {
- return 'male';
- }));
- $this->assertEquals('taylor', $data->get('name'));
- $this->assertEquals('foo', $data->get('email'));
- $this->assertEquals('male', $data->get('gender'));
- }
- public function testPut()
- {
- $data = new Collection(['name' => 'taylor', 'email' => 'foo']);
- $data = $data->put('name', 'dayle');
- $this->assertEquals(['name' => 'dayle', 'email' => 'foo'], $data->all());
- }
- public function testPutWithNoKey()
- {
- $data = new Collection(['taylor', 'shawn']);
- $data = $data->put(null, 'dayle');
- $this->assertEquals(['taylor', 'shawn', 'dayle'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testRandom($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6]);
- $random = $data->random();
- $this->assertIsInt($random);
- $this->assertContains($random, $data->all());
- $random = $data->random(0);
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(0, $random);
- $random = $data->random(1);
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(1, $random);
- $random = $data->random(2);
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(2, $random);
- $random = $data->random('0');
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(0, $random);
- $random = $data->random('1');
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(1, $random);
- $random = $data->random('2');
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(2, $random);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testRandomOnEmptyCollection($collection)
- {
- $data = new $collection;
- $random = $data->random(0);
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(0, $random);
- $random = $data->random('0');
- $this->assertInstanceOf($collection, $random);
- $this->assertCount(0, $random);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeLast($collection)
- {
- $data = new $collection(['taylor', 'dayle', 'shawn']);
- $data = $data->take(-2);
- $this->assertEquals([1 => 'dayle', 2 => 'shawn'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeUntilUsingValue($collection)
- {
- $data = new $collection([1, 2, 3, 4]);
- $data = $data->takeUntil(3);
- $this->assertSame([1, 2], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeUntilUsingCallback($collection)
- {
- $data = new $collection([1, 2, 3, 4]);
- $data = $data->takeUntil(function ($item) {
- return $item >= 3;
- });
- $this->assertSame([1, 2], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeUntilReturnsAllItemsForUnmetValue($collection)
- {
- $data = new $collection([1, 2, 3, 4]);
- $actual = $data->takeUntil(99);
- $this->assertSame($data->toArray(), $actual->toArray());
- $actual = $data->takeUntil(function ($item) {
- return $item >= 99;
- });
- $this->assertSame($data->toArray(), $actual->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeUntilCanBeProxied($collection)
- {
- $data = new $collection([
- new TestSupportCollectionHigherOrderItem('Adam'),
- new TestSupportCollectionHigherOrderItem('Taylor'),
- new TestSupportCollectionHigherOrderItem('Jason'),
- ]);
- $actual = $data->takeUntil->is('Jason');
- $this->assertCount(2, $actual);
- $this->assertSame('Adam', $actual->get(0)->name);
- $this->assertSame('Taylor', $actual->get(1)->name);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeWhileUsingValue($collection)
- {
- $data = new $collection([1, 1, 2, 2, 3, 3]);
- $data = $data->takeWhile(1);
- $this->assertSame([1, 1], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeWhileUsingCallback($collection)
- {
- $data = new $collection([1, 2, 3, 4]);
- $data = $data->takeWhile(function ($item) {
- return $item < 3;
- });
- $this->assertSame([1, 2], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeWhileReturnsNoItemsForUnmetValue($collection)
- {
- $data = new $collection([1, 2, 3, 4]);
- $actual = $data->takeWhile(2);
- $this->assertSame([], $actual->toArray());
- $actual = $data->takeWhile(function ($item) {
- return $item == 99;
- });
- $this->assertSame([], $actual->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTakeWhileCanBeProxied($collection)
- {
- $data = new $collection([
- new TestSupportCollectionHigherOrderItem('Adam'),
- new TestSupportCollectionHigherOrderItem('Adam'),
- new TestSupportCollectionHigherOrderItem('Taylor'),
- new TestSupportCollectionHigherOrderItem('Taylor'),
- ]);
- $actual = $data->takeWhile->is('Adam');
- $this->assertCount(2, $actual);
- $this->assertSame('Adam', $actual->get(0)->name);
- $this->assertSame('Adam', $actual->get(1)->name);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMacroable($collection)
- {
- // Foo() macro : unique values starting with A
- $collection::macro('foo', function () {
- return $this->filter(function ($item) {
- return strpos($item, 'a') === 0;
- })
- ->unique()
- ->values();
- });
- $c = new $collection(['a', 'a', 'aa', 'aaa', 'bar']);
- $this->assertSame(['a', 'aa', 'aaa'], $c->foo()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCanAddMethodsToProxy($collection)
- {
- $collection::macro('adults', function ($callback) {
- return $this->filter(function ($item) use ($callback) {
- return $callback($item) >= 18;
- });
- });
- $collection::proxy('adults');
- $c = new $collection([['age' => 3], ['age' => 12], ['age' => 18], ['age' => 56]]);
- $this->assertSame([['age' => 18], ['age' => 56]], $c->adults->age->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMakeMethod($collection)
- {
- $data = $collection::make('foo');
- $this->assertEquals(['foo'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMakeMethodFromNull($collection)
- {
- $data = $collection::make(null);
- $this->assertEquals([], $data->all());
- $data = $collection::make();
- $this->assertEquals([], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMakeMethodFromCollection($collection)
- {
- $firstCollection = $collection::make(['foo' => 'bar']);
- $secondCollection = $collection::make($firstCollection);
- $this->assertEquals(['foo' => 'bar'], $secondCollection->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMakeMethodFromArray($collection)
- {
- $data = $collection::make(['foo' => 'bar']);
- $this->assertEquals(['foo' => 'bar'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWrapWithScalar($collection)
- {
- $data = $collection::wrap('foo');
- $this->assertEquals(['foo'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWrapWithArray($collection)
- {
- $data = $collection::wrap(['foo']);
- $this->assertEquals(['foo'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWrapWithArrayable($collection)
- {
- $data = $collection::wrap($o = new TestArrayableObject);
- $this->assertEquals([$o], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWrapWithJsonable($collection)
- {
- $data = $collection::wrap($o = new TestJsonableObject);
- $this->assertEquals([$o], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWrapWithJsonSerialize($collection)
- {
- $data = $collection::wrap($o = new TestJsonSerializeObject);
- $this->assertEquals([$o], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWrapWithCollectionClass($collection)
- {
- $data = $collection::wrap($collection::make(['foo']));
- $this->assertEquals(['foo'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWrapWithCollectionSubclass($collection)
- {
- $data = TestCollectionSubclass::wrap($collection::make(['foo']));
- $this->assertEquals(['foo'], $data->all());
- $this->assertInstanceOf(TestCollectionSubclass::class, $data);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnwrapCollection($collection)
- {
- $data = new $collection(['foo']);
- $this->assertEquals(['foo'], $collection::unwrap($data));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnwrapCollectionWithArray($collection)
- {
- $this->assertEquals(['foo'], $collection::unwrap(['foo']));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnwrapCollectionWithScalar($collection)
- {
- $this->assertSame('foo', $collection::unwrap('foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testEmptyMethod($collection)
- {
- $collection = $collection::empty();
- $this->assertCount(0, $collection->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTimesMethod($collection)
- {
- $two = $collection::times(2, function ($number) {
- return 'slug-'.$number;
- });
- $zero = $collection::times(0, function ($number) {
- return 'slug-'.$number;
- });
- $negative = $collection::times(-4, function ($number) {
- return 'slug-'.$number;
- });
- $range = $collection::times(5);
- $this->assertEquals(['slug-1', 'slug-2'], $two->all());
- $this->assertTrue($zero->isEmpty());
- $this->assertTrue($negative->isEmpty());
- $this->assertEquals(range(1, 5), $range->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testRangeMethod($collection)
- {
- $this->assertSame(
- [1, 2, 3, 4, 5],
- $collection::range(1, 5)->all()
- );
- $this->assertSame(
- [-2, -1, 0, 1, 2],
- $collection::range(-2, 2)->all()
- );
- $this->assertSame(
- [-4, -3, -2],
- $collection::range(-4, -2)->all()
- );
- $this->assertSame(
- [5, 4, 3, 2, 1],
- $collection::range(5, 1)->all()
- );
- $this->assertSame(
- [2, 1, 0, -1, -2],
- $collection::range(2, -2)->all()
- );
- $this->assertSame(
- [-2, -3, -4],
- $collection::range(-2, -4)->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConstructMakeFromObject($collection)
- {
- $object = new stdClass;
- $object->foo = 'bar';
- $data = $collection::make($object);
- $this->assertEquals(['foo' => 'bar'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConstructMethod($collection)
- {
- $data = new $collection('foo');
- $this->assertEquals(['foo'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConstructMethodFromNull($collection)
- {
- $data = new $collection(null);
- $this->assertEquals([], $data->all());
- $data = new $collection;
- $this->assertEquals([], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConstructMethodFromCollection($collection)
- {
- $firstCollection = new $collection(['foo' => 'bar']);
- $secondCollection = new $collection($firstCollection);
- $this->assertEquals(['foo' => 'bar'], $secondCollection->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConstructMethodFromArray($collection)
- {
- $data = new $collection(['foo' => 'bar']);
- $this->assertEquals(['foo' => 'bar'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConstructMethodFromObject($collection)
- {
- $object = new stdClass;
- $object->foo = 'bar';
- $data = new $collection($object);
- $this->assertEquals(['foo' => 'bar'], $data->all());
- }
- public function testSplice()
- {
- $data = new Collection(['foo', 'baz']);
- $data->splice(1);
- $this->assertEquals(['foo'], $data->all());
- $data = new Collection(['foo', 'baz']);
- $data->splice(1, 0, 'bar');
- $this->assertEquals(['foo', 'bar', 'baz'], $data->all());
- $data = new Collection(['foo', 'baz']);
- $data->splice(1, 1);
- $this->assertEquals(['foo'], $data->all());
- $data = new Collection(['foo', 'baz']);
- $cut = $data->splice(1, 1, 'bar');
- $this->assertEquals(['foo', 'bar'], $data->all());
- $this->assertEquals(['baz'], $cut->all());
- $data = new Collection(['foo', 'baz']);
- $data->splice(1, 0, ['bar']);
- $this->assertEquals(['foo', 'bar', 'baz'], $data->all());
- $data = new Collection(['foo', 'baz']);
- $data->splice(1, 0, new Collection(['bar']));
- $this->assertEquals(['foo', 'bar', 'baz'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGetPluckValueWithAccessors($collection)
- {
- $model = new TestAccessorEloquentTestStub(['some' => 'foo']);
- $modelTwo = new TestAccessorEloquentTestStub(['some' => 'bar']);
- $data = new $collection([$model, $modelTwo]);
- $this->assertEquals(['foo', 'bar'], $data->pluck('some')->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMap($collection)
- {
- $data = new $collection(['first' => 'taylor', 'last' => 'otwell']);
- $data = $data->map(function ($item, $key) {
- return $key.'-'.strrev($item);
- });
- $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapSpread($collection)
- {
- $c = new $collection([[1, 'a'], [2, 'b']]);
- $result = $c->mapSpread(function ($number, $character) {
- return "{$number}-{$character}";
- });
- $this->assertEquals(['1-a', '2-b'], $result->all());
- $result = $c->mapSpread(function ($number, $character, $key) {
- return "{$number}-{$character}-{$key}";
- });
- $this->assertEquals(['1-a-0', '2-b-1'], $result->all());
- $c = new $collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
- $result = $c->mapSpread(function ($number, $character, $key) {
- return "{$number}-{$character}-{$key}";
- });
- $this->assertEquals(['1-a-0', '2-b-1'], $result->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testFlatMap($collection)
- {
- $data = new $collection([
- ['name' => 'taylor', 'hobbies' => ['programming', 'basketball']],
- ['name' => 'adam', 'hobbies' => ['music', 'powerlifting']],
- ]);
- $data = $data->flatMap(function ($person) {
- return $person['hobbies'];
- });
- $this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapToDictionary($collection)
- {
- $data = new $collection([
- ['id' => 1, 'name' => 'A'],
- ['id' => 2, 'name' => 'B'],
- ['id' => 3, 'name' => 'C'],
- ['id' => 4, 'name' => 'B'],
- ]);
- $groups = $data->mapToDictionary(function ($item, $key) {
- return [$item['name'] => $item['id']];
- });
- $this->assertInstanceOf($collection, $groups);
- $this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
- $this->assertIsArray($groups->get('A'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapToDictionaryWithNumericKeys($collection)
- {
- $data = new $collection([1, 2, 3, 2, 1]);
- $groups = $data->mapToDictionary(function ($item, $key) {
- return [$item => $key];
- });
- $this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapToGroups($collection)
- {
- $data = new $collection([
- ['id' => 1, 'name' => 'A'],
- ['id' => 2, 'name' => 'B'],
- ['id' => 3, 'name' => 'C'],
- ['id' => 4, 'name' => 'B'],
- ]);
- $groups = $data->mapToGroups(function ($item, $key) {
- return [$item['name'] => $item['id']];
- });
- $this->assertInstanceOf($collection, $groups);
- $this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
- $this->assertInstanceOf($collection, $groups->get('A'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapToGroupsWithNumericKeys($collection)
- {
- $data = new $collection([1, 2, 3, 2, 1]);
- $groups = $data->mapToGroups(function ($item, $key) {
- return [$item => $key];
- });
- $this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapWithKeys($collection)
- {
- $data = new $collection([
- ['name' => 'Blastoise', 'type' => 'Water', 'idx' => 9],
- ['name' => 'Charmander', 'type' => 'Fire', 'idx' => 4],
- ['name' => 'Dragonair', 'type' => 'Dragon', 'idx' => 148],
- ]);
- $data = $data->mapWithKeys(function ($pokemon) {
- return [$pokemon['name'] => $pokemon['type']];
- });
- $this->assertEquals(
- ['Blastoise' => 'Water', 'Charmander' => 'Fire', 'Dragonair' => 'Dragon'],
- $data->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapWithKeysIntegerKeys($collection)
- {
- $data = new $collection([
- ['id' => 1, 'name' => 'A'],
- ['id' => 3, 'name' => 'B'],
- ['id' => 2, 'name' => 'C'],
- ]);
- $data = $data->mapWithKeys(function ($item) {
- return [$item['id'] => $item];
- });
- $this->assertSame(
- [1, 3, 2],
- $data->keys()->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapWithKeysMultipleRows($collection)
- {
- $data = new $collection([
- ['id' => 1, 'name' => 'A'],
- ['id' => 2, 'name' => 'B'],
- ['id' => 3, 'name' => 'C'],
- ]);
- $data = $data->mapWithKeys(function ($item) {
- return [$item['id'] => $item['name'], $item['name'] => $item['id']];
- });
- $this->assertSame(
- [
- 1 => 'A',
- 'A' => 1,
- 2 => 'B',
- 'B' => 2,
- 3 => 'C',
- 'C' => 3,
- ],
- $data->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapWithKeysCallbackKey($collection)
- {
- $data = new $collection([
- 3 => ['id' => 1, 'name' => 'A'],
- 5 => ['id' => 3, 'name' => 'B'],
- 4 => ['id' => 2, 'name' => 'C'],
- ]);
- $data = $data->mapWithKeys(function ($item, $key) {
- return [$key => $item['id']];
- });
- $this->assertSame(
- [3, 5, 4],
- $data->keys()->all()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapInto($collection)
- {
- $data = new $collection([
- 'first', 'second',
- ]);
- $data = $data->mapInto(TestCollectionMapIntoObject::class);
- $this->assertSame('first', $data->get(0)->value);
- $this->assertSame('second', $data->get(1)->value);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testNth($collection)
- {
- $data = new $collection([
- 6 => 'a',
- 4 => 'b',
- 7 => 'c',
- 1 => 'd',
- 5 => 'e',
- 3 => 'f',
- ]);
- $this->assertEquals(['a', 'e'], $data->nth(4)->all());
- $this->assertEquals(['b', 'f'], $data->nth(4, 1)->all());
- $this->assertEquals(['c'], $data->nth(4, 2)->all());
- $this->assertEquals(['d'], $data->nth(4, 3)->all());
- $this->assertEquals(['c', 'e'], $data->nth(2, 2)->all());
- $this->assertEquals(['c', 'd', 'e', 'f'], $data->nth(1, 2)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMapWithKeysOverwritingKeys($collection)
- {
- $data = new $collection([
- ['id' => 1, 'name' => 'A'],
- ['id' => 2, 'name' => 'B'],
- ['id' => 1, 'name' => 'C'],
- ]);
- $data = $data->mapWithKeys(function ($item) {
- return [$item['id'] => $item['name']];
- });
- $this->assertSame(
- [
- 1 => 'C',
- 2 => 'B',
- ],
- $data->all()
- );
- }
- public function testTransform()
- {
- $data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
- $data->transform(function ($item, $key) {
- return $key.'-'.strrev($item);
- });
- $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByAttribute($collection)
- {
- $data = new $collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
- $result = $data->groupBy('rating');
- $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
- $result = $data->groupBy('url');
- $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByCallable($collection)
- {
- $data = new $collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
- $result = $data->groupBy([$this, 'sortByRating']);
- $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
- $result = $data->groupBy([$this, 'sortByUrl']);
- $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
- }
- public function sortByRating(array $value)
- {
- return $value['rating'];
- }
- public function sortByUrl(array $value)
- {
- return $value['url'];
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByAttributePreservingKeys($collection)
- {
- $data = new $collection([10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1'], 30 => ['rating' => 2, 'url' => '2']]);
- $result = $data->groupBy('rating', true);
- $expected_result = [
- 1 => [10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1']],
- 2 => [30 => ['rating' => 2, 'url' => '2']],
- ];
- $this->assertEquals($expected_result, $result->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByClosureWhereItemsHaveSingleGroup($collection)
- {
- $data = new $collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
- $result = $data->groupBy(function ($item) {
- return $item['rating'];
- });
- $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByClosureWhereItemsHaveSingleGroupPreservingKeys($collection)
- {
- $data = new $collection([10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1'], 30 => ['rating' => 2, 'url' => '2']]);
- $result = $data->groupBy(function ($item) {
- return $item['rating'];
- }, true);
- $expected_result = [
- 1 => [10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1']],
- 2 => [30 => ['rating' => 2, 'url' => '2']],
- ];
- $this->assertEquals($expected_result, $result->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByClosureWhereItemsHaveMultipleGroups($collection)
- {
- $data = new $collection([
- ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
- ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
- ['user' => 3, 'roles' => ['Role_1']],
- ]);
- $result = $data->groupBy(function ($item) {
- return $item['roles'];
- });
- $expected_result = [
- 'Role_1' => [
- ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
- ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
- ['user' => 3, 'roles' => ['Role_1']],
- ],
- 'Role_2' => [
- ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
- ],
- 'Role_3' => [
- ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
- ],
- ];
- $this->assertEquals($expected_result, $result->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByClosureWhereItemsHaveMultipleGroupsPreservingKeys($collection)
- {
- $data = new $collection([
- 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
- 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
- 30 => ['user' => 3, 'roles' => ['Role_1']],
- ]);
- $result = $data->groupBy(function ($item) {
- return $item['roles'];
- }, true);
- $expected_result = [
- 'Role_1' => [
- 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
- 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
- 30 => ['user' => 3, 'roles' => ['Role_1']],
- ],
- 'Role_2' => [
- 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
- ],
- 'Role_3' => [
- 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
- ],
- ];
- $this->assertEquals($expected_result, $result->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGroupByMultiLevelAndClosurePreservingKeys($collection)
- {
- $data = new $collection([
- 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
- 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
- 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']],
- 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']],
- ]);
- $result = $data->groupBy([
- 'skilllevel',
- function ($item) {
- return $item['roles'];
- },
- ], true);
- $expected_result = [
- 1 => [
- 'Role_1' => [
- 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
- 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
- ],
- 'Role_3' => [
- 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
- ],
- 'Role_2' => [
- 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
- ],
- ],
- 2 => [
- 'Role_1' => [
- 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']],
- ],
- 'Role_2' => [
- 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']],
- ],
- ],
- ];
- $this->assertEquals($expected_result, $result->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testKeyByAttribute($collection)
- {
- $data = new $collection([['rating' => 1, 'name' => '1'], ['rating' => 2, 'name' => '2'], ['rating' => 3, 'name' => '3']]);
- $result = $data->keyBy('rating');
- $this->assertEquals([1 => ['rating' => 1, 'name' => '1'], 2 => ['rating' => 2, 'name' => '2'], 3 => ['rating' => 3, 'name' => '3']], $result->all());
- $result = $data->keyBy(function ($item) {
- return $item['rating'] * 2;
- });
- $this->assertEquals([2 => ['rating' => 1, 'name' => '1'], 4 => ['rating' => 2, 'name' => '2'], 6 => ['rating' => 3, 'name' => '3']], $result->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testKeyByClosure($collection)
- {
- $data = new $collection([
- ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
- ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
- ]);
- $result = $data->keyBy(function ($item, $key) {
- return strtolower($key.'-'.$item['firstname'].$item['lastname']);
- });
- $this->assertEquals([
- '0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
- '1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
- ], $result->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testKeyByObject($collection)
- {
- $data = new $collection([
- ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
- ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
- ]);
- $result = $data->keyBy(function ($item, $key) use ($collection) {
- return new $collection([$key, $item['firstname'], $item['lastname']]);
- });
- $this->assertEquals([
- '[0,"Taylor","Otwell"]' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
- '[1,"Lucas","Michot"]' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
- ], $result->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testContains($collection)
- {
- $c = new $collection([1, 3, 5]);
- $this->assertTrue($c->contains(1));
- $this->assertTrue($c->contains('1'));
- $this->assertFalse($c->contains(2));
- $this->assertFalse($c->contains('2'));
- $c = new $collection(['1']);
- $this->assertTrue($c->contains('1'));
- $this->assertTrue($c->contains(1));
- $c = new $collection([null]);
- $this->assertTrue($c->contains(false));
- $this->assertTrue($c->contains(null));
- $this->assertTrue($c->contains([]));
- $this->assertTrue($c->contains(0));
- $this->assertTrue($c->contains(''));
- $c = new $collection([0]);
- $this->assertTrue($c->contains(0));
- $this->assertTrue($c->contains('0'));
- $this->assertTrue($c->contains(false));
- $this->assertTrue($c->contains(null));
- $this->assertTrue($c->contains(function ($value) {
- return $value < 5;
- }));
- $this->assertFalse($c->contains(function ($value) {
- return $value > 5;
- }));
- $c = new $collection([['v' => 1], ['v' => 3], ['v' => 5]]);
- $this->assertTrue($c->contains('v', 1));
- $this->assertFalse($c->contains('v', 2));
- $c = new $collection(['date', 'class', (object) ['foo' => 50]]);
- $this->assertTrue($c->contains('date'));
- $this->assertTrue($c->contains('class'));
- $this->assertFalse($c->contains('foo'));
- $c = new $collection([['a' => false, 'b' => false], ['a' => true, 'b' => false]]);
- $this->assertTrue($c->contains->a);
- $this->assertFalse($c->contains->b);
- $c = new $collection([
- null, 1, 2,
- ]);
- $this->assertTrue($c->contains(function ($value) {
- return is_null($value);
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSome($collection)
- {
- $c = new $collection([1, 3, 5]);
- $this->assertTrue($c->some(1));
- $this->assertFalse($c->some(2));
- $this->assertTrue($c->some(function ($value) {
- return $value < 5;
- }));
- $this->assertFalse($c->some(function ($value) {
- return $value > 5;
- }));
- $c = new $collection([['v' => 1], ['v' => 3], ['v' => 5]]);
- $this->assertTrue($c->some('v', 1));
- $this->assertFalse($c->some('v', 2));
- $c = new $collection(['date', 'class', (object) ['foo' => 50]]);
- $this->assertTrue($c->some('date'));
- $this->assertTrue($c->some('class'));
- $this->assertFalse($c->some('foo'));
- $c = new $collection([['a' => false, 'b' => false], ['a' => true, 'b' => false]]);
- $this->assertTrue($c->some->a);
- $this->assertFalse($c->some->b);
- $c = new $collection([
- null, 1, 2,
- ]);
- $this->assertTrue($c->some(function ($value) {
- return is_null($value);
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testContainsStrict($collection)
- {
- $c = new $collection([1, 3, 5, '02']);
- $this->assertTrue($c->containsStrict(1));
- $this->assertFalse($c->containsStrict('1'));
- $this->assertFalse($c->containsStrict(2));
- $this->assertTrue($c->containsStrict('02'));
- $this->assertFalse($c->containsStrict(true));
- $this->assertTrue($c->containsStrict(function ($value) {
- return $value < 5;
- }));
- $this->assertFalse($c->containsStrict(function ($value) {
- return $value > 5;
- }));
- $c = new $collection([0]);
- $this->assertTrue($c->containsStrict(0));
- $this->assertFalse($c->containsStrict('0'));
- $this->assertFalse($c->containsStrict(false));
- $this->assertFalse($c->containsStrict(null));
- $c = new $collection([1, null]);
- $this->assertTrue($c->containsStrict(null));
- $this->assertFalse($c->containsStrict(0));
- $this->assertFalse($c->containsStrict(false));
- $c = new $collection([['v' => 1], ['v' => 3], ['v' => '04'], ['v' => 5]]);
- $this->assertTrue($c->containsStrict('v', 1));
- $this->assertFalse($c->containsStrict('v', 2));
- $this->assertFalse($c->containsStrict('v', '1'));
- $this->assertFalse($c->containsStrict('v', 4));
- $this->assertTrue($c->containsStrict('v', '04'));
- $c = new $collection(['date', 'class', (object) ['foo' => 50], '']);
- $this->assertTrue($c->containsStrict('date'));
- $this->assertTrue($c->containsStrict('class'));
- $this->assertFalse($c->containsStrict('foo'));
- $this->assertFalse($c->containsStrict(null));
- $this->assertTrue($c->containsStrict(''));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testContainsWithOperator($collection)
- {
- $c = new $collection([['v' => 1], ['v' => 3], ['v' => '4'], ['v' => 5]]);
- $this->assertTrue($c->contains('v', '=', 4));
- $this->assertTrue($c->contains('v', '==', 4));
- $this->assertFalse($c->contains('v', '===', 4));
- $this->assertTrue($c->contains('v', '>', 4));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGettingSumFromCollection($collection)
- {
- $c = new $collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
- $this->assertEquals(100, $c->sum('foo'));
- $c = new $collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
- $this->assertEquals(100, $c->sum(function ($i) {
- return $i->foo;
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCanSumValuesWithoutACallback($collection)
- {
- $c = new $collection([1, 2, 3, 4, 5]);
- $this->assertEquals(15, $c->sum());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGettingSumFromEmptyCollection($collection)
- {
- $c = new $collection;
- $this->assertEquals(0, $c->sum('foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testValueRetrieverAcceptsDotNotation($collection)
- {
- $c = new $collection([
- (object) ['id' => 1, 'foo' => ['bar' => 'B']], (object) ['id' => 2, 'foo' => ['bar' => 'A']],
- ]);
- $c = $c->sortBy('foo.bar');
- $this->assertEquals([2, 1], $c->pluck('id')->all());
- }
- public function testPullRetrievesItemFromCollection()
- {
- $c = new Collection(['foo', 'bar']);
- $this->assertSame('foo', $c->pull(0));
- }
- public function testPullRemovesItemFromCollection()
- {
- $c = new Collection(['foo', 'bar']);
- $c->pull(0);
- $this->assertEquals([1 => 'bar'], $c->all());
- }
- public function testPullReturnsDefault()
- {
- $c = new Collection([]);
- $value = $c->pull(0, 'foo');
- $this->assertSame('foo', $value);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testRejectRemovesElementsPassingTruthTest($collection)
- {
- $c = new $collection(['foo', 'bar']);
- $this->assertEquals(['foo'], $c->reject('bar')->values()->all());
- $c = new $collection(['foo', 'bar']);
- $this->assertEquals(['foo'], $c->reject(function ($v) {
- return $v === 'bar';
- })->values()->all());
- $c = new $collection(['foo', null]);
- $this->assertEquals(['foo'], $c->reject(null)->values()->all());
- $c = new $collection(['foo', 'bar']);
- $this->assertEquals(['foo', 'bar'], $c->reject('baz')->values()->all());
- $c = new $collection(['foo', 'bar']);
- $this->assertEquals(['foo', 'bar'], $c->reject(function ($v) {
- return $v === 'baz';
- })->values()->all());
- $c = new $collection(['id' => 1, 'primary' => 'foo', 'secondary' => 'bar']);
- $this->assertEquals(['primary' => 'foo', 'secondary' => 'bar'], $c->reject(function ($item, $key) {
- return $key === 'id';
- })->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testRejectWithoutAnArgumentRemovesTruthyValues($collection)
- {
- $data1 = new $collection([
- false,
- true,
- new $collection(),
- 0,
- ]);
- $this->assertSame([0 => false, 3 => 0], $data1->reject()->all());
- $data2 = new $collection([
- 'a' => true,
- 'b' => true,
- 'c' => true,
- ]);
- $this->assertTrue(
- $data2->reject()->isEmpty()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSearchReturnsIndexOfFirstFoundItem($collection)
- {
- $c = new $collection([1, 2, 3, 4, 5, 2, 5, 'foo' => 'bar']);
- $this->assertEquals(1, $c->search(2));
- $this->assertEquals(1, $c->search('2'));
- $this->assertSame('foo', $c->search('bar'));
- $this->assertEquals(4, $c->search(function ($value) {
- return $value > 4;
- }));
- $this->assertSame('foo', $c->search(function ($value) {
- return ! is_numeric($value);
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSearchInStrictMode($collection)
- {
- $c = new $collection([false, 0, 1, [], '']);
- $this->assertFalse($c->search('false', true));
- $this->assertFalse($c->search('1', true));
- $this->assertEquals(0, $c->search(false, true));
- $this->assertEquals(1, $c->search(0, true));
- $this->assertEquals(2, $c->search(1, true));
- $this->assertEquals(3, $c->search([], true));
- $this->assertEquals(4, $c->search('', true));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSearchReturnsFalseWhenItemIsNotFound($collection)
- {
- $c = new $collection([1, 2, 3, 4, 5, 'foo' => 'bar']);
- $this->assertFalse($c->search(6));
- $this->assertFalse($c->search('foo'));
- $this->assertFalse($c->search(function ($value) {
- return $value < 1 && is_numeric($value);
- }));
- $this->assertFalse($c->search(function ($value) {
- return $value === 'nope';
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testKeys($collection)
- {
- $c = new $collection(['name' => 'taylor', 'framework' => 'laravel']);
- $this->assertEquals(['name', 'framework'], $c->keys()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPaginate($collection)
- {
- $c = new $collection(['one', 'two', 'three', 'four']);
- $this->assertEquals(['one', 'two'], $c->forPage(0, 2)->all());
- $this->assertEquals(['one', 'two'], $c->forPage(1, 2)->all());
- $this->assertEquals([2 => 'three', 3 => 'four'], $c->forPage(2, 2)->all());
- $this->assertEquals([], $c->forPage(3, 2)->all());
- }
- public function testPrepend()
- {
- $c = new Collection(['one', 'two', 'three', 'four']);
- $this->assertEquals(
- ['zero', 'one', 'two', 'three', 'four'],
- $c->prepend('zero')->all()
- );
- $c = new Collection(['one' => 1, 'two' => 2]);
- $this->assertEquals(
- ['zero' => 0, 'one' => 1, 'two' => 2],
- $c->prepend(0, 'zero')->all()
- );
- $c = new Collection(['one' => 1, 'two' => 2]);
- $this->assertEquals(
- [null => 0, 'one' => 1, 'two' => 2],
- $c->prepend(0, null)->all()
- );
- }
- public function testPushWithOneItem()
- {
- $expected = [
- 0 => 4,
- 1 => 5,
- 2 => 6,
- 3 => ['a', 'b', 'c'],
- 4 => ['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe'],
- 5 => 'Jonny from Laroe',
- ];
- $data = new Collection([4, 5, 6]);
- $data->push(['a', 'b', 'c']);
- $data->push(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
- $actual = $data->push('Jonny from Laroe')->toArray();
- $this->assertSame($expected, $actual);
- }
- public function testPushWithMultipleItems()
- {
- $expected = [
- 0 => 4,
- 1 => 5,
- 2 => 6,
- 3 => 'Jonny',
- 4 => 'from',
- 5 => 'Laroe',
- 6 => 'Jonny',
- 7 => 'from',
- 8 => 'Laroe',
- 9 => 'a',
- 10 => 'b',
- 11 => 'c',
- ];
- $data = new Collection([4, 5, 6]);
- $data->push('Jonny', 'from', 'Laroe');
- $data->push(...[11 => 'Jonny', 12 => 'from', 13 => 'Laroe']);
- $data->push(...collect(['a', 'b', 'c']));
- $actual = $data->push(...[])->toArray();
- $this->assertSame($expected, $actual);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testZip($collection)
- {
- $c = new $collection([1, 2, 3]);
- $c = $c->zip(new $collection([4, 5, 6]));
- $this->assertInstanceOf($collection, $c);
- $this->assertInstanceOf($collection, $c->get(0));
- $this->assertInstanceOf($collection, $c->get(1));
- $this->assertInstanceOf($collection, $c->get(2));
- $this->assertCount(3, $c);
- $this->assertEquals([1, 4], $c->get(0)->all());
- $this->assertEquals([2, 5], $c->get(1)->all());
- $this->assertEquals([3, 6], $c->get(2)->all());
- $c = new $collection([1, 2, 3]);
- $c = $c->zip([4, 5, 6], [7, 8, 9]);
- $this->assertCount(3, $c);
- $this->assertEquals([1, 4, 7], $c->get(0)->all());
- $this->assertEquals([2, 5, 8], $c->get(1)->all());
- $this->assertEquals([3, 6, 9], $c->get(2)->all());
- $c = new $collection([1, 2, 3]);
- $c = $c->zip([4, 5, 6], [7]);
- $this->assertCount(3, $c);
- $this->assertEquals([1, 4, 7], $c->get(0)->all());
- $this->assertEquals([2, 5, null], $c->get(1)->all());
- $this->assertEquals([3, 6, null], $c->get(2)->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPadPadsArrayWithValue($collection)
- {
- $c = new $collection([1, 2, 3]);
- $c = $c->pad(4, 0);
- $this->assertEquals([1, 2, 3, 0], $c->all());
- $c = new $collection([1, 2, 3, 4, 5]);
- $c = $c->pad(4, 0);
- $this->assertEquals([1, 2, 3, 4, 5], $c->all());
- $c = new $collection([1, 2, 3]);
- $c = $c->pad(-4, 0);
- $this->assertEquals([0, 1, 2, 3], $c->all());
- $c = new $collection([1, 2, 3, 4, 5]);
- $c = $c->pad(-4, 0);
- $this->assertEquals([1, 2, 3, 4, 5], $c->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGettingMaxItemsFromCollection($collection)
- {
- $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
- $this->assertEquals(20, $c->max(function ($item) {
- return $item->foo;
- }));
- $this->assertEquals(20, $c->max('foo'));
- $this->assertEquals(20, $c->max->foo);
- $c = new $collection([['foo' => 10], ['foo' => 20]]);
- $this->assertEquals(20, $c->max('foo'));
- $this->assertEquals(20, $c->max->foo);
- $c = new $collection([1, 2, 3, 4, 5]);
- $this->assertEquals(5, $c->max());
- $c = new $collection;
- $this->assertNull($c->max());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGettingMinItemsFromCollection($collection)
- {
- $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
- $this->assertEquals(10, $c->min(function ($item) {
- return $item->foo;
- }));
- $this->assertEquals(10, $c->min('foo'));
- $this->assertEquals(10, $c->min->foo);
- $c = new $collection([['foo' => 10], ['foo' => 20]]);
- $this->assertEquals(10, $c->min('foo'));
- $this->assertEquals(10, $c->min->foo);
- $c = new $collection([['foo' => 10], ['foo' => 20], ['foo' => null]]);
- $this->assertEquals(10, $c->min('foo'));
- $this->assertEquals(10, $c->min->foo);
- $c = new $collection([1, 2, 3, 4, 5]);
- $this->assertEquals(1, $c->min());
- $c = new $collection([1, null, 3, 4, 5]);
- $this->assertEquals(1, $c->min());
- $c = new $collection([0, 1, 2, 3, 4]);
- $this->assertEquals(0, $c->min());
- $c = new $collection;
- $this->assertNull($c->min());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testOnly($collection)
- {
- $data = new $collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']);
- $this->assertEquals($data->all(), $data->only(null)->all());
- $this->assertEquals(['first' => 'Taylor'], $data->only(['first', 'missing'])->all());
- $this->assertEquals(['first' => 'Taylor'], $data->only('first', 'missing')->all());
- $this->assertEquals(['first' => 'Taylor'], $data->only(collect(['first', 'missing']))->all());
- $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(['first', 'email'])->all());
- $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only('first', 'email')->all());
- $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(collect(['first', 'email']))->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGettingAvgItemsFromCollection($collection)
- {
- $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
- $this->assertEquals(15, $c->avg(function ($item) {
- return $item->foo;
- }));
- $this->assertEquals(15, $c->avg('foo'));
- $this->assertEquals(15, $c->avg->foo);
- $c = new $collection([(object) ['foo' => 10], (object) ['foo' => 20], (object) ['foo' => null]]);
- $this->assertEquals(15, $c->avg(function ($item) {
- return $item->foo;
- }));
- $this->assertEquals(15, $c->avg('foo'));
- $this->assertEquals(15, $c->avg->foo);
- $c = new $collection([['foo' => 10], ['foo' => 20]]);
- $this->assertEquals(15, $c->avg('foo'));
- $this->assertEquals(15, $c->avg->foo);
- $c = new $collection([1, 2, 3, 4, 5]);
- $this->assertEquals(3, $c->avg());
- $c = new $collection;
- $this->assertNull($c->avg());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testJsonSerialize($collection)
- {
- $c = new $collection([
- new TestArrayableObject,
- new TestJsonableObject,
- new TestJsonSerializeObject,
- 'baz',
- ]);
- $this->assertSame([
- ['foo' => 'bar'],
- ['foo' => 'bar'],
- ['foo' => 'bar'],
- 'baz',
- ], $c->jsonSerialize());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCombineWithArray($collection)
- {
- $expected = [
- 1 => 4,
- 2 => 5,
- 3 => 6,
- ];
- $c = new $collection(array_keys($expected));
- $actual = $c->combine(array_values($expected))->toArray();
- $this->assertSame($expected, $actual);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCombineWithCollection($collection)
- {
- $expected = [
- 1 => 4,
- 2 => 5,
- 3 => 6,
- ];
- $keyCollection = new $collection(array_keys($expected));
- $valueCollection = new $collection(array_values($expected));
- $actual = $keyCollection->combine($valueCollection)->toArray();
- $this->assertSame($expected, $actual);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConcatWithArray($collection)
- {
- $expected = [
- 0 => 4,
- 1 => 5,
- 2 => 6,
- 3 => 'a',
- 4 => 'b',
- 5 => 'c',
- 6 => 'Jonny',
- 7 => 'from',
- 8 => 'Laroe',
- 9 => 'Jonny',
- 10 => 'from',
- 11 => 'Laroe',
- ];
- $data = new $collection([4, 5, 6]);
- $data = $data->concat(['a', 'b', 'c']);
- $data = $data->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
- $actual = $data->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe'])->toArray();
- $this->assertSame($expected, $actual);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testConcatWithCollection($collection)
- {
- $expected = [
- 0 => 4,
- 1 => 5,
- 2 => 6,
- 3 => 'a',
- 4 => 'b',
- 5 => 'c',
- 6 => 'Jonny',
- 7 => 'from',
- 8 => 'Laroe',
- 9 => 'Jonny',
- 10 => 'from',
- 11 => 'Laroe',
- ];
- $firstCollection = new $collection([4, 5, 6]);
- $secondCollection = new $collection(['a', 'b', 'c']);
- $thirdCollection = new $collection(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
- $firstCollection = $firstCollection->concat($secondCollection);
- $firstCollection = $firstCollection->concat($thirdCollection);
- $actual = $firstCollection->concat($thirdCollection)->toArray();
- $this->assertSame($expected, $actual);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testDump($collection)
- {
- $log = new Collection;
- VarDumper::setHandler(function ($value) use ($log) {
- $log->add($value);
- });
- (new $collection([1, 2, 3]))->dump('one', 'two');
- $this->assertSame(['one', 'two', [1, 2, 3]], $log->all());
- VarDumper::setHandler(null);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReduce($collection)
- {
- $data = new $collection([1, 2, 3]);
- $this->assertEquals(6, $data->reduce(function ($carry, $element) {
- return $carry += $element;
- }));
- $data = new $collection([
- 'foo' => 'bar',
- 'baz' => 'qux',
- ]);
- $this->assertSame('foobarbazqux', $data->reduce(function ($carry, $element, $key) {
- return $carry .= $key.$element;
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReduceWithKeys($collection)
- {
- $data = new $collection([
- 'foo' => 'bar',
- 'baz' => 'qux',
- ]);
- $this->assertSame('foobarbazqux', $data->reduceWithKeys(function ($carry, $element, $key) {
- return $carry .= $key.$element;
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReduceSpread($collection)
- {
- $data = new $collection([-1, 0, 1, 2, 3, 4, 5]);
- [$sum, $max, $min] = $data->reduceSpread(function ($sum, $max, $min, $value) {
- $sum += $value;
- $max = max($max, $value);
- $min = min($min, $value);
- return [$sum, $max, $min];
- }, 0, PHP_INT_MIN, PHP_INT_MAX);
- $this->assertEquals(14, $sum);
- $this->assertEquals(5, $max);
- $this->assertEquals(-1, $min);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testReduceSpreadThrowsAnExceptionIfReducerDoesNotReturnAnArray($collection)
- {
- $data = new $collection([1]);
- $this->expectException(UnexpectedValueException::class);
- $data->reduceSpread(function () {
- return false;
- }, null);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testRandomThrowsAnExceptionUsingAmountBiggerThanCollectionSize($collection)
- {
- $this->expectException(InvalidArgumentException::class);
- $data = new $collection([1, 2, 3]);
- $data->random(4);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPipe($collection)
- {
- $data = new $collection([1, 2, 3]);
- $this->assertEquals(6, $data->pipe(function ($data) {
- return $data->sum();
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPipeInto($collection)
- {
- $data = new $collection([
- 'first', 'second',
- ]);
- $instance = $data->pipeInto(TestCollectionMapIntoObject::class);
- $this->assertSame($data, $instance->value);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPipeThrough($collection)
- {
- $data = new $collection([1, 2, 3]);
- $result = $data->pipeThrough([
- function ($data) {
- return $data->merge([4, 5]);
- },
- function ($data) {
- return $data->sum();
- },
- ]);
- $this->assertEquals(15, $result);
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMedianValueWithArrayCollection($collection)
- {
- $data = new $collection([1, 2, 2, 4]);
- $this->assertEquals(2, $data->median());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMedianValueByKey($collection)
- {
- $data = new $collection([
- (object) ['foo' => 1],
- (object) ['foo' => 2],
- (object) ['foo' => 2],
- (object) ['foo' => 4],
- ]);
- $this->assertEquals(2, $data->median('foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMedianOnCollectionWithNull($collection)
- {
- $data = new $collection([
- (object) ['foo' => 1],
- (object) ['foo' => 2],
- (object) ['foo' => 4],
- (object) ['foo' => null],
- ]);
- $this->assertEquals(2, $data->median('foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testEvenMedianCollection($collection)
- {
- $data = new $collection([
- (object) ['foo' => 0],
- (object) ['foo' => 3],
- ]);
- $this->assertEquals(1.5, $data->median('foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMedianOutOfOrderCollection($collection)
- {
- $data = new $collection([
- (object) ['foo' => 0],
- (object) ['foo' => 5],
- (object) ['foo' => 3],
- ]);
- $this->assertEquals(3, $data->median('foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMedianOnEmptyCollectionReturnsNull($collection)
- {
- $data = new $collection;
- $this->assertNull($data->median());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testModeOnNullCollection($collection)
- {
- $data = new $collection;
- $this->assertNull($data->mode());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testMode($collection)
- {
- $data = new $collection([1, 2, 3, 4, 4, 5]);
- $this->assertEquals([4], $data->mode());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testModeValueByKey($collection)
- {
- $data = new $collection([
- (object) ['foo' => 1],
- (object) ['foo' => 1],
- (object) ['foo' => 2],
- (object) ['foo' => 4],
- ]);
- $this->assertEquals([1], $data->mode('foo'));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWithMultipleModeValues($collection)
- {
- $data = new $collection([1, 2, 2, 1]);
- $this->assertEquals([1, 2], $data->mode());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSliceOffset($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
- $this->assertEquals([4, 5, 6, 7, 8], $data->slice(3)->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSliceNegativeOffset($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
- $this->assertEquals([6, 7, 8], $data->slice(-3)->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSliceOffsetAndLength($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
- $this->assertEquals([4, 5, 6], $data->slice(3, 3)->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSliceOffsetAndNegativeLength($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
- $this->assertEquals([4, 5, 6, 7], $data->slice(3, -1)->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSliceNegativeOffsetAndLength($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
- $this->assertEquals([4, 5, 6], $data->slice(-5, 3)->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSliceNegativeOffsetAndNegativeLength($collection)
- {
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8]);
- $this->assertEquals([3, 4, 5, 6], $data->slice(-6, -2)->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCollectionFromTraversable($collection)
- {
- $data = new $collection(new ArrayObject([1, 2, 3]));
- $this->assertEquals([1, 2, 3], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCollectionFromTraversableWithKeys($collection)
- {
- $data = new $collection(new ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3]));
- $this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- * @requires PHP >= 8.1
- */
- public function testCollectionFromEnum($collection)
- {
- $data = new $collection(TestEnum::A);
- $this->assertEquals([TestEnum::A], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- * @requires PHP >= 8.1
- */
- public function testCollectionFromBackedEnum($collection)
- {
- $data = new $collection(TestBackedEnum::A);
- $this->assertEquals([TestBackedEnum::A], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitCollectionWithADivisableCount($collection)
- {
- $data = new $collection(['a', 'b', 'c', 'd']);
- $this->assertEquals(
- [['a', 'b'], ['c', 'd']],
- $data->split(2)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- $data = new $collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
- $this->assertEquals(
- [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
- $data->split(2)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitCollectionWithAnUndivisableCount($collection)
- {
- $data = new $collection(['a', 'b', 'c']);
- $this->assertEquals(
- [['a', 'b'], ['c']],
- $data->split(2)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitCollectionWithCountLessThenDivisor($collection)
- {
- $data = new $collection(['a']);
- $this->assertEquals(
- [['a']],
- $data->split(2)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitCollectionIntoThreeWithCountOfFour($collection)
- {
- $data = new $collection(['a', 'b', 'c', 'd']);
- $this->assertEquals(
- [['a', 'b'], ['c'], ['d']],
- $data->split(3)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitCollectionIntoThreeWithCountOfFive($collection)
- {
- $data = new $collection(['a', 'b', 'c', 'd', 'e']);
- $this->assertEquals(
- [['a', 'b'], ['c', 'd'], ['e']],
- $data->split(3)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitCollectionIntoSixWithCountOfTen($collection)
- {
- $data = new $collection(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']);
- $this->assertEquals(
- [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i'], ['j']],
- $data->split(6)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testSplitEmptyCollection($collection)
- {
- $data = new $collection;
- $this->assertEquals(
- [],
- $data->split(2)->map(function (Collection $chunk) {
- return $chunk->values()->toArray();
- })->toArray()
- );
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHigherOrderCollectionGroupBy($collection)
- {
- $data = new $collection([
- new TestSupportCollectionHigherOrderItem,
- new TestSupportCollectionHigherOrderItem('TAYLOR'),
- new TestSupportCollectionHigherOrderItem('foo'),
- ]);
- $this->assertEquals([
- 'taylor' => [$data->get(0)],
- 'TAYLOR' => [$data->get(1)],
- 'foo' => [$data->get(2)],
- ], $data->groupBy->name->toArray());
- $this->assertEquals([
- 'TAYLOR' => [$data->get(0), $data->get(1)],
- 'FOO' => [$data->get(2)],
- ], $data->groupBy->uppercase()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHigherOrderCollectionMap($collection)
- {
- $person1 = (object) ['name' => 'Taylor'];
- $person2 = (object) ['name' => 'Yaz'];
- $data = new $collection([$person1, $person2]);
- $this->assertEquals(['Taylor', 'Yaz'], $data->map->name->toArray());
- $data = new $collection([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);
- $this->assertEquals(['TAYLOR', 'TAYLOR'], $data->each->uppercase()->map->name->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHigherOrderCollectionMapFromArrays($collection)
- {
- $person1 = ['name' => 'Taylor'];
- $person2 = ['name' => 'Yaz'];
- $data = new $collection([$person1, $person2]);
- $this->assertEquals(['Taylor', 'Yaz'], $data->map->name->toArray());
- $data = new $collection([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);
- $this->assertEquals(['TAYLOR', 'TAYLOR'], $data->each->uppercase()->map->name->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPartition($collection)
- {
- $data = new $collection(range(1, 10));
- [$firstPartition, $secondPartition] = $data->partition(function ($i) {
- return $i <= 5;
- })->all();
- $this->assertEquals([1, 2, 3, 4, 5], $firstPartition->values()->toArray());
- $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPartitionCallbackWithKey($collection)
- {
- $data = new $collection(['zero', 'one', 'two', 'three']);
- [$even, $odd] = $data->partition(function ($item, $index) {
- return $index % 2 === 0;
- })->all();
- $this->assertEquals(['zero', 'two'], $even->values()->toArray());
- $this->assertEquals(['one', 'three'], $odd->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPartitionByKey($collection)
- {
- $courses = new $collection([
- ['free' => true, 'title' => 'Basic'], ['free' => false, 'title' => 'Premium'],
- ]);
- [$free, $premium] = $courses->partition('free')->all();
- $this->assertSame([['free' => true, 'title' => 'Basic']], $free->values()->toArray());
- $this->assertSame([['free' => false, 'title' => 'Premium']], $premium->values()->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPartitionWithOperators($collection)
- {
- $data = new $collection([
- ['name' => 'Tim', 'age' => 17],
- ['name' => 'Agatha', 'age' => 62],
- ['name' => 'Kristina', 'age' => 33],
- ['name' => 'Tim', 'age' => 41],
- ]);
- [$tims, $others] = $data->partition('name', 'Tim')->all();
- $this->assertEquals([
- ['name' => 'Tim', 'age' => 17],
- ['name' => 'Tim', 'age' => 41],
- ], $tims->values()->all());
- $this->assertEquals([
- ['name' => 'Agatha', 'age' => 62],
- ['name' => 'Kristina', 'age' => 33],
- ], $others->values()->all());
- [$adults, $minors] = $data->partition('age', '>=', 18)->all();
- $this->assertEquals([
- ['name' => 'Agatha', 'age' => 62],
- ['name' => 'Kristina', 'age' => 33],
- ['name' => 'Tim', 'age' => 41],
- ], $adults->values()->all());
- $this->assertEquals([
- ['name' => 'Tim', 'age' => 17],
- ], $minors->values()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPartitionPreservesKeys($collection)
- {
- $courses = new $collection([
- 'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
- ]);
- [$free, $premium] = $courses->partition('free')->all();
- $this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());
- $this->assertSame(['b' => ['free' => false]], $premium->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testPartitionEmptyCollection($collection)
- {
- $data = new $collection;
- $this->assertCount(2, $data->partition(function () {
- return true;
- }));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHigherOrderPartition($collection)
- {
- $courses = new $collection([
- 'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
- ]);
- [$free, $premium] = $courses->partition->free->all();
- $this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());
- $this->assertSame(['b' => ['free' => false]], $premium->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testTap($collection)
- {
- $data = new $collection([1, 2, 3]);
- $fromTap = [];
- $data = $data->tap(function ($data) use (&$fromTap) {
- $fromTap = $data->slice(0, 1)->toArray();
- });
- $this->assertSame([1], $fromTap);
- $this->assertSame([1, 2, 3], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhen($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->when('adam', function ($data, $newName) {
- return $data->concat([$newName]);
- });
- $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
- $data = new $collection(['michael', 'tom']);
- $data = $data->when(false, function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame(['michael', 'tom'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhenDefault($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->when(false, function ($data) {
- return $data->concat(['adam']);
- }, function ($data) {
- return $data->concat(['taylor']);
- });
- $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhenEmpty($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->whenEmpty(function ($collection) {
- return $data->concat(['adam']);
- });
- $this->assertSame(['michael', 'tom'], $data->toArray());
- $data = new $collection;
- $data = $data->whenEmpty(function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame(['adam'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhenEmptyDefault($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->whenEmpty(function ($data) {
- return $data->concat(['adam']);
- }, function ($data) {
- return $data->concat(['taylor']);
- });
- $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhenNotEmpty($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->whenNotEmpty(function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
- $data = new $collection;
- $data = $data->whenNotEmpty(function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame([], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhenNotEmptyDefault($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->whenNotEmpty(function ($data) {
- return $data->concat(['adam']);
- }, function ($data) {
- return $data->concat(['taylor']);
- });
- $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnless($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->unless(false, function ($data) {
- return $data->concat(['caleb']);
- });
- $this->assertSame(['michael', 'tom', 'caleb'], $data->toArray());
- $data = new $collection(['michael', 'tom']);
- $data = $data->unless(true, function ($data) {
- return $data->concat(['caleb']);
- });
- $this->assertSame(['michael', 'tom'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnlessDefault($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->unless(true, function ($data) {
- return $data->concat(['caleb']);
- }, function ($data) {
- return $data->concat(['taylor']);
- });
- $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnlessEmpty($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->unlessEmpty(function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
- $data = new $collection;
- $data = $data->unlessEmpty(function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame([], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnlessEmptyDefault($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->unlessEmpty(function ($data) {
- return $data->concat(['adam']);
- }, function ($data) {
- return $data->concat(['taylor']);
- });
- $this->assertSame(['michael', 'tom', 'adam'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnlessNotEmpty($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->unlessNotEmpty(function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame(['michael', 'tom'], $data->toArray());
- $data = new $collection;
- $data = $data->unlessNotEmpty(function ($data) {
- return $data->concat(['adam']);
- });
- $this->assertSame(['adam'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUnlessNotEmptyDefault($collection)
- {
- $data = new $collection(['michael', 'tom']);
- $data = $data->unlessNotEmpty(function ($data) {
- return $data->concat(['adam']);
- }, function ($data) {
- return $data->concat(['taylor']);
- });
- $this->assertSame(['michael', 'tom', 'taylor'], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testHasReturnsValidResults($collection)
- {
- $data = new $collection(['foo' => 'one', 'bar' => 'two', 1 => 'three']);
- $this->assertTrue($data->has('foo'));
- $this->assertTrue($data->has('foo', 'bar', 1));
- $this->assertFalse($data->has('foo', 'bar', 1, 'baz'));
- $this->assertFalse($data->has('baz'));
- }
- public function testPutAddsItemToCollection()
- {
- $data = new Collection;
- $this->assertSame([], $data->toArray());
- $data->put('foo', 1);
- $this->assertSame(['foo' => 1], $data->toArray());
- $data->put('bar', ['nested' => 'two']);
- $this->assertSame(['foo' => 1, 'bar' => ['nested' => 'two']], $data->toArray());
- $data->put('foo', 3);
- $this->assertSame(['foo' => 3, 'bar' => ['nested' => 'two']], $data->toArray());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testItThrowsExceptionWhenTryingToAccessNoProxyProperty($collection)
- {
- $data = new $collection;
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('Property [foo] does not exist on this collection instance.');
- $data->foo;
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testGetWithNullReturnsNull($collection)
- {
- $data = new $collection([1, 2, 3]);
- $this->assertNull($data->get(null));
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereNull($collection)
- {
- $data = new $collection([
- ['name' => 'Taylor'],
- ['name' => null],
- ['name' => 'Bert'],
- ['name' => false],
- ['name' => ''],
- ]);
- $this->assertSame([
- 1 => ['name' => null],
- ], $data->whereNull('name')->all());
- $this->assertSame([], $data->whereNull()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereNullWithoutKey($collection)
- {
- $collection = new $collection([1, null, 3, 'null', false, true]);
- $this->assertSame([
- 1 => null,
- ], $collection->whereNull()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereNotNull($collection)
- {
- $data = new $collection($originalData = [
- ['name' => 'Taylor'],
- ['name' => null],
- ['name' => 'Bert'],
- ['name' => false],
- ['name' => ''],
- ]);
- $this->assertSame([
- 0 => ['name' => 'Taylor'],
- 2 => ['name' => 'Bert'],
- 3 => ['name' => false],
- 4 => ['name' => ''],
- ], $data->whereNotNull('name')->all());
- $this->assertSame($originalData, $data->whereNotNull()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testWhereNotNullWithoutKey($collection)
- {
- $data = new $collection([1, null, 3, 'null', false, true]);
- $this->assertSame([
- 0 => 1,
- 2 => 3,
- 3 => 'null',
- 4 => false,
- 5 => true,
- ], $data->whereNotNull()->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testCollect($collection)
- {
- $data = $collection::make([
- 'a' => 1,
- 'b' => 2,
- 'c' => 3,
- ])->collect();
- $this->assertInstanceOf(Collection::class, $data);
- $this->assertSame([
- 'a' => 1,
- 'b' => 2,
- 'c' => 3,
- ], $data->all());
- }
- /**
- * @dataProvider collectionClassProvider
- */
- public function testUndot($collection)
- {
- $data = $collection::make([
- 'name' => 'Taylor',
- 'meta.foo' => 'bar',
- 'meta.baz' => 'boom',
- 'meta.bam.boom' => 'bip',
- ])->undot();
- $this->assertSame([
- 'name' => 'Taylor',
- 'meta' => [
- 'foo' => 'bar',
- 'baz' => 'boom',
- 'bam' => [
- 'boom' => 'bip',
- ],
- ],
- ], $data->all());
- $data = $collection::make([
- 'foo.0' => 'bar',
- 'foo.1' => 'baz',
- 'foo.baz' => 'boom',
- ])->undot();
- $this->assertSame([
- 'foo' => [
- 'bar',
- 'baz',
- 'baz' => 'boom',
- ],
- ], $data->all());
- }
- /**
- * Provides each collection class, respectively.
- *
- * @return array
- */
- public function collectionClassProvider()
- {
- return [
- [Collection::class],
- [LazyCollection::class],
- ];
- }
- }
- class TestSupportCollectionHigherOrderItem
- {
- public $name;
- public function __construct($name = 'taylor')
- {
- $this->name = $name;
- }
- public function uppercase()
- {
- return $this->name = strtoupper($this->name);
- }
- public function is($name)
- {
- return $this->name === $name;
- }
- }
- class TestAccessorEloquentTestStub
- {
- protected $attributes = [];
- public function __construct($attributes)
- {
- $this->attributes = $attributes;
- }
- public function __get($attribute)
- {
- $accessor = 'get'.lcfirst($attribute).'Attribute';
- if (method_exists($this, $accessor)) {
- return $this->$accessor();
- }
- return $this->$attribute;
- }
- public function __isset($attribute)
- {
- $accessor = 'get'.lcfirst($attribute).'Attribute';
- if (method_exists($this, $accessor)) {
- return ! is_null($this->$accessor());
- }
- return isset($this->$attribute);
- }
- public function getSomeAttribute()
- {
- return $this->attributes['some'];
- }
- }
- class TestArrayAccessImplementation implements ArrayAccess
- {
- private $arr;
- public function __construct($arr)
- {
- $this->arr = $arr;
- }
- #[\ReturnTypeWillChange]
- public function offsetExists($offset)
- {
- return isset($this->arr[$offset]);
- }
- #[\ReturnTypeWillChange]
- public function offsetGet($offset)
- {
- return $this->arr[$offset];
- }
- #[\ReturnTypeWillChange]
- public function offsetSet($offset, $value)
- {
- $this->arr[$offset] = $value;
- }
- #[\ReturnTypeWillChange]
- public function offsetUnset($offset)
- {
- unset($this->arr[$offset]);
- }
- }
- class TestArrayableObject implements Arrayable
- {
- public function toArray()
- {
- return ['foo' => 'bar'];
- }
- }
- class TestJsonableObject implements Jsonable
- {
- public function toJson($options = 0)
- {
- return '{"foo":"bar"}';
- }
- }
- class TestJsonSerializeObject implements JsonSerializable
- {
- public function jsonSerialize(): array
- {
- return ['foo' => 'bar'];
- }
- }
- class TestJsonSerializeWithScalarValueObject implements JsonSerializable
- {
- public function jsonSerialize(): string
- {
- return 'foo';
- }
- }
- class TestCollectionMapIntoObject
- {
- public $value;
- public function __construct($value)
- {
- $this->value = $value;
- }
- }
- class TestCollectionSubclass extends Collection
- {
- //
- }
|