-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathArraydotifyTest.php
More file actions
375 lines (316 loc) · 11.6 KB
/
Copy pathArraydotifyTest.php
File metadata and controls
375 lines (316 loc) · 11.6 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
namespace Bow\Tests\Support;
use Bow\Support\Arraydotify;
class ArraydotifyTest extends \PHPUnit\Framework\TestCase
{
protected Arraydotify $dot;
protected array $collection = [
'name' => 'bow',
'lastname' => 'framework',
'bio' => 'The php micro framework',
'author' => [
'name' => 'Franck Dakia',
'email' => 'dakiafranck@gmail.com'
],
'location' => [
'city' => 'Abidjan',
'tel' => "12346678",
"state" => [
'code' => 225,
'abr' => 'CI',
'name' => 'Ivory Coast'
]
]
];
protected function setUp(): void
{
$this->dot = new Arraydotify(['code' => $this->collection]);
}
public function test_instance_creation()
{
$dot = new Arraydotify(['name' => 'test']);
$this->assertInstanceOf(Arraydotify::class, $dot);
}
public function test_static_make()
{
$dot = Arraydotify::make(['name' => 'test']);
$this->assertInstanceOf(Arraydotify::class, $dot);
}
public function test_get_top_level_array()
{
$this->assertTrue(is_array($this->dot['code']));
}
public function test_get_simple_value()
{
$this->assertEquals('bow', $this->dot['code.name']);
$this->assertEquals('framework', $this->dot['code.lastname']);
}
public function test_get_deeply_nested_value()
{
$this->assertEquals('CI', $this->dot['code.location.state.abr']);
$this->assertEquals(225, $this->dot['code.location.state.code']);
$this->assertEquals('Ivory Coast', $this->dot['code.location.state.name']);
}
public function test_get_nested_array()
{
$this->assertTrue(is_array($this->dot['code.location']));
$this->assertTrue(is_array($this->dot['code.author']));
}
public function test_get_nested_array_contains_keys()
{
$location = $this->dot['code.location'];
$this->assertArrayHasKey('city', $location);
$this->assertArrayHasKey('tel', $location);
$this->assertArrayHasKey('state', $location);
$state = $this->dot['code.location.state'];
$this->assertTrue(is_array($state));
$this->assertArrayHasKey('code', $state);
$this->assertArrayHasKey('abr', $state);
$this->assertArrayHasKey('name', $state);
}
public function test_offset_exists()
{
$this->assertTrue(isset($this->dot['code']));
$this->assertTrue(isset($this->dot['code.name']));
$this->assertTrue(isset($this->dot['code.location.state.abr']));
$this->assertFalse(isset($this->dot['nonexistent']));
$this->assertFalse(isset($this->dot['code.nonexistent']));
}
public function test_has_method()
{
$this->assertTrue($this->dot->has('code'));
$this->assertTrue($this->dot->has('code.name'));
$this->assertTrue($this->dot->has('code.location.state.abr'));
$this->assertFalse($this->dot->has('nonexistent'));
}
public function test_get_method()
{
$this->assertEquals('bow', $this->dot->get('code.name'));
$this->assertEquals('default', $this->dot->get('nonexistent', 'default'));
$this->assertNull($this->dot->get('nonexistent'));
}
public function test_get_nonexistent_returns_null()
{
$this->assertNull($this->dot['nonexistent.key']);
$this->assertNull($this->dot['code.nonexistent']);
}
public function test_offset_set_simple_value()
{
$this->dot['code.version'] = '5.0';
$this->assertEquals('5.0', $this->dot['code.version']);
}
public function test_offset_set_nested_value()
{
$this->dot['code.config.debug'] = true;
$this->assertTrue($this->dot['code.config.debug']);
}
public function test_set_method()
{
$this->dot->set('code.environment', 'production');
$this->assertEquals('production', $this->dot->get('code.environment'));
}
public function test_offset_set_overwrites_existing()
{
$this->dot['code.name'] = 'new-name';
$this->assertEquals('new-name', $this->dot['code.name']);
}
public function test_offset_unset()
{
$this->assertTrue(isset($this->dot['code.name']));
unset($this->dot['code.name']);
$this->assertFalse(isset($this->dot['code.name']));
}
public function test_offset_unset_nested()
{
$this->assertTrue(isset($this->dot['code.location.state']));
unset($this->dot['code.location.state']);
$this->assertFalse(isset($this->dot['code.location.state']));
}
public function test_to_array_returns_original_structure()
{
$array = $this->dot->toArray();
$this->assertIsArray($array);
$this->assertArrayHasKey('code', $array);
$this->assertEquals($this->collection, $array['code']);
}
public function test_get_dotified_returns_flat_array()
{
$dotified = $this->dot->getDotified();
$this->assertIsArray($dotified);
$this->assertArrayHasKey('code.name', $dotified);
$this->assertArrayHasKey('code.location.state.abr', $dotified);
$this->assertEquals('bow', $dotified['code.name']);
}
public function test_empty_array()
{
$dot = new Arraydotify([]);
$this->assertEquals([], $dot->toArray());
$this->assertEquals([], $dot->getDotified());
}
public function test_single_level_array()
{
$dot = new Arraydotify(['a' => 1, 'b' => 2, 'c' => 3]);
$this->assertEquals(1, $dot['a']);
$this->assertEquals(2, $dot['b']);
$this->assertEquals(3, $dot['c']);
}
public function test_numeric_keys()
{
$dot = new Arraydotify(['items' => [0 => 'first', 1 => 'second', 2 => 'third']]);
$this->assertEquals('first', $dot['items.0']);
$this->assertEquals('second', $dot['items.1']);
$this->assertEquals('third', $dot['items.2']);
}
public function test_mixed_keys()
{
$dot = new Arraydotify([
'config' => [
'database' => ['host' => 'localhost'],
'cache' => ['driver' => 'redis']
]
]);
$this->assertEquals('localhost', $dot['config.database.host']);
$this->assertEquals('redis', $dot['config.cache.driver']);
}
public function test_set_creates_nested_structure()
{
$dot = new Arraydotify();
$dot['app.name'] = 'MyApp';
$this->assertEquals('MyApp', $dot['app.name']);
$array = $dot->toArray();
$this->assertArrayHasKey('app', $array);
$this->assertArrayHasKey('name', $array['app']);
$this->assertEquals('MyApp', $array['app']['name']);
}
public function test_set_deeply_nested_creates_path()
{
$dot = new Arraydotify();
$dot['level1.level2.level3.level4.value'] = 'deep';
$this->assertEquals('deep', $dot['level1.level2.level3.level4.value']);
$this->assertTrue($dot->has('level1'));
$this->assertTrue($dot->has('level1.level2'));
$this->assertTrue($dot->has('level1.level2.level3'));
$this->assertTrue($dot->has('level1.level2.level3.level4'));
}
public function test_set_array_value()
{
$dot = new Arraydotify(['data' => []]);
$dot['data.items'] = ['apple', 'banana', 'orange'];
$items = $dot['data.items'];
$this->assertIsArray($items);
$this->assertCount(3, $items);
$this->assertContains('apple', $items);
}
public function test_set_null_value()
{
$dot = new Arraydotify(['key' => 'value']);
$dot['key'] = null;
$this->assertNull($dot['key']);
}
public function test_set_overwrites_nested_structure()
{
$dot = new Arraydotify([
'config' => [
'debug' => true,
'app' => ['name' => 'OldApp']
]
]);
$dot['config.app'] = 'NewValue';
$this->assertEquals('NewValue', $dot['config.app']);
$this->assertFalse($dot->has('config.app.name'));
}
public function test_set_multiple_values_same_path()
{
$dot = new Arraydotify();
$dot['user.name'] = 'John';
$dot['user.email'] = 'john@example.com';
$dot['user.age'] = 30;
$this->assertEquals('John', $dot['user.name']);
$this->assertEquals('john@example.com', $dot['user.email']);
$this->assertEquals(30, $dot['user.age']);
$user = $dot['user'];
$this->assertIsArray($user);
$this->assertCount(3, $user);
}
public function test_set_with_numeric_index()
{
$dot = new Arraydotify();
$dot['items.0'] = 'first';
$dot['items.1'] = 'second';
$dot['items.2'] = 'third';
$this->assertEquals('first', $dot['items.0']);
$this->assertEquals('second', $dot['items.1']);
$this->assertEquals('third', $dot['items.2']);
}
public function test_set_boolean_values()
{
$dot = new Arraydotify();
$dot['settings.enabled'] = true;
$dot['settings.disabled'] = false;
$this->assertTrue($dot['settings.enabled']);
$this->assertFalse($dot['settings.disabled']);
}
public function test_set_integer_and_float_values()
{
$dot = new Arraydotify();
$dot['numbers.integer'] = 42;
$dot['numbers.float'] = 3.14;
$dot['numbers.negative'] = -10;
$this->assertSame(42, $dot['numbers.integer']);
$this->assertSame(3.14, $dot['numbers.float']);
$this->assertSame(-10, $dot['numbers.negative']);
}
public function test_set_preserves_existing_siblings()
{
$dot = new Arraydotify([
'config' => [
'app' => 'MyApp',
'version' => '1.0'
]
]);
$dot['config.debug'] = true;
$this->assertEquals('MyApp', $dot['config.app']);
$this->assertEquals('1.0', $dot['config.version']);
$this->assertTrue($dot['config.debug']);
}
public function test_set_updates_both_storage_and_origin()
{
$dot = new Arraydotify();
$dot['new.path.value'] = 'test';
// Check dotified storage
$dotified = $dot->getDotified();
$this->assertArrayHasKey('new.path.value', $dotified);
// Check original structure
$array = $dot->toArray();
$this->assertEquals('test', $array['new']['path']['value']);
}
public function test_set_empty_string()
{
$dot = new Arraydotify();
$dot['empty'] = '';
$this->assertSame('', $dot['empty']);
$this->assertTrue($dot->has('empty'));
}
public function test_set_zero_value()
{
$dot = new Arraydotify();
$dot['zero.int'] = 0;
$dot['zero.float'] = 0.0;
$this->assertSame(0, $dot['zero.int']);
$this->assertSame(0.0, $dot['zero.float']);
}
public function test_set_method_with_complex_path()
{
$dot = new Arraydotify();
$dot->set('api.endpoints.users.list', '/api/v1/users');
$dot->set('api.endpoints.users.create', '/api/v1/users/create');
$dot->set('api.endpoints.posts.list', '/api/v1/posts');
$this->assertEquals('/api/v1/users', $dot->get('api.endpoints.users.list'));
$this->assertEquals('/api/v1/users/create', $dot->get('api.endpoints.users.create'));
$this->assertEquals('/api/v1/posts', $dot->get('api.endpoints.posts.list'));
$endpoints = $dot['api.endpoints'];
$this->assertIsArray($endpoints);
$this->assertArrayHasKey('users', $endpoints);
$this->assertArrayHasKey('posts', $endpoints);
}
}