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\TickHandler; |
8
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class TickHandlerTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function testConstructorWithAllParameters(): void |
14
|
|
|
{ |
15
|
|
|
$commands = [ |
16
|
|
|
$this->createMock(AbstractStandardCommand::class), |
17
|
|
|
$this->createMock(AbstractStandardCommand::class), |
18
|
|
|
]; |
19
|
|
|
$when = 'condition'; |
20
|
|
|
$description = 'Test handler'; |
21
|
|
|
$minimumDelay = 2000; |
22
|
|
|
|
23
|
|
|
$handler = new TickHandler($commands, $when, $description, $minimumDelay); |
24
|
|
|
|
25
|
|
|
$this->assertSame($commands, $handler->commands); |
26
|
|
|
$this->assertSame($when, $handler->when); |
27
|
|
|
$this->assertSame($description, $handler->description); |
28
|
|
|
$this->assertSame($minimumDelay, $handler->minimumDelay); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testConstructorWithDefaultParameters(): void |
32
|
|
|
{ |
33
|
|
|
$handler = new TickHandler(); |
34
|
|
|
|
35
|
|
|
$this->assertSame([], $handler->commands); |
36
|
|
|
$this->assertNull($handler->when); |
37
|
|
|
$this->assertSame('', $handler->description); |
38
|
|
|
$this->assertSame(TickHandler::DEFAULT_MINIMUM_DELAY, $handler->minimumDelay); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testConstructorFiltersInvalidCommands(): void |
42
|
|
|
{ |
43
|
|
|
$validCommand = $this->createMock(AbstractStandardCommand::class); |
44
|
|
|
$invalidCommands = [ |
45
|
|
|
$validCommand, |
46
|
|
|
'not a command', |
47
|
|
|
null, |
48
|
|
|
123, |
49
|
|
|
$this->createMock(AbstractStandardCommand::class), |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$handler = new TickHandler($invalidCommands); |
53
|
|
|
|
54
|
|
|
$this->assertCount(2, $handler->commands); |
55
|
|
|
$this->assertContains($validCommand, $handler->commands); |
56
|
|
|
$this->assertIsArray($handler->commands); |
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
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$handler = new TickHandler($commands); |
71
|
|
|
|
72
|
|
|
$this->assertSame([0 => $command1, 1 => $command2], $handler->commands); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testDefaultMinimumDelayConstant(): void |
76
|
|
|
{ |
77
|
|
|
$this->assertSame(1000, TickHandler::DEFAULT_MINIMUM_DELAY); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testJsonSerializeWithAllProperties(): void |
81
|
|
|
{ |
82
|
|
|
$commands = [ |
83
|
|
|
$this->createMock(AbstractStandardCommand::class), |
84
|
|
|
$this->createMock(AbstractStandardCommand::class), |
85
|
|
|
]; |
86
|
|
|
$when = 'active'; |
87
|
|
|
$description = 'Full handler'; |
88
|
|
|
$minimumDelay = 3000; |
89
|
|
|
|
90
|
|
|
$handler = new TickHandler($commands, $when, $description, $minimumDelay); |
91
|
|
|
$result = $handler->jsonSerialize(); |
92
|
|
|
|
93
|
|
|
$this->assertSame($commands, $result['commands']); |
94
|
|
|
$this->assertSame($description, $result['description']); |
95
|
|
|
$this->assertSame($minimumDelay, $result['minimumDelay']); |
96
|
|
|
$this->assertSame($when, $result['when']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testJsonSerializeWithDefaultValues(): void |
100
|
|
|
{ |
101
|
|
|
$handler = new TickHandler(); |
102
|
|
|
$result = $handler->jsonSerialize(); |
103
|
|
|
|
104
|
|
|
$this->assertEmpty($result); |
105
|
|
|
$this->assertArrayNotHasKey('commands', $result); |
106
|
|
|
$this->assertArrayNotHasKey('description', $result); |
107
|
|
|
$this->assertArrayNotHasKey('minimumDelay', $result); |
108
|
|
|
$this->assertArrayNotHasKey('when', $result); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testJsonSerializeFiltersEmptyCommands(): void |
112
|
|
|
{ |
113
|
|
|
$handler = new TickHandler([], 'condition', 'test', 500); |
114
|
|
|
$result = $handler->jsonSerialize(); |
115
|
|
|
|
116
|
|
|
$this->assertArrayNotHasKey('commands', $result); |
117
|
|
|
$this->assertArrayHasKey('when', $result); |
118
|
|
|
$this->assertArrayHasKey('description', $result); |
119
|
|
|
$this->assertArrayHasKey('minimumDelay', $result); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testJsonSerializeFiltersEmptyDescription(): void |
123
|
|
|
{ |
124
|
|
|
$handler = new TickHandler([], null, '', 2000); |
125
|
|
|
$result = $handler->jsonSerialize(); |
126
|
|
|
|
127
|
|
|
$this->assertArrayNotHasKey('description', $result); |
128
|
|
|
$this->assertArrayHasKey('minimumDelay', $result); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testJsonSerializeFiltersDefaultMinimumDelay(): void |
132
|
|
|
{ |
133
|
|
|
$handler = new TickHandler([], 'when', 'desc', TickHandler::DEFAULT_MINIMUM_DELAY); |
134
|
|
|
$result = $handler->jsonSerialize(); |
135
|
|
|
|
136
|
|
|
$this->assertArrayNotHasKey('minimumDelay', $result); |
137
|
|
|
$this->assertArrayHasKey('when', $result); |
138
|
|
|
$this->assertArrayHasKey('description', $result); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testJsonSerializeFiltersNullWhen(): void |
142
|
|
|
{ |
143
|
|
|
$handler = new TickHandler([], null, 'desc', 500); |
144
|
|
|
$result = $handler->jsonSerialize(); |
145
|
|
|
|
146
|
|
|
$this->assertArrayNotHasKey('when', $result); |
147
|
|
|
$this->assertArrayHasKey('description', $result); |
148
|
|
|
$this->assertArrayHasKey('minimumDelay', $result); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testJsonSerializeFiltersEmptyWhen(): void |
152
|
|
|
{ |
153
|
|
|
$handler = new TickHandler([], '', 'desc', 500); |
154
|
|
|
$result = $handler->jsonSerialize(); |
155
|
|
|
|
156
|
|
|
$this->assertArrayNotHasKey('when', $result); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testJsonSerializeWithNonEmptyCommands(): void |
160
|
|
|
{ |
161
|
|
|
$commands = [$this->createMock(AbstractStandardCommand::class)]; |
162
|
|
|
$handler = new TickHandler($commands); |
163
|
|
|
$result = $handler->jsonSerialize(); |
164
|
|
|
|
165
|
|
|
$this->assertArrayHasKey('commands', $result); |
166
|
|
|
$this->assertSame($commands, $result['commands']); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testJsonSerializeWithNonEmptyDescription(): void |
170
|
|
|
{ |
171
|
|
|
$handler = new TickHandler([], null, 'Test description'); |
172
|
|
|
$result = $handler->jsonSerialize(); |
173
|
|
|
|
174
|
|
|
$this->assertArrayHasKey('description', $result); |
175
|
|
|
$this->assertSame('Test description', $result['description']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testJsonSerializeWithNonDefaultMinimumDelay(): void |
179
|
|
|
{ |
180
|
|
|
$handler = new TickHandler([], null, '', 5000); |
181
|
|
|
$result = $handler->jsonSerialize(); |
182
|
|
|
|
183
|
|
|
$this->assertArrayHasKey('minimumDelay', $result); |
184
|
|
|
$this->assertSame(5000, $result['minimumDelay']); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testJsonSerializeWithNonEmptyWhen(): void |
188
|
|
|
{ |
189
|
|
|
$handler = new TickHandler([], 'viewport.width > 100'); |
190
|
|
|
$result = $handler->jsonSerialize(); |
191
|
|
|
|
192
|
|
|
$this->assertArrayHasKey('when', $result); |
193
|
|
|
$this->assertSame('viewport.width > 100', $result['when']); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testImplementsJsonSerializable(): void |
197
|
|
|
{ |
198
|
|
|
$handler = new TickHandler(); |
199
|
|
|
|
200
|
|
|
$this->assertInstanceOf(\JsonSerializable::class, $handler); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|