1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TypedArraysTest\Unit\Scalars; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use stdClass; |
10
|
|
|
use TypedArrays\AbstractTypedArray; |
11
|
|
|
use TypedArrays\Exceptions\GuardException; |
12
|
|
|
use TypedArrays\Exceptions\InvalidTypeException; |
13
|
|
|
use TypedArrays\Scalars\ImmutableStringMap; |
14
|
|
|
|
15
|
|
|
final class ImmutableStringMapTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function test_construct(): void |
18
|
|
|
{ |
19
|
|
|
$test = new ImmutableStringMap(['key' => 'test']); |
20
|
|
|
|
21
|
|
|
self::assertInstanceOf(ImmutableStringMap::class, $test); |
22
|
|
|
self::assertInstanceOf(AbstractTypedArray::class, $test); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @dataProvider providerInvalidScalarInputType |
27
|
|
|
*/ |
28
|
|
|
public function test_invalid_scalar_input_type(array $arguments): void |
29
|
|
|
{ |
30
|
|
|
$this->expectException(InvalidTypeException::class); |
31
|
|
|
|
32
|
|
|
new ImmutableStringMap($arguments); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function providerInvalidScalarInputType(): Generator |
36
|
|
|
{ |
37
|
|
|
yield 'Receiving integers' => [ |
38
|
|
|
'arguments' => ['key1' => 1, 'key2' => 2], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
yield 'Receiving floats' => [ |
42
|
|
|
'arguments' => ['key1' => 1.23, 'key2' => 4.56], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
yield 'Receiving booleans' => [ |
46
|
|
|
'arguments' => ['key1' => true, 'key2' => false], |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
yield 'Receiving stdClasses' => [ |
50
|
|
|
'arguments' => ['key1' => new stdClass(), 'key2' => new stdClass()], |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
yield 'Receiving a mix of all scalars' => [ |
54
|
|
|
'arguments' => ['key1' => true, 'key2' => 1, 'key3' => 2.3, 'key4' => 'string', 'key5' => new stdClass()], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function test_immutability_of_set(): void |
59
|
|
|
{ |
60
|
|
|
$test = new ImmutableStringMap(['key' => 'test']); |
61
|
|
|
|
62
|
|
|
$this->expectExceptionObject(GuardException::immutableCannotMutate()); |
63
|
|
|
|
64
|
|
|
$test['invalid'] = 'invalid'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_immutability_of_unset(): void |
68
|
|
|
{ |
69
|
|
|
$test = new ImmutableStringMap(['key' => 'test']); |
70
|
|
|
|
71
|
|
|
$this->expectExceptionObject(GuardException::immutableCannotMutate()); |
72
|
|
|
|
73
|
|
|
unset($test['key']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function test_map_constructor_throws_an_exception_when_keys_are_not_specified(): void |
77
|
|
|
{ |
78
|
|
|
$this->expectExceptionObject(GuardException::keysRequiredInMap()); |
79
|
|
|
|
80
|
|
|
new ImmutableStringMap(['invalid']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function test_map_constructor_does_not_throw_any_exception_when_keys_are_specified(): void |
84
|
|
|
{ |
85
|
|
|
$test = new ImmutableStringMap(['valid' => 'test']); |
86
|
|
|
|
87
|
|
|
self::assertSame('test', $test['valid']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function test_map_constructor_doest_not_throw_any_exception_when_empty_array_given(): void |
91
|
|
|
{ |
92
|
|
|
$test = new ImmutableStringMap([]); |
93
|
|
|
|
94
|
|
|
self::assertEmpty((array) $test); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|