4
0

SupportArrTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. namespace Tightenco\Collect\Tests\Support;
  3. use stdClass;
  4. use ArrayObject;
  5. use Tightenco\Collect\Support\Arr;
  6. use InvalidArgumentException;
  7. use Tightenco\Collect\Support\Carbon;
  8. use PHPUnit\Framework\TestCase;
  9. use Tightenco\Collect\Support\Collection;
  10. class SupportArrTest extends TestCase
  11. {
  12. public function testAccessible()
  13. {
  14. $this->assertTrue(Arr::accessible([]));
  15. $this->assertTrue(Arr::accessible([1, 2]));
  16. $this->assertTrue(Arr::accessible(['a' => 1, 'b' => 2]));
  17. $this->assertTrue(Arr::accessible(new Collection));
  18. $this->assertFalse(Arr::accessible(null));
  19. $this->assertFalse(Arr::accessible('abc'));
  20. $this->assertFalse(Arr::accessible(new stdClass));
  21. $this->assertFalse(Arr::accessible((object) ['a' => 1, 'b' => 2]));
  22. }
  23. public function testAdd()
  24. {
  25. $array = Arr::add(['name' => 'Desk'], 'price', 100);
  26. $this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
  27. }
  28. public function testCollapse()
  29. {
  30. $data = [['foo', 'bar'], ['baz']];
  31. $this->assertEquals(['foo', 'bar', 'baz'], Arr::collapse($data));
  32. }
  33. public function testCrossJoin()
  34. {
  35. // Single dimension
  36. $this->assertSame(
  37. [[1, 'a'], [1, 'b'], [1, 'c']],
  38. Arr::crossJoin([1], ['a', 'b', 'c'])
  39. );
  40. // Square matrix
  41. $this->assertSame(
  42. [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
  43. Arr::crossJoin([1, 2], ['a', 'b'])
  44. );
  45. // Rectangular matrix
  46. $this->assertSame(
  47. [[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c']],
  48. Arr::crossJoin([1, 2], ['a', 'b', 'c'])
  49. );
  50. // 3D matrix
  51. $this->assertSame(
  52. [
  53. [1, 'a', 'I'], [1, 'a', 'II'], [1, 'a', 'III'],
  54. [1, 'b', 'I'], [1, 'b', 'II'], [1, 'b', 'III'],
  55. [2, 'a', 'I'], [2, 'a', 'II'], [2, 'a', 'III'],
  56. [2, 'b', 'I'], [2, 'b', 'II'], [2, 'b', 'III'],
  57. ],
  58. Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II', 'III'])
  59. );
  60. // With 1 empty dimension
  61. $this->assertEmpty(Arr::crossJoin([], ['a', 'b'], ['I', 'II', 'III']));
  62. $this->assertEmpty(Arr::crossJoin([1, 2], [], ['I', 'II', 'III']));
  63. $this->assertEmpty(Arr::crossJoin([1, 2], ['a', 'b'], []));
  64. // With empty arrays
  65. $this->assertEmpty(Arr::crossJoin([], [], []));
  66. $this->assertEmpty(Arr::crossJoin([], []));
  67. $this->assertEmpty(Arr::crossJoin([]));
  68. // Not really a proper usage, still, test for preserving BC
  69. $this->assertSame([[]], Arr::crossJoin());
  70. }
  71. public function testDivide()
  72. {
  73. [$keys, $values] = Arr::divide(['name' => 'Desk']);
  74. $this->assertEquals(['name'], $keys);
  75. $this->assertEquals(['Desk'], $values);
  76. }
  77. public function testDot()
  78. {
  79. $array = Arr::dot(['foo' => ['bar' => 'baz']]);
  80. $this->assertEquals(['foo.bar' => 'baz'], $array);
  81. $array = Arr::dot([]);
  82. $this->assertEquals([], $array);
  83. $array = Arr::dot(['foo' => []]);
  84. $this->assertEquals(['foo' => []], $array);
  85. $array = Arr::dot(['foo' => ['bar' => []]]);
  86. $this->assertEquals(['foo.bar' => []], $array);
  87. }
  88. public function testExcept()
  89. {
  90. $array = ['name' => 'Desk', 'price' => 100];
  91. $array = Arr::except($array, ['price']);
  92. $this->assertEquals(['name' => 'Desk'], $array);
  93. }
  94. public function testExists()
  95. {
  96. $this->assertTrue(Arr::exists([1], 0));
  97. $this->assertTrue(Arr::exists([null], 0));
  98. $this->assertTrue(Arr::exists(['a' => 1], 'a'));
  99. $this->assertTrue(Arr::exists(['a' => null], 'a'));
  100. $this->assertTrue(Arr::exists(new Collection(['a' => null]), 'a'));
  101. $this->assertFalse(Arr::exists([1], 1));
  102. $this->assertFalse(Arr::exists([null], 1));
  103. $this->assertFalse(Arr::exists(['a' => 1], 0));
  104. $this->assertFalse(Arr::exists(new Collection(['a' => null]), 'b'));
  105. }
  106. public function testFirst()
  107. {
  108. $array = [100, 200, 300];
  109. $value = Arr::first($array, function ($value) {
  110. return $value >= 150;
  111. });
  112. $this->assertEquals(200, $value);
  113. $this->assertEquals(100, Arr::first($array));
  114. }
  115. public function testLast()
  116. {
  117. $array = [100, 200, 300];
  118. $last = Arr::last($array, function ($value) {
  119. return $value < 250;
  120. });
  121. $this->assertEquals(200, $last);
  122. $last = Arr::last($array, function ($value, $key) {
  123. return $key < 2;
  124. });
  125. $this->assertEquals(200, $last);
  126. $this->assertEquals(300, Arr::last($array));
  127. }
  128. public function testFlatten()
  129. {
  130. // Flat arrays are unaffected
  131. $array = ['#foo', '#bar', '#baz'];
  132. $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
  133. // Nested arrays are flattened with existing flat items
  134. $array = [['#foo', '#bar'], '#baz'];
  135. $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
  136. // Flattened array includes "null" items
  137. $array = [['#foo', null], '#baz', null];
  138. $this->assertEquals(['#foo', null, '#baz', null], Arr::flatten($array));
  139. // Sets of nested arrays are flattened
  140. $array = [['#foo', '#bar'], ['#baz']];
  141. $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
  142. // Deeply nested arrays are flattened
  143. $array = [['#foo', ['#bar']], ['#baz']];
  144. $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
  145. // Nested arrays are flattened alongside arrays
  146. $array = [new Collection(['#foo', '#bar']), ['#baz']];
  147. $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
  148. // Nested arrays containing plain arrays are flattened
  149. $array = [new Collection(['#foo', ['#bar']]), ['#baz']];
  150. $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
  151. // Nested arrays containing arrays are flattened
  152. $array = [['#foo', new Collection(['#bar'])], ['#baz']];
  153. $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
  154. // Nested arrays containing arrays containing arrays are flattened
  155. $array = [['#foo', new Collection(['#bar', ['#zap']])], ['#baz']];
  156. $this->assertEquals(['#foo', '#bar', '#zap', '#baz'], Arr::flatten($array));
  157. }
  158. public function testFlattenWithDepth()
  159. {
  160. // No depth flattens recursively
  161. $array = [['#foo', ['#bar', ['#baz']]], '#zap'];
  162. $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], Arr::flatten($array));
  163. // Specifying a depth only flattens to that depth
  164. $array = [['#foo', ['#bar', ['#baz']]], '#zap'];
  165. $this->assertEquals(['#foo', ['#bar', ['#baz']], '#zap'], Arr::flatten($array, 1));
  166. $array = [['#foo', ['#bar', ['#baz']]], '#zap'];
  167. $this->assertEquals(['#foo', '#bar', ['#baz'], '#zap'], Arr::flatten($array, 2));
  168. }
  169. public function testGet()
  170. {
  171. $array = ['products.desk' => ['price' => 100]];
  172. $this->assertEquals(['price' => 100], Arr::get($array, 'products.desk'));
  173. $array = ['products' => ['desk' => ['price' => 100]]];
  174. $value = Arr::get($array, 'products.desk');
  175. $this->assertEquals(['price' => 100], $value);
  176. // Test null array values
  177. $array = ['foo' => null, 'bar' => ['baz' => null]];
  178. $this->assertNull(Arr::get($array, 'foo', 'default'));
  179. $this->assertNull(Arr::get($array, 'bar.baz', 'default'));
  180. // Test direct ArrayAccess object
  181. $array = ['products' => ['desk' => ['price' => 100]]];
  182. $arrayAccessObject = new ArrayObject($array);
  183. $value = Arr::get($arrayAccessObject, 'products.desk');
  184. $this->assertEquals(['price' => 100], $value);
  185. // Test array containing ArrayAccess object
  186. $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]);
  187. $array = ['child' => $arrayAccessChild];
  188. $value = Arr::get($array, 'child.products.desk');
  189. $this->assertEquals(['price' => 100], $value);
  190. // Test array containing multiple nested ArrayAccess objects
  191. $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]);
  192. $arrayAccessParent = new ArrayObject(['child' => $arrayAccessChild]);
  193. $array = ['parent' => $arrayAccessParent];
  194. $value = Arr::get($array, 'parent.child.products.desk');
  195. $this->assertEquals(['price' => 100], $value);
  196. // Test missing ArrayAccess object field
  197. $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]);
  198. $arrayAccessParent = new ArrayObject(['child' => $arrayAccessChild]);
  199. $array = ['parent' => $arrayAccessParent];
  200. $value = Arr::get($array, 'parent.child.desk');
  201. $this->assertNull($value);
  202. // Test missing ArrayAccess object field
  203. $arrayAccessObject = new ArrayObject(['products' => ['desk' => null]]);
  204. $array = ['parent' => $arrayAccessObject];
  205. $value = Arr::get($array, 'parent.products.desk.price');
  206. $this->assertNull($value);
  207. // Test null ArrayAccess object fields
  208. $array = new ArrayObject(['foo' => null, 'bar' => new ArrayObject(['baz' => null])]);
  209. $this->assertNull(Arr::get($array, 'foo', 'default'));
  210. $this->assertNull(Arr::get($array, 'bar.baz', 'default'));
  211. // Test null key returns the whole array
  212. $array = ['foo', 'bar'];
  213. $this->assertEquals($array, Arr::get($array, null));
  214. // Test $array not an array
  215. $this->assertSame('default', Arr::get(null, 'foo', 'default'));
  216. $this->assertSame('default', Arr::get(false, 'foo', 'default'));
  217. // Test $array not an array and key is null
  218. $this->assertSame('default', Arr::get(null, null, 'default'));
  219. // Test $array is empty and key is null
  220. $this->assertEmpty(Arr::get([], null));
  221. $this->assertEmpty(Arr::get([], null, 'default'));
  222. // Test numeric keys
  223. $array = [
  224. 'products' => [
  225. ['name' => 'desk'],
  226. ['name' => 'chair'],
  227. ],
  228. ];
  229. $this->assertEquals('desk', Arr::get($array, 'products.0.name'));
  230. $this->assertEquals('chair', Arr::get($array, 'products.1.name'));
  231. }
  232. public function testHas()
  233. {
  234. $array = ['products.desk' => ['price' => 100]];
  235. $this->assertTrue(Arr::has($array, 'products.desk'));
  236. $array = ['products' => ['desk' => ['price' => 100]]];
  237. $this->assertTrue(Arr::has($array, 'products.desk'));
  238. $this->assertTrue(Arr::has($array, 'products.desk.price'));
  239. $this->assertFalse(Arr::has($array, 'products.foo'));
  240. $this->assertFalse(Arr::has($array, 'products.desk.foo'));
  241. $array = ['foo' => null, 'bar' => ['baz' => null]];
  242. $this->assertTrue(Arr::has($array, 'foo'));
  243. $this->assertTrue(Arr::has($array, 'bar.baz'));
  244. $array = new ArrayObject(['foo' => 10, 'bar' => new ArrayObject(['baz' => 10])]);
  245. $this->assertTrue(Arr::has($array, 'foo'));
  246. $this->assertTrue(Arr::has($array, 'bar'));
  247. $this->assertTrue(Arr::has($array, 'bar.baz'));
  248. $this->assertFalse(Arr::has($array, 'xxx'));
  249. $this->assertFalse(Arr::has($array, 'xxx.yyy'));
  250. $this->assertFalse(Arr::has($array, 'foo.xxx'));
  251. $this->assertFalse(Arr::has($array, 'bar.xxx'));
  252. $array = new ArrayObject(['foo' => null, 'bar' => new ArrayObject(['baz' => null])]);
  253. $this->assertTrue(Arr::has($array, 'foo'));
  254. $this->assertTrue(Arr::has($array, 'bar.baz'));
  255. $array = ['foo', 'bar'];
  256. $this->assertFalse(Arr::has($array, null));
  257. $this->assertFalse(Arr::has(null, 'foo'));
  258. $this->assertFalse(Arr::has(false, 'foo'));
  259. $this->assertFalse(Arr::has(null, null));
  260. $this->assertFalse(Arr::has([], null));
  261. $array = ['products' => ['desk' => ['price' => 100]]];
  262. $this->assertTrue(Arr::has($array, ['products.desk']));
  263. $this->assertTrue(Arr::has($array, ['products.desk', 'products.desk.price']));
  264. $this->assertTrue(Arr::has($array, ['products', 'products']));
  265. $this->assertFalse(Arr::has($array, ['foo']));
  266. $this->assertFalse(Arr::has($array, []));
  267. $this->assertFalse(Arr::has($array, ['products.desk', 'products.price']));
  268. $array = [
  269. 'products' => [
  270. ['name' => 'desk'],
  271. ],
  272. ];
  273. $this->assertTrue(Arr::has($array, 'products.0.name'));
  274. $this->assertFalse(Arr::has($array, 'products.0.price'));
  275. $this->assertFalse(Arr::has([], [null]));
  276. $this->assertFalse(Arr::has(null, [null]));
  277. }
  278. public function testIsAssoc()
  279. {
  280. $this->assertTrue(Arr::isAssoc(['a' => 'a', 0 => 'b']));
  281. $this->assertTrue(Arr::isAssoc([1 => 'a', 0 => 'b']));
  282. $this->assertTrue(Arr::isAssoc([1 => 'a', 2 => 'b']));
  283. $this->assertFalse(Arr::isAssoc([0 => 'a', 1 => 'b']));
  284. $this->assertFalse(Arr::isAssoc(['a', 'b']));
  285. }
  286. public function testOnly()
  287. {
  288. $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
  289. $array = Arr::only($array, ['name', 'price']);
  290. $this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
  291. }
  292. public function testPluck()
  293. {
  294. $array = [
  295. ['developer' => ['name' => 'Taylor']],
  296. ['developer' => ['name' => 'Abigail']],
  297. ];
  298. $array = Arr::pluck($array, 'developer.name');
  299. $this->assertEquals(['Taylor', 'Abigail'], $array);
  300. }
  301. public function testPluckWithArrayValue()
  302. {
  303. $array = [
  304. ['developer' => ['name' => 'Taylor']],
  305. ['developer' => ['name' => 'Abigail']],
  306. ];
  307. $array = Arr::pluck($array, ['developer', 'name']);
  308. $this->assertEquals(['Taylor', 'Abigail'], $array);
  309. }
  310. public function testPluckWithKeys()
  311. {
  312. $array = [
  313. ['name' => 'Taylor', 'role' => 'developer'],
  314. ['name' => 'Abigail', 'role' => 'developer'],
  315. ];
  316. $test1 = Arr::pluck($array, 'role', 'name');
  317. $test2 = Arr::pluck($array, null, 'name');
  318. $this->assertEquals([
  319. 'Taylor' => 'developer',
  320. 'Abigail' => 'developer',
  321. ], $test1);
  322. $this->assertEquals([
  323. 'Taylor' => ['name' => 'Taylor', 'role' => 'developer'],
  324. 'Abigail' => ['name' => 'Abigail', 'role' => 'developer'],
  325. ], $test2);
  326. }
  327. public function testPluckWithCarbonKeys()
  328. {
  329. $array = [
  330. ['start' => new Carbon('2017-07-25 00:00:00'), 'end' => new Carbon('2017-07-30 00:00:00')],
  331. ];
  332. $array = Arr::pluck($array, 'end', 'start');
  333. $this->assertEquals(['2017-07-25 00:00:00' => '2017-07-30 00:00:00'], $array);
  334. }
  335. public function testPrepend()
  336. {
  337. $array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero');
  338. $this->assertEquals(['zero', 'one', 'two', 'three', 'four'], $array);
  339. $array = Arr::prepend(['one' => 1, 'two' => 2], 0, 'zero');
  340. $this->assertEquals(['zero' => 0, 'one' => 1, 'two' => 2], $array);
  341. }
  342. public function testPull()
  343. {
  344. $array = ['name' => 'Desk', 'price' => 100];
  345. $name = Arr::pull($array, 'name');
  346. $this->assertEquals('Desk', $name);
  347. $this->assertEquals(['price' => 100], $array);
  348. // Only works on first level keys
  349. $array = ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane'];
  350. $name = Arr::pull($array, 'joe@example.com');
  351. $this->assertEquals('Joe', $name);
  352. $this->assertEquals(['jane@localhost' => 'Jane'], $array);
  353. // Does not work for nested keys
  354. $array = ['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']];
  355. $name = Arr::pull($array, 'emails.joe@example.com');
  356. $this->assertNull($name);
  357. $this->assertEquals(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array);
  358. }
  359. public function testQuery()
  360. {
  361. $this->assertSame('', Arr::query([]));
  362. $this->assertSame('foo=bar', Arr::query(['foo' => 'bar']));
  363. $this->assertSame('foo=bar&bar=baz', Arr::query(['foo' => 'bar', 'bar' => 'baz']));
  364. $this->assertSame('foo=bar&bar=1', Arr::query(['foo' => 'bar', 'bar' => true]));
  365. $this->assertSame('foo=bar', Arr::query(['foo' => 'bar', 'bar' => null]));
  366. $this->assertSame('foo=bar&bar=', Arr::query(['foo' => 'bar', 'bar' => '']));
  367. }
  368. public function testRandom()
  369. {
  370. $random = Arr::random(['foo', 'bar', 'baz']);
  371. $this->assertContains($random, ['foo', 'bar', 'baz']);
  372. $random = Arr::random(['foo', 'bar', 'baz'], 0);
  373. $this->assertIsArray($random);
  374. $this->assertCount(0, $random);
  375. $random = Arr::random(['foo', 'bar', 'baz'], 1);
  376. $this->assertIsArray($random);
  377. $this->assertCount(1, $random);
  378. $this->assertContains($random[0], ['foo', 'bar', 'baz']);
  379. $random = Arr::random(['foo', 'bar', 'baz'], 2);
  380. $this->assertIsArray($random);
  381. $this->assertCount(2, $random);
  382. $this->assertContains($random[0], ['foo', 'bar', 'baz']);
  383. $this->assertContains($random[1], ['foo', 'bar', 'baz']);
  384. $random = Arr::random(['foo', 'bar', 'baz'], '0');
  385. $this->assertIsArray($random);
  386. $this->assertCount(0, $random);
  387. $random = Arr::random(['foo', 'bar', 'baz'], '1');
  388. $this->assertIsArray($random);
  389. $this->assertCount(1, $random);
  390. $this->assertContains($random[0], ['foo', 'bar', 'baz']);
  391. $random = Arr::random(['foo', 'bar', 'baz'], '2');
  392. $this->assertIsArray($random);
  393. $this->assertCount(2, $random);
  394. $this->assertContains($random[0], ['foo', 'bar', 'baz']);
  395. $this->assertContains($random[1], ['foo', 'bar', 'baz']);
  396. }
  397. public function testRandomOnEmptyArray()
  398. {
  399. $random = Arr::random([], 0);
  400. $this->assertIsArray($random);
  401. $this->assertCount(0, $random);
  402. $random = Arr::random([], '0');
  403. $this->assertIsArray($random);
  404. $this->assertCount(0, $random);
  405. }
  406. public function testRandomThrowsAnErrorWhenRequestingMoreItemsThanAreAvailable()
  407. {
  408. $exceptions = 0;
  409. try {
  410. Arr::random([]);
  411. } catch (InvalidArgumentException $e) {
  412. $exceptions++;
  413. }
  414. try {
  415. Arr::random([], 1);
  416. } catch (InvalidArgumentException $e) {
  417. $exceptions++;
  418. }
  419. try {
  420. Arr::random([], 2);
  421. } catch (InvalidArgumentException $e) {
  422. $exceptions++;
  423. }
  424. $this->assertSame(3, $exceptions);
  425. }
  426. public function testSet()
  427. {
  428. $array = ['products' => ['desk' => ['price' => 100]]];
  429. Arr::set($array, 'products.desk.price', 200);
  430. $this->assertEquals(['products' => ['desk' => ['price' => 200]]], $array);
  431. }
  432. public function testShuffleWithSeed()
  433. {
  434. $this->assertEquals(
  435. Arr::shuffle(range(0, 100, 10), 1234),
  436. Arr::shuffle(range(0, 100, 10), 1234)
  437. );
  438. }
  439. public function testSort()
  440. {
  441. $unsorted = [
  442. ['name' => 'Desk'],
  443. ['name' => 'Chair'],
  444. ];
  445. $expected = [
  446. ['name' => 'Chair'],
  447. ['name' => 'Desk'],
  448. ];
  449. $sorted = array_values(Arr::sort($unsorted));
  450. $this->assertEquals($expected, $sorted);
  451. // sort with closure
  452. $sortedWithClosure = array_values(Arr::sort($unsorted, function ($value) {
  453. return $value['name'];
  454. }));
  455. $this->assertEquals($expected, $sortedWithClosure);
  456. // sort with dot notation
  457. $sortedWithDotNotation = array_values(Arr::sort($unsorted, 'name'));
  458. $this->assertEquals($expected, $sortedWithDotNotation);
  459. }
  460. public function testSortRecursive()
  461. {
  462. $array = [
  463. 'users' => [
  464. [
  465. // should sort associative arrays by keys
  466. 'name' => 'joe',
  467. 'mail' => 'joe@example.com',
  468. // should sort deeply nested arrays
  469. 'numbers' => [2, 1, 0],
  470. ],
  471. [
  472. 'name' => 'jane',
  473. 'age' => 25,
  474. ],
  475. ],
  476. 'repositories' => [
  477. // should use weird `sort()` behavior on arrays of arrays
  478. ['id' => 1],
  479. ['id' => 0],
  480. ],
  481. // should sort non-associative arrays by value
  482. 20 => [2, 1, 0],
  483. 30 => [
  484. // should sort non-incrementing numerical keys by keys
  485. 2 => 'a',
  486. 1 => 'b',
  487. 0 => 'c',
  488. ],
  489. ];
  490. $expect = [
  491. 20 => [0, 1, 2],
  492. 30 => [
  493. 0 => 'c',
  494. 1 => 'b',
  495. 2 => 'a',
  496. ],
  497. 'repositories' => [
  498. ['id' => 0],
  499. ['id' => 1],
  500. ],
  501. 'users' => [
  502. [
  503. 'age' => 25,
  504. 'name' => 'jane',
  505. ],
  506. [
  507. 'mail' => 'joe@example.com',
  508. 'name' => 'joe',
  509. 'numbers' => [0, 1, 2],
  510. ],
  511. ],
  512. ];
  513. $this->assertEquals($expect, Arr::sortRecursive($array));
  514. }
  515. public function testWhere()
  516. {
  517. $array = [100, '200', 300, '400', 500];
  518. $array = Arr::where($array, function ($value, $key) {
  519. return is_string($value);
  520. });
  521. $this->assertEquals([1 => 200, 3 => 400], $array);
  522. }
  523. public function testWhereKey()
  524. {
  525. $array = ['10' => 1, 'foo' => 3, 20 => 2];
  526. $array = Arr::where($array, function ($value, $key) {
  527. return is_numeric($key);
  528. });
  529. $this->assertEquals(['10' => 1, 20 => 2], $array);
  530. }
  531. public function testForget()
  532. {
  533. $array = ['products' => ['desk' => ['price' => 100]]];
  534. Arr::forget($array, null);
  535. $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array);
  536. $array = ['products' => ['desk' => ['price' => 100]]];
  537. Arr::forget($array, []);
  538. $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array);
  539. $array = ['products' => ['desk' => ['price' => 100]]];
  540. Arr::forget($array, 'products.desk');
  541. $this->assertEquals(['products' => []], $array);
  542. $array = ['products' => ['desk' => ['price' => 100]]];
  543. Arr::forget($array, 'products.desk.price');
  544. $this->assertEquals(['products' => ['desk' => []]], $array);
  545. $array = ['products' => ['desk' => ['price' => 100]]];
  546. Arr::forget($array, 'products.final.price');
  547. $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array);
  548. $array = ['shop' => ['cart' => [150 => 0]]];
  549. Arr::forget($array, 'shop.final.cart');
  550. $this->assertEquals(['shop' => ['cart' => [150 => 0]]], $array);
  551. $array = ['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]];
  552. Arr::forget($array, 'products.desk.price.taxes');
  553. $this->assertEquals(['products' => ['desk' => ['price' => ['original' => 50]]]], $array);
  554. $array = ['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]];
  555. Arr::forget($array, 'products.desk.final.taxes');
  556. $this->assertEquals(['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]], $array);
  557. $array = ['products' => ['desk' => ['price' => 50], null => 'something']];
  558. Arr::forget($array, ['products.amount.all', 'products.desk.price']);
  559. $this->assertEquals(['products' => ['desk' => [], null => 'something']], $array);
  560. // Only works on first level keys
  561. $array = ['joe@example.com' => 'Joe', 'jane@example.com' => 'Jane'];
  562. Arr::forget($array, 'joe@example.com');
  563. $this->assertEquals(['jane@example.com' => 'Jane'], $array);
  564. // Does not work for nested keys
  565. $array = ['emails' => ['joe@example.com' => ['name' => 'Joe'], 'jane@localhost' => ['name' => 'Jane']]];
  566. Arr::forget($array, ['emails.joe@example.com', 'emails.jane@localhost']);
  567. $this->assertEquals(['emails' => ['joe@example.com' => ['name' => 'Joe']]], $array);
  568. }
  569. public function testWrap()
  570. {
  571. $string = 'a';
  572. $array = ['a'];
  573. $object = new stdClass;
  574. $object->value = 'a';
  575. $this->assertEquals(['a'], Arr::wrap($string));
  576. $this->assertEquals($array, Arr::wrap($array));
  577. $this->assertEquals([$object], Arr::wrap($object));
  578. $this->assertEquals([], Arr::wrap(null));
  579. $this->assertEquals([null], Arr::wrap([null]));
  580. $this->assertEquals([null, null], Arr::wrap([null, null]));
  581. $this->assertEquals([''], Arr::wrap(''));
  582. $this->assertEquals([''], Arr::wrap(['']));
  583. $this->assertEquals([false], Arr::wrap(false));
  584. $this->assertEquals([false], Arr::wrap([false]));
  585. $this->assertEquals([0], Arr::wrap(0));
  586. $obj = new stdClass;
  587. $obj->value = 'a';
  588. $obj = unserialize(serialize($obj));
  589. $this->assertEquals([$obj], Arr::wrap($obj));
  590. $this->assertSame($obj, Arr::wrap($obj)[0]);
  591. }
  592. }