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\ImmutableIntegerMap; |
14
|
|
|
|
15
|
|
|
final class ImmutableIntegerMapTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function test_construct(): void |
18
|
|
|
{ |
19
|
|
|
$test = new ImmutableIntegerMap(['key' => 1]); |
20
|
|
|
|
21
|
|
|
self::assertInstanceOf(ImmutableIntegerMap::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 ImmutableIntegerMap($arguments); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function providerInvalidScalarInputType(): Generator |
36
|
|
|
{ |
37
|
|
|
yield 'Receiving floats' => [ |
38
|
|
|
'arguments' => ['key1' => 1.23, 'key2' => 4.56], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
yield 'Receiving booleans' => [ |
42
|
|
|
'arguments' => ['key1' => true, 'key2' => false], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
yield 'Receiving stdClasses' => [ |
46
|
|
|
'arguments' => ['key1' => new stdClass(), 'key2' => new stdClass()], |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
yield 'Receiving strings' => [ |
50
|
|
|
'arguments' => ['key1' => 'str1', 'key2' => 'str2'], |
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 ImmutableIntegerMap(['key' => 1337]); |
61
|
|
|
|
62
|
|
|
$this->expectExceptionObject(GuardException::immutableCannotMutate()); |
63
|
|
|
|
64
|
|
|
$test['invalid'] = 46; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_immutability_of_unset(): void |
68
|
|
|
{ |
69
|
|
|
$test = new ImmutableIntegerMap(['key' => 1984]); |
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 ImmutableIntegerMap([13]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function test_map_constructor_does_not_throw_any_exception_when_keys_are_specified(): void |
84
|
|
|
{ |
85
|
|
|
$test = new ImmutableIntegerMap(['valid' => 3]); |
86
|
|
|
|
87
|
|
|
self::assertSame(3, $test['valid']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function test_map_constructor_doest_not_throw_any_exception_when_empty_array_given(): void |
91
|
|
|
{ |
92
|
|
|
$test = new ImmutableIntegerMap([]); |
93
|
|
|
|
94
|
|
|
self::assertEmpty((array) $test); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|