|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koded\Stdlib; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Exceptions\ReadOnlyException; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class ImmutableObjectTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var Immutable |
|
13
|
|
|
*/ |
|
14
|
|
|
private $SUT; |
|
15
|
|
|
|
|
16
|
|
|
public function test_should_load_the_array() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->assertCount(9, $this->SUT); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function test_should_disallow_cloning() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->expectException(ReadOnlyException::class); |
|
24
|
|
|
$this->expectExceptionMessage('Cloning the Koded\Stdlib\Immutable instance is not allowed'); |
|
25
|
|
|
clone $this->SUT; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function test_should_disallow_magically_setting_values() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->expectException(ReadOnlyException::class); |
|
31
|
|
|
$this->expectExceptionMessage('Koded\Stdlib\Immutable instance is read-only'); |
|
32
|
|
|
$this->SUT->fubar = 'no'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function test_should_transform_immutable_to_argument_object() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->assertInstanceOf(Arguments::class, $this->SUT->toArgument()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function test_should_get_things() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->assertNull($this->SUT->get('fubar')); |
|
43
|
|
|
$this->assertTrue($this->SUT->get(true)); |
|
44
|
|
|
$this->assertTrue($this->SUT->has(null)); |
|
45
|
|
|
$this->assertSame('bar', $this->SUT->foo); |
|
46
|
|
|
$this->assertSame(0, $this->SUT->get('0')); |
|
47
|
|
|
$this->assertSame(0, $this->SUT->{0}); |
|
48
|
|
|
|
|
49
|
|
|
// because why not |
|
50
|
|
|
$this->assertSame(0, $this->SUT->get(0)); |
|
51
|
|
|
|
|
52
|
|
|
// now strict has left the building |
|
53
|
|
|
$this->assertSame('null', $this->SUT->get(false)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function test_should_return_null_on_non_existing_key() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->assertNull($this->SUT->yabbadabbadoo); |
|
59
|
|
|
$this->assertNull($this->SUT->get('yabbadabbadoo')); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function test_should_find_things() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->assertEquals('not found', $this->SUT->find('key3', 'not found')); |
|
65
|
|
|
$this->assertSame('found me', $this->SUT->find('array.key3.key3-1.key3-1-1')); |
|
66
|
|
|
|
|
67
|
|
|
// find() fails if nested key has dot(s) |
|
68
|
|
|
$this->assertNull($this->SUT->find('array.key4.0')); |
|
69
|
|
|
|
|
70
|
|
|
// but it's fine if key is not nested |
|
71
|
|
|
$this->assertSame('four', $this->SUT->find('one.two.three')); |
|
72
|
|
|
|
|
73
|
|
|
// also NULL value |
|
74
|
|
|
$this->assertNull($this->SUT->find('one.null')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function test_should_filter_out_the_data() |
|
78
|
|
|
{ |
|
79
|
|
|
$expected = [ |
|
80
|
|
|
'0' => 0, |
|
81
|
|
|
'null' => null, |
|
82
|
|
|
'one.two.three' => 'four', |
|
83
|
|
|
true => true, |
|
84
|
|
|
'non-existing' => null, |
|
85
|
|
|
'one.null' => null |
|
86
|
|
|
|
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
$this->assertEquals($expected, $this->SUT->extract([ |
|
90
|
|
|
true, |
|
91
|
|
|
'one.two.three', |
|
92
|
|
|
'0', |
|
93
|
|
|
'null', |
|
94
|
|
|
'non-existing', |
|
95
|
|
|
'one.null' |
|
96
|
|
|
])); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function test_equals_method() |
|
100
|
|
|
{ |
|
101
|
|
|
$data = new Immutable([ |
|
102
|
|
|
'e1' => null, |
|
103
|
|
|
'e2' => '', |
|
104
|
|
|
'01' => 0, |
|
105
|
|
|
'02' => '0', |
|
106
|
|
|
'foo1' => 'Foo', |
|
107
|
|
|
'foo2' => 'foo', |
|
108
|
|
|
'obj1' => new \stdClass, |
|
109
|
|
|
'obj2' => new \stdClass, |
|
110
|
|
|
'false' => false, |
|
111
|
|
|
]); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertFalse($data->equals('e1', 'e2')); |
|
114
|
|
|
$this->assertFalse($data->equals('01', '02')); |
|
115
|
|
|
$this->assertFalse($data->equals('foo1', 'foo2')); |
|
116
|
|
|
$this->assertFalse($data->equals('obj1', 'obj2')); |
|
117
|
|
|
$this->assertFalse($data->equals('false', '01')); |
|
118
|
|
|
$this->assertFalse($data->equals('false', '02')); |
|
119
|
|
|
|
|
120
|
|
|
// NULL checks |
|
121
|
|
|
$this->assertTrue($data->equals('e1', 'non-existent')); |
|
122
|
|
|
$this->assertTrue($data->equals('non-existent-1', 'non-existent-2')); |
|
123
|
|
|
$this->assertFalse($data->equals('e2', 'non-existent')); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function setUp(): void |
|
127
|
|
|
{ |
|
128
|
|
|
$this->SUT = new Immutable(require __DIR__ . '/fixtures/nested-array.php'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|