|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MaxBeckers\AmazonAlexa\Test\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use MaxBeckers\AmazonAlexa\Exception\InvalidSsmlException; |
|
8
|
|
|
use MaxBeckers\AmazonAlexa\Helper\SsmlGenerator; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
|
|
11
|
|
|
class SsmlGeneratorTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
public static function getVoices(): array |
|
14
|
|
|
{ |
|
15
|
|
|
return array_map(function (string $item): array { |
|
16
|
|
|
return [$item]; |
|
17
|
|
|
}, SsmlGenerator::VOICES); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testClear(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
23
|
|
|
$this->assertSame('<speak></speak>', $ssmlGenerator->getSsml()); |
|
24
|
|
|
$ssmlGenerator->say('Just a Test.'); |
|
25
|
|
|
$this->assertSame('<speak>Just a Test.</speak>', $ssmlGenerator->getSsml()); |
|
26
|
|
|
$ssmlGenerator->clear(); |
|
27
|
|
|
$this->assertSame('<speak></speak>', $ssmlGenerator->getSsml()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testSay(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
33
|
|
|
$this->assertSame('<speak></speak>', $ssmlGenerator->getSsml()); |
|
34
|
|
|
$ssmlGenerator->say('Just a Test.'); |
|
35
|
|
|
$this->assertSame('<speak>Just a Test.</speak>', $ssmlGenerator->getSsml()); |
|
36
|
|
|
$ssmlGenerator->say('And add more text.'); |
|
37
|
|
|
$this->assertSame('<speak>Just a Test. And add more text.</speak>', $ssmlGenerator->getSsml()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testSaySpecialChars(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
43
|
|
|
$ssmlGenerator->escapeSpecialChars = true; |
|
44
|
|
|
$ssmlGenerator->say('This & that.'); |
|
45
|
|
|
$this->assertSame('<speak>This & that.</speak>', $ssmlGenerator->getSsml()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testPlayMp3InvalidUrl(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
51
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
52
|
|
|
$ssmlGenerator->playMp3('http://invalid.mp3'); |
|
53
|
|
|
$ssmlGenerator->playMp3('https://my.file/invalidmp3'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testPlayMp3(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
59
|
|
|
$ssmlGenerator->playMp3('https://valid.mp3?start=3'); |
|
60
|
|
|
$this->assertSame('<speak><audio src="https://valid.mp3?start=3" /></speak>', $ssmlGenerator->getSsml()); |
|
61
|
|
|
$ssmlGenerator->playMp3('https://valid.mp3'); |
|
62
|
|
|
$this->assertSame('<speak><audio src="https://valid.mp3?start=3" /> <audio src="https://valid.mp3" /></speak>', $ssmlGenerator->getSsml()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testPlayMp3Soundbank(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
68
|
|
|
$ssmlGenerator->playMp3('soundbank://valid.item'); |
|
69
|
|
|
$this->assertSame('<speak><audio src="soundbank://valid.item" /></speak>', $ssmlGenerator->getSsml()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testPauseStrengthInvalid(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
75
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
76
|
|
|
$ssmlGenerator->pauseStrength('invalid'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testPauseStrength(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
82
|
|
|
$ssmlGenerator->say('one'); |
|
83
|
|
|
$ssmlGenerator->pauseStrength(SsmlGenerator::BREAK_STRENGTH_MEDIUM); |
|
84
|
|
|
$ssmlGenerator->say('two'); |
|
85
|
|
|
$this->assertSame(sprintf('<speak>one <break strength="%s" /> two</speak>', SsmlGenerator::BREAK_STRENGTH_MEDIUM), $ssmlGenerator->getSsml()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testPauseTimeInvalid(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
91
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
92
|
|
|
$ssmlGenerator->pauseTime('300'); |
|
93
|
|
|
$ssmlGenerator->pauseTime('invalid'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testPauseTime(): void |
|
97
|
|
|
{ |
|
98
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
99
|
|
|
$ssmlGenerator->say('one'); |
|
100
|
|
|
$ssmlGenerator->pauseTime('50ms'); |
|
101
|
|
|
$ssmlGenerator->say('two'); |
|
102
|
|
|
$ssmlGenerator->pauseTime('1s'); |
|
103
|
|
|
$ssmlGenerator->say('three'); |
|
104
|
|
|
$this->assertSame('<speak>one <break time="50ms" /> two <break time="1s" /> three</speak>', $ssmlGenerator->getSsml()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function testSayWithAmazonEffectInvalid(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
110
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
111
|
|
|
$ssmlGenerator->sayWithAmazonEffect('Just a test.', 'invalid'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testSayWithAmazonEffect(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
117
|
|
|
$ssmlGenerator->sayWithAmazonEffect('Just a test.', SsmlGenerator::AMAZON_EFFECT_WHISPERED); |
|
118
|
|
|
$this->assertSame(sprintf('<speak><amazon:effect name="%s">Just a test.</amazon:effect></speak>', SsmlGenerator::AMAZON_EFFECT_WHISPERED), $ssmlGenerator->getSsml()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testWhisper(): void |
|
122
|
|
|
{ |
|
123
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
124
|
|
|
$ssmlGenerator->whisper('Just a test.'); |
|
125
|
|
|
$this->assertSame(sprintf('<speak><amazon:effect name="%s">Just a test.</amazon:effect></speak>', SsmlGenerator::AMAZON_EFFECT_WHISPERED), $ssmlGenerator->getSsml()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function testEmphasisInvalid(): void |
|
129
|
|
|
{ |
|
130
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
131
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
132
|
|
|
$ssmlGenerator->emphasis('Just a test.', 'invalid'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testEmphasis(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
138
|
|
|
$ssmlGenerator->emphasis('Just a test.', SsmlGenerator::EMPHASIS_LEVEL_STRONG); |
|
139
|
|
|
$this->assertSame(sprintf('<speak><emphasis level="%s">Just a test.</emphasis></speak>', SsmlGenerator::EMPHASIS_LEVEL_STRONG), $ssmlGenerator->getSsml()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testPronounceInLanguageInvalid(): void |
|
143
|
|
|
{ |
|
144
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
145
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
146
|
|
|
$ssmlGenerator->pronounceInLanguage('in-VA', 'invalid'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function testEPronounceInLanguage(): void |
|
150
|
|
|
{ |
|
151
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
152
|
|
|
$ssmlGenerator->pronounceInLanguage(SsmlGenerator::LANGUAGE_EN_US, 'Just a test.'); |
|
153
|
|
|
$this->assertSame(sprintf('<speak><lang xml:lang="%s">Just a test.</lang></speak>', SsmlGenerator::LANGUAGE_EN_US), $ssmlGenerator->getSsml()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testParagraph(): void |
|
157
|
|
|
{ |
|
158
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
159
|
|
|
$ssmlGenerator->paragraph('Just a test'); |
|
160
|
|
|
$this->assertSame('<speak><p>Just a test</p></speak>', $ssmlGenerator->getSsml()); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function testPhonemeInvalid(): void |
|
164
|
|
|
{ |
|
165
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
166
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
167
|
|
|
$ssmlGenerator->phoneme('invalid', 'invalid', 'Just a test'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testPhonemeIpa(): void |
|
171
|
|
|
{ |
|
172
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
173
|
|
|
$ssmlGenerator->phoneme(SsmlGenerator::PHONEME_ALPHABET_IPA, 'ˈbɑ.təl', 'bottle'); |
|
174
|
|
|
$this->assertSame(sprintf('<speak><phoneme alphabet="%s" ph="ˈbɑ.təl">bottle</phoneme></speak>', SsmlGenerator::PHONEME_ALPHABET_IPA), $ssmlGenerator->getSsml()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testPhonemeXSampa(): void |
|
178
|
|
|
{ |
|
179
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
180
|
|
|
$ssmlGenerator->phoneme(SsmlGenerator::PHONEME_ALPHABET_X_SAMPA, '"bA.t@l', 'bottle'); |
|
181
|
|
|
$this->assertSame(sprintf('<speak><phoneme alphabet="%s" ph=""bA.t@l">bottle</phoneme></speak>', SsmlGenerator::PHONEME_ALPHABET_X_SAMPA), $ssmlGenerator->getSsml()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testProsodyInvalid(): void |
|
185
|
|
|
{ |
|
186
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
187
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
188
|
|
|
$ssmlGenerator->prosody('invalid', 'invalid', 'Just a Test'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function testProsody(): void |
|
192
|
|
|
{ |
|
193
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
194
|
|
|
$ssmlGenerator->prosody(SsmlGenerator::PROSODY_PITCH, SsmlGenerator::PROSODY_PITCH_MEDIUM, 'Just a test.'); |
|
195
|
|
|
$this->assertSame(sprintf('<speak><prosody %s="%s">Just a test.</prosody></speak>', SsmlGenerator::PROSODY_PITCH, SsmlGenerator::PROSODY_PITCH_MEDIUM), $ssmlGenerator->getSsml()); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testSentence(): void |
|
199
|
|
|
{ |
|
200
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
201
|
|
|
$ssmlGenerator->sentence('Just a test'); |
|
202
|
|
|
$this->assertSame('<speak><s>Just a test</s></speak>', $ssmlGenerator->getSsml()); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function testSayAsInvalid(): void |
|
206
|
|
|
{ |
|
207
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
208
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
209
|
|
|
$ssmlGenerator->sayAs('invalid', 'Just a Test'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function testSayAs(): void |
|
213
|
|
|
{ |
|
214
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
215
|
|
|
$ssmlGenerator->sayAs(SsmlGenerator::SAY_AS_INTERPRET_CHARACTERS, 'Test'); |
|
216
|
|
|
$this->assertSame(sprintf('<speak><say-as interpret-as="%s">Test</say-as></speak>', SsmlGenerator::SAY_AS_INTERPRET_CHARACTERS), $ssmlGenerator->getSsml()); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function testSayAsDate(): void |
|
220
|
|
|
{ |
|
221
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
222
|
|
|
$ssmlGenerator->sayAs(SsmlGenerator::SAY_AS_INTERPRET_DATE, '01011970', 'dmy'); |
|
223
|
|
|
$this->assertSame(sprintf('<speak><say-as interpret-as="%s" format="%s">01011970</say-as></speak>', SsmlGenerator::SAY_AS_INTERPRET_DATE, 'dmy'), $ssmlGenerator->getSsml()); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function testAlias(): void |
|
227
|
|
|
{ |
|
228
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
229
|
|
|
$ssmlGenerator->alias('magnesium', 'Mg'); |
|
230
|
|
|
$this->assertSame('<speak><sub alias="magnesium">Mg</sub></speak>', $ssmlGenerator->getSsml()); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function testSayWithVoiceInvalid(): void |
|
234
|
|
|
{ |
|
235
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
236
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
237
|
|
|
$ssmlGenerator->sayWithVoice('invalid', 'Just a Test'); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @dataProvider getVoices |
|
242
|
|
|
*/ |
|
243
|
|
|
public function testSayWithVoice(string $voice): void |
|
244
|
|
|
{ |
|
245
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
246
|
|
|
$ssmlGenerator->sayWithVoice($voice, 'Just a test.'); |
|
247
|
|
|
$this->assertSame(sprintf('<speak><voice name="%s">Just a test.</voice></speak>', $voice), $ssmlGenerator->getSsml()); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function testWordInvalid(): void |
|
251
|
|
|
{ |
|
252
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
253
|
|
|
$this->expectException(InvalidSsmlException::class); |
|
254
|
|
|
$ssmlGenerator->word('invalid', 'Just a Test'); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function testWord(): void |
|
258
|
|
|
{ |
|
259
|
|
|
$ssmlGenerator = new SsmlGenerator(); |
|
260
|
|
|
$ssmlGenerator->word(SsmlGenerator::INTERPRET_WORD_VB, 'Just a test.'); |
|
261
|
|
|
$this->assertSame(sprintf('<speak><w role="%s">Just a test.</w></speak>', SsmlGenerator::INTERPRET_WORD_VB), $ssmlGenerator->getSsml()); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|