VisibilityChangeHandlerTest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 118
dl 0
loc 221
rs 10
c 1
b 0
f 1
wmc 19

19 Methods

Rating   Name   Duplication   Size   Complexity  
A testJsonSerializeFiltersEmptyCommands() 0 8 1
A testConstructorWithOnlyInvalidCommands() 0 5 1
A testJsonSerializeWithWhitespaceWhen() 0 7 1
A testConstructorWithEmptyCommands() 0 5 1
A testConstructorReindexesArray() 0 17 1
A testImplementsJsonSerializable() 0 5 1
A testJsonSerializeFiltersNullDescription() 0 9 1
A testJsonSerializeFiltersNullWhen() 0 9 1
A testJsonSerializeWithOnlyDescription() 0 9 1
A testJsonSerializeWithOnlyWhen() 0 9 1
A testJsonSerializeFiltersEmptyWhen() 0 9 1
A testJsonSerializeWithOnlyCommands() 0 10 1
A testConstructorWithAllParameters() 0 14 1
A testJsonSerializeWithAllProperties() 0 15 1
A testJsonSerializeFiltersEmptyDescription() 0 9 1
A testConstructorFiltersInvalidCommands() 0 20 1
A testJsonSerializeWithWhitespaceDescription() 0 7 1
A testConstructorWithDefaultParameters() 0 7 1
A testJsonSerializeWithDefaultValues() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\APL\Document;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\VisibilityChangeHandler;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
9
use PHPUnit\Framework\TestCase;
10
11
class VisibilityChangeHandlerTest extends TestCase
12
{
13
    public function testConstructorWithAllParameters(): void
14
    {
15
        $commands = [
16
            $this->createMock(AbstractStandardCommand::class),
17
            $this->createMock(AbstractStandardCommand::class),
18
        ];
19
        $when = 'viewport.width > 100';
20
        $description = 'Test visibility handler';
21
22
        $handler = new VisibilityChangeHandler($commands, $when, $description);
23
24
        $this->assertSame($commands, $handler->commands);
25
        $this->assertSame($when, $handler->when);
26
        $this->assertSame($description, $handler->description);
27
    }
28
29
    public function testConstructorWithDefaultParameters(): void
30
    {
31
        $handler = new VisibilityChangeHandler();
32
33
        $this->assertSame([], $handler->commands);
34
        $this->assertNull($handler->when);
35
        $this->assertNull($handler->description);
36
    }
37
38
    public function testConstructorFiltersInvalidCommands(): void
39
    {
40
        $validCommand1 = $this->createMock(AbstractStandardCommand::class);
41
        $validCommand2 = $this->createMock(AbstractStandardCommand::class);
42
        $invalidCommands = [
43
            $validCommand1,
44
            'not a command',
45
            null,
46
            123,
47
            $validCommand2,
48
            false,
49
            [],
50
        ];
51
52
        $handler = new VisibilityChangeHandler($invalidCommands);
53
54
        $this->assertCount(2, $handler->commands);
55
        $this->assertSame($validCommand1, $handler->commands[0]);
56
        $this->assertSame($validCommand2, $handler->commands[1]);
57
        $this->assertContainsOnlyInstancesOf(AbstractStandardCommand::class, $handler->commands);
58
    }
59
60
    public function testConstructorReindexesArray(): void
61
    {
62
        $command1 = $this->createMock(AbstractStandardCommand::class);
63
        $command2 = $this->createMock(AbstractStandardCommand::class);
64
        $commands = [
65
            0 => $command1,
66
            2 => 'invalid',
67
            5 => $command2,
68
            7 => null,
69
        ];
70
71
        $handler = new VisibilityChangeHandler($commands);
72
73
        $this->assertSame([0 => $command1, 1 => $command2], $handler->commands);
74
        $this->assertArrayHasKey(0, $handler->commands);
75
        $this->assertArrayHasKey(1, $handler->commands);
76
        $this->assertArrayNotHasKey(2, $handler->commands);
77
    }
78
79
    public function testConstructorWithEmptyCommands(): void
80
    {
81
        $handler = new VisibilityChangeHandler([]);
82
83
        $this->assertSame([], $handler->commands);
84
    }
85
86
    public function testConstructorWithOnlyInvalidCommands(): void
87
    {
88
        $handler = new VisibilityChangeHandler(['string', 123, null, false]);
89
90
        $this->assertSame([], $handler->commands);
91
    }
92
93
    public function testJsonSerializeWithAllProperties(): void
94
    {
95
        $commands = [
96
            $this->createMock(AbstractStandardCommand::class),
97
            $this->createMock(AbstractStandardCommand::class),
98
        ];
99
        $when = 'element.visible';
100
        $description = 'Visibility change handler';
101
102
        $handler = new VisibilityChangeHandler($commands, $when, $description);
103
        $result = $handler->jsonSerialize();
104
105
        $this->assertSame($commands, $result['commands']);
106
        $this->assertSame($description, $result['description']);
107
        $this->assertSame($when, $result['when']);
108
    }
109
110
    public function testJsonSerializeWithDefaultValues(): void
111
    {
112
        $handler = new VisibilityChangeHandler();
113
        $result = $handler->jsonSerialize();
114
115
        $this->assertEmpty($result);
116
        $this->assertArrayNotHasKey('commands', $result);
117
        $this->assertArrayNotHasKey('description', $result);
118
        $this->assertArrayNotHasKey('when', $result);
119
    }
120
121
    public function testJsonSerializeFiltersEmptyCommands(): void
122
    {
123
        $handler = new VisibilityChangeHandler([], 'condition', 'test');
124
        $result = $handler->jsonSerialize();
125
126
        $this->assertArrayNotHasKey('commands', $result);
127
        $this->assertArrayHasKey('description', $result);
128
        $this->assertArrayHasKey('when', $result);
129
    }
130
131
    public function testJsonSerializeFiltersNullDescription(): void
132
    {
133
        $commands = [$this->createMock(AbstractStandardCommand::class)];
134
        $handler = new VisibilityChangeHandler($commands, 'when', null);
135
        $result = $handler->jsonSerialize();
136
137
        $this->assertArrayHasKey('commands', $result);
138
        $this->assertArrayHasKey('when', $result);
139
        $this->assertArrayNotHasKey('description', $result);
140
    }
141
142
    public function testJsonSerializeFiltersEmptyDescription(): void
143
    {
144
        $commands = [$this->createMock(AbstractStandardCommand::class)];
145
        $handler = new VisibilityChangeHandler($commands, 'when', '');
146
        $result = $handler->jsonSerialize();
147
148
        $this->assertArrayHasKey('commands', $result);
149
        $this->assertArrayHasKey('when', $result);
150
        $this->assertArrayNotHasKey('description', $result);
151
    }
152
153
    public function testJsonSerializeFiltersNullWhen(): void
154
    {
155
        $commands = [$this->createMock(AbstractStandardCommand::class)];
156
        $handler = new VisibilityChangeHandler($commands, null, 'description');
157
        $result = $handler->jsonSerialize();
158
159
        $this->assertArrayHasKey('commands', $result);
160
        $this->assertArrayHasKey('description', $result);
161
        $this->assertArrayNotHasKey('when', $result);
162
    }
163
164
    public function testJsonSerializeFiltersEmptyWhen(): void
165
    {
166
        $commands = [$this->createMock(AbstractStandardCommand::class)];
167
        $handler = new VisibilityChangeHandler($commands, '', 'description');
168
        $result = $handler->jsonSerialize();
169
170
        $this->assertArrayHasKey('commands', $result);
171
        $this->assertArrayHasKey('description', $result);
172
        $this->assertArrayNotHasKey('when', $result);
173
    }
174
175
    public function testJsonSerializeWithOnlyCommands(): void
176
    {
177
        $commands = [$this->createMock(AbstractStandardCommand::class)];
178
        $handler = new VisibilityChangeHandler($commands);
179
        $result = $handler->jsonSerialize();
180
181
        $this->assertArrayHasKey('commands', $result);
182
        $this->assertArrayNotHasKey('description', $result);
183
        $this->assertArrayNotHasKey('when', $result);
184
        $this->assertSame($commands, $result['commands']);
185
    }
186
187
    public function testJsonSerializeWithOnlyDescription(): void
188
    {
189
        $handler = new VisibilityChangeHandler([], null, 'Test description');
190
        $result = $handler->jsonSerialize();
191
192
        $this->assertArrayHasKey('description', $result);
193
        $this->assertArrayNotHasKey('commands', $result);
194
        $this->assertArrayNotHasKey('when', $result);
195
        $this->assertSame('Test description', $result['description']);
196
    }
197
198
    public function testJsonSerializeWithOnlyWhen(): void
199
    {
200
        $handler = new VisibilityChangeHandler([], 'visibility.changed');
201
        $result = $handler->jsonSerialize();
202
203
        $this->assertArrayHasKey('when', $result);
204
        $this->assertArrayNotHasKey('commands', $result);
205
        $this->assertArrayNotHasKey('description', $result);
206
        $this->assertSame('visibility.changed', $result['when']);
207
    }
208
209
    public function testJsonSerializeWithWhitespaceDescription(): void
210
    {
211
        $handler = new VisibilityChangeHandler([], null, '   ');
212
        $result = $handler->jsonSerialize();
213
214
        $this->assertArrayHasKey('description', $result);
215
        $this->assertSame('   ', $result['description']);
216
    }
217
218
    public function testJsonSerializeWithWhitespaceWhen(): void
219
    {
220
        $handler = new VisibilityChangeHandler([], '   ');
221
        $result = $handler->jsonSerialize();
222
223
        $this->assertArrayHasKey('when', $result);
224
        $this->assertSame('   ', $result['when']);
225
    }
226
227
    public function testImplementsJsonSerializable(): void
228
    {
229
        $handler = new VisibilityChangeHandler();
230
231
        $this->assertInstanceOf(\JsonSerializable::class, $handler);
232
    }
233
}
234