MutableIntegerMapTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 112
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A test_construct() 0 6 1
A test_invalid_scalar_input_type_on_instantiate() 0 5 1
A test_invalid_scalar_input_type_on_add() 0 6 1
A providerInvalidScalarInputTypeOnInstantiate() 0 20 1
A test_map_setter_does_not_throw_any_exception_when_key_is_specified() 0 7 1
A test_map_setter_throws_an_exception_when_key_is_not_specified() 0 7 1
A providerInvalidScalarInputTypeOnAdd() 0 16 1
A test_map_constructor_does_not_throw_any_exception_when_keys_are_specified() 0 5 1
A test_map_constructor_throws_an_exception_when_keys_are_not_specified() 0 5 1
A test_map_constructor_doest_not_throw_any_exception_when_empty_array_given() 0 5 1
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\MutableIntegerMap;
14
15
final class MutableIntegerMapTest extends TestCase
16
{
17
    public function test_construct(): void
18
    {
19
        $test = new MutableIntegerMap(['key' => 1]);
20
21
        self::assertInstanceOf(MutableIntegerMap::class, $test);
22
        self::assertInstanceOf(AbstractTypedArray::class, $test);
23
    }
24
25
    /**
26
     * @dataProvider providerInvalidScalarInputTypeOnInstantiate
27
     */
28
    public function test_invalid_scalar_input_type_on_instantiate(array $arguments): void
29
    {
30
        $this->expectException(InvalidTypeException::class);
31
32
        new MutableIntegerMap($arguments);
33
    }
34
35
    public function providerInvalidScalarInputTypeOnInstantiate(): 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
    /**
59
     * @dataProvider providerInvalidScalarInputTypeOnAdd
60
     *
61
     * @param mixed $argument
62
     */
63
    public function test_invalid_scalar_input_type_on_add($argument): void
64
    {
65
        $this->expectException(InvalidTypeException::class);
66
67
        $test = new MutableIntegerMap([]);
68
        $test['invalid'] = $argument;
69
    }
70
71
    public function providerInvalidScalarInputTypeOnAdd(): Generator
72
    {
73
        yield 'Adding float' => [
74
            'argument' => 1.23,
75
        ];
76
77
        yield 'Adding boolean' => [
78
            'argument' => true,
79
        ];
80
81
        yield 'Adding stdClass' => [
82
            'argument' => new stdClass(),
83
        ];
84
85
        yield 'Adding string' => [
86
            'argument' => 'str1',
87
        ];
88
    }
89
90
    public function test_map_constructor_throws_an_exception_when_keys_are_not_specified(): void
91
    {
92
        $this->expectExceptionObject(GuardException::keysRequiredInMap());
93
94
        new MutableIntegerMap([13]);
95
    }
96
97
    public function test_map_constructor_does_not_throw_any_exception_when_keys_are_specified(): void
98
    {
99
        $test = new MutableIntegerMap(['valid' => 3]);
100
101
        self::assertSame(3, $test['valid']);
102
    }
103
104
    public function test_map_constructor_doest_not_throw_any_exception_when_empty_array_given(): void
105
    {
106
        $test = new MutableIntegerMap([]);
107
108
        self::assertEmpty((array) $test);
109
    }
110
111
    public function test_map_setter_throws_an_exception_when_key_is_not_specified(): void
112
    {
113
        $test = new MutableIntegerMap(['valid' => 14]);
114
115
        $this->expectExceptionObject(GuardException::keysRequiredInMap());
116
117
        $test[] = 15;
118
    }
119
120
    public function test_map_setter_does_not_throw_any_exception_when_key_is_specified(): void
121
    {
122
        $test = new MutableIntegerMap(['key' => 92]);
123
124
        $test['valid'] = 65;
125
126
        self::assertSame(65, $test['valid']);
127
    }
128
}
129