Skip to content

Commit 275dc08

Browse files
author
Nguyễn Xuân Quỳnh
committed
Move Arr tests to the right location
1 parent c70f52f commit 275dc08

2 files changed

Lines changed: 99 additions & 278 deletions

File tree

tests/Support/SupportArrTest.php

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ public function testAdd()
2929
{
3030
$array = Arr::add(['name' => 'Desk'], 'price', 100);
3131
$this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
32+
33+
$this->assertEquals(['surname' => 'Mövsümov'], Arr::add([], 'surname', 'Mövsümov'));
34+
$this->assertEquals(['developer' => ['name' => 'Ferid']], Arr::add([], 'developer.name', 'Ferid'));
3235
}
3336

3437
public function testCollapse()
3538
{
3639
$data = [['foo', 'bar'], ['baz']];
3740
$this->assertEquals(['foo', 'bar', 'baz'], Arr::collapse($data));
41+
42+
$array = [[1], [2], [3], ['foo', 'bar'], collect(['baz', 'boom'])];
43+
$this->assertEquals([1, 2, 3, 'foo', 'bar', 'baz', 'boom'], Arr::collapse($array));
3844
}
3945

4046
public function testCrossJoin()
@@ -102,13 +108,21 @@ public function testDot()
102108

103109
$array = Arr::dot(['foo' => ['bar' => []]]);
104110
$this->assertEquals(['foo.bar' => []], $array);
111+
112+
$array = Arr::dot(['name' => 'taylor', 'languages' => ['php' => true]]);
113+
$this->assertEquals($array, ['name' => 'taylor', 'languages.php' => true]);
105114
}
106115

107116
public function testExcept()
108117
{
109-
$array = ['name' => 'Desk', 'price' => 100];
110-
$array = Arr::except($array, ['price']);
111-
$this->assertEquals(['name' => 'Desk'], $array);
118+
$array = ['name' => 'taylor', 'age' => 26];
119+
$this->assertEquals(['age' => 26], Arr::except($array, ['name']));
120+
$this->assertEquals(['age' => 26], Arr::except($array, 'name'));
121+
122+
$array = ['name' => 'taylor', 'framework' => ['language' => 'PHP', 'name' => 'Laravel']];
123+
$this->assertEquals(['name' => 'taylor'], Arr::except($array, 'framework'));
124+
$this->assertEquals(['name' => 'taylor', 'framework' => ['name' => 'Laravel']], Arr::except($array, 'framework.language'));
125+
$this->assertEquals(['framework' => ['language' => 'PHP']], Arr::except($array, ['name', 'framework.name']));
112126
}
113127

114128
public function testExists()
@@ -282,6 +296,13 @@ public function testGet()
282296
];
283297
$this->assertEquals('desk', Arr::get($array, 'products.0.name'));
284298
$this->assertEquals('chair', Arr::get($array, 'products.1.name'));
299+
300+
// Test return default value for non-existing key.
301+
$array = ['names' => ['developer' => 'taylor']];
302+
$this->assertEquals('dayle', Arr::get($array, 'names.otherDeveloper', 'dayle'));
303+
$this->assertEquals('dayle', Arr::get($array, 'names.otherDeveloper', function () {
304+
return 'dayle';
305+
}));
285306
}
286307

287308
public function testHas()
@@ -361,10 +382,45 @@ public function testOnly()
361382
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
362383
$array = Arr::only($array, ['name', 'price']);
363384
$this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
385+
$this->assertEmpty(Arr::only($array, ['nonExistingKey']));
364386
}
365387

366388
public function testPluck()
367389
{
390+
$data = [
391+
'post-1' => [
392+
'comments' => [
393+
'tags' => [
394+
'#foo', '#bar',
395+
],
396+
],
397+
],
398+
'post-2' => [
399+
'comments' => [
400+
'tags' => [
401+
'#baz',
402+
],
403+
],
404+
],
405+
];
406+
407+
$this->assertEquals([
408+
0 => [
409+
'tags' => [
410+
'#foo', '#bar',
411+
],
412+
],
413+
1 => [
414+
'tags' => [
415+
'#baz',
416+
],
417+
],
418+
], Arr::pluck($data, 'comments'));
419+
420+
$this->assertEquals([['#foo', '#bar'], ['#baz']], Arr::pluck($data, 'comments.tags'));
421+
$this->assertEquals([null, null], Arr::pluck($data, 'foo'));
422+
$this->assertEquals([null, null], Arr::pluck($data, 'foo.bar'));
423+
368424
$array = [
369425
['developer' => ['name' => 'Taylor']],
370426
['developer' => ['name' => 'Abigail']],
@@ -415,6 +471,45 @@ public function testPluckWithCarbonKeys()
415471
$this->assertEquals(['2017-07-25 00:00:00' => '2017-07-30 00:00:00'], $array);
416472
}
417473

474+
public function testArrayPluckWithArrayAndObjectValues()
475+
{
476+
$array = [(object) ['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']];
477+
$this->assertEquals(['taylor', 'dayle'], Arr::pluck($array, 'name'));
478+
$this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], Arr::pluck($array, 'email', 'name'));
479+
}
480+
481+
public function testArrayPluckWithNestedKeys()
482+
{
483+
$array = [['user' => ['taylor', 'otwell']], ['user' => ['dayle', 'rees']]];
484+
$this->assertEquals(['taylor', 'dayle'], Arr::pluck($array, 'user.0'));
485+
$this->assertEquals(['taylor', 'dayle'], Arr::pluck($array, ['user', 0]));
486+
$this->assertEquals(['taylor' => 'otwell', 'dayle' => 'rees'], Arr::pluck($array, 'user.1', 'user.0'));
487+
$this->assertEquals(['taylor' => 'otwell', 'dayle' => 'rees'], Arr::pluck($array, ['user', 1], ['user', 0]));
488+
}
489+
490+
public function testArrayPluckWithNestedArrays()
491+
{
492+
$array = [
493+
[
494+
'account' => 'a',
495+
'users' => [
496+
['first' => 'taylor', 'last' => 'otwell', 'email' => 'taylorotwell@gmail.com'],
497+
],
498+
],
499+
[
500+
'account' => 'b',
501+
'users' => [
502+
['first' => 'abigail', 'last' => 'otwell'],
503+
['first' => 'dayle', 'last' => 'rees'],
504+
],
505+
],
506+
];
507+
508+
$this->assertEquals([['taylor'], ['abigail', 'dayle']], Arr::pluck($array, 'users.*.first'));
509+
$this->assertEquals(['a' => ['taylor'], 'b' => ['abigail', 'dayle']], Arr::pluck($array, 'users.*.first', 'account'));
510+
$this->assertEquals([['taylorotwell@gmail.com'], [null, null]], Arr::pluck($array, 'users.*.email'));
511+
}
512+
418513
public function testPrepend()
419514
{
420515
$array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero');
@@ -633,7 +728,7 @@ public function testWhere()
633728
return is_string($value);
634729
});
635730

636-
$this->assertEquals([1 => 200, 3 => 400], $array);
731+
$this->assertEquals([1 => '200', 3 => '400'], $array);
637732
}
638733

639734
public function testWhereKey()

0 commit comments

Comments
 (0)