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\ImmutableIntegerArray; |
14
|
|
|
|
15
|
|
|
final class ImmutableIntegerArrayTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function test_construct(): void |
18
|
|
|
{ |
19
|
|
|
$test = new ImmutableIntegerArray([1]); |
20
|
|
|
|
21
|
|
|
self::assertInstanceOf(ImmutableIntegerArray::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 ImmutableIntegerArray($arguments); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function providerInvalidScalarInputType(): Generator |
36
|
|
|
{ |
37
|
|
|
yield 'Receiving floats' => [ |
38
|
|
|
'arguments' => [1.23, 4.56], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
yield 'Receiving booleans' => [ |
42
|
|
|
'arguments' => [true, false], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
yield 'Receiving stdClasses' => [ |
46
|
|
|
'arguments' => [new stdClass(), new stdClass()], |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
yield 'Receiving strings' => [ |
50
|
|
|
'arguments' => ['str1', 'str2'], |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
yield 'Receiving a mix of all scalars' => [ |
54
|
|
|
'arguments' => [true, 1, 2.3, 'string', new stdClass()], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function test_immutability_of_set(): void |
59
|
|
|
{ |
60
|
|
|
$test = new ImmutableIntegerArray([1337]); |
61
|
|
|
|
62
|
|
|
$this->expectExceptionObject(GuardException::immutableCannotMutate()); |
63
|
|
|
|
64
|
|
|
$test[] = 46; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_immutability_of_unset(): void |
68
|
|
|
{ |
69
|
|
|
$test = new ImmutableIntegerArray([1984]); |
70
|
|
|
|
71
|
|
|
$this->expectExceptionObject(GuardException::immutableCannotMutate()); |
72
|
|
|
|
73
|
|
|
unset($test[0]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|