forked from zendframework/zend-feed
/
EntryTest.php
419 lines (371 loc) · 12.7 KB
/
EntryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Feed\Writer\Extension\ITunes;
use PHPUnit\Framework\TestCase;
use Zend\Feed\Writer;
use Zend\Feed\Writer\Exception\ExceptionInterface;
/**
* @group Zend_Feed
* @group Zend_Feed_Writer
*/
class EntryTest extends TestCase
{
public function testSetBlock()
{
$entry = new Writer\Entry;
$entry->setItunesBlock('yes');
$this->assertEquals('yes', $entry->getItunesBlock());
}
public function testSetBlockThrowsExceptionOnNonAlphaValue()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesBlock('123');
}
public function testSetBlockThrowsExceptionIfValueGreaterThan255CharsLength()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesBlock(str_repeat('a', 256));
}
public function testAddAuthors()
{
$entry = new Writer\Entry;
$entry->addItunesAuthors(['joe', 'jane']);
$this->assertEquals(['joe', 'jane'], $entry->getItunesAuthors());
}
public function testAddAuthor()
{
$entry = new Writer\Entry;
$entry->addItunesAuthor('joe');
$this->assertEquals(['joe'], $entry->getItunesAuthors());
}
public function testAddAuthorThrowsExceptionIfValueGreaterThan255CharsLength()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->addItunesAuthor(str_repeat('a', 256));
}
public function testSetDurationAsSeconds()
{
$entry = new Writer\Entry;
$entry->setItunesDuration(23);
$this->assertEquals(23, $entry->getItunesDuration());
}
public function testSetDurationAsMinutesAndSeconds()
{
$entry = new Writer\Entry;
$entry->setItunesDuration('23:23');
$this->assertEquals('23:23', $entry->getItunesDuration());
}
public function testSetDurationAsHoursMinutesAndSeconds()
{
$entry = new Writer\Entry;
$entry->setItunesDuration('23:23:23');
$this->assertEquals('23:23:23', $entry->getItunesDuration());
}
public function testSetDurationThrowsExceptionOnUnknownFormat()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesDuration('abc');
}
public function testSetDurationThrowsExceptionOnInvalidSeconds()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesDuration('23:456');
}
public function testSetDurationThrowsExceptionOnInvalidMinutes()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesDuration('23:234:45');
}
public function testSetExplicitToYes()
{
$entry = new Writer\Entry;
$entry->setItunesExplicit('yes');
$this->assertEquals('yes', $entry->getItunesExplicit());
}
public function testSetExplicitToNo()
{
$entry = new Writer\Entry;
$entry->setItunesExplicit('no');
$this->assertEquals('no', $entry->getItunesExplicit());
}
public function testSetExplicitToClean()
{
$entry = new Writer\Entry;
$entry->setItunesExplicit('clean');
$this->assertEquals('clean', $entry->getItunesExplicit());
}
public function testSetExplicitThrowsExceptionOnUnknownTerm()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesExplicit('abc');
}
public function testSetKeywords()
{
$entry = new Writer\Entry;
$words = [
'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12'
];
set_error_handler(function ($errno, $errstr) {
return (bool) preg_match('/itunes:keywords/', $errstr);
}, \E_USER_DEPRECATED);
$entry->setItunesKeywords($words);
restore_error_handler();
$this->assertEquals($words, $entry->getItunesKeywords());
}
public function testSetKeywordsThrowsExceptionIfMaxKeywordsExceeded()
{
$entry = new Writer\Entry;
$words = [
'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12', 'a13'
];
set_error_handler(function ($errno, $errstr) {
return (bool) preg_match('/itunes:keywords/', $errstr);
}, \E_USER_DEPRECATED);
try {
$entry->setItunesKeywords($words);
$this->fail('Expected exception when setting more keywords than allowed');
} catch (ExceptionInterface $e) {
} finally {
restore_error_handler();
}
}
public function testSetKeywordsThrowsExceptionIfFormattedKeywordsExceeds255CharLength()
{
$entry = new Writer\Entry;
$words = [
str_repeat('a', 253), str_repeat('b', 2)
];
set_error_handler(function ($errno, $errstr) {
return (bool) preg_match('/itunes:keywords/', $errstr);
}, \E_USER_DEPRECATED);
try {
$entry->setItunesKeywords($words);
$this->fail('Expected exception when setting keywords exceeding character length');
} catch (ExceptionInterface $e) {
} finally {
restore_error_handler();
}
}
public function testSetTitle()
{
$entry = new Writer\Entry;
$entry->setItunesTitle('abc');
$this->assertEquals('abc', $entry->getItunesTitle());
}
public function testSetTitleThrowsExceptionWhenValueExceeds255Chars()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesTitle(str_repeat('a', 256));
}
public function testSetSubtitle()
{
$entry = new Writer\Entry;
$entry->setItunesSubtitle('abc');
$this->assertEquals('abc', $entry->getItunesSubtitle());
}
public function testSetSubtitleThrowsExceptionWhenValueExceeds255Chars()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesSubtitle(str_repeat('a', 256));
}
public function testSetSummary()
{
$entry = new Writer\Entry;
$entry->setItunesSummary('abc');
$this->assertEquals('abc', $entry->getItunesSummary());
}
public function testSetSummaryThrowsExceptionWhenValueExceeds255Chars()
{
$this->expectException(ExceptionInterface::class);
$entry = new Writer\Entry;
$entry->setItunesSummary(str_repeat('a', 4001));
}
public function invalidImageUrls()
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.1],
'string' => ['scheme:/host.path'],
'invalid-extension-gif' => ['https://example.com/image.gif', 'file extension'],
'invalid-extension-uc' => ['https://example.com/image.PNG', 'file extension'],
'array' => [['https://example.com/image.png']],
'object' => [(object) ['image' => 'https://example.com/image.png']],
];
}
/**
* @dataProvider invalidImageUrls
* @param mixed $url
* @param string $expectedMessage
*/
public function testSetItunesImageRaisesExceptionForInvalidUrl($url, $expectedMessage = 'valid URI')
{
$entry = new Writer\Entry();
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage($expectedMessage);
$entry->setItunesImage($url);
}
public function validImageUrls()
{
return [
'jpg' => ['https://example.com/image.jpg'],
'png' => ['https://example.com/image.png'],
];
}
/**
* @dataProvider validImageUrls
* @param string $url
*/
public function testSetItunesImageSetsInternalDataWithValidUrl($url)
{
$entry = new Writer\Entry();
$entry->setItunesImage($url);
$this->assertEquals($url, $entry->getItunesImage());
}
public function nonNumericEpisodeNumbers()
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'zero-float' => [0.000],
'float' => [1.1],
'string' => ['not-a-number'],
'array' => [[1]],
'object' => [(object) ['number' => 1]],
];
}
/**
* @dataProvider nonNumericEpisodeNumbers
* @param mixed $number
*/
public function testSetEpisodeRaisesExceptionForNonNumericEpisodeNumbers($number)
{
$entry = new Writer\Entry();
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage('may only be an integer');
$entry->setItunesEpisode($number);
}
public function testSetEpisodeSetsNumberInEntry()
{
$entry = new Writer\Entry();
$entry->setItunesEpisode(42);
$this->assertEquals(42, $entry->getItunesEpisode());
}
public function invalidEpisodeTypes()
{
return [
'null' => [null],
'true' => [true],
'false' => [false],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.1],
'string' => ['not-a-type'],
'array' => [['full']],
'object' => [(object) ['type' => 'full']],
];
}
/**
* @dataProvider invalidEpisodeTypes
* @param mixed $type
*/
public function testSetEpisodeTypeRaisesExceptionForInvalidTypes($type)
{
$entry = new Writer\Entry();
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage('MUST be one of');
$entry->setItunesEpisodeType($type);
}
public function validEpisodeTypes()
{
return [
'full' => ['full'],
'trailer' => ['trailer'],
'bonus' => ['bonus'],
];
}
/**
* @dataProvider validEpisodeTypes
* @param string $type
*/
public function testEpisodeTypeMaybeMutatedWithAcceptedValues($type)
{
$entry = new Writer\Entry();
$entry->setItunesEpisodeType($type);
$this->assertEquals($type, $entry->getItunesEpisodeType());
}
public function invalidClosedCaptioningFlags()
{
return [
'null' => [null],
'zero' => [0],
'int' => [1],
'zero-float' => [0.0],
'float' => [1.1],
'string' => ['Yes'],
'array' => [['Yes']],
'object' => [(object) ['isClosedCaptioned' => 'Yes']],
];
}
/**
* @dataProvider invalidClosedCaptioningFlags
* @param mixed $status
*/
public function testSettingClosedCaptioningToNonBooleanRaisesException($status)
{
$entry = new Writer\Entry();
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage('MUST be a boolean');
$entry->setItunesIsClosedCaptioned($status);
}
public function testSettingClosedCaptioningToFalseDoesNothing()
{
$entry = new Writer\Entry();
$entry->setItunesIsClosedCaptioned(false);
$this->assertNull($entry->getItunesIsClosedCaptioned());
}
public function testSettingClosedCaptioningToTrueUpdatesContainer()
{
$entry = new Writer\Entry();
$entry->setItunesIsClosedCaptioned(true);
$this->assertTrue($entry->getItunesIsClosedCaptioned());
}
/**
* @dataProvider nonNumericEpisodeNumbers
* @param mixed $number
*/
public function testSetSeasonRaisesExceptionForNonNumericSeasonNumbers($number)
{
$entry = new Writer\Entry();
$this->expectException(ExceptionInterface::class);
$this->expectExceptionMessage('may only be an integer');
$entry->setItunesSeason($number);
}
public function testSetSeasonSetsNumberInEntry()
{
$entry = new Writer\Entry();
$entry->setItunesSeason(42);
$this->assertEquals(42, $entry->getItunesSeason());
}
}