Passed
Push — master ( 572884...88a5e5 )
by Chema
01:20 queued 11s
created

TypedArrayTest.php$2 ➔ collectionType()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TypedArraysTest\Unit;
6
7
use PHPUnit\Framework\TestCase;
8
use TypedArrays\AbstractTypedArray;
9
use TypedArrays\Exceptions\GuardException;
10
use TypedArrays\Exceptions\InvalidTypeException;
11
use TypedArrays\Scalars\MutableStringArray;
12
use TypedArraysTest\Unit\Fixtures\SimpleObject;
13
use TypedArraysTest\Unit\Fixtures\TypedArraySimpleObjects;
14
15
final class TypedArrayTest extends TestCase
16
{
17
    public function test_valid_enforcement_types(): void
18
    {
19
        $validScalar = new MutableStringArray();
20
        self::assertInstanceOf(AbstractTypedArray::class, $validScalar);
21
22
        $validClass = new TypedArraySimpleObjects();
23
        self::assertInstanceOf(AbstractTypedArray::class, $validClass);
24
    }
25
26
    public function test_keys_are_preserved(): void
27
    {
28
        $input = [
29
            'key1' => 'value1',
30
            'key2' => 'value2',
31
        ];
32
33
        $test = (array) new MutableStringArray($input);
34
35
        self::assertSame(['key1', 'key2'], array_keys($test));
36
    }
37
38
    public function test_invalid_scalar_enforcement_type(): void
39
    {
40
        $this->expectExceptionObject(GuardException::invalidEnforceType('array'));
41
42
        new class() extends AbstractTypedArray {
43
            protected function typeToEnforce(): string
44
            {
45
                return 'array';
46
            }
47
        };
48
    }
49
50
    public function test_invalid_class_enforcement_type(): void
51
    {
52
        $this->expectExceptionObject(GuardException::invalidEnforceType('InvalidClassName'));
53
54
        new class([]) extends AbstractTypedArray {
55
            protected function typeToEnforce(): string
56
            {
57
                return 'InvalidClassName';
58
            }
59
        };
60
    }
61
62
    public function test_invalid_class_collection_type(): void
63
    {
64
        $this->expectExceptionObject(GuardException::invalidCollectionType('InvalidCollectionType'));
65
66
        new class([]) extends AbstractTypedArray {
67
            protected function typeToEnforce(): string
68
            {
69
                return self::SCALAR_STRING;
70
            }
71
72
            protected function collectionType(): string
73
            {
74
                return 'InvalidCollectionType';
75
            }
76
        };
77
    }
78
79
    public function test_valid_input_types(): void
80
    {
81
        $scalars = new MutableStringArray(['test', 'test-again']);
82
        self::assertInstanceOf(AbstractTypedArray::class, $scalars);
83
84
        $classes = new TypedArraySimpleObjects([new SimpleObject(), new SimpleObject()]);
85
        self::assertInstanceOf(AbstractTypedArray::class, $classes);
86
    }
87
88
    public function test_can_use_as_array(): void
89
    {
90
        $test = new MutableStringArray(['test1', 'test2']);
91
92
        self::assertSame('test1', $test[0]);
93
        self::assertSame('test2', $test[1]);
94
        self::assertTrue(isset($test[0]));
95
        self::assertFalse(isset($test[100]));
96
97
        self::assertCount(2, $test);
98
    }
99
100
    public function test_null_not_allow_in_array(): void
101
    {
102
        $this->expectExceptionObject(InvalidTypeException::onInstantiate(MutableStringArray::class, 'NULL', 'string'));
103
        new MutableStringArray([null, 'test2']);
104
    }
105
}
106