1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Infrastructure |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Micrometa\Tests\Ports; |
38
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Application\Item\PropertyList; |
40
|
|
|
use Jkphl\Micrometa\Application\Value\StringValue; |
41
|
|
|
use Jkphl\Micrometa\Infrastructure\Factory\MicroformatsFactory; |
42
|
|
|
use Jkphl\Micrometa\Ports\Item\Item; |
43
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemInterface; |
44
|
|
|
use Jkphl\Micrometa\Ports\Item\ItemList; |
45
|
|
|
use Jkphl\Micrometa\Tests\AbstractTestBase; |
46
|
|
|
use Jkphl\Micrometa\Tests\MicroformatsFeedTrait; |
47
|
|
|
use Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Parser factory tests |
51
|
|
|
* |
52
|
|
|
* @package Jkphl\Micrometa |
53
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
54
|
|
|
*/ |
55
|
|
|
class ItemTest extends AbstractTestBase |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* Use the Microformats feed method |
59
|
|
|
*/ |
60
|
|
|
use MicroformatsFeedTrait; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test an item |
64
|
|
|
*/ |
65
|
|
|
public function testItemTypes() |
66
|
|
|
{ |
67
|
|
|
$feedItem = $this->getFeedItem(); |
68
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
69
|
|
|
|
70
|
|
|
// Test the item type |
71
|
|
|
$this->assertTrue($feedItem->isOfType('h-feed')); |
72
|
|
|
$this->assertTrue($feedItem->isOfType('h-feed', MicroformatsFactory::MF2_PROFILE_URI)); |
73
|
|
|
$this->assertFalse($feedItem->isOfType('invalid', MicroformatsFactory::MF2_PROFILE_URI)); |
74
|
|
|
|
75
|
|
|
// Test other item properties |
76
|
|
|
$this->assertEquals('feed-id', $feedItem->getId()); |
77
|
|
|
$this->assertEquals('feed-language', $feedItem->getLanguage()); |
78
|
|
|
$this->assertEquals('feed-value', $feedItem->getValue()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Test the item properties |
83
|
|
|
*/ |
84
|
|
|
public function testItemProperties() |
85
|
|
|
{ |
86
|
|
|
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException'); |
87
|
|
|
$this->expectExceptionCode('1491672553'); |
88
|
|
|
$feedItem = $this->getFeedItem(); |
89
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
90
|
|
|
|
91
|
|
|
$properties = $feedItem->getProperties(); |
92
|
|
|
$this->assertInstanceOf(PropertyList::class, $properties); |
93
|
|
|
$this->assertEquals(3, count($properties)); |
94
|
|
|
|
95
|
|
|
// Get an unknown property |
96
|
|
|
$feedItem->getProperty('name', null, 2); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Test the item export |
101
|
|
|
*/ |
102
|
|
|
public function testItemExport() |
103
|
|
|
{ |
104
|
|
|
$feedItem = $this->getFeedItem(); |
105
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
106
|
|
|
|
107
|
|
|
$export = $feedItem->toObject(); |
108
|
|
|
$this->assertInstanceOf(\stdClass::class, $export); |
109
|
|
|
foreach (['format', 'types', 'properties', 'items'] as $property) { |
110
|
|
|
$this->assertTrue(isset($export->$property)); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Test an unprofiled property |
116
|
|
|
*/ |
117
|
|
|
public function testItemUnprofiledProperty() |
118
|
|
|
{ |
119
|
|
|
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException'); |
120
|
|
|
$this->expectExceptionCode('1488315604'); |
121
|
|
|
$feedItem = $this->getFeedItem(); |
122
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
123
|
|
|
|
124
|
|
|
// Test the item name as an unprofiled property value list |
125
|
|
|
$feedNameList = $feedItem->getProperty('name'); |
126
|
|
|
$this->assertTrue(is_array($feedNameList)); |
127
|
|
|
$this->assertEquals(1, count($feedNameList)); |
128
|
|
|
$this->assertInstanceOf(StringValue::class, $feedNameList[0]); |
129
|
|
|
$this->assertEquals('John Doe\'s Blog', strval($feedNameList[0])); |
130
|
|
|
|
131
|
|
|
// Test the item name as an unprofiled single property value |
132
|
|
|
$feedName = $feedItem->getProperty('name', null, 0); |
133
|
|
|
$this->assertInstanceOf(StringValue::class, $feedName); |
134
|
|
|
$this->assertEquals('John Doe\'s Blog', strval($feedName)); |
135
|
|
|
|
136
|
|
|
// Test an invalid unprofiled property |
137
|
|
|
$feedItem->getProperty('invalid'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Test a profiled property |
142
|
|
|
*/ |
143
|
|
|
public function testItemProfiledProperty() |
144
|
|
|
{ |
145
|
|
|
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException'); |
146
|
|
|
$this->expectExceptionCode('1488315604'); |
147
|
|
|
$feedItem = $this->getFeedItem(); |
148
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
149
|
|
|
|
150
|
|
|
// Test the item name as an unprofiled property value list |
151
|
|
|
$feedNameList = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI); |
152
|
|
|
$this->assertTrue(is_array($feedNameList)); |
153
|
|
|
$this->assertEquals(1, count($feedNameList)); |
154
|
|
|
$this->assertInstanceOf(StringValue::class, $feedNameList[0]); |
155
|
|
|
$this->assertEquals('John Doe\'s Blog', strval($feedNameList[0])); |
156
|
|
|
|
157
|
|
|
// Test the item name as an unprofiled single property value |
158
|
|
|
$feedName = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0); |
159
|
|
|
$this->assertInstanceOf(StringValue::class, $feedName); |
160
|
|
|
$this->assertEquals('John Doe\'s Blog', strval($feedName)); |
161
|
|
|
|
162
|
|
|
// Test an invalid unprofiled property |
163
|
|
|
$feedItem->getProperty('invalid', MicroformatsFactory::MF2_PROFILE_URI); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Test an unprofiled property |
168
|
|
|
*/ |
169
|
|
|
public function testItemAliasedProperty() |
170
|
|
|
{ |
171
|
|
|
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException'); |
172
|
|
|
$this->expectExceptionCode('1488315604'); |
173
|
|
|
$feedItem = $this->getFeedItem(); |
174
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
175
|
|
|
|
176
|
|
|
// Run the custom item property tests |
177
|
|
|
$this->runCustomItemPropertyTests($feedItem); |
178
|
|
|
|
179
|
|
|
// Test an invalid property |
180
|
|
|
$feedItem->invalidProperty; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Run the custom item property tests |
185
|
|
|
* |
186
|
|
|
* @param Item $feedItem Feed item |
187
|
|
|
*/ |
188
|
|
|
protected function runCustomItemPropertyTests(Item $feedItem) |
189
|
|
|
{ |
190
|
|
|
// Test the custom item property as an unprofiled property value list |
191
|
|
|
$feedCustomPropList = $feedItem->getProperty('custom-property'); |
192
|
|
|
$this->assertTrue(is_array($feedCustomPropList)); |
193
|
|
|
$this->assertEquals(1, count($feedCustomPropList)); |
194
|
|
|
$this->assertInstanceOf(StringValue::class, $feedCustomPropList[0]); |
195
|
|
|
$this->assertEquals('Property for alias testing', strval($feedCustomPropList[0])); |
196
|
|
|
|
197
|
|
|
// Test the custom item property as an unprofiled single property value |
198
|
|
|
$feedCustomProp = $feedItem->getProperty('custom-property', null, 0); |
199
|
|
|
$this->assertInstanceOf(StringValue::class, $feedCustomProp); |
200
|
|
|
$this->assertEquals('Property for alias testing', strval($feedCustomProp)); |
201
|
|
|
|
202
|
|
|
// Test the custom item property via the convenience getter |
203
|
|
|
$feedCustomProp = $feedItem->customProperty; |
204
|
|
|
$this->assertInstanceOf(StringValue::class, $feedCustomProp); |
205
|
|
|
$this->assertEquals('Property for alias testing', strval($feedCustomProp)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Test a property stack |
210
|
|
|
*/ |
211
|
|
|
public function testItemPropertyStack() |
212
|
|
|
{ |
213
|
|
|
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException'); |
214
|
|
|
$this->expectExceptionCode('1488315604'); |
215
|
|
|
$feedItem = $this->getFeedItem(); |
216
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
217
|
|
|
|
218
|
|
|
// Request a valid property stack |
219
|
|
|
$propertyValues = $feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'name'); |
220
|
|
|
$this->assertEquals(['John Doe\'s Blog'], $propertyValues); |
221
|
|
|
|
222
|
|
|
// Request unknown properties only |
223
|
|
|
$feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'invalid'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Test a property item |
228
|
|
|
*/ |
229
|
|
|
public function testItemPropertyItem() |
230
|
|
|
{ |
231
|
|
|
$feedItem = $this->getFeedItem(); |
232
|
|
|
$this->assertInstanceOf(Item::class, $feedItem); |
233
|
|
|
|
234
|
|
|
// Request a valid property stack |
235
|
|
|
/** @var ItemInterface[] $authors */ |
236
|
|
|
$authors = $feedItem->getFirstProperty('author'); |
237
|
|
|
$this->assertTrue(is_array($authors)); |
238
|
|
|
$this->assertInstanceOf(ItemInterface::class, $authors[0]); |
239
|
|
|
|
240
|
|
|
// Test the author name as an unprofiled single property value |
241
|
|
|
$authorName = $authors[0]->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0); |
242
|
|
|
$this->assertInstanceOf(StringValue::class, $authorName); |
243
|
|
|
$this->assertEquals('John Doe', strval($authorName)); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Test nested items |
248
|
|
|
*/ |
249
|
|
|
public function testItemNestedItems() |
250
|
|
|
{ |
251
|
|
|
$feedItem = $this->getFeedItem(); |
252
|
|
|
self::assertInstanceOf(Item::class, $feedItem); |
253
|
|
|
|
254
|
|
|
// Test the number of nested items |
255
|
|
|
$this->assertEquals(2, count($feedItem)); |
256
|
|
|
$this->assertEquals(2, count($feedItem->getItems())); |
257
|
|
|
foreach ($feedItem as $itemIndex => $entryItem) { |
|
|
|
|
258
|
|
|
$this->assertInstanceOf(ItemInterface::class, $entryItem); |
259
|
|
|
$this->assertTrue(is_int($itemIndex)); |
260
|
|
|
} |
261
|
|
|
$this->assertInstanceOf(ItemInterface::class, $feedItem->getFirstItem('h-entry')); |
262
|
|
|
$this->assertInstanceOf( |
263
|
|
|
ItemInterface::class, |
264
|
|
|
$feedItem->getFirstItem('h-entry', MicroformatsFactory::MF2_PROFILE_URI) |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
// Test the second entry item |
268
|
|
|
$entryItem = $feedItem->getItems('h-entry')[1]; |
269
|
|
|
self::assertInstanceOf(ItemInterface::class, $entryItem); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Test non-existent nested item |
274
|
|
|
*/ |
275
|
|
|
public function testNonExistentNestedItems() |
276
|
|
|
{ |
277
|
|
|
$this->expectException(OutOfBoundsException::class); |
278
|
|
|
$this->expectExceptionCode('1492418999'); |
279
|
|
|
|
280
|
|
|
$feedItem = $this->getFeedItem(); |
281
|
|
|
|
282
|
|
|
$this->assertEquals('John Doe', $feedItem->hEntry()->author->name); |
283
|
|
|
$feedItem->hEntry(2); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Test the item list export |
288
|
|
|
*/ |
289
|
|
|
public function testItemListExport() |
290
|
|
|
{ |
291
|
|
|
$this->expectException(OutOfBoundsException::class); |
292
|
|
|
$this->expectExceptionCode('1492030227'); |
293
|
|
|
|
294
|
|
|
$feedItem = $this->getFeedItem(); |
295
|
|
|
$itemList = new ItemList([$feedItem]); |
296
|
|
|
$this->assertInstanceOf(ItemList::class, $itemList); |
297
|
|
|
|
298
|
|
|
$export = $itemList->toObject(); |
299
|
|
|
$this->assertInstanceOf(\stdClass::class, $export); |
300
|
|
|
$this->assertTrue(isset($export->items)); |
301
|
|
|
$this->assertTrue(is_array($export->items)); |
302
|
|
|
$this->assertEquals($feedItem->toObject(), current($export->items)); |
303
|
|
|
|
304
|
|
|
$itemList->getFirstItem('invalid'); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Test the item list immutability |
309
|
|
|
*/ |
310
|
|
|
public function testItemListImmutabilitySet() |
311
|
|
|
{ |
312
|
|
|
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\RuntimeException'); |
313
|
|
|
$this->expectExceptionCode('1495988721'); |
314
|
|
|
$feedItem = $this->getFeedItem(); |
315
|
|
|
$itemList = new ItemList([$feedItem]); |
316
|
|
|
$this->assertInstanceOf(ItemList::class, $itemList); |
317
|
|
|
$this->assertEquals($feedItem, $itemList[0]); |
318
|
|
|
$itemList[1] = $feedItem; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Test the item list immutability |
323
|
|
|
*/ |
324
|
|
|
public function testItemListImmutabilityUnset() |
325
|
|
|
{ |
326
|
|
|
$this->expectException('Jkphl\Micrometa\Ports\Exceptions\RuntimeException'); |
327
|
|
|
$this->expectExceptionCode('1495988721'); |
328
|
|
|
$feedItem = $this->getFeedItem(); |
329
|
|
|
$itemList = new ItemList([$feedItem]); |
330
|
|
|
$this->assertInstanceOf(ItemList::class, $itemList); |
331
|
|
|
$this->assertEquals($feedItem, $itemList[0]); |
332
|
|
|
unset($itemList[0]); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|