|
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\KeyHandler; |
|
8
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
class KeyHandlerTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public function testConstructorWithAllParameters(): void |
|
14
|
|
|
{ |
|
15
|
|
|
$commands = [ |
|
16
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
17
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
18
|
|
|
]; |
|
19
|
|
|
$propagate = true; |
|
20
|
|
|
$when = 'event.key == "Enter"'; |
|
21
|
|
|
|
|
22
|
|
|
$keyHandler = new KeyHandler($commands, $propagate, $when); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertSame($commands, $keyHandler->commands); |
|
25
|
|
|
$this->assertTrue($keyHandler->propagate); |
|
26
|
|
|
$this->assertSame($when, $keyHandler->when); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testConstructorWithDefaultParameters(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$keyHandler = new KeyHandler(); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertSame([], $keyHandler->commands); |
|
34
|
|
|
$this->assertFalse($keyHandler->propagate); |
|
35
|
|
|
$this->assertNull($keyHandler->when); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testJsonSerializeWithAllProperties(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$commands = [ |
|
41
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
42
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
43
|
|
|
]; |
|
44
|
|
|
$propagate = true; |
|
45
|
|
|
$when = 'event.type == "KeyDown"'; |
|
46
|
|
|
|
|
47
|
|
|
$keyHandler = new KeyHandler($commands, $propagate, $when); |
|
48
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame($commands, $result['commands']); |
|
51
|
|
|
$this->assertTrue($result['propagate']); |
|
52
|
|
|
$this->assertSame($when, $result['when']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testJsonSerializeWithDefaultValues(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$keyHandler = new KeyHandler(); |
|
58
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertSame([], $result['commands']); |
|
61
|
|
|
$this->assertFalse($result['propagate']); |
|
62
|
|
|
$this->assertArrayNotHasKey('when', $result); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testJsonSerializeWithEmptyCommands(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$keyHandler = new KeyHandler([], true, 'condition'); |
|
68
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertSame([], $result['commands']); |
|
71
|
|
|
$this->assertTrue($result['propagate']); |
|
72
|
|
|
$this->assertSame('condition', $result['when']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function testJsonSerializeWithNonEmptyCommands(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$commands = [$this->createMock(AbstractStandardCommand::class)]; |
|
78
|
|
|
$keyHandler = new KeyHandler($commands); |
|
79
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertSame($commands, $result['commands']); |
|
82
|
|
|
$this->assertFalse($result['propagate']); |
|
83
|
|
|
$this->assertArrayNotHasKey('when', $result); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testJsonSerializeWithPropagateFalse(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$keyHandler = new KeyHandler([], false); |
|
89
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertFalse($result['propagate']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testJsonSerializeWithPropagateTrue(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$keyHandler = new KeyHandler([], true); |
|
97
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertTrue($result['propagate']); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testJsonSerializeFiltersNullWhen(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$keyHandler = new KeyHandler([], false, null); |
|
105
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertArrayNotHasKey('when', $result); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testJsonSerializeWithEmptyStringWhen(): void |
|
111
|
|
|
{ |
|
112
|
|
|
$keyHandler = new KeyHandler([], false, ''); |
|
113
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertArrayHasKey('when', $result); |
|
116
|
|
|
$this->assertSame('', $result['when']); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testJsonSerializeWithValidWhenExpression(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$when = 'event.key == "Escape"'; |
|
122
|
|
|
$keyHandler = new KeyHandler([], false, $when); |
|
123
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertArrayHasKey('when', $result); |
|
126
|
|
|
$this->assertSame($when, $result['when']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testJsonSerializeStructure(): void |
|
130
|
|
|
{ |
|
131
|
|
|
$commands = [$this->createMock(AbstractStandardCommand::class)]; |
|
132
|
|
|
$keyHandler = new KeyHandler($commands, true, 'condition'); |
|
133
|
|
|
$result = $keyHandler->jsonSerialize(); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertIsArray($result); |
|
136
|
|
|
$this->assertArrayHasKey('commands', $result); |
|
137
|
|
|
$this->assertArrayHasKey('propagate', $result); |
|
138
|
|
|
$this->assertArrayHasKey('when', $result); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testImplementsJsonSerializable(): void |
|
142
|
|
|
{ |
|
143
|
|
|
$keyHandler = new KeyHandler(); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertInstanceOf(\JsonSerializable::class, $keyHandler); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|