|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MaxBeckers\AmazonAlexa\Test\Response\Directives\APL\Component; |
|
6
|
|
|
|
|
7
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\TextComponent; |
|
8
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType; |
|
9
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\FontStyle; |
|
10
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\FontWeight; |
|
11
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\TextAlign; |
|
12
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\TextAlignVertical; |
|
13
|
|
|
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
|
|
16
|
|
|
class TextComponentTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testConstructorWithAllParameters(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$color = '#ff0000'; |
|
21
|
|
|
$fontFamily = 'Arial'; |
|
22
|
|
|
$fontSize = '20dp'; |
|
23
|
|
|
$fontStyle = FontStyle::ITALIC; |
|
24
|
|
|
$fontWeight = FontWeight::BOLD; |
|
25
|
|
|
$lang = 'en-US'; |
|
26
|
|
|
$letterSpacing = '2px'; |
|
27
|
|
|
$lineHeight = '150%'; |
|
28
|
|
|
$maxLines = 5; |
|
29
|
|
|
$onTextLayout = [$this->createMock(AbstractStandardCommand::class)]; |
|
30
|
|
|
$text = 'Hello World'; |
|
31
|
|
|
$textAlign = TextAlign::CENTER; |
|
32
|
|
|
$textAlignVertical = TextAlignVertical::CENTER; |
|
33
|
|
|
|
|
34
|
|
|
$component = new TextComponent( |
|
35
|
|
|
$color, |
|
36
|
|
|
$fontFamily, |
|
37
|
|
|
$fontSize, |
|
38
|
|
|
$fontStyle, |
|
39
|
|
|
$fontWeight, |
|
40
|
|
|
$lang, |
|
41
|
|
|
$letterSpacing, |
|
42
|
|
|
$lineHeight, |
|
43
|
|
|
$maxLines, |
|
44
|
|
|
$onTextLayout, |
|
45
|
|
|
$text, |
|
46
|
|
|
$textAlign, |
|
47
|
|
|
$textAlignVertical |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame($color, $component->color); |
|
51
|
|
|
$this->assertSame($fontFamily, $component->fontFamily); |
|
52
|
|
|
$this->assertSame($fontSize, $component->fontSize); |
|
53
|
|
|
$this->assertSame($fontStyle, $component->fontStyle); |
|
54
|
|
|
$this->assertSame($fontWeight, $component->fontWeight); |
|
55
|
|
|
$this->assertSame($lang, $component->lang); |
|
56
|
|
|
$this->assertSame($letterSpacing, $component->letterSpacing); |
|
57
|
|
|
$this->assertSame($lineHeight, $component->lineHeight); |
|
58
|
|
|
$this->assertSame($maxLines, $component->maxLines); |
|
59
|
|
|
$this->assertSame($onTextLayout, $component->onTextLayout); |
|
60
|
|
|
$this->assertSame($text, $component->text); |
|
61
|
|
|
$this->assertSame($textAlign, $component->textAlign); |
|
62
|
|
|
$this->assertSame($textAlignVertical, $component->textAlignVertical); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testConstructorWithDefaultParameters(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$component = new TextComponent(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertNull($component->color); |
|
70
|
|
|
$this->assertSame('sans-serif', $component->fontFamily); |
|
71
|
|
|
$this->assertSame('40dp', $component->fontSize); |
|
72
|
|
|
$this->assertNull($component->fontStyle); |
|
73
|
|
|
$this->assertNull($component->fontWeight); |
|
74
|
|
|
$this->assertSame('', $component->lang); |
|
75
|
|
|
$this->assertSame('0', $component->letterSpacing); |
|
76
|
|
|
$this->assertSame('125%', $component->lineHeight); |
|
77
|
|
|
$this->assertSame(0, $component->maxLines); |
|
78
|
|
|
$this->assertNull($component->onTextLayout); |
|
79
|
|
|
$this->assertSame('', $component->text); |
|
80
|
|
|
$this->assertNull($component->textAlign); |
|
81
|
|
|
$this->assertNull($component->textAlignVertical); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testJsonSerializeWithAllProperties(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$color = '#blue'; |
|
87
|
|
|
$fontStyle = FontStyle::NORMAL; |
|
88
|
|
|
$fontWeight = FontWeight::BOLD; |
|
89
|
|
|
$onTextLayout = [ |
|
90
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
91
|
|
|
$this->createMock(AbstractStandardCommand::class), |
|
92
|
|
|
]; |
|
93
|
|
|
$textAlign = TextAlign::LEFT; |
|
94
|
|
|
$textAlignVertical = TextAlignVertical::TOP; |
|
95
|
|
|
|
|
96
|
|
|
$component = new TextComponent( |
|
97
|
|
|
color: $color, |
|
98
|
|
|
fontFamily: 'Helvetica', |
|
99
|
|
|
fontSize: '16dp', |
|
100
|
|
|
fontStyle: $fontStyle, |
|
101
|
|
|
fontWeight: $fontWeight, |
|
102
|
|
|
lang: 'fr-FR', |
|
103
|
|
|
letterSpacing: '1px', |
|
104
|
|
|
lineHeight: '140%', |
|
105
|
|
|
maxLines: 3, |
|
106
|
|
|
onTextLayout: $onTextLayout, |
|
107
|
|
|
text: 'Bonjour le monde', |
|
108
|
|
|
textAlign: $textAlign, |
|
109
|
|
|
textAlignVertical: $textAlignVertical |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
$result = $component->jsonSerialize(); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertSame(APLComponentType::TEXT->value, $result['type']); |
|
115
|
|
|
$this->assertSame($color, $result['color']); |
|
116
|
|
|
$this->assertSame('Helvetica', $result['fontFamily']); |
|
117
|
|
|
$this->assertSame('16dp', $result['fontSize']); |
|
118
|
|
|
$this->assertSame($fontStyle->value, $result['fontStyle']); |
|
119
|
|
|
$this->assertSame($fontWeight->value, $result['fontWeight']); |
|
120
|
|
|
$this->assertSame('fr-FR', $result['lang']); |
|
121
|
|
|
$this->assertSame('1px', $result['letterSpacing']); |
|
122
|
|
|
$this->assertSame('140%', $result['lineHeight']); |
|
123
|
|
|
$this->assertSame(3, $result['maxLines']); |
|
124
|
|
|
$this->assertSame($onTextLayout, $result['onTextLayout']); |
|
125
|
|
|
$this->assertSame('Bonjour le monde', $result['text']); |
|
126
|
|
|
$this->assertSame($textAlign->value, $result['textAlign']); |
|
127
|
|
|
$this->assertSame($textAlignVertical->value, $result['textAlignVertical']); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testJsonSerializeWithDefaultValues(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$component = new TextComponent(); |
|
133
|
|
|
$result = $component->jsonSerialize(); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertSame(APLComponentType::TEXT->value, $result['type']); |
|
136
|
|
|
$this->assertArrayNotHasKey('fontFamily', $result); |
|
137
|
|
|
$this->assertArrayNotHasKey('fontSize', $result); |
|
138
|
|
|
$this->assertArrayNotHasKey('lang', $result); |
|
139
|
|
|
$this->assertArrayNotHasKey('letterSpacing', $result); |
|
140
|
|
|
$this->assertArrayNotHasKey('lineHeight', $result); |
|
141
|
|
|
$this->assertArrayNotHasKey('maxLines', $result); |
|
142
|
|
|
$this->assertArrayNotHasKey('text', $result); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testJsonSerializeWithNonDefaultValues(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$component = new TextComponent( |
|
148
|
|
|
fontFamily: 'Times New Roman', |
|
149
|
|
|
fontSize: '24dp', |
|
150
|
|
|
lang: 'de-DE', |
|
151
|
|
|
letterSpacing: '3px', |
|
152
|
|
|
lineHeight: '160%', |
|
153
|
|
|
maxLines: 10, |
|
154
|
|
|
text: 'Hallo Welt' |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
$result = $component->jsonSerialize(); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertSame('Times New Roman', $result['fontFamily']); |
|
160
|
|
|
$this->assertSame('24dp', $result['fontSize']); |
|
161
|
|
|
$this->assertSame('de-DE', $result['lang']); |
|
162
|
|
|
$this->assertSame('3px', $result['letterSpacing']); |
|
163
|
|
|
$this->assertSame('160%', $result['lineHeight']); |
|
164
|
|
|
$this->assertSame(10, $result['maxLines']); |
|
165
|
|
|
$this->assertSame('Hallo Welt', $result['text']); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function testJsonSerializeWithEmptyOnTextLayout(): void |
|
169
|
|
|
{ |
|
170
|
|
|
$component = new TextComponent(onTextLayout: []); |
|
171
|
|
|
$result = $component->jsonSerialize(); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertArrayNotHasKey('onTextLayout', $result); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function testJsonSerializeWithNullValues(): void |
|
177
|
|
|
{ |
|
178
|
|
|
$component = new TextComponent(); |
|
179
|
|
|
$result = $component->jsonSerialize(); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertArrayNotHasKey('color', $result); |
|
182
|
|
|
$this->assertArrayNotHasKey('fontStyle', $result); |
|
183
|
|
|
$this->assertArrayNotHasKey('fontWeight', $result); |
|
184
|
|
|
$this->assertArrayNotHasKey('onTextLayout', $result); |
|
185
|
|
|
$this->assertArrayNotHasKey('textAlign', $result); |
|
186
|
|
|
$this->assertArrayNotHasKey('textAlignVertical', $result); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function testJsonSerializeWithDifferentFontStyles(): void |
|
190
|
|
|
{ |
|
191
|
|
|
$fontStyles = [FontStyle::NORMAL, FontStyle::ITALIC]; |
|
192
|
|
|
|
|
193
|
|
|
foreach ($fontStyles as $fontStyle) { |
|
194
|
|
|
$component = new TextComponent(fontStyle: $fontStyle); |
|
195
|
|
|
$result = $component->jsonSerialize(); |
|
196
|
|
|
|
|
197
|
|
|
$this->assertSame($fontStyle->value, $result['fontStyle']); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function testJsonSerializeWithDifferentFontWeights(): void |
|
202
|
|
|
{ |
|
203
|
|
|
$fontWeights = [FontWeight::W100, FontWeight::NORMAL, FontWeight::BOLD]; |
|
204
|
|
|
|
|
205
|
|
|
foreach ($fontWeights as $fontWeight) { |
|
206
|
|
|
$component = new TextComponent(fontWeight: $fontWeight); |
|
207
|
|
|
$result = $component->jsonSerialize(); |
|
208
|
|
|
|
|
209
|
|
|
$this->assertSame($fontWeight->value, $result['fontWeight']); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function testJsonSerializeWithDifferentTextAlignments(): void |
|
214
|
|
|
{ |
|
215
|
|
|
$alignments = [TextAlign::LEFT, TextAlign::CENTER, TextAlign::RIGHT]; |
|
216
|
|
|
|
|
217
|
|
|
foreach ($alignments as $align) { |
|
218
|
|
|
$component = new TextComponent(textAlign: $align); |
|
219
|
|
|
$result = $component->jsonSerialize(); |
|
220
|
|
|
|
|
221
|
|
|
$this->assertSame($align->value, $result['textAlign']); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function testJsonSerializeWithDifferentVerticalAlignments(): void |
|
226
|
|
|
{ |
|
227
|
|
|
$alignments = [TextAlignVertical::TOP, TextAlignVertical::CENTER, TextAlignVertical::BOTTOM]; |
|
228
|
|
|
|
|
229
|
|
|
foreach ($alignments as $align) { |
|
230
|
|
|
$component = new TextComponent(textAlignVertical: $align); |
|
231
|
|
|
$result = $component->jsonSerialize(); |
|
232
|
|
|
|
|
233
|
|
|
$this->assertSame($align->value, $result['textAlignVertical']); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function testTypeConstant(): void |
|
238
|
|
|
{ |
|
239
|
|
|
$this->assertSame(APLComponentType::TEXT, TextComponent::TYPE); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function testExtendsAPLBaseComponent(): void |
|
243
|
|
|
{ |
|
244
|
|
|
$component = new TextComponent(); |
|
245
|
|
|
|
|
246
|
|
|
$this->assertInstanceOf(\MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\APLBaseComponent::class, $component); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function testImplementsJsonSerializable(): void |
|
250
|
|
|
{ |
|
251
|
|
|
$component = new TextComponent(); |
|
252
|
|
|
|
|
253
|
|
|
$this->assertInstanceOf(\JsonSerializable::class, $component); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|