| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- <?php
- namespace Tightenco\Collect\Tests\Support;
- use stdClass;
- use ArrayObject;
- use Tightenco\Collect\Support\Arr;
- use InvalidArgumentException;
- use Tightenco\Collect\Support\Carbon;
- use PHPUnit\Framework\TestCase;
- use Tightenco\Collect\Support\Collection;
- class SupportArrTest extends TestCase
- {
- public function testAccessible()
- {
- $this->assertTrue(Arr::accessible([]));
- $this->assertTrue(Arr::accessible([1, 2]));
- $this->assertTrue(Arr::accessible(['a' => 1, 'b' => 2]));
- $this->assertTrue(Arr::accessible(new Collection));
- $this->assertFalse(Arr::accessible(null));
- $this->assertFalse(Arr::accessible('abc'));
- $this->assertFalse(Arr::accessible(new stdClass));
- $this->assertFalse(Arr::accessible((object) ['a' => 1, 'b' => 2]));
- }
- public function testAdd()
- {
- $array = Arr::add(['name' => 'Desk'], 'price', 100);
- $this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
- }
- public function testCollapse()
- {
- $data = [['foo', 'bar'], ['baz']];
- $this->assertEquals(['foo', 'bar', 'baz'], Arr::collapse($data));
- }
- public function testCrossJoin()
- {
- // Single dimension
- $this->assertSame(
- [[1, 'a'], [1, 'b'], [1, 'c']],
- Arr::crossJoin([1], ['a', 'b', 'c'])
- );
- // Square matrix
- $this->assertSame(
- [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']],
- Arr::crossJoin([1, 2], ['a', 'b'])
- );
- // Rectangular matrix
- $this->assertSame(
- [[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c']],
- Arr::crossJoin([1, 2], ['a', 'b', 'c'])
- );
- // 3D matrix
- $this->assertSame(
- [
- [1, 'a', 'I'], [1, 'a', 'II'], [1, 'a', 'III'],
- [1, 'b', 'I'], [1, 'b', 'II'], [1, 'b', 'III'],
- [2, 'a', 'I'], [2, 'a', 'II'], [2, 'a', 'III'],
- [2, 'b', 'I'], [2, 'b', 'II'], [2, 'b', 'III'],
- ],
- Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II', 'III'])
- );
- // With 1 empty dimension
- $this->assertEmpty(Arr::crossJoin([], ['a', 'b'], ['I', 'II', 'III']));
- $this->assertEmpty(Arr::crossJoin([1, 2], [], ['I', 'II', 'III']));
- $this->assertEmpty(Arr::crossJoin([1, 2], ['a', 'b'], []));
- // With empty arrays
- $this->assertEmpty(Arr::crossJoin([], [], []));
- $this->assertEmpty(Arr::crossJoin([], []));
- $this->assertEmpty(Arr::crossJoin([]));
- // Not really a proper usage, still, test for preserving BC
- $this->assertSame([[]], Arr::crossJoin());
- }
- public function testDivide()
- {
- [$keys, $values] = Arr::divide(['name' => 'Desk']);
- $this->assertEquals(['name'], $keys);
- $this->assertEquals(['Desk'], $values);
- }
- public function testDot()
- {
- $array = Arr::dot(['foo' => ['bar' => 'baz']]);
- $this->assertEquals(['foo.bar' => 'baz'], $array);
- $array = Arr::dot([]);
- $this->assertEquals([], $array);
- $array = Arr::dot(['foo' => []]);
- $this->assertEquals(['foo' => []], $array);
- $array = Arr::dot(['foo' => ['bar' => []]]);
- $this->assertEquals(['foo.bar' => []], $array);
- }
- public function testExcept()
- {
- $array = ['name' => 'Desk', 'price' => 100];
- $array = Arr::except($array, ['price']);
- $this->assertEquals(['name' => 'Desk'], $array);
- }
- public function testExists()
- {
- $this->assertTrue(Arr::exists([1], 0));
- $this->assertTrue(Arr::exists([null], 0));
- $this->assertTrue(Arr::exists(['a' => 1], 'a'));
- $this->assertTrue(Arr::exists(['a' => null], 'a'));
- $this->assertTrue(Arr::exists(new Collection(['a' => null]), 'a'));
- $this->assertFalse(Arr::exists([1], 1));
- $this->assertFalse(Arr::exists([null], 1));
- $this->assertFalse(Arr::exists(['a' => 1], 0));
- $this->assertFalse(Arr::exists(new Collection(['a' => null]), 'b'));
- }
- public function testFirst()
- {
- $array = [100, 200, 300];
- $value = Arr::first($array, function ($value) {
- return $value >= 150;
- });
- $this->assertEquals(200, $value);
- $this->assertEquals(100, Arr::first($array));
- }
- public function testLast()
- {
- $array = [100, 200, 300];
- $last = Arr::last($array, function ($value) {
- return $value < 250;
- });
- $this->assertEquals(200, $last);
- $last = Arr::last($array, function ($value, $key) {
- return $key < 2;
- });
- $this->assertEquals(200, $last);
- $this->assertEquals(300, Arr::last($array));
- }
- public function testFlatten()
- {
- // Flat arrays are unaffected
- $array = ['#foo', '#bar', '#baz'];
- $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
- // Nested arrays are flattened with existing flat items
- $array = [['#foo', '#bar'], '#baz'];
- $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
- // Flattened array includes "null" items
- $array = [['#foo', null], '#baz', null];
- $this->assertEquals(['#foo', null, '#baz', null], Arr::flatten($array));
- // Sets of nested arrays are flattened
- $array = [['#foo', '#bar'], ['#baz']];
- $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
- // Deeply nested arrays are flattened
- $array = [['#foo', ['#bar']], ['#baz']];
- $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
- // Nested arrays are flattened alongside arrays
- $array = [new Collection(['#foo', '#bar']), ['#baz']];
- $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
- // Nested arrays containing plain arrays are flattened
- $array = [new Collection(['#foo', ['#bar']]), ['#baz']];
- $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
- // Nested arrays containing arrays are flattened
- $array = [['#foo', new Collection(['#bar'])], ['#baz']];
- $this->assertEquals(['#foo', '#bar', '#baz'], Arr::flatten($array));
- // Nested arrays containing arrays containing arrays are flattened
- $array = [['#foo', new Collection(['#bar', ['#zap']])], ['#baz']];
- $this->assertEquals(['#foo', '#bar', '#zap', '#baz'], Arr::flatten($array));
- }
- public function testFlattenWithDepth()
- {
- // No depth flattens recursively
- $array = [['#foo', ['#bar', ['#baz']]], '#zap'];
- $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], Arr::flatten($array));
- // Specifying a depth only flattens to that depth
- $array = [['#foo', ['#bar', ['#baz']]], '#zap'];
- $this->assertEquals(['#foo', ['#bar', ['#baz']], '#zap'], Arr::flatten($array, 1));
- $array = [['#foo', ['#bar', ['#baz']]], '#zap'];
- $this->assertEquals(['#foo', '#bar', ['#baz'], '#zap'], Arr::flatten($array, 2));
- }
- public function testGet()
- {
- $array = ['products.desk' => ['price' => 100]];
- $this->assertEquals(['price' => 100], Arr::get($array, 'products.desk'));
- $array = ['products' => ['desk' => ['price' => 100]]];
- $value = Arr::get($array, 'products.desk');
- $this->assertEquals(['price' => 100], $value);
- // Test null array values
- $array = ['foo' => null, 'bar' => ['baz' => null]];
- $this->assertNull(Arr::get($array, 'foo', 'default'));
- $this->assertNull(Arr::get($array, 'bar.baz', 'default'));
- // Test direct ArrayAccess object
- $array = ['products' => ['desk' => ['price' => 100]]];
- $arrayAccessObject = new ArrayObject($array);
- $value = Arr::get($arrayAccessObject, 'products.desk');
- $this->assertEquals(['price' => 100], $value);
- // Test array containing ArrayAccess object
- $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]);
- $array = ['child' => $arrayAccessChild];
- $value = Arr::get($array, 'child.products.desk');
- $this->assertEquals(['price' => 100], $value);
- // Test array containing multiple nested ArrayAccess objects
- $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]);
- $arrayAccessParent = new ArrayObject(['child' => $arrayAccessChild]);
- $array = ['parent' => $arrayAccessParent];
- $value = Arr::get($array, 'parent.child.products.desk');
- $this->assertEquals(['price' => 100], $value);
- // Test missing ArrayAccess object field
- $arrayAccessChild = new ArrayObject(['products' => ['desk' => ['price' => 100]]]);
- $arrayAccessParent = new ArrayObject(['child' => $arrayAccessChild]);
- $array = ['parent' => $arrayAccessParent];
- $value = Arr::get($array, 'parent.child.desk');
- $this->assertNull($value);
- // Test missing ArrayAccess object field
- $arrayAccessObject = new ArrayObject(['products' => ['desk' => null]]);
- $array = ['parent' => $arrayAccessObject];
- $value = Arr::get($array, 'parent.products.desk.price');
- $this->assertNull($value);
- // Test null ArrayAccess object fields
- $array = new ArrayObject(['foo' => null, 'bar' => new ArrayObject(['baz' => null])]);
- $this->assertNull(Arr::get($array, 'foo', 'default'));
- $this->assertNull(Arr::get($array, 'bar.baz', 'default'));
- // Test null key returns the whole array
- $array = ['foo', 'bar'];
- $this->assertEquals($array, Arr::get($array, null));
- // Test $array not an array
- $this->assertSame('default', Arr::get(null, 'foo', 'default'));
- $this->assertSame('default', Arr::get(false, 'foo', 'default'));
- // Test $array not an array and key is null
- $this->assertSame('default', Arr::get(null, null, 'default'));
- // Test $array is empty and key is null
- $this->assertEmpty(Arr::get([], null));
- $this->assertEmpty(Arr::get([], null, 'default'));
- // Test numeric keys
- $array = [
- 'products' => [
- ['name' => 'desk'],
- ['name' => 'chair'],
- ],
- ];
- $this->assertEquals('desk', Arr::get($array, 'products.0.name'));
- $this->assertEquals('chair', Arr::get($array, 'products.1.name'));
- }
- public function testHas()
- {
- $array = ['products.desk' => ['price' => 100]];
- $this->assertTrue(Arr::has($array, 'products.desk'));
- $array = ['products' => ['desk' => ['price' => 100]]];
- $this->assertTrue(Arr::has($array, 'products.desk'));
- $this->assertTrue(Arr::has($array, 'products.desk.price'));
- $this->assertFalse(Arr::has($array, 'products.foo'));
- $this->assertFalse(Arr::has($array, 'products.desk.foo'));
- $array = ['foo' => null, 'bar' => ['baz' => null]];
- $this->assertTrue(Arr::has($array, 'foo'));
- $this->assertTrue(Arr::has($array, 'bar.baz'));
- $array = new ArrayObject(['foo' => 10, 'bar' => new ArrayObject(['baz' => 10])]);
- $this->assertTrue(Arr::has($array, 'foo'));
- $this->assertTrue(Arr::has($array, 'bar'));
- $this->assertTrue(Arr::has($array, 'bar.baz'));
- $this->assertFalse(Arr::has($array, 'xxx'));
- $this->assertFalse(Arr::has($array, 'xxx.yyy'));
- $this->assertFalse(Arr::has($array, 'foo.xxx'));
- $this->assertFalse(Arr::has($array, 'bar.xxx'));
- $array = new ArrayObject(['foo' => null, 'bar' => new ArrayObject(['baz' => null])]);
- $this->assertTrue(Arr::has($array, 'foo'));
- $this->assertTrue(Arr::has($array, 'bar.baz'));
- $array = ['foo', 'bar'];
- $this->assertFalse(Arr::has($array, null));
- $this->assertFalse(Arr::has(null, 'foo'));
- $this->assertFalse(Arr::has(false, 'foo'));
- $this->assertFalse(Arr::has(null, null));
- $this->assertFalse(Arr::has([], null));
- $array = ['products' => ['desk' => ['price' => 100]]];
- $this->assertTrue(Arr::has($array, ['products.desk']));
- $this->assertTrue(Arr::has($array, ['products.desk', 'products.desk.price']));
- $this->assertTrue(Arr::has($array, ['products', 'products']));
- $this->assertFalse(Arr::has($array, ['foo']));
- $this->assertFalse(Arr::has($array, []));
- $this->assertFalse(Arr::has($array, ['products.desk', 'products.price']));
- $array = [
- 'products' => [
- ['name' => 'desk'],
- ],
- ];
- $this->assertTrue(Arr::has($array, 'products.0.name'));
- $this->assertFalse(Arr::has($array, 'products.0.price'));
- $this->assertFalse(Arr::has([], [null]));
- $this->assertFalse(Arr::has(null, [null]));
- }
- public function testIsAssoc()
- {
- $this->assertTrue(Arr::isAssoc(['a' => 'a', 0 => 'b']));
- $this->assertTrue(Arr::isAssoc([1 => 'a', 0 => 'b']));
- $this->assertTrue(Arr::isAssoc([1 => 'a', 2 => 'b']));
- $this->assertFalse(Arr::isAssoc([0 => 'a', 1 => 'b']));
- $this->assertFalse(Arr::isAssoc(['a', 'b']));
- }
- public function testOnly()
- {
- $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
- $array = Arr::only($array, ['name', 'price']);
- $this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
- }
- public function testPluck()
- {
- $array = [
- ['developer' => ['name' => 'Taylor']],
- ['developer' => ['name' => 'Abigail']],
- ];
- $array = Arr::pluck($array, 'developer.name');
- $this->assertEquals(['Taylor', 'Abigail'], $array);
- }
- public function testPluckWithArrayValue()
- {
- $array = [
- ['developer' => ['name' => 'Taylor']],
- ['developer' => ['name' => 'Abigail']],
- ];
- $array = Arr::pluck($array, ['developer', 'name']);
- $this->assertEquals(['Taylor', 'Abigail'], $array);
- }
- public function testPluckWithKeys()
- {
- $array = [
- ['name' => 'Taylor', 'role' => 'developer'],
- ['name' => 'Abigail', 'role' => 'developer'],
- ];
- $test1 = Arr::pluck($array, 'role', 'name');
- $test2 = Arr::pluck($array, null, 'name');
- $this->assertEquals([
- 'Taylor' => 'developer',
- 'Abigail' => 'developer',
- ], $test1);
- $this->assertEquals([
- 'Taylor' => ['name' => 'Taylor', 'role' => 'developer'],
- 'Abigail' => ['name' => 'Abigail', 'role' => 'developer'],
- ], $test2);
- }
- public function testPluckWithCarbonKeys()
- {
- $array = [
- ['start' => new Carbon('2017-07-25 00:00:00'), 'end' => new Carbon('2017-07-30 00:00:00')],
- ];
- $array = Arr::pluck($array, 'end', 'start');
- $this->assertEquals(['2017-07-25 00:00:00' => '2017-07-30 00:00:00'], $array);
- }
- public function testPrepend()
- {
- $array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero');
- $this->assertEquals(['zero', 'one', 'two', 'three', 'four'], $array);
- $array = Arr::prepend(['one' => 1, 'two' => 2], 0, 'zero');
- $this->assertEquals(['zero' => 0, 'one' => 1, 'two' => 2], $array);
- }
- public function testPull()
- {
- $array = ['name' => 'Desk', 'price' => 100];
- $name = Arr::pull($array, 'name');
- $this->assertEquals('Desk', $name);
- $this->assertEquals(['price' => 100], $array);
- // Only works on first level keys
- $array = ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane'];
- $name = Arr::pull($array, 'joe@example.com');
- $this->assertEquals('Joe', $name);
- $this->assertEquals(['jane@localhost' => 'Jane'], $array);
- // Does not work for nested keys
- $array = ['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']];
- $name = Arr::pull($array, 'emails.joe@example.com');
- $this->assertNull($name);
- $this->assertEquals(['emails' => ['joe@example.com' => 'Joe', 'jane@localhost' => 'Jane']], $array);
- }
- public function testQuery()
- {
- $this->assertSame('', Arr::query([]));
- $this->assertSame('foo=bar', Arr::query(['foo' => 'bar']));
- $this->assertSame('foo=bar&bar=baz', Arr::query(['foo' => 'bar', 'bar' => 'baz']));
- $this->assertSame('foo=bar&bar=1', Arr::query(['foo' => 'bar', 'bar' => true]));
- $this->assertSame('foo=bar', Arr::query(['foo' => 'bar', 'bar' => null]));
- $this->assertSame('foo=bar&bar=', Arr::query(['foo' => 'bar', 'bar' => '']));
- }
- public function testRandom()
- {
- $random = Arr::random(['foo', 'bar', 'baz']);
- $this->assertContains($random, ['foo', 'bar', 'baz']);
- $random = Arr::random(['foo', 'bar', 'baz'], 0);
- $this->assertIsArray($random);
- $this->assertCount(0, $random);
- $random = Arr::random(['foo', 'bar', 'baz'], 1);
- $this->assertIsArray($random);
- $this->assertCount(1, $random);
- $this->assertContains($random[0], ['foo', 'bar', 'baz']);
- $random = Arr::random(['foo', 'bar', 'baz'], 2);
- $this->assertIsArray($random);
- $this->assertCount(2, $random);
- $this->assertContains($random[0], ['foo', 'bar', 'baz']);
- $this->assertContains($random[1], ['foo', 'bar', 'baz']);
- $random = Arr::random(['foo', 'bar', 'baz'], '0');
- $this->assertIsArray($random);
- $this->assertCount(0, $random);
- $random = Arr::random(['foo', 'bar', 'baz'], '1');
- $this->assertIsArray($random);
- $this->assertCount(1, $random);
- $this->assertContains($random[0], ['foo', 'bar', 'baz']);
- $random = Arr::random(['foo', 'bar', 'baz'], '2');
- $this->assertIsArray($random);
- $this->assertCount(2, $random);
- $this->assertContains($random[0], ['foo', 'bar', 'baz']);
- $this->assertContains($random[1], ['foo', 'bar', 'baz']);
- }
- public function testRandomOnEmptyArray()
- {
- $random = Arr::random([], 0);
- $this->assertIsArray($random);
- $this->assertCount(0, $random);
- $random = Arr::random([], '0');
- $this->assertIsArray($random);
- $this->assertCount(0, $random);
- }
- public function testRandomThrowsAnErrorWhenRequestingMoreItemsThanAreAvailable()
- {
- $exceptions = 0;
- try {
- Arr::random([]);
- } catch (InvalidArgumentException $e) {
- $exceptions++;
- }
- try {
- Arr::random([], 1);
- } catch (InvalidArgumentException $e) {
- $exceptions++;
- }
- try {
- Arr::random([], 2);
- } catch (InvalidArgumentException $e) {
- $exceptions++;
- }
- $this->assertSame(3, $exceptions);
- }
- public function testSet()
- {
- $array = ['products' => ['desk' => ['price' => 100]]];
- Arr::set($array, 'products.desk.price', 200);
- $this->assertEquals(['products' => ['desk' => ['price' => 200]]], $array);
- }
- public function testShuffleWithSeed()
- {
- $this->assertEquals(
- Arr::shuffle(range(0, 100, 10), 1234),
- Arr::shuffle(range(0, 100, 10), 1234)
- );
- }
- public function testSort()
- {
- $unsorted = [
- ['name' => 'Desk'],
- ['name' => 'Chair'],
- ];
- $expected = [
- ['name' => 'Chair'],
- ['name' => 'Desk'],
- ];
- $sorted = array_values(Arr::sort($unsorted));
- $this->assertEquals($expected, $sorted);
- // sort with closure
- $sortedWithClosure = array_values(Arr::sort($unsorted, function ($value) {
- return $value['name'];
- }));
- $this->assertEquals($expected, $sortedWithClosure);
- // sort with dot notation
- $sortedWithDotNotation = array_values(Arr::sort($unsorted, 'name'));
- $this->assertEquals($expected, $sortedWithDotNotation);
- }
- public function testSortRecursive()
- {
- $array = [
- 'users' => [
- [
- // should sort associative arrays by keys
- 'name' => 'joe',
- 'mail' => 'joe@example.com',
- // should sort deeply nested arrays
- 'numbers' => [2, 1, 0],
- ],
- [
- 'name' => 'jane',
- 'age' => 25,
- ],
- ],
- 'repositories' => [
- // should use weird `sort()` behavior on arrays of arrays
- ['id' => 1],
- ['id' => 0],
- ],
- // should sort non-associative arrays by value
- 20 => [2, 1, 0],
- 30 => [
- // should sort non-incrementing numerical keys by keys
- 2 => 'a',
- 1 => 'b',
- 0 => 'c',
- ],
- ];
- $expect = [
- 20 => [0, 1, 2],
- 30 => [
- 0 => 'c',
- 1 => 'b',
- 2 => 'a',
- ],
- 'repositories' => [
- ['id' => 0],
- ['id' => 1],
- ],
- 'users' => [
- [
- 'age' => 25,
- 'name' => 'jane',
- ],
- [
- 'mail' => 'joe@example.com',
- 'name' => 'joe',
- 'numbers' => [0, 1, 2],
- ],
- ],
- ];
- $this->assertEquals($expect, Arr::sortRecursive($array));
- }
- public function testWhere()
- {
- $array = [100, '200', 300, '400', 500];
- $array = Arr::where($array, function ($value, $key) {
- return is_string($value);
- });
- $this->assertEquals([1 => 200, 3 => 400], $array);
- }
- public function testWhereKey()
- {
- $array = ['10' => 1, 'foo' => 3, 20 => 2];
- $array = Arr::where($array, function ($value, $key) {
- return is_numeric($key);
- });
- $this->assertEquals(['10' => 1, 20 => 2], $array);
- }
- public function testForget()
- {
- $array = ['products' => ['desk' => ['price' => 100]]];
- Arr::forget($array, null);
- $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array);
- $array = ['products' => ['desk' => ['price' => 100]]];
- Arr::forget($array, []);
- $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array);
- $array = ['products' => ['desk' => ['price' => 100]]];
- Arr::forget($array, 'products.desk');
- $this->assertEquals(['products' => []], $array);
- $array = ['products' => ['desk' => ['price' => 100]]];
- Arr::forget($array, 'products.desk.price');
- $this->assertEquals(['products' => ['desk' => []]], $array);
- $array = ['products' => ['desk' => ['price' => 100]]];
- Arr::forget($array, 'products.final.price');
- $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array);
- $array = ['shop' => ['cart' => [150 => 0]]];
- Arr::forget($array, 'shop.final.cart');
- $this->assertEquals(['shop' => ['cart' => [150 => 0]]], $array);
- $array = ['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]];
- Arr::forget($array, 'products.desk.price.taxes');
- $this->assertEquals(['products' => ['desk' => ['price' => ['original' => 50]]]], $array);
- $array = ['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]];
- Arr::forget($array, 'products.desk.final.taxes');
- $this->assertEquals(['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]], $array);
- $array = ['products' => ['desk' => ['price' => 50], null => 'something']];
- Arr::forget($array, ['products.amount.all', 'products.desk.price']);
- $this->assertEquals(['products' => ['desk' => [], null => 'something']], $array);
- // Only works on first level keys
- $array = ['joe@example.com' => 'Joe', 'jane@example.com' => 'Jane'];
- Arr::forget($array, 'joe@example.com');
- $this->assertEquals(['jane@example.com' => 'Jane'], $array);
- // Does not work for nested keys
- $array = ['emails' => ['joe@example.com' => ['name' => 'Joe'], 'jane@localhost' => ['name' => 'Jane']]];
- Arr::forget($array, ['emails.joe@example.com', 'emails.jane@localhost']);
- $this->assertEquals(['emails' => ['joe@example.com' => ['name' => 'Joe']]], $array);
- }
- public function testWrap()
- {
- $string = 'a';
- $array = ['a'];
- $object = new stdClass;
- $object->value = 'a';
- $this->assertEquals(['a'], Arr::wrap($string));
- $this->assertEquals($array, Arr::wrap($array));
- $this->assertEquals([$object], Arr::wrap($object));
- $this->assertEquals([], Arr::wrap(null));
- $this->assertEquals([null], Arr::wrap([null]));
- $this->assertEquals([null, null], Arr::wrap([null, null]));
- $this->assertEquals([''], Arr::wrap(''));
- $this->assertEquals([''], Arr::wrap(['']));
- $this->assertEquals([false], Arr::wrap(false));
- $this->assertEquals([false], Arr::wrap([false]));
- $this->assertEquals([0], Arr::wrap(0));
- $obj = new stdClass;
- $obj->value = 'a';
- $obj = unserialize(serialize($obj));
- $this->assertEquals([$obj], Arr::wrap($obj));
- $this->assertSame($obj, Arr::wrap($obj)[0]);
- }
- }
|