Skip to content

Commit 7799d12

Browse files
committed
- updates the unit tests
1 parent 2f568fb commit 7799d12

File tree

3 files changed

+87
-18
lines changed

3 files changed

+87
-18
lines changed

tests/ExtendedArgumentsTest.php

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,42 @@ public function test_should_flatten_the_array($data)
2828
$this->assertEquals($expected, $arguments->flatten());
2929
}
3030

31+
public function test_set_without_dot_key()
32+
{
33+
$data = new ExtendedArguments();
34+
35+
$data->set('foo', 'bar');
36+
$this->assertSame('bar', $data->get('foo'));
37+
38+
$data->set('foo.bar', 'gir');
39+
40+
$this->assertFalse($data->has('bar'),
41+
'Sub-keys are not scanned');
42+
$this->assertTrue($data->has('foo.bar'));
43+
$this->assertSame('gir', $data->get('foo.bar'));
44+
$this->assertSame(['bar' => 'gir'], $data->get('foo'),
45+
'Finds all sub-keys');
46+
}
47+
48+
public function test_delete_without_dot_key()
49+
{
50+
$data = new ExtendedArguments(['foo' => 'bar']);
51+
52+
$other = $data->delete('foo.bar');
53+
$this->assertSame($data, $other,
54+
'Does not modify if key is not found');
55+
56+
$data->delete('foo');
57+
$this->assertFalse($data->has('foo'));
58+
59+
$data->set('foo.bar', 'baz');
60+
$this->assertTrue($data->has('foo.bar'));
61+
62+
$data->delete('bar');
63+
$this->assertTrue($data->has('foo.bar'),
64+
'Sub-item is not deleted');
65+
}
66+
3167
/**
3268
* @dataProvider data
3369
*/
@@ -95,14 +131,19 @@ public function test_flatten($input)
95131

96132
public function test_frakked_up_keys()
97133
{
98-
$data = include __DIR__ . '/fixtures/nested-array.php';
134+
$actual = include __DIR__ . '/fixtures/nested-array.php';
135+
$expected = include __DIR__ . '/fixtures/nested-array-transformed.php';
136+
$arguments = new ExtendedArguments($actual);
99137

100-
$arguments = new ExtendedArguments($data);
101-
$this->assertSame($data, $arguments->toArray(), 'The data is intact');
138+
$this->assertSame($expected, $arguments->toArray(),
139+
'The data is transformed internally');
102140

103141
// 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 frakked up as empty string");
142+
$this->assertSame(true, $arguments->get('array.1'),
143+
'The key is TRUE and now juggled into string 1');
144+
145+
$this->assertSame('null', $arguments->get(''),
146+
'The key is NULL but frakked up as empty string');
106147
}
107148

108149
/** @dataProvider data */
@@ -111,10 +152,17 @@ public function test_extract($data)
111152
$arguments = new ExtendedArguments($data);
112153
$this->assertEquals([
113154
'key3.1' => 'B',
155+
'key2.key21' => 'B',
114156
'key4.key41.key412' => 'E',
115157
'key2' => ['key21' => 'B'],
116158
'non-existent' => null
117-
], $arguments->extract(['key3.1', 'key4.key41.key412', 'key2', 'non-existent']));
159+
], $arguments->extract([
160+
'key3.1',
161+
'key2.key21',
162+
'key4.key41.key412',
163+
'key2',
164+
'non-existent'
165+
]));
118166
}
119167

120168
public function data()

tests/FunctionsTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace Tests\Koded\Stdlib;
44

5-
use Koded\Stdlib\{Arguments, ExtendedArguments, Immutable};
5+
use Koded\Stdlib\{Arguments, Immutable};
66
use PHPUnit\Framework\TestCase;
77
use function Koded\Stdlib\{arguments,
88
camel_to_snake_case,
9-
extended_arguments,
109
htmlencode,
1110
is_associative,
1211
now,
@@ -37,16 +36,6 @@ public function test_arguments_function()
3736
$this->assertSame([1, 2, 3, 'foo' => 'bar'], $value->toArray());
3837
}
3938

40-
public function test_extended_arguments_function()
41-
{
42-
$value = extended_arguments([1, 2, 3]);
43-
$this->assertInstanceOf(ExtendedArguments::class, $value);
44-
$this->assertSame([1, 2, 3], $value->toArray());
45-
46-
$value->foo = 'bar';
47-
$this->assertSame([1, 2, 3, 'foo' => 'bar'], $value->toArray());
48-
}
49-
5039
public function test_htmlescape_function()
5140
{
5241
$value = htmlencode('<script>');
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
return [
4+
'foo' => 'bar',
5+
'0' => 0,
6+
null => 'null',
7+
'null' => null,
8+
'list' => [
9+
1, 2, 3
10+
],
11+
'array' => [
12+
'key1' => 1,
13+
'key2' => [
14+
'key2.1' => null
15+
],
16+
'key3' => [
17+
'key3-1' => [
18+
'key3-1-1' => 'found me'
19+
],
20+
21+
],
22+
'key4.0' => 'nested keys cannot have dots',
23+
true => true,
24+
],
25+
'one' => [
26+
'two' => [
27+
'three' => 'four'
28+
],
29+
'null' => null
30+
],
31+
'1' => true,
32+
];

0 commit comments

Comments
 (0)