1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/data-structure project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Tests\DataStructure; |
10
|
|
|
|
11
|
|
|
use Daikon\Tests\DataStructure\Fixture\PlainMap; |
12
|
|
|
use DateTime; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use stdClass; |
16
|
|
|
|
17
|
|
|
final class MapTest extends TestCase |
18
|
|
|
{ |
19
|
1 |
|
public function testConstructWithoutParams(): void |
20
|
|
|
{ |
21
|
1 |
|
$this->assertInstanceOf(PlainMap::class, new PlainMap); |
22
|
1 |
|
} |
23
|
|
|
|
24
|
1 |
|
public function testConstructWithParams(): void |
25
|
|
|
{ |
26
|
1 |
|
$map = new PlainMap(['k' => 'v']); |
27
|
|
|
/** @psalm-suppress RedundantCondition */ |
28
|
1 |
|
$this->assertInstanceOf(PlainMap::class, $map); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function testConstructWithObjects(): void |
32
|
|
|
{ |
33
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
34
|
1 |
|
$this->expectExceptionCode(32); |
35
|
1 |
|
$this->expectExceptionMessage( |
36
|
|
|
'Invalid value type given to Daikon\Tests\DataStructure\Fixture\PlainMap, '. |
37
|
1 |
|
"expected scalar or array but was given 'DateTime'" |
38
|
|
|
); |
39
|
1 |
|
new PlainMap(['a1337' => new DateTime]); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function testConstructWithIntegerKey(): void |
43
|
|
|
{ |
44
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
45
|
1 |
|
$this->expectExceptionCode(16); |
46
|
1 |
|
$this->expectExceptionMessage('Key must be a valid string'); |
47
|
1 |
|
new PlainMap([0 => 'v0']); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function testKeys(): void |
51
|
|
|
{ |
52
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 1]); |
53
|
1 |
|
$this->assertSame(['k0', 'k1'], $map->keys()); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
1 |
|
public function testHas(): void |
57
|
|
|
{ |
58
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
59
|
1 |
|
$this->assertTrue($map->has('k0')); |
60
|
1 |
|
$this->assertTrue($map->has('k1')); |
61
|
1 |
|
$this->assertFalse($map->has('K0')); |
62
|
1 |
|
$this->assertFalse($map->has('K1')); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function testGet(): void |
66
|
|
|
{ |
67
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 1]); |
68
|
1 |
|
$this->assertSame('v0', $map->get('k0')); |
69
|
1 |
|
$this->assertSame(1, $map->get('k1')); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
public function testGetWithDefault(): void |
73
|
|
|
{ |
74
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
75
|
1 |
|
$this->assertSame('y', $map->get('x', 'y')); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
public function testGetWithNullDefault(): void |
79
|
|
|
{ |
80
|
1 |
|
$map = new PlainMap(['k0' => 'v0']); |
81
|
1 |
|
$this->assertNull($map->get('x', null)); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
1 |
|
public function testGetWithInvalidDefault(): void |
85
|
|
|
{ |
86
|
1 |
|
$map = new PlainMap(['k0' => 'v0']); |
87
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
88
|
1 |
|
$this->expectExceptionCode(32); |
89
|
1 |
|
$this->expectExceptionMessage( |
90
|
|
|
'Invalid value type given to Daikon\Tests\DataStructure\Fixture\PlainMap, '. |
91
|
1 |
|
"expected scalar or array but was given 'stdClass'" |
92
|
|
|
); |
93
|
1 |
|
$map->get('x', new stdClass); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function testGetWithNoDefault(): void |
97
|
|
|
{ |
98
|
1 |
|
$map = new PlainMap(['k0' => 'v0']); |
99
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
100
|
1 |
|
$this->expectExceptionCode(32); |
101
|
1 |
|
$this->expectExceptionMessage("Key 'x' not found and no default provided"); |
102
|
1 |
|
$map->get('x'); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function testGetThrowsForInternalProperties(): void |
106
|
|
|
{ |
107
|
1 |
|
$map = new PlainMap(['k0' => 'v1']); |
108
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
109
|
1 |
|
$this->expectExceptionCode(32); |
110
|
1 |
|
$map->validTypes; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
public function testWith(): void |
114
|
|
|
{ |
115
|
1 |
|
$map = new PlainMap(['k0' => 'v0']); |
116
|
1 |
|
$unwrappedMap = $map->with('k1', 'v1')->unwrap(); |
117
|
1 |
|
$this->assertSame('v0', $unwrappedMap['k0']); |
118
|
1 |
|
$this->assertSame('v1', $unwrappedMap['k1']); |
119
|
1 |
|
$this->assertCount(2, $unwrappedMap); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
public function testWithFailsOnUnacceptableType(): void |
123
|
|
|
{ |
124
|
1 |
|
$map = new PlainMap(['k0' => 'v0']); |
125
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
126
|
1 |
|
$this->expectExceptionCode(32); |
127
|
1 |
|
$this->expectExceptionMessage( |
128
|
|
|
'Invalid value type given to Daikon\Tests\DataStructure\Fixture\PlainMap, '. |
129
|
1 |
|
"expected scalar or array but was given 'stdClass'" |
130
|
|
|
); |
131
|
1 |
|
$map->with('k1', new stdClass); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function testWithout(): void |
135
|
|
|
{ |
136
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1', 'k2' => 'v2']); |
137
|
1 |
|
$unwrappedMap = $map->unwrap(); |
138
|
1 |
|
$prunedMap = $map->without('k1')->unwrap(); |
139
|
1 |
|
$this->assertSame($unwrappedMap['k0'], $prunedMap['k0']); |
140
|
1 |
|
$this->assertCount(2, $prunedMap); |
141
|
1 |
|
$this->assertSame('v0', $prunedMap['k0']); |
142
|
1 |
|
$this->assertSame('v2', $prunedMap['k2']); |
143
|
1 |
|
$this->assertArrayNotHasKey('k1', $prunedMap); |
144
|
1 |
|
} |
145
|
|
|
|
146
|
1 |
|
public function testWithoutWithNotExistentKey(): void |
147
|
|
|
{ |
148
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
149
|
1 |
|
$this->expectException(InvalidArgumentException::class); |
150
|
1 |
|
$this->expectExceptionCode(32); |
151
|
1 |
|
$this->expectExceptionMessage("Key 'k2' not found"); |
152
|
1 |
|
$map->without('k2'); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
public function testFirst(): void |
156
|
|
|
{ |
157
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
158
|
1 |
|
$unwrappedMap = $map->unwrap(); |
159
|
1 |
|
$this->assertSame('v0', $unwrappedMap['k0']); |
160
|
1 |
|
$this->assertSame($unwrappedMap['k0'], $map->first()); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
|
public function testLast(): void |
164
|
|
|
{ |
165
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
166
|
1 |
|
$unwrappedMap = $map->unwrap(); |
167
|
1 |
|
$this->assertSame('v1', $unwrappedMap['k1']); |
168
|
1 |
|
$this->assertSame($unwrappedMap['k1'], $map->last()); |
169
|
1 |
|
} |
170
|
|
|
|
171
|
1 |
|
public function testIsEmpty(): void |
172
|
|
|
{ |
173
|
1 |
|
$map = new PlainMap(['k0' => 'v1']); |
174
|
1 |
|
$this->assertFalse($map->isEmpty()); |
175
|
1 |
|
$map = new PlainMap; |
176
|
1 |
|
$this->assertTrue($map->isEmpty()); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
1 |
|
public function testEquals(): void |
180
|
|
|
{ |
181
|
1 |
|
$map0 = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
182
|
1 |
|
$map1 = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
183
|
1 |
|
$map2 = new PlainMap(['k2' => 'v2']); |
184
|
1 |
|
$this->assertTrue($map0->equals($map1)); |
185
|
1 |
|
$this->assertFalse($map1->equals($map2)); |
186
|
1 |
|
$this->assertFalse($map0->equals($map2)); |
187
|
1 |
|
} |
188
|
|
|
|
189
|
1 |
|
public function testCount(): void |
190
|
|
|
{ |
191
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1']); |
192
|
1 |
|
$this->assertCount(2, $map); |
193
|
1 |
|
} |
194
|
|
|
|
195
|
1 |
|
public function testunwrap(): void |
196
|
|
|
{ |
197
|
1 |
|
$state = ['k0' => 'v0', 'k1' => 'v1']; |
198
|
1 |
|
$map = new PlainMap($state); |
199
|
1 |
|
$this->assertSame($state, $map->unwrap()); |
200
|
1 |
|
} |
201
|
|
|
|
202
|
1 |
|
public function testGetIterator(): void |
203
|
|
|
{ |
204
|
1 |
|
$state = ['k0' => 'v0', 'k1' => 'v1']; |
205
|
1 |
|
$map = new PlainMap($state); |
206
|
1 |
|
foreach ($map as $key => $value) { |
207
|
1 |
|
$this->assertSame($state[$key], $value); |
208
|
|
|
} |
209
|
1 |
|
} |
210
|
|
|
|
211
|
1 |
|
public function testClone(): void |
212
|
|
|
{ |
213
|
1 |
|
$t0 = [new stdClass]; |
214
|
1 |
|
$map = new PlainMap(['k0' => 'v0', 'k1' => 'v1', 'k2' => $t0]); |
215
|
1 |
|
$clonedMap = clone $map; |
216
|
1 |
|
$this->assertNotSame($map->getIterator(), $clonedMap->getIterator()); |
217
|
1 |
|
$this->assertEquals($map->getIterator(), $clonedMap->getIterator()); |
218
|
1 |
|
$this->assertSame('v1', $map->get('k1')); |
219
|
|
|
//@todo handle deep clone objects in array? |
220
|
|
|
// $this->assertNotSame($t0, $clonedMap->get('k2')); |
221
|
1 |
|
$this->assertEquals($t0, $clonedMap->get('k2')); |
222
|
1 |
|
} |
223
|
|
|
} |
224
|
|
|
|