1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koded\Stdlib; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
class ExtendedArgumentsTest extends TestCase |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @dataProvider data |
12
|
|
|
*/ |
13
|
|
|
public function test_should_flatten_the_array($data) |
14
|
|
|
{ |
15
|
|
|
$expected = new ExtendedArguments([ |
16
|
|
|
'key1' => 'A', |
17
|
|
|
'key2.key21' => 'B', |
18
|
|
|
'key4.key41.key411' => 'D', |
19
|
|
|
'key4.key41.key412' => 'E', |
20
|
|
|
'key4.key42' => 'F', |
21
|
|
|
'key3.0' => 'A', |
22
|
|
|
'key3.1' => 'B', |
23
|
|
|
'key3.2' => 'C', |
24
|
|
|
'key5' => null, |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
$arguments = new ExtendedArguments($data); |
28
|
|
|
$this->assertEquals($expected, $arguments->flatten()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @dataProvider data |
33
|
|
|
*/ |
34
|
|
|
public function test_get($data) |
35
|
|
|
{ |
36
|
|
|
$arguments = new ExtendedArguments($data); |
37
|
|
|
|
38
|
|
|
$this->assertSame('A', $arguments->get('key1')); |
39
|
|
|
$this->assertSame('B', $arguments->get('key2.key21')); |
40
|
|
|
$this->assertSame(['key21' => 'B'], $arguments->get('key2')); |
41
|
|
|
$this->assertSame(['A', 'B', 'C'], $arguments->get('key3')); |
42
|
|
|
$this->assertSame('D', $arguments->get('key4.key41.key411')); |
43
|
|
|
$this->assertSame(['key411' => 'D', 'key412' => 'E'], $arguments->get('key4.key41')); |
44
|
|
|
$this->assertSame('F', $arguments->get('key4.key42')); |
45
|
|
|
$this->assertNull($arguments->get('key5')); |
46
|
|
|
|
47
|
|
|
$this->assertSame('fubar', $arguments->get('key3.yolo', 'fubar'), 'Returns a default value if not found'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** @dataProvider data */ |
51
|
|
|
public function test_append($input) |
52
|
|
|
{ |
53
|
|
|
$arguments = new ExtendedArguments($input); |
54
|
|
|
$arguments->append('key2.key21', 'C'); |
55
|
|
|
$this->assertEquals(['B', 'C'], $arguments->get('key2.key21')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @dataProvider data */ |
59
|
|
|
public function test_has($input) |
60
|
|
|
{ |
61
|
|
|
$arguments = new ExtendedArguments($input); |
62
|
|
|
$this->assertTrue($arguments->has('key2.key21')); |
63
|
|
|
$this->assertFalse($arguments->has('key2.key21.fubar')); |
64
|
|
|
$this->assertTrue($arguments->has('key5')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @dataProvider data */ |
68
|
|
|
public function test_delete($input) |
69
|
|
|
{ |
70
|
|
|
$arguments = new ExtendedArguments($input); |
71
|
|
|
$this->assertEquals('E', $arguments->get('key4.key41.key412')); |
72
|
|
|
|
73
|
|
|
$arguments->delete('key4.key41.key412'); |
74
|
|
|
$this->assertSame('this is deleted', $arguments->get('key4.key41.key412', 'this is deleted')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @dataProvider data */ |
78
|
|
|
public function test_flatten($input) |
79
|
|
|
{ |
80
|
|
|
$arguments = new ExtendedArguments($input); |
81
|
|
|
$expected = [ |
82
|
|
|
'key1' => 'A', |
83
|
|
|
'key2.key21' => 'B', |
84
|
|
|
'key3.0' => 'A', |
85
|
|
|
'key3.1' => 'B', |
86
|
|
|
'key3.2' => 'C', |
87
|
|
|
'key4.key41.key411' => 'D', |
88
|
|
|
'key4.key41.key412' => 'E', |
89
|
|
|
'key4.key42' => 'F', |
90
|
|
|
'key5' => null |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$this->assertEquals(new ExtendedArguments($expected), $arguments->flatten()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function test_frakked_up_keys() |
97
|
|
|
{ |
98
|
|
|
$data = include __DIR__ . '/fixtures/nested-array.php'; |
99
|
|
|
|
100
|
|
|
$arguments = new ExtendedArguments($data); |
101
|
|
|
$this->assertSame($data, $arguments->toArray(), 'The data is intact'); |
102
|
|
|
|
103
|
|
|
// But, the keys are messed up now... |
104
|
|
|
$this->assertSame(true, $arguments->get('array.1'), "The key is TRUE and now juggled into string 1"); |
105
|
|
|
$this->assertSame('null', $arguments->get(''), "The key is NULL but fucked into empty string"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** @dataProvider data */ |
109
|
|
|
public function test_extract($data) |
110
|
|
|
{ |
111
|
|
|
$arguments = new ExtendedArguments($data); |
112
|
|
|
$this->assertEquals([ |
113
|
|
|
'key3.1' => 'B', |
114
|
|
|
'key4.key41.key412' => 'E', |
115
|
|
|
'key2' => ['key21' => 'B'], |
116
|
|
|
'non-existent' => null |
117
|
|
|
], $arguments->extract(['key3.1', 'key4.key41.key412', 'key2', 'non-existent'])); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function data() |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
[ |
124
|
|
|
[ |
125
|
|
|
'key1' => 'A', |
126
|
|
|
'key2' => [ |
127
|
|
|
'key21' => 'B', |
128
|
|
|
], |
129
|
|
|
'key3' => ['A', 'B', 'C'], |
130
|
|
|
'key4' => [ |
131
|
|
|
'key41' => [ |
132
|
|
|
'key411' => 'D', |
133
|
|
|
'key412' => 'E', |
134
|
|
|
], |
135
|
|
|
'key42' => 'F', |
136
|
|
|
], |
137
|
|
|
'key5' => null, |
138
|
|
|
] |
139
|
|
|
] |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|