1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koded\Stdlib; |
4
|
|
|
|
5
|
|
|
use Koded\Stdlib\Interfaces\{Argument, Data}; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class ArgumentObjectTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var Argument |
13
|
|
|
*/ |
14
|
|
|
private $SUT; |
15
|
|
|
|
16
|
|
|
public function test_set() |
17
|
|
|
{ |
18
|
|
|
$this->assertEquals([ |
19
|
|
|
'foo' => 1, |
20
|
|
|
'bar' => 2, |
21
|
|
|
'baz' => 3, |
22
|
|
|
'qux' => 'zim' |
23
|
|
|
], $this->SUT->toArray()); |
24
|
|
|
|
25
|
|
|
$result = $this->SUT->set('foo', 'gir'); |
26
|
|
|
|
27
|
|
|
$this->assertEquals([ |
28
|
|
|
'foo' => 'gir', |
29
|
|
|
'bar' => 2, |
30
|
|
|
'baz' => 3, |
31
|
|
|
'qux' => 'zim' |
32
|
|
|
], $this->SUT->toArray()); |
33
|
|
|
|
34
|
|
|
$this->assertInstanceOf(Arguments::class, $result); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @depends test_set |
39
|
|
|
*/ |
40
|
|
|
public function test_import() |
41
|
|
|
{ |
42
|
|
|
$result = $this->SUT->import(['foo' => 42, 'qux' => 2]); |
43
|
|
|
|
44
|
|
|
$this->assertEquals([ |
45
|
|
|
'foo' => 42, |
46
|
|
|
'bar' => 2, |
47
|
|
|
'baz' => 3, |
48
|
|
|
'qux' => 2 |
49
|
|
|
], $this->SUT->toArray()); |
50
|
|
|
$this->assertInstanceOf(Arguments::class, $result); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function test_upsert() |
54
|
|
|
{ |
55
|
|
|
// Not added because 'foo' already exist |
56
|
|
|
$result = $this->SUT->upsert('foo', 42); |
57
|
|
|
|
58
|
|
|
$this->assertEquals([ |
59
|
|
|
'foo' => 1, |
60
|
|
|
'bar' => 2, |
61
|
|
|
'baz' => 3, |
62
|
|
|
'qux' => 'zim' |
63
|
|
|
], $this->SUT->toArray()); |
64
|
|
|
|
65
|
|
|
// Added because it does not exist |
66
|
|
|
$this->SUT->upsert('poo', 42); |
67
|
|
|
|
68
|
|
|
$this->assertEquals([ |
69
|
|
|
'foo' => 1, |
70
|
|
|
'bar' => 2, |
71
|
|
|
'baz' => 3, |
72
|
|
|
'qux' => 'zim', |
73
|
|
|
'poo' => 42 |
74
|
|
|
], $this->SUT->toArray()); |
75
|
|
|
|
76
|
|
|
$this->assertInstanceOf(Arguments::class, $result); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function test_bind() |
80
|
|
|
{ |
81
|
|
|
$var = 42; |
82
|
|
|
$result = $this->SUT->bind('foo', $var); |
83
|
|
|
$this->assertEquals([ |
84
|
|
|
'foo' => 42, |
85
|
|
|
'bar' => 2, |
86
|
|
|
'baz' => 3, |
87
|
|
|
'qux' => 'zim', |
88
|
|
|
], $this->SUT->toArray()); |
89
|
|
|
|
90
|
|
|
$var = 0; |
91
|
|
|
$this->assertEquals([ |
92
|
|
|
'foo' => 0, |
93
|
|
|
'bar' => 2, |
94
|
|
|
'baz' => 3, |
95
|
|
|
'qux' => 'zim', |
96
|
|
|
], $this->SUT->toArray()); |
97
|
|
|
|
98
|
|
|
$this->assertInstanceOf(Arguments::class, $result); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function test_pull() |
102
|
|
|
{ |
103
|
|
|
$value = $this->SUT->pull('foo'); |
104
|
|
|
$this->assertEquals([ |
105
|
|
|
'bar' => 2, |
106
|
|
|
'baz' => 3, |
107
|
|
|
'qux' => 'zim', |
108
|
|
|
], $this->SUT->toArray()); |
109
|
|
|
|
110
|
|
|
$this->assertSame(1, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function test_delete() |
114
|
|
|
{ |
115
|
|
|
$result = $this->SUT->delete('foo'); |
116
|
|
|
|
117
|
|
|
$this->assertEquals([ |
118
|
|
|
'bar' => 2, |
119
|
|
|
'baz' => 3, |
120
|
|
|
'qux' => 'zim', |
121
|
|
|
], $this->SUT->toArray()); |
122
|
|
|
|
123
|
|
|
// non-existing key is ignored |
124
|
|
|
$this->SUT->delete('non-existing'); |
125
|
|
|
|
126
|
|
|
$this->assertEquals([ |
127
|
|
|
'bar' => 2, |
128
|
|
|
'baz' => 3, |
129
|
|
|
'qux' => 'zim', |
130
|
|
|
], $this->SUT->toArray()); |
131
|
|
|
$this->assertInstanceOf(Arguments::class, $result); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function test_it_should_transform_immutable_to_argument_object() |
135
|
|
|
{ |
136
|
|
|
$SUT = new Arguments([]); |
137
|
|
|
$this->assertInstanceOf(Data::class, $SUT->toImmutable()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function test_magic_setter() |
141
|
|
|
{ |
142
|
|
|
$SUT = new Arguments(['foo' => 'bar']); |
143
|
|
|
$SUT->foo = 'qux'; |
144
|
|
|
|
145
|
|
|
$this->assertSame('qux', $SUT->foo); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function test_iterator() |
149
|
|
|
{ |
150
|
|
|
$SUT = new Arguments([1, 2, 3]); |
151
|
|
|
$this->assertSame([1, 2, 3], iterator_to_array($SUT)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function test_cloning() |
155
|
|
|
{ |
156
|
|
|
$clone = clone $this->SUT; |
157
|
|
|
$this->assertEquals($this->SUT, $clone); |
158
|
|
|
$this->assertNotSame($this->SUT, $clone); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function test_should_clear_the_storage() |
162
|
|
|
{ |
163
|
|
|
$SUT = new Arguments(['foo' => 'bar']); |
164
|
|
|
$this->assertCount(1, $SUT); |
165
|
|
|
|
166
|
|
|
$SUT->clear(); |
167
|
|
|
$this->assertCount(0, $SUT); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function test_should_filter_the_data() |
171
|
|
|
{ |
172
|
|
|
$SUT = new Arguments([ |
173
|
|
|
'FOO.FOO' => 'foo', |
174
|
|
|
'FOO.BAR' => 'bar', |
175
|
|
|
'gir' => 'qux', |
176
|
|
|
]); |
177
|
|
|
|
178
|
|
|
$this->assertEquals(new Arguments([ |
179
|
|
|
'FOO' => 'foo', |
180
|
|
|
'BAR' => 'bar', |
181
|
|
|
'gir' => 'qux' |
182
|
|
|
]), $SUT->namespace('FOO.', false)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function test_should_filter_prefixed_indexes() |
186
|
|
|
{ |
187
|
|
|
$SUT = new Arguments([ |
188
|
|
|
'foo' => 'foo', |
189
|
|
|
'foo.bar' => 'bar', |
190
|
|
|
'foo.FOO' => 'DANG!', // this one overrides the non-prefixed |
191
|
|
|
'gir' => 'qux', |
192
|
|
|
]); |
193
|
|
|
|
194
|
|
|
$this->assertEquals(new Arguments([ |
195
|
|
|
'foo' => 'DANG!', |
196
|
|
|
'bar' => 'bar', |
197
|
|
|
'gir' => 'qux' |
198
|
|
|
]), $SUT->namespace('foo.')); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Tests indirect array modification introduced in 7.1.4+ |
203
|
|
|
*/ |
204
|
|
|
public function test_expected_dynamic_object_property_setter() |
205
|
|
|
{ |
206
|
|
|
$this->SUT->schizo = 'phrenic'; |
207
|
|
|
$this->assertFalse(empty($this->SUT->schizo)); |
208
|
|
|
$this->assertSame('phrenic', $this->SUT->schizo); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function test_indirect_array_modification() |
212
|
|
|
{ |
213
|
|
|
$args = new Arguments; |
214
|
|
|
$args->indirect['modification']['BC'] = 'break'; |
215
|
|
|
|
216
|
|
|
$this->assertSame(['indirect' => ['modification' => ['BC' => 'break']]], $args->toArray()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
protected function setUp(): void |
220
|
|
|
{ |
221
|
|
|
$this->SUT = new Arguments([ |
222
|
|
|
'foo' => 1, |
223
|
|
|
'bar' => 2, |
224
|
|
|
'baz' => 3, |
225
|
|
|
'qux' => 'zim' |
226
|
|
|
]); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|