SupportCollectionTest.php 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123
  1. <?php
  2. namespace Tightenco\Collect\Tests\Support;
  3. use stdClass;
  4. use Exception;
  5. use ArrayAccess;
  6. use ArrayObject;
  7. use Mockery as m;
  8. use ArrayIterator;
  9. use CachingIterator;
  10. use ReflectionClass;
  11. use JsonSerializable;
  12. use PHPUnit\Framework\TestCase;
  13. use Tightenco\Collect\Support\Collection;
  14. use Tightenco\Collect\Support\HtmlString;
  15. use Tightenco\Collect\Contracts\Support\Jsonable;
  16. use Tightenco\Collect\Contracts\Support\Arrayable;
  17. class SupportCollectionTest extends TestCase
  18. {
  19. public function testFirstReturnsFirstItemInCollection()
  20. {
  21. $c = new Collection(['foo', 'bar']);
  22. $this->assertEquals('foo', $c->first());
  23. }
  24. public function testFirstWithCallback()
  25. {
  26. $data = new Collection(['foo', 'bar', 'baz']);
  27. $result = $data->first(function ($value) {
  28. return $value === 'bar';
  29. });
  30. $this->assertEquals('bar', $result);
  31. }
  32. public function testFirstWithCallbackAndDefault()
  33. {
  34. $data = new Collection(['foo', 'bar']);
  35. $result = $data->first(function ($value) {
  36. return $value === 'baz';
  37. }, 'default');
  38. $this->assertEquals('default', $result);
  39. }
  40. public function testFirstWithDefaultAndWithoutCallback()
  41. {
  42. $data = new Collection;
  43. $result = $data->first(null, 'default');
  44. $this->assertEquals('default', $result);
  45. }
  46. public function testFirstWhere()
  47. {
  48. $data = new Collection([
  49. ['material' => 'paper', 'type' => 'book'],
  50. ['material' => 'rubber', 'type' => 'gasket'],
  51. ]);
  52. $this->assertEquals('book', $data->firstWhere('material', 'paper')['type']);
  53. $this->assertEquals('gasket', $data->firstWhere('material', 'rubber')['type']);
  54. $this->assertNull($data->firstWhere('material', 'nonexistant'));
  55. $this->assertNull($data->firstWhere('nonexistant', 'key'));
  56. }
  57. public function testLastReturnsLastItemInCollection()
  58. {
  59. $c = new Collection(['foo', 'bar']);
  60. $this->assertEquals('bar', $c->last());
  61. }
  62. public function testLastWithCallback()
  63. {
  64. $data = new Collection([100, 200, 300]);
  65. $result = $data->last(function ($value) {
  66. return $value < 250;
  67. });
  68. $this->assertEquals(200, $result);
  69. $result = $data->last(function ($value, $key) {
  70. return $key < 2;
  71. });
  72. $this->assertEquals(200, $result);
  73. }
  74. public function testLastWithCallbackAndDefault()
  75. {
  76. $data = new Collection(['foo', 'bar']);
  77. $result = $data->last(function ($value) {
  78. return $value === 'baz';
  79. }, 'default');
  80. $this->assertEquals('default', $result);
  81. }
  82. public function testLastWithDefaultAndWithoutCallback()
  83. {
  84. $data = new Collection;
  85. $result = $data->last(null, 'default');
  86. $this->assertEquals('default', $result);
  87. }
  88. public function testPopReturnsAndRemovesLastItemInCollection()
  89. {
  90. $c = new Collection(['foo', 'bar']);
  91. $this->assertEquals('bar', $c->pop());
  92. $this->assertEquals('foo', $c->first());
  93. }
  94. public function testShiftReturnsAndRemovesFirstItemInCollection()
  95. {
  96. $c = new Collection(['foo', 'bar']);
  97. $this->assertEquals('foo', $c->shift());
  98. $this->assertEquals('bar', $c->first());
  99. }
  100. public function testEmptyCollectionIsEmpty()
  101. {
  102. $c = new Collection;
  103. $this->assertTrue($c->isEmpty());
  104. }
  105. public function testEmptyCollectionIsNotEmpty()
  106. {
  107. $c = new Collection(['foo', 'bar']);
  108. $this->assertFalse($c->isEmpty());
  109. $this->assertTrue($c->isNotEmpty());
  110. }
  111. public function testCollectionIsConstructed()
  112. {
  113. $collection = new Collection('foo');
  114. $this->assertSame(['foo'], $collection->all());
  115. $collection = new Collection(2);
  116. $this->assertSame([2], $collection->all());
  117. $collection = new Collection(false);
  118. $this->assertSame([false], $collection->all());
  119. $collection = new Collection(null);
  120. $this->assertEmpty($collection->all());
  121. $collection = new Collection;
  122. $this->assertEmpty($collection->all());
  123. }
  124. public function testCollectionShuffleWithSeed()
  125. {
  126. $collection = new Collection(range(0, 100, 10));
  127. $firstRandom = $collection->shuffle(1234);
  128. $secondRandom = $collection->shuffle(1234);
  129. $this->assertEquals($firstRandom, $secondRandom);
  130. }
  131. public function testGetArrayableItems()
  132. {
  133. $collection = new Collection;
  134. $class = new ReflectionClass($collection);
  135. $method = $class->getMethod('getArrayableItems');
  136. $method->setAccessible(true);
  137. $items = new TestArrayableObject;
  138. $array = $method->invokeArgs($collection, [$items]);
  139. $this->assertSame(['foo' => 'bar'], $array);
  140. $items = new TestJsonableObject;
  141. $array = $method->invokeArgs($collection, [$items]);
  142. $this->assertSame(['foo' => 'bar'], $array);
  143. $items = new TestJsonSerializeObject;
  144. $array = $method->invokeArgs($collection, [$items]);
  145. $this->assertSame(['foo' => 'bar'], $array);
  146. $items = new Collection(['foo' => 'bar']);
  147. $array = $method->invokeArgs($collection, [$items]);
  148. $this->assertSame(['foo' => 'bar'], $array);
  149. $items = ['foo' => 'bar'];
  150. $array = $method->invokeArgs($collection, [$items]);
  151. $this->assertSame(['foo' => 'bar'], $array);
  152. }
  153. public function testToArrayCallsToArrayOnEachItemInCollection()
  154. {
  155. $item1 = m::mock(Arrayable::class);
  156. $item1->shouldReceive('toArray')->once()->andReturn('foo.array');
  157. $item2 = m::mock(Arrayable::class);
  158. $item2->shouldReceive('toArray')->once()->andReturn('bar.array');
  159. $c = new Collection([$item1, $item2]);
  160. $results = $c->toArray();
  161. $this->assertEquals(['foo.array', 'bar.array'], $results);
  162. }
  163. public function testJsonSerializeCallsToArrayOrJsonSerializeOnEachItemInCollection()
  164. {
  165. $item1 = m::mock(JsonSerializable::class);
  166. $item1->shouldReceive('jsonSerialize')->once()->andReturn('foo.json');
  167. $item2 = m::mock(Arrayable::class);
  168. $item2->shouldReceive('toArray')->once()->andReturn('bar.array');
  169. $c = new Collection([$item1, $item2]);
  170. $results = $c->jsonSerialize();
  171. $this->assertEquals(['foo.json', 'bar.array'], $results);
  172. }
  173. public function testToJsonEncodesTheJsonSerializeResult()
  174. {
  175. $c = $this->getMockBuilder(Collection::class)->setMethods(['jsonSerialize'])->getMock();
  176. $c->expects($this->once())->method('jsonSerialize')->will($this->returnValue('foo'));
  177. $results = $c->toJson();
  178. $this->assertJsonStringEqualsJsonString(json_encode('foo'), $results);
  179. }
  180. public function testCastingToStringJsonEncodesTheToArrayResult()
  181. {
  182. $c = $this->getMockBuilder(Collection::class)->setMethods(['jsonSerialize'])->getMock();
  183. $c->expects($this->once())->method('jsonSerialize')->will($this->returnValue('foo'));
  184. $this->assertJsonStringEqualsJsonString(json_encode('foo'), (string) $c);
  185. }
  186. public function testOffsetAccess()
  187. {
  188. $c = new Collection(['name' => 'taylor']);
  189. $this->assertEquals('taylor', $c['name']);
  190. $c['name'] = 'dayle';
  191. $this->assertEquals('dayle', $c['name']);
  192. $this->assertTrue(isset($c['name']));
  193. unset($c['name']);
  194. $this->assertFalse(isset($c['name']));
  195. $c[] = 'jason';
  196. $this->assertEquals('jason', $c[0]);
  197. }
  198. public function testArrayAccessOffsetExists()
  199. {
  200. $c = new Collection(['foo', 'bar']);
  201. $this->assertTrue($c->offsetExists(0));
  202. $this->assertTrue($c->offsetExists(1));
  203. $this->assertFalse($c->offsetExists(1000));
  204. }
  205. public function testArrayAccessOffsetGet()
  206. {
  207. $c = new Collection(['foo', 'bar']);
  208. $this->assertEquals('foo', $c->offsetGet(0));
  209. $this->assertEquals('bar', $c->offsetGet(1));
  210. }
  211. public function testArrayAccessOffsetSet()
  212. {
  213. $c = new Collection(['foo', 'foo']);
  214. $c->offsetSet(1, 'bar');
  215. $this->assertEquals('bar', $c[1]);
  216. $c->offsetSet(null, 'qux');
  217. $this->assertEquals('qux', $c[2]);
  218. }
  219. public function testArrayAccessOffsetUnset()
  220. {
  221. $c = new Collection(['foo', 'bar']);
  222. $c->offsetUnset(1);
  223. $this->assertFalse(isset($c[1]));
  224. }
  225. public function testForgetSingleKey()
  226. {
  227. $c = new Collection(['foo', 'bar']);
  228. $c->forget(0);
  229. $this->assertFalse(isset($c['foo']));
  230. $c = new Collection(['foo' => 'bar', 'baz' => 'qux']);
  231. $c->forget('foo');
  232. $this->assertFalse(isset($c['foo']));
  233. }
  234. public function testForgetArrayOfKeys()
  235. {
  236. $c = new Collection(['foo', 'bar', 'baz']);
  237. $c->forget([0, 2]);
  238. $this->assertFalse(isset($c[0]));
  239. $this->assertFalse(isset($c[2]));
  240. $this->assertTrue(isset($c[1]));
  241. $c = new Collection(['name' => 'taylor', 'foo' => 'bar', 'baz' => 'qux']);
  242. $c->forget(['foo', 'baz']);
  243. $this->assertFalse(isset($c['foo']));
  244. $this->assertFalse(isset($c['baz']));
  245. $this->assertTrue(isset($c['name']));
  246. }
  247. public function testCountable()
  248. {
  249. $c = new Collection(['foo', 'bar']);
  250. $this->assertCount(2, $c);
  251. }
  252. public function testIterable()
  253. {
  254. $c = new Collection(['foo']);
  255. $this->assertInstanceOf(ArrayIterator::class, $c->getIterator());
  256. $this->assertEquals(['foo'], $c->getIterator()->getArrayCopy());
  257. }
  258. public function testCachingIterator()
  259. {
  260. $c = new Collection(['foo']);
  261. $this->assertInstanceOf(CachingIterator::class, $c->getCachingIterator());
  262. }
  263. public function testFilter()
  264. {
  265. $c = new Collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
  266. $this->assertEquals([1 => ['id' => 2, 'name' => 'World']], $c->filter(function ($item) {
  267. return $item['id'] == 2;
  268. })->all());
  269. $c = new Collection(['', 'Hello', '', 'World']);
  270. $this->assertEquals(['Hello', 'World'], $c->filter()->values()->toArray());
  271. $c = new Collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
  272. $this->assertEquals(['first' => 'Hello', 'second' => 'World'], $c->filter(function ($item, $key) {
  273. return $key != 'id';
  274. })->all());
  275. }
  276. public function testHigherOrderKeyBy()
  277. {
  278. $c = new Collection([
  279. ['id' => 'id1', 'name' => 'first'],
  280. ['id' => 'id2', 'name' => 'second'],
  281. ]);
  282. $this->assertEquals(['id1' => 'first', 'id2' => 'second'], $c->keyBy->id->map->name->all());
  283. }
  284. public function testHigherOrderUnique()
  285. {
  286. $c = new Collection([
  287. ['id' => '1', 'name' => 'first'],
  288. ['id' => '1', 'name' => 'second'],
  289. ]);
  290. $this->assertCount(1, $c->unique->id);
  291. }
  292. public function testHigherOrderFilter()
  293. {
  294. $c = new Collection([
  295. new class {
  296. public $name = 'Alex';
  297. public function active()
  298. {
  299. return true;
  300. }
  301. },
  302. new class {
  303. public $name = 'John';
  304. public function active()
  305. {
  306. return false;
  307. }
  308. },
  309. ]);
  310. $this->assertCount(1, $c->filter->active());
  311. }
  312. public function testWhere()
  313. {
  314. $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  315. $this->assertEquals(
  316. [['v' => 3], ['v' => '3']],
  317. $c->where('v', 3)->values()->all()
  318. );
  319. $this->assertEquals(
  320. [['v' => 3], ['v' => '3']],
  321. $c->where('v', '=', 3)->values()->all()
  322. );
  323. $this->assertEquals(
  324. [['v' => 3], ['v' => '3']],
  325. $c->where('v', '==', 3)->values()->all()
  326. );
  327. $this->assertEquals(
  328. [['v' => 3], ['v' => '3']],
  329. $c->where('v', 'garbage', 3)->values()->all()
  330. );
  331. $this->assertEquals(
  332. [['v' => 3]],
  333. $c->where('v', '===', 3)->values()->all()
  334. );
  335. $this->assertEquals(
  336. [['v' => 1], ['v' => 2], ['v' => 4]],
  337. $c->where('v', '<>', 3)->values()->all()
  338. );
  339. $this->assertEquals(
  340. [['v' => 1], ['v' => 2], ['v' => 4]],
  341. $c->where('v', '!=', 3)->values()->all()
  342. );
  343. $this->assertEquals(
  344. [['v' => 1], ['v' => 2], ['v' => '3'], ['v' => 4]],
  345. $c->where('v', '!==', 3)->values()->all()
  346. );
  347. $this->assertEquals(
  348. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3']],
  349. $c->where('v', '<=', 3)->values()->all()
  350. );
  351. $this->assertEquals(
  352. [['v' => 3], ['v' => '3'], ['v' => 4]],
  353. $c->where('v', '>=', 3)->values()->all()
  354. );
  355. $this->assertEquals(
  356. [['v' => 1], ['v' => 2]],
  357. $c->where('v', '<', 3)->values()->all()
  358. );
  359. $this->assertEquals(
  360. [['v' => 4]],
  361. $c->where('v', '>', 3)->values()->all()
  362. );
  363. $object = (object) ['foo' => 'bar'];
  364. $this->assertEquals(
  365. [],
  366. $c->where('v', $object)->values()->all()
  367. );
  368. $this->assertEquals(
  369. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  370. $c->where('v', '<>', $object)->values()->all()
  371. );
  372. $this->assertEquals(
  373. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  374. $c->where('v', '!=', $object)->values()->all()
  375. );
  376. $this->assertEquals(
  377. [['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  378. $c->where('v', '!==', $object)->values()->all()
  379. );
  380. $this->assertEquals(
  381. [],
  382. $c->where('v', '>', $object)->values()->all()
  383. );
  384. $c = new Collection([['v' => 1], ['v' => $object]]);
  385. $this->assertEquals(
  386. [['v' => $object]],
  387. $c->where('v', $object)->values()->all()
  388. );
  389. $this->assertEquals(
  390. [['v' => 1], ['v' => $object]],
  391. $c->where('v', '<>', null)->values()->all()
  392. );
  393. $this->assertEquals(
  394. [],
  395. $c->where('v', '<', null)->values()->all()
  396. );
  397. $c = new Collection([['v' => 1], ['v' => new HtmlString('hello')]]);
  398. $this->assertEquals(
  399. [['v' => new HtmlString('hello')]],
  400. $c->where('v', 'hello')->values()->all()
  401. );
  402. $c = new Collection([['v' => 1], ['v' => 'hello']]);
  403. $this->assertEquals(
  404. [['v' => 'hello']],
  405. $c->where('v', new HtmlString('hello'))->values()->all()
  406. );
  407. $c = new Collection([['v' => 1], ['v' => 2], ['v' => null]]);
  408. $this->assertEquals(
  409. [['v' => 1], ['v' => 2]],
  410. $c->where('v')->values()->all()
  411. );
  412. }
  413. public function testWhereStrict()
  414. {
  415. $c = new Collection([['v' => 3], ['v' => '3']]);
  416. $this->assertEquals(
  417. [['v' => 3]],
  418. $c->whereStrict('v', 3)->values()->all()
  419. );
  420. }
  421. public function testWhereInstanceOf()
  422. {
  423. $c = new Collection([new stdClass, new stdClass, new Collection, new stdClass]);
  424. $this->assertCount(3, $c->whereInstanceOf(stdClass::class));
  425. }
  426. public function testWhereIn()
  427. {
  428. $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  429. $this->assertEquals([['v' => 1], ['v' => 3], ['v' => '3']], $c->whereIn('v', [1, 3])->values()->all());
  430. }
  431. public function testWhereInStrict()
  432. {
  433. $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  434. $this->assertEquals([['v' => 1], ['v' => 3]], $c->whereInStrict('v', [1, 3])->values()->all());
  435. }
  436. public function testWhereNotIn()
  437. {
  438. $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  439. $this->assertEquals([['v' => 2], ['v' => 4]], $c->whereNotIn('v', [1, 3])->values()->all());
  440. }
  441. public function testWhereNotInStrict()
  442. {
  443. $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  444. $this->assertEquals([['v' => 2], ['v' => '3'], ['v' => 4]], $c->whereNotInStrict('v', [1, 3])->values()->all());
  445. }
  446. public function testValues()
  447. {
  448. $c = new Collection([['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]);
  449. $this->assertEquals([['id' => 2, 'name' => 'World']], $c->filter(function ($item) {
  450. return $item['id'] == 2;
  451. })->values()->all());
  452. }
  453. public function testBetween()
  454. {
  455. $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  456. $this->assertEquals([['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]],
  457. $c->whereBetween('v', [2, 4])->values()->all());
  458. $this->assertEquals([['v' => 1]], $c->whereBetween('v', [-1, 1])->all());
  459. $this->assertEquals([['v' => 3], ['v' => '3']], $c->whereBetween('v', [3, 3])->values()->all());
  460. }
  461. public function testWhereNotBetween()
  462. {
  463. $c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
  464. $this->assertEquals([['v' => 1]], $c->whereNotBetween('v', [2, 4])->values()->all());
  465. $this->assertEquals([['v' => 2], ['v' => 3], ['v' => 3], ['v' => 4]], $c->whereNotBetween('v', [-1, 1])->values()->all());
  466. $this->assertEquals([['v' => 1], ['v' => '2'], ['v' => '4']], $c->whereNotBetween('v', [3, 3])->values()->all());
  467. }
  468. public function testFlatten()
  469. {
  470. // Flat arrays are unaffected
  471. $c = new Collection(['#foo', '#bar', '#baz']);
  472. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  473. // Nested arrays are flattened with existing flat items
  474. $c = new Collection([['#foo', '#bar'], '#baz']);
  475. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  476. // Sets of nested arrays are flattened
  477. $c = new Collection([['#foo', '#bar'], ['#baz']]);
  478. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  479. // Deeply nested arrays are flattened
  480. $c = new Collection([['#foo', ['#bar']], ['#baz']]);
  481. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  482. // Nested collections are flattened alongside arrays
  483. $c = new Collection([new Collection(['#foo', '#bar']), ['#baz']]);
  484. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  485. // Nested collections containing plain arrays are flattened
  486. $c = new Collection([new Collection(['#foo', ['#bar']]), ['#baz']]);
  487. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  488. // Nested arrays containing collections are flattened
  489. $c = new Collection([['#foo', new Collection(['#bar'])], ['#baz']]);
  490. $this->assertEquals(['#foo', '#bar', '#baz'], $c->flatten()->all());
  491. // Nested arrays containing collections containing arrays are flattened
  492. $c = new Collection([['#foo', new Collection(['#bar', ['#zap']])], ['#baz']]);
  493. $this->assertEquals(['#foo', '#bar', '#zap', '#baz'], $c->flatten()->all());
  494. }
  495. public function testFlattenWithDepth()
  496. {
  497. // No depth flattens recursively
  498. $c = new Collection([['#foo', ['#bar', ['#baz']]], '#zap']);
  499. $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
  500. // Specifying a depth only flattens to that depth
  501. $c = new Collection([['#foo', ['#bar', ['#baz']]], '#zap']);
  502. $this->assertEquals(['#foo', ['#bar', ['#baz']], '#zap'], $c->flatten(1)->all());
  503. $c = new Collection([['#foo', ['#bar', ['#baz']]], '#zap']);
  504. $this->assertEquals(['#foo', '#bar', ['#baz'], '#zap'], $c->flatten(2)->all());
  505. }
  506. public function testFlattenIgnoresKeys()
  507. {
  508. // No depth ignores keys
  509. $c = new Collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
  510. $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
  511. // Depth of 1 ignores keys
  512. $c = new Collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
  513. $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten(1)->all());
  514. }
  515. public function testMergeNull()
  516. {
  517. $c = new Collection(['name' => 'Hello']);
  518. $this->assertEquals(['name' => 'Hello'], $c->merge(null)->all());
  519. }
  520. public function testMergeArray()
  521. {
  522. $c = new Collection(['name' => 'Hello']);
  523. $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->merge(['id' => 1])->all());
  524. }
  525. public function testMergeCollection()
  526. {
  527. $c = new Collection(['name' => 'Hello']);
  528. $this->assertEquals(['name' => 'World', 'id' => 1], $c->merge(new Collection(['name' => 'World', 'id' => 1]))->all());
  529. }
  530. public function testUnionNull()
  531. {
  532. $c = new Collection(['name' => 'Hello']);
  533. $this->assertEquals(['name' => 'Hello'], $c->union(null)->all());
  534. }
  535. public function testUnionArray()
  536. {
  537. $c = new Collection(['name' => 'Hello']);
  538. $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(['id' => 1])->all());
  539. }
  540. public function testUnionCollection()
  541. {
  542. $c = new Collection(['name' => 'Hello']);
  543. $this->assertEquals(['name' => 'Hello', 'id' => 1], $c->union(new Collection(['name' => 'World', 'id' => 1]))->all());
  544. }
  545. public function testDiffCollection()
  546. {
  547. $c = new Collection(['id' => 1, 'first_word' => 'Hello']);
  548. $this->assertEquals(['id' => 1], $c->diff(new Collection(['first_word' => 'Hello', 'last_word' => 'World']))->all());
  549. }
  550. public function testDiffUsingWithCollection()
  551. {
  552. $c = new Collection(['en_GB', 'fr', 'HR']);
  553. // demonstrate that diffKeys wont support case insensitivity
  554. $this->assertEquals(['en_GB', 'fr', 'HR'], $c->diff(new Collection(['en_gb', 'hr']))->values()->toArray());
  555. // allow for case insensitive difference
  556. $this->assertEquals(['fr'], $c->diffUsing(new Collection(['en_gb', 'hr']), 'strcasecmp')->values()->toArray());
  557. }
  558. public function testDiffUsingWithNull()
  559. {
  560. $c = new Collection(['en_GB', 'fr', 'HR']);
  561. $this->assertEquals(['en_GB', 'fr', 'HR'], $c->diffUsing(null, 'strcasecmp')->values()->toArray());
  562. }
  563. public function testDiffNull()
  564. {
  565. $c = new Collection(['id' => 1, 'first_word' => 'Hello']);
  566. $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c->diff(null)->all());
  567. }
  568. public function testDiffKeys()
  569. {
  570. $c1 = new Collection(['id' => 1, 'first_word' => 'Hello']);
  571. $c2 = new Collection(['id' => 123, 'foo_bar' => 'Hello']);
  572. $this->assertEquals(['first_word' => 'Hello'], $c1->diffKeys($c2)->all());
  573. }
  574. public function testDiffKeysUsing()
  575. {
  576. $c1 = new Collection(['id' => 1, 'first_word' => 'Hello']);
  577. $c2 = new Collection(['ID' => 123, 'foo_bar' => 'Hello']);
  578. // demonstrate that diffKeys wont support case insensitivity
  579. $this->assertEquals(['id'=>1, 'first_word'=> 'Hello'], $c1->diffKeys($c2)->all());
  580. // allow for case insensitive difference
  581. $this->assertEquals(['first_word' => 'Hello'], $c1->diffKeysUsing($c2, 'strcasecmp')->all());
  582. }
  583. public function testDiffAssoc()
  584. {
  585. $c1 = new Collection(['id' => 1, 'first_word' => 'Hello', 'not_affected' => 'value']);
  586. $c2 = new Collection(['id' => 123, 'foo_bar' => 'Hello', 'not_affected' => 'value']);
  587. $this->assertEquals(['id' => 1, 'first_word' => 'Hello'], $c1->diffAssoc($c2)->all());
  588. }
  589. public function testDiffAssocUsing()
  590. {
  591. $c1 = new Collection(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red']);
  592. $c2 = new Collection(['A' => 'green', 'yellow', 'red']);
  593. // demonstrate that the case of the keys will affect the output when diffAssoc is used
  594. $this->assertEquals(['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssoc($c2)->all());
  595. // allow for case insensitive difference
  596. $this->assertEquals(['b' => 'brown', 'c' => 'blue', 'red'], $c1->diffAssocUsing($c2, 'strcasecmp')->all());
  597. }
  598. public function testEach()
  599. {
  600. $c = new Collection($original = [1, 2, 'foo' => 'bar', 'bam' => 'baz']);
  601. $result = [];
  602. $c->each(function ($item, $key) use (&$result) {
  603. $result[$key] = $item;
  604. });
  605. $this->assertEquals($original, $result);
  606. $result = [];
  607. $c->each(function ($item, $key) use (&$result) {
  608. $result[$key] = $item;
  609. if (is_string($key)) {
  610. return false;
  611. }
  612. });
  613. $this->assertEquals([1, 2, 'foo' => 'bar'], $result);
  614. }
  615. public function testEachSpread()
  616. {
  617. $c = new Collection([[1, 'a'], [2, 'b']]);
  618. $result = [];
  619. $c->eachSpread(function ($number, $character) use (&$result) {
  620. $result[] = [$number, $character];
  621. });
  622. $this->assertEquals($c->all(), $result);
  623. $result = [];
  624. $c->eachSpread(function ($number, $character) use (&$result) {
  625. $result[] = [$number, $character];
  626. return false;
  627. });
  628. $this->assertEquals([[1, 'a']], $result);
  629. $result = [];
  630. $c->eachSpread(function ($number, $character, $key) use (&$result) {
  631. $result[] = [$number, $character, $key];
  632. });
  633. $this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
  634. $c = new Collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
  635. $result = [];
  636. $c->eachSpread(function ($number, $character, $key) use (&$result) {
  637. $result[] = [$number, $character, $key];
  638. });
  639. $this->assertEquals([[1, 'a', 0], [2, 'b', 1]], $result);
  640. }
  641. public function testIntersectNull()
  642. {
  643. $c = new Collection(['id' => 1, 'first_word' => 'Hello']);
  644. $this->assertEquals([], $c->intersect(null)->all());
  645. }
  646. public function testIntersectCollection()
  647. {
  648. $c = new Collection(['id' => 1, 'first_word' => 'Hello']);
  649. $this->assertEquals(['first_word' => 'Hello'], $c->intersect(new Collection(['first_world' => 'Hello', 'last_word' => 'World']))->all());
  650. }
  651. public function testIntersectByKeysNull()
  652. {
  653. $c = new Collection(['name' => 'Mateus', 'age' => 18]);
  654. $this->assertEquals([], $c->intersectByKeys(null)->all());
  655. }
  656. public function testIntersectByKeys()
  657. {
  658. $c = new Collection(['name' => 'Mateus', 'age' => 18]);
  659. $this->assertEquals(['name' => 'Mateus'], $c->intersectByKeys(new Collection(['name' => 'Mateus', 'surname' => 'Guimaraes']))->all());
  660. }
  661. public function testUnique()
  662. {
  663. $c = new Collection(['Hello', 'World', 'World']);
  664. $this->assertEquals(['Hello', 'World'], $c->unique()->all());
  665. $c = new Collection([[1, 2], [1, 2], [2, 3], [3, 4], [2, 3]]);
  666. $this->assertEquals([[1, 2], [2, 3], [3, 4]], $c->unique()->values()->all());
  667. }
  668. public function testUniqueWithCallback()
  669. {
  670. $c = new Collection([
  671. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  672. 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'],
  673. 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
  674. 4 => ['id' => 4, 'first' => 'Abigail', 'last' => 'Otwell'],
  675. 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'],
  676. 6 => ['id' => 6, 'first' => 'Taylor', 'last' => 'Swift'],
  677. ]);
  678. $this->assertEquals([
  679. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  680. 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
  681. ], $c->unique('first')->all());
  682. $this->assertEquals([
  683. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  684. 3 => ['id' => 3, 'first' => 'Abigail', 'last' => 'Otwell'],
  685. 5 => ['id' => 5, 'first' => 'Taylor', 'last' => 'Swift'],
  686. ], $c->unique(function ($item) {
  687. return $item['first'].$item['last'];
  688. })->all());
  689. $this->assertEquals([
  690. 1 => ['id' => 1, 'first' => 'Taylor', 'last' => 'Otwell'],
  691. 2 => ['id' => 2, 'first' => 'Taylor', 'last' => 'Otwell'],
  692. ], $c->unique(function ($item, $key) {
  693. return $key % 2;
  694. })->all());
  695. }
  696. public function testUniqueStrict()
  697. {
  698. $c = new Collection([
  699. [
  700. 'id' => '0',
  701. 'name' => 'zero',
  702. ],
  703. [
  704. 'id' => '00',
  705. 'name' => 'double zero',
  706. ],
  707. [
  708. 'id' => '0',
  709. 'name' => 'again zero',
  710. ],
  711. ]);
  712. $this->assertEquals([
  713. [
  714. 'id' => '0',
  715. 'name' => 'zero',
  716. ],
  717. [
  718. 'id' => '00',
  719. 'name' => 'double zero',
  720. ],
  721. ], $c->uniqueStrict('id')->all());
  722. }
  723. public function testCollapse()
  724. {
  725. $data = new Collection([[$object1 = new stdClass], [$object2 = new stdClass]]);
  726. $this->assertEquals([$object1, $object2], $data->collapse()->all());
  727. }
  728. public function testCollapseWithNestedCollections()
  729. {
  730. $data = new Collection([new Collection([1, 2, 3]), new Collection([4, 5, 6])]);
  731. $this->assertEquals([1, 2, 3, 4, 5, 6], $data->collapse()->all());
  732. }
  733. public function testCrossJoin()
  734. {
  735. // Cross join with an array
  736. $this->assertEquals(
  737. [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
  738. (new Collection([1, 2]))->crossJoin(['a', 'b'])->all()
  739. );
  740. // Cross join with a collection
  741. $this->assertEquals(
  742. [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
  743. (new Collection([1, 2]))->crossJoin(new Collection(['a', 'b']))->all()
  744. );
  745. // Cross join with 2 collections
  746. $this->assertEquals(
  747. [
  748. [1, 'a', 'I'], [1, 'a', 'II'],
  749. [1, 'b', 'I'], [1, 'b', 'II'],
  750. [2, 'a', 'I'], [2, 'a', 'II'],
  751. [2, 'b', 'I'], [2, 'b', 'II'],
  752. ],
  753. (new Collection([1, 2]))->crossJoin(
  754. new Collection(['a', 'b']),
  755. new Collection(['I', 'II'])
  756. )->all()
  757. );
  758. }
  759. public function testSort()
  760. {
  761. $data = (new Collection([5, 3, 1, 2, 4]))->sort();
  762. $this->assertEquals([1, 2, 3, 4, 5], $data->values()->all());
  763. $data = (new Collection([-1, -3, -2, -4, -5, 0, 5, 3, 1, 2, 4]))->sort();
  764. $this->assertEquals([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], $data->values()->all());
  765. $data = (new Collection(['foo', 'bar-10', 'bar-1']))->sort();
  766. $this->assertEquals(['bar-1', 'bar-10', 'foo'], $data->values()->all());
  767. }
  768. public function testSortWithCallback()
  769. {
  770. $data = (new Collection([5, 3, 1, 2, 4]))->sort(function ($a, $b) {
  771. if ($a === $b) {
  772. return 0;
  773. }
  774. return ($a < $b) ? -1 : 1;
  775. });
  776. $this->assertEquals(range(1, 5), array_values($data->all()));
  777. }
  778. public function testSortBy()
  779. {
  780. $data = new Collection(['taylor', 'dayle']);
  781. $data = $data->sortBy(function ($x) {
  782. return $x;
  783. });
  784. $this->assertEquals(['dayle', 'taylor'], array_values($data->all()));
  785. $data = new Collection(['dayle', 'taylor']);
  786. $data = $data->sortByDesc(function ($x) {
  787. return $x;
  788. });
  789. $this->assertEquals(['taylor', 'dayle'], array_values($data->all()));
  790. }
  791. public function testSortByString()
  792. {
  793. $data = new Collection([['name' => 'taylor'], ['name' => 'dayle']]);
  794. $data = $data->sortBy('name', SORT_STRING);
  795. $this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
  796. $data = new Collection([['name' => 'taylor'], ['name' => 'dayle']]);
  797. $data = $data->sortBy('name', SORT_STRING);
  798. $this->assertEquals([['name' => 'dayle'], ['name' => 'taylor']], array_values($data->all()));
  799. }
  800. public function testSortByAlwaysReturnsAssoc()
  801. {
  802. $data = new Collection(['a' => 'taylor', 'b' => 'dayle']);
  803. $data = $data->sortBy(function ($x) {
  804. return $x;
  805. });
  806. $this->assertEquals(['b' => 'dayle', 'a' => 'taylor'], $data->all());
  807. $data = new Collection(['taylor', 'dayle']);
  808. $data = $data->sortBy(function ($x) {
  809. return $x;
  810. });
  811. $this->assertEquals([1 => 'dayle', 0 => 'taylor'], $data->all());
  812. }
  813. public function testSortKeys()
  814. {
  815. $data = new Collection(['b' => 'dayle', 'a' => 'taylor']);
  816. $this->assertEquals(['a' => 'taylor', 'b' => 'dayle'], $data->sortKeys()->all());
  817. }
  818. public function testSortKeysDesc()
  819. {
  820. $data = new Collection(['a' => 'taylor', 'b' => 'dayle']);
  821. $this->assertEquals(['b' => 'dayle', 'a' => 'taylor'], $data->sortKeys()->all());
  822. }
  823. public function testReverse()
  824. {
  825. $data = new Collection(['zaeed', 'alan']);
  826. $reversed = $data->reverse();
  827. $this->assertSame([1 => 'alan', 0 => 'zaeed'], $reversed->all());
  828. $data = new Collection(['name' => 'taylor', 'framework' => 'laravel']);
  829. $reversed = $data->reverse();
  830. $this->assertSame(['framework' => 'laravel', 'name' => 'taylor'], $reversed->all());
  831. }
  832. public function testFlip()
  833. {
  834. $data = new Collection(['name' => 'taylor', 'framework' => 'laravel']);
  835. $this->assertEquals(['taylor' => 'name', 'laravel' => 'framework'], $data->flip()->toArray());
  836. }
  837. public function testChunk()
  838. {
  839. $data = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  840. $data = $data->chunk(3);
  841. $this->assertInstanceOf(Collection::class, $data);
  842. $this->assertInstanceOf(Collection::class, $data[0]);
  843. $this->assertCount(4, $data);
  844. $this->assertEquals([1, 2, 3], $data[0]->toArray());
  845. $this->assertEquals([9 => 10], $data[3]->toArray());
  846. }
  847. public function testChunkWhenGivenZeroAsSize()
  848. {
  849. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  850. $this->assertEquals(
  851. [],
  852. $collection->chunk(0)->toArray()
  853. );
  854. }
  855. public function testChunkWhenGivenLessThanZero()
  856. {
  857. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  858. $this->assertEquals(
  859. [],
  860. $collection->chunk(-1)->toArray()
  861. );
  862. }
  863. public function testEvery()
  864. {
  865. $c = new Collection([]);
  866. $this->assertTrue($c->every('key', 'value'));
  867. $this->assertTrue($c->every(function () {
  868. return false;
  869. }));
  870. $c = new Collection([['age' => 18], ['age' => 20], ['age' => 20]]);
  871. $this->assertFalse($c->every('age', 18));
  872. $this->assertTrue($c->every('age', '>=', 18));
  873. $this->assertTrue($c->every(function ($item) {
  874. return $item['age'] >= 18;
  875. }));
  876. $this->assertFalse($c->every(function ($item) {
  877. return $item['age'] >= 20;
  878. }));
  879. $c = new Collection([null, null]);
  880. $this->assertTrue($c->every(function ($item) {
  881. return $item === null;
  882. }));
  883. $c = new Collection([['active' => true], ['active' => true]]);
  884. $this->assertTrue($c->every('active'));
  885. $this->assertTrue($c->every->active);
  886. $this->assertFalse($c->push(['active' => false])->every->active);
  887. }
  888. public function testExcept()
  889. {
  890. $data = new Collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']);
  891. $this->assertEquals(['first' => 'Taylor'], $data->except(['last', 'email', 'missing'])->all());
  892. $this->assertEquals(['first' => 'Taylor'], $data->except('last', 'email', 'missing')->all());
  893. $this->assertEquals(['first' => 'Taylor'], $data->except(collect(['last', 'email', 'missing']))->all());
  894. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except(['last'])->all());
  895. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->except('last')->all());
  896. }
  897. public function testExceptSelf()
  898. {
  899. $data = new Collection(['first' => 'Taylor', 'last' => 'Otwell']);
  900. $this->assertEquals(['first' => 'Taylor', 'last' => 'Otwell'], $data->except($data)->all());
  901. }
  902. public function testPluckWithArrayAndObjectValues()
  903. {
  904. $data = new Collection([(object) ['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]);
  905. $this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], $data->pluck('email', 'name')->all());
  906. $this->assertEquals(['foo', 'bar'], $data->pluck('email')->all());
  907. }
  908. public function testPluckWithArrayAccessValues()
  909. {
  910. $data = new Collection([
  911. new TestArrayAccessImplementation(['name' => 'taylor', 'email' => 'foo']),
  912. new TestArrayAccessImplementation(['name' => 'dayle', 'email' => 'bar']),
  913. ]);
  914. $this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], $data->pluck('email', 'name')->all());
  915. $this->assertEquals(['foo', 'bar'], $data->pluck('email')->all());
  916. }
  917. public function testHas()
  918. {
  919. $data = new Collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
  920. $this->assertTrue($data->has('first'));
  921. $this->assertFalse($data->has('third'));
  922. $this->assertTrue($data->has(['first', 'second']));
  923. $this->assertFalse($data->has(['third', 'first']));
  924. }
  925. public function testImplode()
  926. {
  927. $data = new Collection([['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]);
  928. $this->assertEquals('foobar', $data->implode('email'));
  929. $this->assertEquals('foo,bar', $data->implode('email', ','));
  930. $data = new Collection(['taylor', 'dayle']);
  931. $this->assertEquals('taylordayle', $data->implode(''));
  932. $this->assertEquals('taylor,dayle', $data->implode(','));
  933. }
  934. public function testTake()
  935. {
  936. $data = new Collection(['taylor', 'dayle', 'shawn']);
  937. $data = $data->take(2);
  938. $this->assertEquals(['taylor', 'dayle'], $data->all());
  939. }
  940. public function testPut()
  941. {
  942. $data = new Collection(['name' => 'taylor', 'email' => 'foo']);
  943. $data = $data->put('name', 'dayle');
  944. $this->assertEquals(['name' => 'dayle', 'email' => 'foo'], $data->all());
  945. }
  946. public function testPutWithNoKey()
  947. {
  948. $data = new Collection(['taylor', 'shawn']);
  949. $data = $data->put(null, 'dayle');
  950. $this->assertEquals(['taylor', 'shawn', 'dayle'], $data->all());
  951. }
  952. public function testRandom()
  953. {
  954. $data = new Collection([1, 2, 3, 4, 5, 6]);
  955. $random = $data->random();
  956. $this->assertIsInt($random);
  957. $this->assertContains($random, $data->all());
  958. $random = $data->random(0);
  959. $this->assertInstanceOf(Collection::class, $random);
  960. $this->assertCount(0, $random);
  961. $random = $data->random(1);
  962. $this->assertInstanceOf(Collection::class, $random);
  963. $this->assertCount(1, $random);
  964. $random = $data->random(2);
  965. $this->assertInstanceOf(Collection::class, $random);
  966. $this->assertCount(2, $random);
  967. $random = $data->random('0');
  968. $this->assertInstanceOf(Collection::class, $random);
  969. $this->assertCount(0, $random);
  970. $random = $data->random('1');
  971. $this->assertInstanceOf(Collection::class, $random);
  972. $this->assertCount(1, $random);
  973. $random = $data->random('2');
  974. $this->assertInstanceOf(Collection::class, $random);
  975. $this->assertCount(2, $random);
  976. }
  977. public function testRandomOnEmptyCollection()
  978. {
  979. $data = new Collection;
  980. $random = $data->random(0);
  981. $this->assertInstanceOf(Collection::class, $random);
  982. $this->assertCount(0, $random);
  983. $random = $data->random('0');
  984. $this->assertInstanceOf(Collection::class, $random);
  985. $this->assertCount(0, $random);
  986. }
  987. public function testTakeLast()
  988. {
  989. $data = new Collection(['taylor', 'dayle', 'shawn']);
  990. $data = $data->take(-2);
  991. $this->assertEquals([1 => 'dayle', 2 => 'shawn'], $data->all());
  992. }
  993. public function testMacroable()
  994. {
  995. // Foo() macro : unique values starting with A
  996. Collection::macro('foo', function () {
  997. return $this->filter(function ($item) {
  998. return strpos($item, 'a') === 0;
  999. })
  1000. ->unique()
  1001. ->values();
  1002. });
  1003. $c = new Collection(['a', 'a', 'aa', 'aaa', 'bar']);
  1004. $this->assertSame(['a', 'aa', 'aaa'], $c->foo()->all());
  1005. }
  1006. public function testCanAddMethodsToProxy()
  1007. {
  1008. Collection::macro('adults', function ($callback) {
  1009. return $this->filter(function ($item) use ($callback) {
  1010. return $callback($item) >= 18;
  1011. });
  1012. });
  1013. Collection::proxy('adults');
  1014. $c = new Collection([['age' => 3], ['age' => 12], ['age' => 18], ['age' => 56]]);
  1015. $this->assertSame([['age' => 18], ['age' => 56]], $c->adults->age->values()->all());
  1016. }
  1017. public function testMakeMethod()
  1018. {
  1019. $collection = Collection::make('foo');
  1020. $this->assertEquals(['foo'], $collection->all());
  1021. }
  1022. public function testMakeMethodFromNull()
  1023. {
  1024. $collection = Collection::make(null);
  1025. $this->assertEquals([], $collection->all());
  1026. $collection = Collection::make();
  1027. $this->assertEquals([], $collection->all());
  1028. }
  1029. public function testMakeMethodFromCollection()
  1030. {
  1031. $firstCollection = Collection::make(['foo' => 'bar']);
  1032. $secondCollection = Collection::make($firstCollection);
  1033. $this->assertEquals(['foo' => 'bar'], $secondCollection->all());
  1034. }
  1035. public function testMakeMethodFromArray()
  1036. {
  1037. $collection = Collection::make(['foo' => 'bar']);
  1038. $this->assertEquals(['foo' => 'bar'], $collection->all());
  1039. }
  1040. public function testWrapWithScalar()
  1041. {
  1042. $collection = Collection::wrap('foo');
  1043. $this->assertEquals(['foo'], $collection->all());
  1044. }
  1045. public function testWrapWithArray()
  1046. {
  1047. $collection = Collection::wrap(['foo']);
  1048. $this->assertEquals(['foo'], $collection->all());
  1049. }
  1050. public function testWrapWithArrayable()
  1051. {
  1052. $collection = Collection::wrap($o = new TestArrayableObject);
  1053. $this->assertEquals([$o], $collection->all());
  1054. }
  1055. public function testWrapWithJsonable()
  1056. {
  1057. $collection = Collection::wrap($o = new TestJsonableObject);
  1058. $this->assertEquals([$o], $collection->all());
  1059. }
  1060. public function testWrapWithJsonSerialize()
  1061. {
  1062. $collection = Collection::wrap($o = new TestJsonSerializeObject);
  1063. $this->assertEquals([$o], $collection->all());
  1064. }
  1065. public function testWrapWithCollectionClass()
  1066. {
  1067. $collection = Collection::wrap(Collection::make(['foo']));
  1068. $this->assertEquals(['foo'], $collection->all());
  1069. }
  1070. public function testWrapWithCollectionSubclass()
  1071. {
  1072. $collection = TestCollectionSubclass::wrap(Collection::make(['foo']));
  1073. $this->assertEquals(['foo'], $collection->all());
  1074. $this->assertInstanceOf(TestCollectionSubclass::class, $collection);
  1075. }
  1076. public function testUnwrapCollection()
  1077. {
  1078. $collection = new Collection(['foo']);
  1079. $this->assertEquals(['foo'], Collection::unwrap($collection));
  1080. }
  1081. public function testUnwrapCollectionWithArray()
  1082. {
  1083. $this->assertEquals(['foo'], Collection::unwrap(['foo']));
  1084. }
  1085. public function testUnwrapCollectionWithScalar()
  1086. {
  1087. $this->assertEquals('foo', Collection::unwrap('foo'));
  1088. }
  1089. public function testTimesMethod()
  1090. {
  1091. $two = Collection::times(2, function ($number) {
  1092. return 'slug-'.$number;
  1093. });
  1094. $zero = Collection::times(0, function ($number) {
  1095. return 'slug-'.$number;
  1096. });
  1097. $negative = Collection::times(-4, function ($number) {
  1098. return 'slug-'.$number;
  1099. });
  1100. $range = Collection::times(5);
  1101. $this->assertEquals(['slug-1', 'slug-2'], $two->all());
  1102. $this->assertTrue($zero->isEmpty());
  1103. $this->assertTrue($negative->isEmpty());
  1104. $this->assertEquals(range(1, 5), $range->all());
  1105. }
  1106. public function testConstructMakeFromObject()
  1107. {
  1108. $object = new stdClass;
  1109. $object->foo = 'bar';
  1110. $collection = Collection::make($object);
  1111. $this->assertEquals(['foo' => 'bar'], $collection->all());
  1112. }
  1113. public function testConstructMethod()
  1114. {
  1115. $collection = new Collection('foo');
  1116. $this->assertEquals(['foo'], $collection->all());
  1117. }
  1118. public function testConstructMethodFromNull()
  1119. {
  1120. $collection = new Collection(null);
  1121. $this->assertEquals([], $collection->all());
  1122. $collection = new Collection;
  1123. $this->assertEquals([], $collection->all());
  1124. }
  1125. public function testConstructMethodFromCollection()
  1126. {
  1127. $firstCollection = new Collection(['foo' => 'bar']);
  1128. $secondCollection = new Collection($firstCollection);
  1129. $this->assertEquals(['foo' => 'bar'], $secondCollection->all());
  1130. }
  1131. public function testConstructMethodFromArray()
  1132. {
  1133. $collection = new Collection(['foo' => 'bar']);
  1134. $this->assertEquals(['foo' => 'bar'], $collection->all());
  1135. }
  1136. public function testConstructMethodFromObject()
  1137. {
  1138. $object = new stdClass;
  1139. $object->foo = 'bar';
  1140. $collection = new Collection($object);
  1141. $this->assertEquals(['foo' => 'bar'], $collection->all());
  1142. }
  1143. public function testSplice()
  1144. {
  1145. $data = new Collection(['foo', 'baz']);
  1146. $data->splice(1);
  1147. $this->assertEquals(['foo'], $data->all());
  1148. $data = new Collection(['foo', 'baz']);
  1149. $data->splice(1, 0, 'bar');
  1150. $this->assertEquals(['foo', 'bar', 'baz'], $data->all());
  1151. $data = new Collection(['foo', 'baz']);
  1152. $data->splice(1, 1);
  1153. $this->assertEquals(['foo'], $data->all());
  1154. $data = new Collection(['foo', 'baz']);
  1155. $cut = $data->splice(1, 1, 'bar');
  1156. $this->assertEquals(['foo', 'bar'], $data->all());
  1157. $this->assertEquals(['baz'], $cut->all());
  1158. }
  1159. public function testGetPluckValueWithAccessors()
  1160. {
  1161. $model = new TestAccessorEloquentTestStub(['some' => 'foo']);
  1162. $modelTwo = new TestAccessorEloquentTestStub(['some' => 'bar']);
  1163. $data = new Collection([$model, $modelTwo]);
  1164. $this->assertEquals(['foo', 'bar'], $data->pluck('some')->all());
  1165. }
  1166. public function testMap()
  1167. {
  1168. $data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
  1169. $data = $data->map(function ($item, $key) {
  1170. return $key.'-'.strrev($item);
  1171. });
  1172. $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
  1173. }
  1174. public function testMapSpread()
  1175. {
  1176. $c = new Collection([[1, 'a'], [2, 'b']]);
  1177. $result = $c->mapSpread(function ($number, $character) {
  1178. return "{$number}-{$character}";
  1179. });
  1180. $this->assertEquals(['1-a', '2-b'], $result->all());
  1181. $result = $c->mapSpread(function ($number, $character, $key) {
  1182. return "{$number}-{$character}-{$key}";
  1183. });
  1184. $this->assertEquals(['1-a-0', '2-b-1'], $result->all());
  1185. $c = new Collection([new Collection([1, 'a']), new Collection([2, 'b'])]);
  1186. $result = $c->mapSpread(function ($number, $character, $key) {
  1187. return "{$number}-{$character}-{$key}";
  1188. });
  1189. $this->assertEquals(['1-a-0', '2-b-1'], $result->all());
  1190. }
  1191. public function testFlatMap()
  1192. {
  1193. $data = new Collection([
  1194. ['name' => 'taylor', 'hobbies' => ['programming', 'basketball']],
  1195. ['name' => 'adam', 'hobbies' => ['music', 'powerlifting']],
  1196. ]);
  1197. $data = $data->flatMap(function ($person) {
  1198. return $person['hobbies'];
  1199. });
  1200. $this->assertEquals(['programming', 'basketball', 'music', 'powerlifting'], $data->all());
  1201. }
  1202. public function testMapToDictionary()
  1203. {
  1204. $data = new Collection([
  1205. ['id' => 1, 'name' => 'A'],
  1206. ['id' => 2, 'name' => 'B'],
  1207. ['id' => 3, 'name' => 'C'],
  1208. ['id' => 4, 'name' => 'B'],
  1209. ]);
  1210. $groups = $data->mapToDictionary(function ($item, $key) {
  1211. return [$item['name'] => $item['id']];
  1212. });
  1213. $this->assertInstanceOf(Collection::class, $groups);
  1214. $this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
  1215. $this->assertIsArray($groups['A']);
  1216. }
  1217. public function testMapToDictionaryWithNumericKeys()
  1218. {
  1219. $data = new Collection([1, 2, 3, 2, 1]);
  1220. $groups = $data->mapToDictionary(function ($item, $key) {
  1221. return [$item => $key];
  1222. });
  1223. $this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
  1224. }
  1225. public function testMapToGroups()
  1226. {
  1227. $data = new Collection([
  1228. ['id' => 1, 'name' => 'A'],
  1229. ['id' => 2, 'name' => 'B'],
  1230. ['id' => 3, 'name' => 'C'],
  1231. ['id' => 4, 'name' => 'B'],
  1232. ]);
  1233. $groups = $data->mapToGroups(function ($item, $key) {
  1234. return [$item['name'] => $item['id']];
  1235. });
  1236. $this->assertInstanceOf(Collection::class, $groups);
  1237. $this->assertEquals(['A' => [1], 'B' => [2, 4], 'C' => [3]], $groups->toArray());
  1238. $this->assertInstanceOf(Collection::class, $groups['A']);
  1239. }
  1240. public function testMapToGroupsWithNumericKeys()
  1241. {
  1242. $data = new Collection([1, 2, 3, 2, 1]);
  1243. $groups = $data->mapToGroups(function ($item, $key) {
  1244. return [$item => $key];
  1245. });
  1246. $this->assertEquals([1 => [0, 4], 2 => [1, 3], 3 => [2]], $groups->toArray());
  1247. }
  1248. public function testMapWithKeys()
  1249. {
  1250. $data = new Collection([
  1251. ['name' => 'Blastoise', 'type' => 'Water', 'idx' => 9],
  1252. ['name' => 'Charmander', 'type' => 'Fire', 'idx' => 4],
  1253. ['name' => 'Dragonair', 'type' => 'Dragon', 'idx' => 148],
  1254. ]);
  1255. $data = $data->mapWithKeys(function ($pokemon) {
  1256. return [$pokemon['name'] => $pokemon['type']];
  1257. });
  1258. $this->assertEquals(
  1259. ['Blastoise' => 'Water', 'Charmander' => 'Fire', 'Dragonair' => 'Dragon'],
  1260. $data->all()
  1261. );
  1262. }
  1263. public function testMapWithKeysIntegerKeys()
  1264. {
  1265. $data = new Collection([
  1266. ['id' => 1, 'name' => 'A'],
  1267. ['id' => 3, 'name' => 'B'],
  1268. ['id' => 2, 'name' => 'C'],
  1269. ]);
  1270. $data = $data->mapWithKeys(function ($item) {
  1271. return [$item['id'] => $item];
  1272. });
  1273. $this->assertSame(
  1274. [1, 3, 2],
  1275. $data->keys()->all()
  1276. );
  1277. }
  1278. public function testMapWithKeysMultipleRows()
  1279. {
  1280. $data = new Collection([
  1281. ['id' => 1, 'name' => 'A'],
  1282. ['id' => 2, 'name' => 'B'],
  1283. ['id' => 3, 'name' => 'C'],
  1284. ]);
  1285. $data = $data->mapWithKeys(function ($item) {
  1286. return [$item['id'] => $item['name'], $item['name'] => $item['id']];
  1287. });
  1288. $this->assertSame(
  1289. [
  1290. 1 => 'A',
  1291. 'A' => 1,
  1292. 2 => 'B',
  1293. 'B' => 2,
  1294. 3 => 'C',
  1295. 'C' => 3,
  1296. ],
  1297. $data->all()
  1298. );
  1299. }
  1300. public function testMapWithKeysCallbackKey()
  1301. {
  1302. $data = new Collection([
  1303. 3 => ['id' => 1, 'name' => 'A'],
  1304. 5 => ['id' => 3, 'name' => 'B'],
  1305. 4 => ['id' => 2, 'name' => 'C'],
  1306. ]);
  1307. $data = $data->mapWithKeys(function ($item, $key) {
  1308. return [$key => $item['id']];
  1309. });
  1310. $this->assertSame(
  1311. [3, 5, 4],
  1312. $data->keys()->all()
  1313. );
  1314. }
  1315. public function testMapInto()
  1316. {
  1317. $data = new Collection([
  1318. 'first', 'second',
  1319. ]);
  1320. $data = $data->mapInto(TestCollectionMapIntoObject::class);
  1321. $this->assertEquals('first', $data[0]->value);
  1322. $this->assertEquals('second', $data[1]->value);
  1323. }
  1324. public function testNth()
  1325. {
  1326. $data = new Collection([
  1327. 6 => 'a',
  1328. 4 => 'b',
  1329. 7 => 'c',
  1330. 1 => 'd',
  1331. 5 => 'e',
  1332. 3 => 'f',
  1333. ]);
  1334. $this->assertEquals(['a', 'e'], $data->nth(4)->all());
  1335. $this->assertEquals(['b', 'f'], $data->nth(4, 1)->all());
  1336. $this->assertEquals(['c'], $data->nth(4, 2)->all());
  1337. $this->assertEquals(['d'], $data->nth(4, 3)->all());
  1338. }
  1339. public function testMapWithKeysOverwritingKeys()
  1340. {
  1341. $data = new Collection([
  1342. ['id' => 1, 'name' => 'A'],
  1343. ['id' => 2, 'name' => 'B'],
  1344. ['id' => 1, 'name' => 'C'],
  1345. ]);
  1346. $data = $data->mapWithKeys(function ($item) {
  1347. return [$item['id'] => $item['name']];
  1348. });
  1349. $this->assertSame(
  1350. [
  1351. 1 => 'C',
  1352. 2 => 'B',
  1353. ],
  1354. $data->all()
  1355. );
  1356. }
  1357. public function testTransform()
  1358. {
  1359. $data = new Collection(['first' => 'taylor', 'last' => 'otwell']);
  1360. $data->transform(function ($item, $key) {
  1361. return $key.'-'.strrev($item);
  1362. });
  1363. $this->assertEquals(['first' => 'first-rolyat', 'last' => 'last-llewto'], $data->all());
  1364. }
  1365. public function testGroupByAttribute()
  1366. {
  1367. $data = new Collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
  1368. $result = $data->groupBy('rating');
  1369. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  1370. $result = $data->groupBy('url');
  1371. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  1372. }
  1373. public function testGroupByAttributePreservingKeys()
  1374. {
  1375. $data = new Collection([10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1'], 30 => ['rating' => 2, 'url' => '2']]);
  1376. $result = $data->groupBy('rating', true);
  1377. $expected_result = [
  1378. 1 => [10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1']],
  1379. 2 => [30 => ['rating' => 2, 'url' => '2']],
  1380. ];
  1381. $this->assertEquals($expected_result, $result->toArray());
  1382. }
  1383. public function testGroupByClosureWhereItemsHaveSingleGroup()
  1384. {
  1385. $data = new Collection([['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1'], ['rating' => 2, 'url' => '2']]);
  1386. $result = $data->groupBy(function ($item) {
  1387. return $item['rating'];
  1388. });
  1389. $this->assertEquals([1 => [['rating' => 1, 'url' => '1'], ['rating' => 1, 'url' => '1']], 2 => [['rating' => 2, 'url' => '2']]], $result->toArray());
  1390. }
  1391. public function testGroupByClosureWhereItemsHaveSingleGroupPreservingKeys()
  1392. {
  1393. $data = new Collection([10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1'], 30 => ['rating' => 2, 'url' => '2']]);
  1394. $result = $data->groupBy(function ($item) {
  1395. return $item['rating'];
  1396. }, true);
  1397. $expected_result = [
  1398. 1 => [10 => ['rating' => 1, 'url' => '1'], 20 => ['rating' => 1, 'url' => '1']],
  1399. 2 => [30 => ['rating' => 2, 'url' => '2']],
  1400. ];
  1401. $this->assertEquals($expected_result, $result->toArray());
  1402. }
  1403. public function testGroupByClosureWhereItemsHaveMultipleGroups()
  1404. {
  1405. $data = new Collection([
  1406. ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  1407. ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  1408. ['user' => 3, 'roles' => ['Role_1']],
  1409. ]);
  1410. $result = $data->groupBy(function ($item) {
  1411. return $item['roles'];
  1412. });
  1413. $expected_result = [
  1414. 'Role_1' => [
  1415. ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  1416. ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  1417. ['user' => 3, 'roles' => ['Role_1']],
  1418. ],
  1419. 'Role_2' => [
  1420. ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  1421. ],
  1422. 'Role_3' => [
  1423. ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  1424. ],
  1425. ];
  1426. $this->assertEquals($expected_result, $result->toArray());
  1427. }
  1428. public function testGroupByClosureWhereItemsHaveMultipleGroupsPreservingKeys()
  1429. {
  1430. $data = new Collection([
  1431. 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  1432. 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  1433. 30 => ['user' => 3, 'roles' => ['Role_1']],
  1434. ]);
  1435. $result = $data->groupBy(function ($item) {
  1436. return $item['roles'];
  1437. }, true);
  1438. $expected_result = [
  1439. 'Role_1' => [
  1440. 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  1441. 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  1442. 30 => ['user' => 3, 'roles' => ['Role_1']],
  1443. ],
  1444. 'Role_2' => [
  1445. 20 => ['user' => 2, 'roles' => ['Role_1', 'Role_2']],
  1446. ],
  1447. 'Role_3' => [
  1448. 10 => ['user' => 1, 'roles' => ['Role_1', 'Role_3']],
  1449. ],
  1450. ];
  1451. $this->assertEquals($expected_result, $result->toArray());
  1452. }
  1453. public function testGroupByMultiLevelAndClosurePreservingKeys()
  1454. {
  1455. $data = new Collection([
  1456. 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
  1457. 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
  1458. 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']],
  1459. 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']],
  1460. ]);
  1461. $result = $data->groupBy([
  1462. 'skilllevel',
  1463. function ($item) {
  1464. return $item['roles'];
  1465. },
  1466. ], true);
  1467. $expected_result = [
  1468. 1 => [
  1469. 'Role_1' => [
  1470. 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
  1471. 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
  1472. ],
  1473. 'Role_3' => [
  1474. 10 => ['user' => 1, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_3']],
  1475. ],
  1476. 'Role_2' => [
  1477. 20 => ['user' => 2, 'skilllevel' => 1, 'roles' => ['Role_1', 'Role_2']],
  1478. ],
  1479. ],
  1480. 2 => [
  1481. 'Role_1' => [
  1482. 30 => ['user' => 3, 'skilllevel' => 2, 'roles' => ['Role_1']],
  1483. ],
  1484. 'Role_2' => [
  1485. 40 => ['user' => 4, 'skilllevel' => 2, 'roles' => ['Role_2']],
  1486. ],
  1487. ],
  1488. ];
  1489. $this->assertEquals($expected_result, $result->toArray());
  1490. }
  1491. public function testKeyByAttribute()
  1492. {
  1493. $data = new Collection([['rating' => 1, 'name' => '1'], ['rating' => 2, 'name' => '2'], ['rating' => 3, 'name' => '3']]);
  1494. $result = $data->keyBy('rating');
  1495. $this->assertEquals([1 => ['rating' => 1, 'name' => '1'], 2 => ['rating' => 2, 'name' => '2'], 3 => ['rating' => 3, 'name' => '3']], $result->all());
  1496. $result = $data->keyBy(function ($item) {
  1497. return $item['rating'] * 2;
  1498. });
  1499. $this->assertEquals([2 => ['rating' => 1, 'name' => '1'], 4 => ['rating' => 2, 'name' => '2'], 6 => ['rating' => 3, 'name' => '3']], $result->all());
  1500. }
  1501. public function testKeyByClosure()
  1502. {
  1503. $data = new Collection([
  1504. ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
  1505. ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
  1506. ]);
  1507. $result = $data->keyBy(function ($item, $key) {
  1508. return strtolower($key.'-'.$item['firstname'].$item['lastname']);
  1509. });
  1510. $this->assertEquals([
  1511. '0-taylorotwell' => ['firstname' => 'Taylor', 'lastname' => 'Otwell', 'locale' => 'US'],
  1512. '1-lucasmichot' => ['firstname' => 'Lucas', 'lastname' => 'Michot', 'locale' => 'FR'],
  1513. ], $result->all());
  1514. }
  1515. public function testContains()
  1516. {
  1517. $c = new Collection([1, 3, 5]);
  1518. $this->assertTrue($c->contains(1));
  1519. $this->assertTrue($c->contains('1'));
  1520. $this->assertFalse($c->contains(2));
  1521. $this->assertFalse($c->contains('2'));
  1522. $c = new Collection(['1']);
  1523. $this->assertTrue($c->contains('1'));
  1524. $this->assertTrue($c->contains(1));
  1525. $c = new Collection([null]);
  1526. $this->assertTrue($c->contains(false));
  1527. $this->assertTrue($c->contains(null));
  1528. $this->assertTrue($c->contains([]));
  1529. $this->assertTrue($c->contains(0));
  1530. $this->assertTrue($c->contains(''));
  1531. $c = new Collection([0]);
  1532. $this->assertTrue($c->contains(0));
  1533. $this->assertTrue($c->contains('0'));
  1534. $this->assertTrue($c->contains(false));
  1535. $this->assertTrue($c->contains(null));
  1536. $this->assertTrue($c->contains(function ($value) {
  1537. return $value < 5;
  1538. }));
  1539. $this->assertFalse($c->contains(function ($value) {
  1540. return $value > 5;
  1541. }));
  1542. $c = new Collection([['v' => 1], ['v' => 3], ['v' => 5]]);
  1543. $this->assertTrue($c->contains('v', 1));
  1544. $this->assertFalse($c->contains('v', 2));
  1545. $c = new Collection(['date', 'class', (object) ['foo' => 50]]);
  1546. $this->assertTrue($c->contains('date'));
  1547. $this->assertTrue($c->contains('class'));
  1548. $this->assertFalse($c->contains('foo'));
  1549. $c = new Collection([['a' => false, 'b' => false], ['a' => true, 'b' => false]]);
  1550. $this->assertTrue($c->contains->a);
  1551. $this->assertFalse($c->contains->b);
  1552. $c = new Collection([
  1553. null, 1, 2,
  1554. ]);
  1555. $this->assertTrue($c->contains(function ($value) {
  1556. return is_null($value);
  1557. }));
  1558. }
  1559. public function testSome()
  1560. {
  1561. $c = new Collection([1, 3, 5]);
  1562. $this->assertTrue($c->some(1));
  1563. $this->assertFalse($c->some(2));
  1564. $this->assertTrue($c->some(function ($value) {
  1565. return $value < 5;
  1566. }));
  1567. $this->assertFalse($c->some(function ($value) {
  1568. return $value > 5;
  1569. }));
  1570. $c = new Collection([['v' => 1], ['v' => 3], ['v' => 5]]);
  1571. $this->assertTrue($c->some('v', 1));
  1572. $this->assertFalse($c->some('v', 2));
  1573. $c = new Collection(['date', 'class', (object) ['foo' => 50]]);
  1574. $this->assertTrue($c->some('date'));
  1575. $this->assertTrue($c->some('class'));
  1576. $this->assertFalse($c->some('foo'));
  1577. $c = new Collection([['a' => false, 'b' => false], ['a' => true, 'b' => false]]);
  1578. $this->assertTrue($c->some->a);
  1579. $this->assertFalse($c->some->b);
  1580. $c = new Collection([
  1581. null, 1, 2,
  1582. ]);
  1583. $this->assertTrue($c->some(function ($value) {
  1584. return is_null($value);
  1585. }));
  1586. }
  1587. public function testContainsStrict()
  1588. {
  1589. $c = new Collection([1, 3, 5, '02']);
  1590. $this->assertTrue($c->containsStrict(1));
  1591. $this->assertFalse($c->containsStrict('1'));
  1592. $this->assertFalse($c->containsStrict(2));
  1593. $this->assertTrue($c->containsStrict('02'));
  1594. $this->assertFalse($c->containsStrict(true));
  1595. $this->assertTrue($c->containsStrict(function ($value) {
  1596. return $value < 5;
  1597. }));
  1598. $this->assertFalse($c->containsStrict(function ($value) {
  1599. return $value > 5;
  1600. }));
  1601. $c = new Collection([0]);
  1602. $this->assertTrue($c->containsStrict(0));
  1603. $this->assertFalse($c->containsStrict('0'));
  1604. $this->assertFalse($c->containsStrict(false));
  1605. $this->assertFalse($c->containsStrict(null));
  1606. $c = new Collection([1, null]);
  1607. $this->assertTrue($c->containsStrict(null));
  1608. $this->assertFalse($c->containsStrict(0));
  1609. $this->assertFalse($c->containsStrict(false));
  1610. $c = new Collection([['v' => 1], ['v' => 3], ['v' => '04'], ['v' => 5]]);
  1611. $this->assertTrue($c->containsStrict('v', 1));
  1612. $this->assertFalse($c->containsStrict('v', 2));
  1613. $this->assertFalse($c->containsStrict('v', '1'));
  1614. $this->assertFalse($c->containsStrict('v', 4));
  1615. $this->assertTrue($c->containsStrict('v', '04'));
  1616. $c = new Collection(['date', 'class', (object) ['foo' => 50], '']);
  1617. $this->assertTrue($c->containsStrict('date'));
  1618. $this->assertTrue($c->containsStrict('class'));
  1619. $this->assertFalse($c->containsStrict('foo'));
  1620. $this->assertFalse($c->containsStrict(null));
  1621. $this->assertTrue($c->containsStrict(''));
  1622. }
  1623. public function testContainsWithOperator()
  1624. {
  1625. $c = new Collection([['v' => 1], ['v' => 3], ['v' => '4'], ['v' => 5]]);
  1626. $this->assertTrue($c->contains('v', '=', 4));
  1627. $this->assertTrue($c->contains('v', '==', 4));
  1628. $this->assertFalse($c->contains('v', '===', 4));
  1629. $this->assertTrue($c->contains('v', '>', 4));
  1630. }
  1631. public function testGettingSumFromCollection()
  1632. {
  1633. $c = new Collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
  1634. $this->assertEquals(100, $c->sum('foo'));
  1635. $c = new Collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
  1636. $this->assertEquals(100, $c->sum(function ($i) {
  1637. return $i->foo;
  1638. }));
  1639. }
  1640. public function testCanSumValuesWithoutACallback()
  1641. {
  1642. $c = new Collection([1, 2, 3, 4, 5]);
  1643. $this->assertEquals(15, $c->sum());
  1644. }
  1645. public function testGettingSumFromEmptyCollection()
  1646. {
  1647. $c = new Collection;
  1648. $this->assertEquals(0, $c->sum('foo'));
  1649. }
  1650. public function testValueRetrieverAcceptsDotNotation()
  1651. {
  1652. $c = new Collection([
  1653. (object) ['id' => 1, 'foo' => ['bar' => 'B']], (object) ['id' => 2, 'foo' => ['bar' => 'A']],
  1654. ]);
  1655. $c = $c->sortBy('foo.bar');
  1656. $this->assertEquals([2, 1], $c->pluck('id')->all());
  1657. }
  1658. public function testPullRetrievesItemFromCollection()
  1659. {
  1660. $c = new Collection(['foo', 'bar']);
  1661. $this->assertEquals('foo', $c->pull(0));
  1662. }
  1663. public function testPullRemovesItemFromCollection()
  1664. {
  1665. $c = new Collection(['foo', 'bar']);
  1666. $c->pull(0);
  1667. $this->assertEquals([1 => 'bar'], $c->all());
  1668. }
  1669. public function testPullReturnsDefault()
  1670. {
  1671. $c = new Collection([]);
  1672. $value = $c->pull(0, 'foo');
  1673. $this->assertEquals('foo', $value);
  1674. }
  1675. public function testRejectRemovesElementsPassingTruthTest()
  1676. {
  1677. $c = new Collection(['foo', 'bar']);
  1678. $this->assertEquals(['foo'], $c->reject('bar')->values()->all());
  1679. $c = new Collection(['foo', 'bar']);
  1680. $this->assertEquals(['foo'], $c->reject(function ($v) {
  1681. return $v == 'bar';
  1682. })->values()->all());
  1683. $c = new Collection(['foo', null]);
  1684. $this->assertEquals(['foo'], $c->reject(null)->values()->all());
  1685. $c = new Collection(['foo', 'bar']);
  1686. $this->assertEquals(['foo', 'bar'], $c->reject('baz')->values()->all());
  1687. $c = new Collection(['foo', 'bar']);
  1688. $this->assertEquals(['foo', 'bar'], $c->reject(function ($v) {
  1689. return $v == 'baz';
  1690. })->values()->all());
  1691. $c = new Collection(['id' => 1, 'primary' => 'foo', 'secondary' => 'bar']);
  1692. $this->assertEquals(['primary' => 'foo', 'secondary' => 'bar'], $c->reject(function ($item, $key) {
  1693. return $key == 'id';
  1694. })->all());
  1695. }
  1696. public function testSearchReturnsIndexOfFirstFoundItem()
  1697. {
  1698. $c = new Collection([1, 2, 3, 4, 5, 2, 5, 'foo' => 'bar']);
  1699. $this->assertEquals(1, $c->search(2));
  1700. $this->assertEquals(1, $c->search('2'));
  1701. $this->assertEquals('foo', $c->search('bar'));
  1702. $this->assertEquals(4, $c->search(function ($value) {
  1703. return $value > 4;
  1704. }));
  1705. $this->assertEquals('foo', $c->search(function ($value) {
  1706. return ! is_numeric($value);
  1707. }));
  1708. }
  1709. public function testSearchInStrictMode()
  1710. {
  1711. $c = new Collection([false, 0, 1, [], '']);
  1712. $this->assertFalse($c->search('false', true));
  1713. $this->assertFalse($c->search('1', true));
  1714. $this->assertEquals(0, $c->search(false, true));
  1715. $this->assertEquals(1, $c->search(0, true));
  1716. $this->assertEquals(2, $c->search(1, true));
  1717. $this->assertEquals(3, $c->search([], true));
  1718. $this->assertEquals(4, $c->search('', true));
  1719. }
  1720. public function testSearchReturnsFalseWhenItemIsNotFound()
  1721. {
  1722. $c = new Collection([1, 2, 3, 4, 5, 'foo' => 'bar']);
  1723. $this->assertFalse($c->search(6));
  1724. $this->assertFalse($c->search('foo'));
  1725. $this->assertFalse($c->search(function ($value) {
  1726. return $value < 1 && is_numeric($value);
  1727. }));
  1728. $this->assertFalse($c->search(function ($value) {
  1729. return $value == 'nope';
  1730. }));
  1731. }
  1732. public function testKeys()
  1733. {
  1734. $c = new Collection(['name' => 'taylor', 'framework' => 'laravel']);
  1735. $this->assertEquals(['name', 'framework'], $c->keys()->all());
  1736. }
  1737. public function testPaginate()
  1738. {
  1739. $c = new Collection(['one', 'two', 'three', 'four']);
  1740. $this->assertEquals(['one', 'two'], $c->forPage(0, 2)->all());
  1741. $this->assertEquals(['one', 'two'], $c->forPage(1, 2)->all());
  1742. $this->assertEquals([2 => 'three', 3 => 'four'], $c->forPage(2, 2)->all());
  1743. $this->assertEquals([], $c->forPage(3, 2)->all());
  1744. }
  1745. public function testPrepend()
  1746. {
  1747. $c = new Collection(['one', 'two', 'three', 'four']);
  1748. $this->assertEquals(['zero', 'one', 'two', 'three', 'four'], $c->prepend('zero')->all());
  1749. $c = new Collection(['one' => 1, 'two' => 2]);
  1750. $this->assertEquals(['zero' => 0, 'one' => 1, 'two' => 2], $c->prepend(0, 'zero')->all());
  1751. }
  1752. public function testZip()
  1753. {
  1754. $c = new Collection([1, 2, 3]);
  1755. $c = $c->zip(new Collection([4, 5, 6]));
  1756. $this->assertInstanceOf(Collection::class, $c);
  1757. $this->assertInstanceOf(Collection::class, $c[0]);
  1758. $this->assertInstanceOf(Collection::class, $c[1]);
  1759. $this->assertInstanceOf(Collection::class, $c[2]);
  1760. $this->assertCount(3, $c);
  1761. $this->assertEquals([1, 4], $c[0]->all());
  1762. $this->assertEquals([2, 5], $c[1]->all());
  1763. $this->assertEquals([3, 6], $c[2]->all());
  1764. $c = new Collection([1, 2, 3]);
  1765. $c = $c->zip([4, 5, 6], [7, 8, 9]);
  1766. $this->assertCount(3, $c);
  1767. $this->assertEquals([1, 4, 7], $c[0]->all());
  1768. $this->assertEquals([2, 5, 8], $c[1]->all());
  1769. $this->assertEquals([3, 6, 9], $c[2]->all());
  1770. $c = new Collection([1, 2, 3]);
  1771. $c = $c->zip([4, 5, 6], [7]);
  1772. $this->assertCount(3, $c);
  1773. $this->assertEquals([1, 4, 7], $c[0]->all());
  1774. $this->assertEquals([2, 5, null], $c[1]->all());
  1775. $this->assertEquals([3, 6, null], $c[2]->all());
  1776. }
  1777. public function testPadPadsArrayWithValue()
  1778. {
  1779. $c = new Collection([1, 2, 3]);
  1780. $c = $c->pad(4, 0);
  1781. $this->assertEquals([1, 2, 3, 0], $c->all());
  1782. $c = new Collection([1, 2, 3, 4, 5]);
  1783. $c = $c->pad(4, 0);
  1784. $this->assertEquals([1, 2, 3, 4, 5], $c->all());
  1785. $c = new Collection([1, 2, 3]);
  1786. $c = $c->pad(-4, 0);
  1787. $this->assertEquals([0, 1, 2, 3], $c->all());
  1788. $c = new Collection([1, 2, 3, 4, 5]);
  1789. $c = $c->pad(-4, 0);
  1790. $this->assertEquals([1, 2, 3, 4, 5], $c->all());
  1791. }
  1792. public function testGettingMaxItemsFromCollection()
  1793. {
  1794. $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
  1795. $this->assertEquals(20, $c->max(function ($item) {
  1796. return $item->foo;
  1797. }));
  1798. $this->assertEquals(20, $c->max('foo'));
  1799. $this->assertEquals(20, $c->max->foo);
  1800. $c = new Collection([['foo' => 10], ['foo' => 20]]);
  1801. $this->assertEquals(20, $c->max('foo'));
  1802. $this->assertEquals(20, $c->max->foo);
  1803. $c = new Collection([1, 2, 3, 4, 5]);
  1804. $this->assertEquals(5, $c->max());
  1805. $c = new Collection;
  1806. $this->assertNull($c->max());
  1807. }
  1808. public function testGettingMinItemsFromCollection()
  1809. {
  1810. $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
  1811. $this->assertEquals(10, $c->min(function ($item) {
  1812. return $item->foo;
  1813. }));
  1814. $this->assertEquals(10, $c->min('foo'));
  1815. $this->assertEquals(10, $c->min->foo);
  1816. $c = new Collection([['foo' => 10], ['foo' => 20]]);
  1817. $this->assertEquals(10, $c->min('foo'));
  1818. $this->assertEquals(10, $c->min->foo);
  1819. $c = new Collection([['foo' => 10], ['foo' => 20], ['foo' => null]]);
  1820. $this->assertEquals(10, $c->min('foo'));
  1821. $this->assertEquals(10, $c->min->foo);
  1822. $c = new Collection([1, 2, 3, 4, 5]);
  1823. $this->assertEquals(1, $c->min());
  1824. $c = new Collection([1, null, 3, 4, 5]);
  1825. $this->assertEquals(1, $c->min());
  1826. $c = new Collection([0, 1, 2, 3, 4]);
  1827. $this->assertEquals(0, $c->min());
  1828. $c = new Collection;
  1829. $this->assertNull($c->min());
  1830. }
  1831. public function testOnly()
  1832. {
  1833. $data = new Collection(['first' => 'Taylor', 'last' => 'Otwell', 'email' => 'taylorotwell@gmail.com']);
  1834. $this->assertEquals($data->all(), $data->only(null)->all());
  1835. $this->assertEquals(['first' => 'Taylor'], $data->only(['first', 'missing'])->all());
  1836. $this->assertEquals(['first' => 'Taylor'], $data->only('first', 'missing')->all());
  1837. $this->assertEquals(['first' => 'Taylor'], $data->only(collect(['first', 'missing']))->all());
  1838. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(['first', 'email'])->all());
  1839. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only('first', 'email')->all());
  1840. $this->assertEquals(['first' => 'Taylor', 'email' => 'taylorotwell@gmail.com'], $data->only(collect(['first', 'email']))->all());
  1841. }
  1842. public function testGettingAvgItemsFromCollection()
  1843. {
  1844. $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20]]);
  1845. $this->assertEquals(15, $c->avg(function ($item) {
  1846. return $item->foo;
  1847. }));
  1848. $this->assertEquals(15, $c->avg('foo'));
  1849. $this->assertEquals(15, $c->avg->foo);
  1850. $c = new Collection([(object) ['foo' => 10], (object) ['foo' => 20], (object) ['foo' => null]]);
  1851. $this->assertEquals(15, $c->avg(function ($item) {
  1852. return $item->foo;
  1853. }));
  1854. $this->assertEquals(15, $c->avg('foo'));
  1855. $this->assertEquals(15, $c->avg->foo);
  1856. $c = new Collection([['foo' => 10], ['foo' => 20]]);
  1857. $this->assertEquals(15, $c->avg('foo'));
  1858. $this->assertEquals(15, $c->avg->foo);
  1859. $c = new Collection([1, 2, 3, 4, 5]);
  1860. $this->assertEquals(3, $c->avg());
  1861. $c = new Collection;
  1862. $this->assertNull($c->avg());
  1863. }
  1864. public function testJsonSerialize()
  1865. {
  1866. $c = new Collection([
  1867. new TestArrayableObject,
  1868. new TestJsonableObject,
  1869. new TestJsonSerializeObject,
  1870. 'baz',
  1871. ]);
  1872. $this->assertSame([
  1873. ['foo' => 'bar'],
  1874. ['foo' => 'bar'],
  1875. ['foo' => 'bar'],
  1876. 'baz',
  1877. ], $c->jsonSerialize());
  1878. }
  1879. public function testCombineWithArray()
  1880. {
  1881. $expected = [
  1882. 1 => 4,
  1883. 2 => 5,
  1884. 3 => 6,
  1885. ];
  1886. $c = new Collection(array_keys($expected));
  1887. $actual = $c->combine(array_values($expected))->toArray();
  1888. $this->assertSame($expected, $actual);
  1889. }
  1890. public function testCombineWithCollection()
  1891. {
  1892. $expected = [
  1893. 1 => 4,
  1894. 2 => 5,
  1895. 3 => 6,
  1896. ];
  1897. $keyCollection = new Collection(array_keys($expected));
  1898. $valueCollection = new Collection(array_values($expected));
  1899. $actual = $keyCollection->combine($valueCollection)->toArray();
  1900. $this->assertSame($expected, $actual);
  1901. }
  1902. public function testConcatWithArray()
  1903. {
  1904. $expected = [
  1905. 0 => 4,
  1906. 1 => 5,
  1907. 2 => 6,
  1908. 3 => 'a',
  1909. 4 => 'b',
  1910. 5 => 'c',
  1911. 6 => 'Jonny',
  1912. 7 => 'from',
  1913. 8 => 'Laroe',
  1914. 9 => 'Jonny',
  1915. 10 => 'from',
  1916. 11 => 'Laroe',
  1917. ];
  1918. $collection = new Collection([4, 5, 6]);
  1919. $collection = $collection->concat(['a', 'b', 'c']);
  1920. $collection = $collection->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
  1921. $actual = $collection->concat(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe'])->toArray();
  1922. $this->assertSame($expected, $actual);
  1923. }
  1924. public function testConcatWithCollection()
  1925. {
  1926. $expected = [
  1927. 0 => 4,
  1928. 1 => 5,
  1929. 2 => 6,
  1930. 3 => 'a',
  1931. 4 => 'b',
  1932. 5 => 'c',
  1933. 6 => 'Jonny',
  1934. 7 => 'from',
  1935. 8 => 'Laroe',
  1936. 9 => 'Jonny',
  1937. 10 => 'from',
  1938. 11 => 'Laroe',
  1939. ];
  1940. $firstCollection = new Collection([4, 5, 6]);
  1941. $secondCollection = new Collection(['a', 'b', 'c']);
  1942. $thirdCollection = new Collection(['who' => 'Jonny', 'preposition' => 'from', 'where' => 'Laroe']);
  1943. $firstCollection = $firstCollection->concat($secondCollection);
  1944. $firstCollection = $firstCollection->concat($thirdCollection);
  1945. $actual = $firstCollection->concat($thirdCollection)->toArray();
  1946. $this->assertSame($expected, $actual);
  1947. }
  1948. public function testReduce()
  1949. {
  1950. $data = new Collection([1, 2, 3]);
  1951. $this->assertEquals(6, $data->reduce(function ($carry, $element) {
  1952. return $carry += $element;
  1953. }));
  1954. }
  1955. /**
  1956. * @expectedException \InvalidArgumentException
  1957. */
  1958. public function testRandomThrowsAnExceptionUsingAmountBiggerThanCollectionSize()
  1959. {
  1960. $data = new Collection([1, 2, 3]);
  1961. $data->random(4);
  1962. }
  1963. public function testPipe()
  1964. {
  1965. $collection = new Collection([1, 2, 3]);
  1966. $this->assertEquals(6, $collection->pipe(function ($collection) {
  1967. return $collection->sum();
  1968. }));
  1969. }
  1970. public function testMedianValueWithArrayCollection()
  1971. {
  1972. $collection = new Collection([1, 2, 2, 4]);
  1973. $this->assertEquals(2, $collection->median());
  1974. }
  1975. public function testMedianValueByKey()
  1976. {
  1977. $collection = new Collection([
  1978. (object) ['foo' => 1],
  1979. (object) ['foo' => 2],
  1980. (object) ['foo' => 2],
  1981. (object) ['foo' => 4],
  1982. ]);
  1983. $this->assertEquals(2, $collection->median('foo'));
  1984. }
  1985. public function testMedianOnCollectionWithNull()
  1986. {
  1987. $collection = new Collection([
  1988. (object) ['foo' => 1],
  1989. (object) ['foo' => 2],
  1990. (object) ['foo' => 4],
  1991. (object) ['foo' => null],
  1992. ]);
  1993. $this->assertEquals(2, $collection->median('foo'));
  1994. }
  1995. public function testEvenMedianCollection()
  1996. {
  1997. $collection = new Collection([
  1998. (object) ['foo' => 0],
  1999. (object) ['foo' => 3],
  2000. ]);
  2001. $this->assertEquals(1.5, $collection->median('foo'));
  2002. }
  2003. public function testMedianOutOfOrderCollection()
  2004. {
  2005. $collection = new Collection([
  2006. (object) ['foo' => 0],
  2007. (object) ['foo' => 5],
  2008. (object) ['foo' => 3],
  2009. ]);
  2010. $this->assertEquals(3, $collection->median('foo'));
  2011. }
  2012. public function testMedianOnEmptyCollectionReturnsNull()
  2013. {
  2014. $collection = new Collection;
  2015. $this->assertNull($collection->median());
  2016. }
  2017. public function testModeOnNullCollection()
  2018. {
  2019. $collection = new Collection;
  2020. $this->assertNull($collection->mode());
  2021. }
  2022. public function testMode()
  2023. {
  2024. $collection = new Collection([1, 2, 3, 4, 4, 5]);
  2025. $this->assertEquals([4], $collection->mode());
  2026. }
  2027. public function testModeValueByKey()
  2028. {
  2029. $collection = new Collection([
  2030. (object) ['foo' => 1],
  2031. (object) ['foo' => 1],
  2032. (object) ['foo' => 2],
  2033. (object) ['foo' => 4],
  2034. ]);
  2035. $this->assertEquals([1], $collection->mode('foo'));
  2036. }
  2037. public function testWithMultipleModeValues()
  2038. {
  2039. $collection = new Collection([1, 2, 2, 1]);
  2040. $this->assertEquals([1, 2], $collection->mode());
  2041. }
  2042. public function testSliceOffset()
  2043. {
  2044. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
  2045. $this->assertEquals([4, 5, 6, 7, 8], $collection->slice(3)->values()->toArray());
  2046. }
  2047. public function testSliceNegativeOffset()
  2048. {
  2049. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
  2050. $this->assertEquals([6, 7, 8], $collection->slice(-3)->values()->toArray());
  2051. }
  2052. public function testSliceOffsetAndLength()
  2053. {
  2054. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
  2055. $this->assertEquals([4, 5, 6], $collection->slice(3, 3)->values()->toArray());
  2056. }
  2057. public function testSliceOffsetAndNegativeLength()
  2058. {
  2059. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
  2060. $this->assertEquals([4, 5, 6, 7], $collection->slice(3, -1)->values()->toArray());
  2061. }
  2062. public function testSliceNegativeOffsetAndLength()
  2063. {
  2064. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
  2065. $this->assertEquals([4, 5, 6], $collection->slice(-5, 3)->values()->toArray());
  2066. }
  2067. public function testSliceNegativeOffsetAndNegativeLength()
  2068. {
  2069. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]);
  2070. $this->assertEquals([3, 4, 5, 6], $collection->slice(-6, -2)->values()->toArray());
  2071. }
  2072. public function testCollectionFromTraversable()
  2073. {
  2074. $collection = new Collection(new ArrayObject([1, 2, 3]));
  2075. $this->assertEquals([1, 2, 3], $collection->toArray());
  2076. }
  2077. public function testCollectionFromTraversableWithKeys()
  2078. {
  2079. $collection = new Collection(new ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3]));
  2080. $this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $collection->toArray());
  2081. }
  2082. public function testSplitCollectionWithADivisableCount()
  2083. {
  2084. $collection = new Collection(['a', 'b', 'c', 'd']);
  2085. $this->assertEquals(
  2086. [['a', 'b'], ['c', 'd']],
  2087. $collection->split(2)->map(function (Collection $chunk) {
  2088. return $chunk->values()->toArray();
  2089. })->toArray()
  2090. );
  2091. $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  2092. $this->assertEquals(
  2093. [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]],
  2094. $collection->split(2)->map(function (Collection $chunk) {
  2095. return $chunk->values()->toArray();
  2096. })->toArray()
  2097. );
  2098. }
  2099. public function testSplitCollectionWithAnUndivisableCount()
  2100. {
  2101. $collection = new Collection(['a', 'b', 'c']);
  2102. $this->assertEquals(
  2103. [['a', 'b'], ['c']],
  2104. $collection->split(2)->map(function (Collection $chunk) {
  2105. return $chunk->values()->toArray();
  2106. })->toArray()
  2107. );
  2108. }
  2109. public function testSplitCollectionWithCountLessThenDivisor()
  2110. {
  2111. $collection = new Collection(['a']);
  2112. $this->assertEquals(
  2113. [['a']],
  2114. $collection->split(2)->map(function (Collection $chunk) {
  2115. return $chunk->values()->toArray();
  2116. })->toArray()
  2117. );
  2118. }
  2119. public function testSplitCollectionIntoThreeWithCountOfFour()
  2120. {
  2121. $collection = new Collection(['a', 'b', 'c', 'd']);
  2122. $this->assertEquals(
  2123. [['a', 'b'], ['c'], ['d']],
  2124. $collection->split(3)->map(function (Collection $chunk) {
  2125. return $chunk->values()->toArray();
  2126. })->toArray()
  2127. );
  2128. }
  2129. public function testSplitCollectionIntoThreeWithCountOfFive()
  2130. {
  2131. $collection = new Collection(['a', 'b', 'c', 'd', 'e']);
  2132. $this->assertEquals(
  2133. [['a', 'b'], ['c', 'd'], ['e']],
  2134. $collection->split(3)->map(function (Collection $chunk) {
  2135. return $chunk->values()->toArray();
  2136. })->toArray()
  2137. );
  2138. }
  2139. public function testSplitCollectionIntoSixWithCountOfTen()
  2140. {
  2141. $collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']);
  2142. $this->assertEquals(
  2143. [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h'], ['i'], ['j']],
  2144. $collection->split(6)->map(function (Collection $chunk) {
  2145. return $chunk->values()->toArray();
  2146. })->toArray()
  2147. );
  2148. }
  2149. public function testSplitEmptyCollection()
  2150. {
  2151. $collection = new Collection;
  2152. $this->assertEquals(
  2153. [],
  2154. $collection->split(2)->map(function (Collection $chunk) {
  2155. return $chunk->values()->toArray();
  2156. })->toArray()
  2157. );
  2158. }
  2159. public function testHigherOrderCollectionGroupBy()
  2160. {
  2161. $collection = collect([
  2162. new TestSupportCollectionHigherOrderItem,
  2163. new TestSupportCollectionHigherOrderItem('TAYLOR'),
  2164. new TestSupportCollectionHigherOrderItem('foo'),
  2165. ]);
  2166. $this->assertEquals([
  2167. 'taylor' => [$collection[0]],
  2168. 'TAYLOR' => [$collection[1]],
  2169. 'foo' => [$collection[2]],
  2170. ], $collection->groupBy->name->toArray());
  2171. $this->assertEquals([
  2172. 'TAYLOR' => [$collection[0], $collection[1]],
  2173. 'FOO' => [$collection[2]],
  2174. ], $collection->groupBy->uppercase()->toArray());
  2175. }
  2176. public function testHigherOrderCollectionMap()
  2177. {
  2178. $person1 = (object) ['name' => 'Taylor'];
  2179. $person2 = (object) ['name' => 'Yaz'];
  2180. $collection = collect([$person1, $person2]);
  2181. $this->assertEquals(['Taylor', 'Yaz'], $collection->map->name->toArray());
  2182. $collection = collect([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);
  2183. $this->assertEquals(['TAYLOR', 'TAYLOR'], $collection->each->uppercase()->map->name->toArray());
  2184. }
  2185. public function testHigherOrderCollectionMapFromArrays()
  2186. {
  2187. $person1 = ['name' => 'Taylor'];
  2188. $person2 = ['name' => 'Yaz'];
  2189. $collection = collect([$person1, $person2]);
  2190. $this->assertEquals(['Taylor', 'Yaz'], $collection->map->name->toArray());
  2191. $collection = collect([new TestSupportCollectionHigherOrderItem, new TestSupportCollectionHigherOrderItem]);
  2192. $this->assertEquals(['TAYLOR', 'TAYLOR'], $collection->each->uppercase()->map->name->toArray());
  2193. }
  2194. public function testPartition()
  2195. {
  2196. $collection = new Collection(range(1, 10));
  2197. [$firstPartition, $secondPartition] = $collection->partition(function ($i) {
  2198. return $i <= 5;
  2199. });
  2200. $this->assertEquals([1, 2, 3, 4, 5], $firstPartition->values()->toArray());
  2201. $this->assertEquals([6, 7, 8, 9, 10], $secondPartition->values()->toArray());
  2202. }
  2203. public function testPartitionCallbackWithKey()
  2204. {
  2205. $collection = new Collection(['zero', 'one', 'two', 'three']);
  2206. [$even, $odd] = $collection->partition(function ($item, $index) {
  2207. return $index % 2 === 0;
  2208. });
  2209. $this->assertEquals(['zero', 'two'], $even->values()->toArray());
  2210. $this->assertEquals(['one', 'three'], $odd->values()->toArray());
  2211. }
  2212. public function testPartitionByKey()
  2213. {
  2214. $courses = new Collection([
  2215. ['free' => true, 'title' => 'Basic'], ['free' => false, 'title' => 'Premium'],
  2216. ]);
  2217. [$free, $premium] = $courses->partition('free');
  2218. $this->assertSame([['free' => true, 'title' => 'Basic']], $free->values()->toArray());
  2219. $this->assertSame([['free' => false, 'title' => 'Premium']], $premium->values()->toArray());
  2220. }
  2221. public function testPartitionWithOperators()
  2222. {
  2223. $collection = new Collection([
  2224. ['name' => 'Tim', 'age' => 17],
  2225. ['name' => 'Agatha', 'age' => 62],
  2226. ['name' => 'Kristina', 'age' => 33],
  2227. ['name' => 'Tim', 'age' => 41],
  2228. ]);
  2229. [$tims, $others] = $collection->partition('name', 'Tim');
  2230. $this->assertEquals($tims->values()->all(), [
  2231. ['name' => 'Tim', 'age' => 17],
  2232. ['name' => 'Tim', 'age' => 41],
  2233. ]);
  2234. $this->assertEquals($others->values()->all(), [
  2235. ['name' => 'Agatha', 'age' => 62],
  2236. ['name' => 'Kristina', 'age' => 33],
  2237. ]);
  2238. [$adults, $minors] = $collection->partition('age', '>=', 18);
  2239. $this->assertEquals($adults->values()->all(), [
  2240. ['name' => 'Agatha', 'age' => 62],
  2241. ['name' => 'Kristina', 'age' => 33],
  2242. ['name' => 'Tim', 'age' => 41],
  2243. ]);
  2244. $this->assertEquals($minors->values()->all(), [
  2245. ['name' => 'Tim', 'age' => 17],
  2246. ]);
  2247. }
  2248. public function testPartitionPreservesKeys()
  2249. {
  2250. $courses = new Collection([
  2251. 'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
  2252. ]);
  2253. [$free, $premium] = $courses->partition('free');
  2254. $this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());
  2255. $this->assertSame(['b' => ['free' => false]], $premium->toArray());
  2256. }
  2257. public function testPartitionEmptyCollection()
  2258. {
  2259. $collection = new Collection;
  2260. $this->assertCount(2, $collection->partition(function () {
  2261. return true;
  2262. }));
  2263. }
  2264. public function testHigherOrderPartition()
  2265. {
  2266. $courses = new Collection([
  2267. 'a' => ['free' => true], 'b' => ['free' => false], 'c' => ['free' => true],
  2268. ]);
  2269. [$free, $premium] = $courses->partition->free;
  2270. $this->assertSame(['a' => ['free' => true], 'c' => ['free' => true]], $free->toArray());
  2271. $this->assertSame(['b' => ['free' => false]], $premium->toArray());
  2272. }
  2273. public function testTap()
  2274. {
  2275. $collection = new Collection([1, 2, 3]);
  2276. $fromTap = [];
  2277. $collection = $collection->tap(function ($collection) use (&$fromTap) {
  2278. $fromTap = $collection->slice(0, 1)->toArray();
  2279. });
  2280. $this->assertSame([1], $fromTap);
  2281. $this->assertSame([1, 2, 3], $collection->toArray());
  2282. }
  2283. public function testWhen()
  2284. {
  2285. $collection = new Collection(['michael', 'tom']);
  2286. $collection->when('adam', function ($collection, $newName) {
  2287. return $collection->push($newName);
  2288. });
  2289. $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
  2290. $collection = new Collection(['michael', 'tom']);
  2291. $collection->when(false, function ($collection) {
  2292. return $collection->push('adam');
  2293. });
  2294. $this->assertSame(['michael', 'tom'], $collection->toArray());
  2295. }
  2296. public function testWhenDefault()
  2297. {
  2298. $collection = new Collection(['michael', 'tom']);
  2299. $collection->when(false, function ($collection) {
  2300. return $collection->push('adam');
  2301. }, function ($collection) {
  2302. return $collection->push('taylor');
  2303. });
  2304. $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
  2305. }
  2306. public function testWhenEmpty()
  2307. {
  2308. $collection = new Collection(['michael', 'tom']);
  2309. $collection->whenEmpty(function ($collection) {
  2310. return $collection->push('adam');
  2311. });
  2312. $this->assertSame(['michael', 'tom'], $collection->toArray());
  2313. $collection = new Collection;
  2314. $collection->whenEmpty(function ($collection) {
  2315. return $collection->push('adam');
  2316. });
  2317. $this->assertSame(['adam'], $collection->toArray());
  2318. }
  2319. public function testWhenEmptyDefault()
  2320. {
  2321. $collection = new Collection(['michael', 'tom']);
  2322. $collection->whenEmpty(function ($collection) {
  2323. return $collection->push('adam');
  2324. }, function ($collection) {
  2325. return $collection->push('taylor');
  2326. });
  2327. $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
  2328. }
  2329. public function testWhenNotEmpty()
  2330. {
  2331. $collection = new Collection(['michael', 'tom']);
  2332. $collection->whenNotEmpty(function ($collection) {
  2333. return $collection->push('adam');
  2334. });
  2335. $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
  2336. $collection = new Collection;
  2337. $collection->whenNotEmpty(function ($collection) {
  2338. return $collection->push('adam');
  2339. });
  2340. $this->assertSame([], $collection->toArray());
  2341. }
  2342. public function testWhenNotEmptyDefault()
  2343. {
  2344. $collection = new Collection(['michael', 'tom']);
  2345. $collection->whenNotEmpty(function ($collection) {
  2346. return $collection->push('adam');
  2347. }, function ($collection) {
  2348. return $collection->push('taylor');
  2349. });
  2350. $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
  2351. }
  2352. public function testUnless()
  2353. {
  2354. $collection = new Collection(['michael', 'tom']);
  2355. $collection->unless(false, function ($collection) {
  2356. return $collection->push('caleb');
  2357. });
  2358. $this->assertSame(['michael', 'tom', 'caleb'], $collection->toArray());
  2359. $collection = new Collection(['michael', 'tom']);
  2360. $collection->unless(true, function ($collection) {
  2361. return $collection->push('caleb');
  2362. });
  2363. $this->assertSame(['michael', 'tom'], $collection->toArray());
  2364. }
  2365. public function testUnlessDefault()
  2366. {
  2367. $collection = new Collection(['michael', 'tom']);
  2368. $collection->unless(true, function ($collection) {
  2369. return $collection->push('caleb');
  2370. }, function ($collection) {
  2371. return $collection->push('taylor');
  2372. });
  2373. $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
  2374. }
  2375. public function testUnlessEmpty()
  2376. {
  2377. $collection = new Collection(['michael', 'tom']);
  2378. $collection->unlessEmpty(function ($collection) {
  2379. return $collection->push('adam');
  2380. });
  2381. $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
  2382. $collection = new Collection;
  2383. $collection->unlessEmpty(function ($collection) {
  2384. return $collection->push('adam');
  2385. });
  2386. $this->assertSame([], $collection->toArray());
  2387. }
  2388. public function testUnlessEmptyDefault()
  2389. {
  2390. $collection = new Collection(['michael', 'tom']);
  2391. $collection->unlessEmpty(function ($collection) {
  2392. return $collection->push('adam');
  2393. }, function ($collection) {
  2394. return $collection->push('taylor');
  2395. });
  2396. $this->assertSame(['michael', 'tom', 'adam'], $collection->toArray());
  2397. }
  2398. public function testUnlessNotEmpty()
  2399. {
  2400. $collection = new Collection(['michael', 'tom']);
  2401. $collection->unlessNotEmpty(function ($collection) {
  2402. return $collection->push('adam');
  2403. });
  2404. $this->assertSame(['michael', 'tom'], $collection->toArray());
  2405. $collection = new Collection;
  2406. $collection->unlessNotEmpty(function ($collection) {
  2407. return $collection->push('adam');
  2408. });
  2409. $this->assertSame(['adam'], $collection->toArray());
  2410. }
  2411. public function testUnlessNotEmptyDefault()
  2412. {
  2413. $collection = new Collection(['michael', 'tom']);
  2414. $collection->unlessNotEmpty(function ($collection) {
  2415. return $collection->push('adam');
  2416. }, function ($collection) {
  2417. return $collection->push('taylor');
  2418. });
  2419. $this->assertSame(['michael', 'tom', 'taylor'], $collection->toArray());
  2420. }
  2421. public function testHasReturnsValidResults()
  2422. {
  2423. $collection = new Collection(['foo' => 'one', 'bar' => 'two', 1 => 'three']);
  2424. $this->assertTrue($collection->has('foo'));
  2425. $this->assertTrue($collection->has('foo', 'bar', 1));
  2426. $this->assertFalse($collection->has('foo', 'bar', 1, 'baz'));
  2427. $this->assertFalse($collection->has('baz'));
  2428. }
  2429. public function testPutAddsItemToCollection()
  2430. {
  2431. $collection = new Collection;
  2432. $this->assertSame([], $collection->toArray());
  2433. $collection->put('foo', 1);
  2434. $this->assertSame(['foo' => 1], $collection->toArray());
  2435. $collection->put('bar', ['nested' => 'two']);
  2436. $this->assertSame(['foo' => 1, 'bar' => ['nested' => 'two']], $collection->toArray());
  2437. $collection->put('foo', 3);
  2438. $this->assertSame(['foo' => 3, 'bar' => ['nested' => 'two']], $collection->toArray());
  2439. }
  2440. public function testItThrowsExceptionWhenTryingToAccessNoProxyProperty()
  2441. {
  2442. $collection = new Collection;
  2443. $this->expectException(Exception::class);
  2444. $this->expectExceptionMessage('Property [foo] does not exist on this collection instance.');
  2445. $collection->foo;
  2446. }
  2447. public function testGetWithNullReturnsNull()
  2448. {
  2449. $collection = new Collection([1, 2, 3]);
  2450. $this->assertNull($collection->get(null));
  2451. }
  2452. }
  2453. class TestSupportCollectionHigherOrderItem
  2454. {
  2455. public $name;
  2456. public function __construct($name = 'taylor')
  2457. {
  2458. $this->name = $name;
  2459. }
  2460. public function uppercase()
  2461. {
  2462. return $this->name = strtoupper($this->name);
  2463. }
  2464. }
  2465. class TestAccessorEloquentTestStub
  2466. {
  2467. protected $attributes = [];
  2468. public function __construct($attributes)
  2469. {
  2470. $this->attributes = $attributes;
  2471. }
  2472. public function __get($attribute)
  2473. {
  2474. $accessor = 'get'.lcfirst($attribute).'Attribute';
  2475. if (method_exists($this, $accessor)) {
  2476. return $this->$accessor();
  2477. }
  2478. return $this->$attribute;
  2479. }
  2480. public function __isset($attribute)
  2481. {
  2482. $accessor = 'get'.lcfirst($attribute).'Attribute';
  2483. if (method_exists($this, $accessor)) {
  2484. return ! is_null($this->$accessor());
  2485. }
  2486. return isset($this->$attribute);
  2487. }
  2488. public function getSomeAttribute()
  2489. {
  2490. return $this->attributes['some'];
  2491. }
  2492. }
  2493. class TestArrayAccessImplementation implements ArrayAccess
  2494. {
  2495. private $arr;
  2496. public function __construct($arr)
  2497. {
  2498. $this->arr = $arr;
  2499. }
  2500. public function offsetExists($offset)
  2501. {
  2502. return isset($this->arr[$offset]);
  2503. }
  2504. public function offsetGet($offset)
  2505. {
  2506. return $this->arr[$offset];
  2507. }
  2508. public function offsetSet($offset, $value)
  2509. {
  2510. $this->arr[$offset] = $value;
  2511. }
  2512. public function offsetUnset($offset)
  2513. {
  2514. unset($this->arr[$offset]);
  2515. }
  2516. }
  2517. class TestArrayableObject implements Arrayable
  2518. {
  2519. public function toArray()
  2520. {
  2521. return ['foo' => 'bar'];
  2522. }
  2523. }
  2524. class TestJsonableObject implements Jsonable
  2525. {
  2526. public function toJson($options = 0)
  2527. {
  2528. return '{"foo":"bar"}';
  2529. }
  2530. }
  2531. class TestJsonSerializeObject implements JsonSerializable
  2532. {
  2533. public function jsonSerialize()
  2534. {
  2535. return ['foo' => 'bar'];
  2536. }
  2537. }
  2538. class TestCollectionMapIntoObject
  2539. {
  2540. public $value;
  2541. public function __construct($value)
  2542. {
  2543. $this->value = $value;
  2544. }
  2545. }
  2546. class TestCollectionSubclass extends Collection
  2547. {
  2548. //
  2549. }