|
1
|
|
|
<?php namespace nyx\utils\tests; |
|
2
|
|
|
|
|
3
|
|
|
// Internal dependencies |
|
4
|
|
|
use nyx\utils\Arr; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Arr Tests |
|
8
|
|
|
* |
|
9
|
|
|
* @package Nyx\Utils\Tests |
|
10
|
|
|
* @version 0.0.1 |
|
11
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
12
|
|
|
* @copyright 2012-2016 Nyx Dev Team |
|
13
|
|
|
* @link http://docs.muyo.io/nyx/utils/index.html |
|
14
|
|
|
*/ |
|
15
|
|
|
class ArrTest extends \PHPUnit\Framework\TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
// Arr::add() |
|
|
|
|
|
|
18
|
|
|
public function testArrayAdd() |
|
19
|
|
|
{ |
|
20
|
|
|
$source = ['alpha' => 'foo']; |
|
21
|
|
|
$target = ['alpha' => 'foo', 'beta' => 'bar']; |
|
22
|
|
|
|
|
23
|
|
|
// Add once. Should set the 'beta' key to the 'bar' string. |
|
24
|
|
|
Arr::add($source, 'beta', 'bar'); |
|
25
|
|
|
$this->assertEquals($target, $source); |
|
26
|
|
|
|
|
27
|
|
|
// Add again. Should not change the source array anymore. |
|
28
|
|
|
Arr::add($source, 'beta', 'bar'); |
|
29
|
|
|
$this->assertEquals($target, $source); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// Arr::last() |
|
|
|
|
|
|
33
|
|
|
public function testArrayLast() |
|
34
|
|
|
{ |
|
35
|
|
|
$source = [1, 2, 3, 4, 5]; |
|
36
|
|
|
|
|
37
|
|
|
// Early return on empty array - should return null (default value) |
|
38
|
|
|
$this->assertNull(Arr::last([])); |
|
39
|
|
|
|
|
40
|
|
|
// Early return on empty array - should return the given default value. |
|
41
|
|
|
$this->assertEquals('default', Arr::last([], 5, 'default')); |
|
42
|
|
|
|
|
43
|
|
|
// Return the last element (default behaviour) - should return '5'. |
|
44
|
|
|
$this->assertEquals(5, Arr::last($source)); |
|
45
|
|
|
|
|
46
|
|
|
// Return the last n elements |
|
47
|
|
|
$this->assertEquals(5, Arr::last($source, 1)); |
|
48
|
|
|
$this->assertEquals([3, 4, 5], Arr::last($source, 3)); |
|
49
|
|
|
$this->assertEquals([2, 3, 4, 5], Arr::last($source, 4)); |
|
50
|
|
|
|
|
51
|
|
|
// Return more elements than there actually are in $source - should return full $source |
|
52
|
|
|
$this->assertEquals($source, Arr::last($source, 10)); |
|
53
|
|
|
|
|
54
|
|
|
// -- Slicing with a truth test - this one will always fail, should return default value instead. |
|
55
|
|
|
$truthTest = function($value, $key) { |
|
|
|
|
|
|
56
|
|
|
return $value === 'does_not_exist_in_$source'; |
|
57
|
|
|
}; |
|
58
|
|
|
|
|
59
|
|
|
$this->assertNull(Arr::last($source, $truthTest)); |
|
60
|
|
|
$this->assertEquals('default', Arr::last($source, $truthTest, 'default')); |
|
61
|
|
|
|
|
62
|
|
|
// -- Slicing with a truth test - this one should return the first value smaller than 4 and nothing else. |
|
63
|
|
|
$truthTest = function($value, $key) { |
|
|
|
|
|
|
64
|
|
|
return $value < 4; |
|
65
|
|
|
}; |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals(3, Arr::last($source, $truthTest)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Arr::set() |
|
|
|
|
|
|
71
|
|
|
public function testArraySet() |
|
72
|
|
|
{ |
|
73
|
|
|
// Set once. Should set the 'beta' key to the 'bar' string. |
|
74
|
|
|
$source = ['alpha' => 'foo']; |
|
75
|
|
|
$target = ['alpha' => 'foo', 'beta' => 'bar']; |
|
76
|
|
|
|
|
77
|
|
|
Arr::set($source, 'beta', 'bar'); |
|
78
|
|
|
$this->assertEquals($target, $source); |
|
79
|
|
|
|
|
80
|
|
|
// Set again. Should change the source array for the 'beta' key. |
|
81
|
|
|
$target = ['alpha' => 'foo', 'beta' => 'baz']; |
|
82
|
|
|
|
|
83
|
|
|
Arr::set($source, 'beta', 'baz'); |
|
84
|
|
|
$this->assertEquals($target, $source); |
|
85
|
|
|
|
|
86
|
|
|
// Set with a delimited key. Should overwrite the 'beta' key to make |
|
87
|
|
|
// it an array holding the 'nested' array with a 'key' key => 'baz' |
|
88
|
|
|
$target = ['alpha' => 'foo', 'beta' => [ |
|
89
|
|
|
'nested' => [ |
|
90
|
|
|
'key' => 'baz' |
|
91
|
|
|
] |
|
92
|
|
|
]]; |
|
93
|
|
|
|
|
94
|
|
|
Arr::set($source, 'beta.nested.key', 'baz'); |
|
95
|
|
|
$this->assertEquals($target, $source); |
|
96
|
|
|
|
|
97
|
|
|
// Set with a delimited key with a custom delimiter. Should overwrite |
|
98
|
|
|
// the beta.nested.key from the previous test from baz to ugachaka. |
|
99
|
|
|
$target = ['alpha' => 'foo', 'beta' => [ |
|
100
|
|
|
'nested' => [ |
|
101
|
|
|
'key' => 'ugachaka' |
|
102
|
|
|
] |
|
103
|
|
|
]]; |
|
104
|
|
|
|
|
105
|
|
|
Arr::set($source, 'beta::nested::key', 'ugachaka', '::'); |
|
106
|
|
|
$this->assertEquals($target, $source); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.