providerInvalidScalarInputTypeOnInstantiate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 20
rs 9.9332
c 1
b 0
f 0
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\MutableFloatMap;
14
15
final class MutableFloatMapTest extends TestCase
16
{
17
    public function test_construct(): void
18
    {
19
        $test = new MutableFloatMap(['key' => 1.5]);
20
21
        self::assertInstanceOf(MutableFloatMap::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 MutableFloatMap($arguments);
33
    }
34
35
    public function providerInvalidScalarInputTypeOnInstantiate(): Generator
36
    {
37
        yield 'Receiving integers' => [
38
            'arguments' => ['key1' => 1, 'key2' => 2],
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 MutableFloatMap([]);
68
        $test['invalid'] = $argument;
69
    }
70
71
    public function providerInvalidScalarInputTypeOnAdd(): Generator
72
    {
73
        yield 'Adding integer' => [
74
            'argument' => 1,
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 MutableFloatMap([1.3]);
95
    }
96
97
    public function test_map_constructor_does_not_throw_any_exception_when_keys_are_specified(): void
98
    {
99
        $test = new MutableFloatMap(['valid' => 0.1]);
100
101
        self::assertSame(0.1, $test['valid']);
102
    }
103
104
    public function test_map_constructor_doest_not_throw_any_exception_when_empty_array_given(): void
105
    {
106
        $test = new MutableFloatMap([]);
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 MutableFloatMap(['valid' => 4.1]);
114
115
        $this->expectExceptionObject(GuardException::keysRequiredInMap());
116
117
        $test[] = 4.2;
118
    }
119
120
    public function test_map_setter_does_not_throw_any_exception_when_key_is_specified(): void
121
    {
122
        $test = new MutableFloatMap(['key' => 1.3]);
123
124
        $test['valid'] = 5.6;
125
126
        self::assertSame(5.6, $test['valid']);
127
    }
128
}
129