-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgumentObjectTest.php
More file actions
275 lines (227 loc) · 6.92 KB
/
Copy pathArgumentObjectTest.php
File metadata and controls
275 lines (227 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
namespace Tests\Koded\Stdlib;
use Koded\Stdlib\{Arguments, Data};
use PHPUnit\Framework\TestCase;
error_reporting(E_ALL & ~E_DEPRECATED);
class ArgumentObjectTest extends TestCase
{
private Arguments $SUT;
public function test_set()
{
$this->assertEquals([
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qux' => 'zim'
], $this->SUT->toArray());
$result = $this->SUT->set('foo', 'gir');
$this->assertEquals([
'foo' => 'gir',
'bar' => 2,
'baz' => 3,
'qux' => 'zim'
], $this->SUT->toArray());
$this->assertInstanceOf(Arguments::class, $result);
}
/**
* @depends test_set
*/
public function test_import()
{
$result = $this->SUT->import(['foo' => 42, 'qux' => 2]);
$this->assertEquals([
'foo' => 42,
'bar' => 2,
'baz' => 3,
'qux' => 2
], $this->SUT->toArray());
$this->assertInstanceOf(Arguments::class, $result);
}
public function test_upsert()
{
// Not added because 'foo' already exist
$result = $this->SUT->upsert('foo', 42);
$this->assertEquals([
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qux' => 'zim'
], $this->SUT->toArray());
// Added because it does not exist
$this->SUT->upsert('poo', 42);
$this->assertEquals([
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qux' => 'zim',
'poo' => 42
], $this->SUT->toArray());
$this->assertInstanceOf(Arguments::class, $result);
}
public function test_bind()
{
$var = 42;
$result = $this->SUT->bind('foo', $var);
$this->assertEquals([
'foo' => 42,
'bar' => 2,
'baz' => 3,
'qux' => 'zim',
], $this->SUT->toArray());
$var = 0;
$this->assertEquals([
'foo' => 0,
'bar' => 2,
'baz' => 3,
'qux' => 'zim',
], $this->SUT->toArray());
$this->assertInstanceOf(Arguments::class, $result);
}
public function test_pull()
{
$value = $this->SUT->pull('foo');
$this->assertEquals([
'bar' => 2,
'baz' => 3,
'qux' => 'zim',
], $this->SUT->toArray());
$this->assertSame(1, $value);
}
public function test_delete()
{
$result = $this->SUT->delete('foo');
$this->assertEquals([
'bar' => 2,
'baz' => 3,
'qux' => 'zim',
], $this->SUT->toArray());
// non-existing key is ignored
$this->SUT->delete('non-existing');
$this->assertEquals([
'bar' => 2,
'baz' => 3,
'qux' => 'zim',
], $this->SUT->toArray());
$this->assertInstanceOf(Arguments::class, $result);
}
public function test_it_should_transform_immutable_to_argument_object()
{
$SUT = new Arguments([]);
$this->assertInstanceOf(Data::class, $SUT->toImmutable());
}
public function test_magic_setter()
{
$SUT = new Arguments(['foo' => 'bar']);
$SUT->foo = 'qux';
$this->assertSame('qux', $SUT->foo);
}
public function test_iterator()
{
$SUT = new Arguments([1, 2, 3]);
$this->assertSame([1, 2, 3], iterator_to_array($SUT));
}
public function test_cloning()
{
$clone = clone $this->SUT;
$this->assertEquals($this->SUT, $clone);
$this->assertNotSame($this->SUT, $clone);
}
public function test_should_clear_the_storage()
{
$SUT = new Arguments(['foo' => 'bar']);
$this->assertCount(1, $SUT);
$SUT->clear();
$this->assertCount(0, $SUT);
}
public function test_should_filter_the_data()
{
$SUT = new Arguments([
'FOO.FOO' => 'foo',
'FOO.BAR' => 'bar',
'gir' => 'qux',
]);
$this->assertEquals(new Arguments([
'FOO' => 'foo',
'BAR' => 'bar',
'gir' => 'qux'
]), $SUT->namespace('FOO.', false));
}
public function test_should_filter_prefixed_indexes()
{
$SUT = new Arguments([
'foo' => 'foo',
'foo.bar' => 'bar',
'foo.FOO' => 'DANG!', // this one overrides the non-prefixed
'gir' => 'qux',
]);
$this->assertEquals(new Arguments([
'foo' => 'DANG!',
'bar' => 'bar',
'gir' => 'qux'
]), $SUT->namespace('foo.'));
}
/**
* Tests indirect array modification introduced in 7.1.4+
*/
public function test_expected_dynamic_object_property_setter()
{
$this->SUT->schizo = 'phrenic';
$this->assertFalse(empty($this->SUT->schizo));
$this->assertSame('phrenic', $this->SUT->schizo);
}
public function test_indirect_array_modification()
{
$args = new Arguments;
$args->indirect['modification']['BC'] = 'break';
$this->assertSame(['indirect' => ['modification' => ['BC' => 'break']]], $args->toArray());
}
/**
* @dataProvider pain
*/
public function test_some_implicit_conversion_pain($store)
{
// Here comes the fun...
$this->assertFalse($store->has(3.14), 'Key DOES NOT exist. Lets see...');
$this->assertNotSame('oh no', $store->get(3.14), 'Wait what? (test is false positive btw)');
$this->assertSame(null, $store->get(3.14), 'I expected something else');
$this->assertSame('oh no', $store->get(3), 'The amazing implicit conversion of 3.14 and the override of the existing key 3');
$this->assertSame('yes', $store->get('3.140'), 'Run PHP, run!');
// Here comes more amazingness with PHPUnit...
$store = $store->toArray();
$this->assertArrayHasKey('1', $store);
$this->assertArrayHasKey(1, $store, 'Because of course');
$this->assertArrayHasKey(2, $store);
$this->assertArrayHasKey('2', $store, 'Why not?');
$this->assertArrayHasKey('3', $store);
$this->assertArrayHasKey(3, $store, 'We already tested this, just to confirm the sanity');
}
public function pain()
{
$data = [
0 => 0,
'1' => 1,
2 => 2,
'3' => 3,
3.14 => 'oh no',
'3.140' => 'yes',
];
$store = new Arguments;
foreach ($data as $index => $value) {
$store->set($index, $value);
}
return [
[new Arguments($data)],
[(new Arguments)->import($data)],
[$store],
];
}
protected function setUp(): void
{
$this->SUT = new Arguments([
'foo' => 1,
'bar' => 2,
'baz' => 3,
'qux' => 'zim'
]);
}
}