|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* micrometa |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Micrometa |
|
8
|
|
|
* @subpackage Jkphl\Micrometa\Tests\Domain |
|
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\Infrastructure; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Application\Contract\ParserInterface; |
|
40
|
|
|
use Jkphl\Micrometa\Application\Item\Item; |
|
41
|
|
|
use Jkphl\Micrometa\Application\Item\ItemInterface; |
|
42
|
|
|
use Jkphl\Micrometa\Application\Value\StringValue; |
|
43
|
|
|
use Jkphl\Micrometa\Domain\Item\Iri; |
|
44
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\JsonLD; |
|
45
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\LinkType; |
|
46
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\Microdata; |
|
47
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\Microformats; |
|
48
|
|
|
use Jkphl\Micrometa\Infrastructure\Parser\RdfaLite; |
|
49
|
|
|
use Jkphl\Micrometa\Tests\AbstractTestBase; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Parser tests |
|
53
|
|
|
* |
|
54
|
|
|
* @package Jkphl\Micrometa |
|
55
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
|
56
|
|
|
*/ |
|
57
|
|
|
class ParserTest extends AbstractTestBase |
|
58
|
|
|
{ |
|
59
|
|
|
/** |
|
60
|
|
|
* Test the JSON-LD parser with multiple languages |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testLanguageJsonLDParser() |
|
63
|
|
|
{ |
|
64
|
|
|
$items = $this->parseItems('json-ld/jsonld-languages.html', JsonLD::class); |
|
65
|
|
|
$this->assertTrue(is_array($items)); |
|
66
|
|
|
$this->assertEquals(1, count($items)); |
|
67
|
|
|
$this->assertInstanceOf(Item::class, $items[0]); |
|
68
|
|
|
$this->assertEquals(JsonLD::FORMAT, $items[0]->getFormat()); |
|
69
|
|
|
$this->assertEquals('http://example.com/id1', $items[0]->getId()); |
|
70
|
|
|
|
|
71
|
|
|
/** @var StringValue[] $propertyValues */ |
|
72
|
|
|
$propertyValues = $items[0]->getProperty('http://example.com/term6'); |
|
73
|
|
|
$this->assertTrue(is_array($propertyValues)); |
|
74
|
|
|
foreach ([null, null, 'en', 'de'] as $index => $language) { |
|
75
|
|
|
$this->assertInstanceOf(StringValue::class, $propertyValues[$index]); |
|
76
|
|
|
$this->assertEquals(strval($index + 1), strval($propertyValues[$index])); |
|
77
|
|
|
$this->assertEquals($language, $propertyValues[$index]->getLanguage()); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Parse items from fixture with a particular parser type |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $fixture Fixture |
|
85
|
|
|
* @param string $parser Parser class name |
|
86
|
|
|
* @param int $errorThreshold Error threshold |
|
87
|
|
|
* |
|
88
|
|
|
* @return ItemInterface[] Items |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function parseItems(string $fixture, string $parser, int $errorThreshold = 400) |
|
91
|
|
|
{ |
|
92
|
|
|
list($uri, $dom) = $this->getUriFixture($fixture); |
|
93
|
|
|
/** @var ParserInterface $parser */ |
|
94
|
|
|
$parser = new $parser($uri, self::getLogger($errorThreshold)); |
|
95
|
|
|
|
|
96
|
|
|
return $parser->parseDom($dom)->getItems(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Test the JSON-LD parser with multiple documents and file cache |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testMultipleJsonLDParser() |
|
103
|
|
|
{ |
|
104
|
|
|
$items = $this->parseItems('json-ld/jsonld-examples.html', JsonLD::class, 0); |
|
105
|
|
|
$this->assertTrue(is_array($items)); |
|
106
|
|
|
$this->assertEquals(5, count($items)); |
|
107
|
|
|
$this->assertInstanceOf(Item::class, $items[0]); |
|
108
|
|
|
$this->assertEquals(JsonLD::FORMAT, $items[0]->getFormat()); |
|
109
|
|
|
$this->assertEquals('https://jsonld-examples.com/#header_website', $items[0]->getId()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Test the JSON-LD parser with an invalid document |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testInvalidJsonLDParser() |
|
116
|
|
|
{ |
|
117
|
|
|
$items = $this->parseItems('json-ld/jsonld-invalid.html', JsonLD::class, 0); |
|
118
|
|
|
$this->assertTrue(is_array($items)); |
|
119
|
|
|
$this->assertEquals(0, count($items)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Test the JSON-LD parser with an invalid document |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testFixOnSemicolonForJsonLDParser() |
|
126
|
|
|
{ |
|
127
|
|
|
$items = $this->parseItems('json-ld/jsonld-ending-semicolon.html', JsonLD::class, 0); |
|
128
|
|
|
$this->assertTrue(is_array($items)); |
|
129
|
|
|
$this->assertEquals(1, count($items)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Test the Microformats parser |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testMicroformatsParser() |
|
136
|
|
|
{ |
|
137
|
|
|
$items = $this->parseItems('microformats/entry.html', Microformats::class); |
|
138
|
|
|
$this->assertTrue(is_array($items)); |
|
139
|
|
|
$this->assertEquals(1, count($items)); |
|
140
|
|
|
$this->assertInstanceOf(Item::class, $items[0]); |
|
141
|
|
|
$this->assertEquals(Microformats::FORMAT, $items[0]->getFormat()); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Test the Microformats parser with nested items |
|
146
|
|
|
*/ |
|
147
|
|
|
public function testNestedMicroformatsParser() |
|
148
|
|
|
{ |
|
149
|
|
|
$items = $this->parseItems('microformats/nested-events.html', Microformats::class); |
|
150
|
|
|
$this->assertTrue(is_array($items)); |
|
151
|
|
|
$this->assertEquals(1, count($items)); |
|
152
|
|
|
$this->assertInstanceOf(Item::class, $items[0]); |
|
153
|
|
|
$this->assertEquals(Microformats::FORMAT, $items[0]->getFormat()); |
|
154
|
|
|
$this->assertEquals(2, count($items[0]->getChildren())); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Test the HTML Microdata parser |
|
159
|
|
|
*/ |
|
160
|
|
|
public function testMicrodataParser() |
|
161
|
|
|
{ |
|
162
|
|
|
$items = $this->parseItems('html-microdata/article-microdata.html', Microdata::class); |
|
163
|
|
|
$expectedItemFormat = Microdata::FORMAT; |
|
164
|
|
|
$expectedItemIri = new Iri('http://schema.org/', 'NewsArticle'); |
|
165
|
|
|
$this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Assert that items are of a particular type |
|
170
|
|
|
* |
|
171
|
|
|
* @param ItemInterface[] $items Items |
|
172
|
|
|
* @param int $expectedItemFormat Expected item format |
|
173
|
|
|
* @param Iri $expectedItemIri Expected item IRI |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function assertItemParsedAs(array $items, int $expectedItemFormat, Iri $expectedItemIri) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->assertIsArray($items); |
|
178
|
|
|
$this->assertCount(1, $items); |
|
179
|
|
|
$this->assertInstanceOf(Item::class, $items[0]); |
|
180
|
|
|
$this->assertEquals($expectedItemFormat, $items[0]->getFormat()); |
|
181
|
|
|
$this->assertEquals([$expectedItemIri], $items[0]->getType()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Test the RDFa Lite 1.1 parser |
|
186
|
|
|
*/ |
|
187
|
|
|
public function testRdfaLiteParser() |
|
188
|
|
|
{ |
|
189
|
|
|
$items = $this->parseItems('rdfa-lite/article-rdfa-lite.html', RdfaLite::class); |
|
190
|
|
|
$expectedItemFormat = RdfaLite::FORMAT; |
|
191
|
|
|
$expectedItemIri = new Iri('http://schema.org/', 'NewsArticle'); |
|
192
|
|
|
$this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Test the LinkType parser |
|
197
|
|
|
*/ |
|
198
|
|
|
public function testLinkTypeParser() |
|
199
|
|
|
{ |
|
200
|
|
|
$items = $this->parseItems('link-type/valid-test.html', LinkType::class); |
|
201
|
|
|
$this->assertTrue(is_array($items)); |
|
202
|
|
|
$this->assertEquals(4, count($items)); |
|
203
|
|
|
$this->assertInstanceOf(Item::class, $items[0]); |
|
204
|
|
|
$this->assertEquals(LinkType::FORMAT, $items[0]->getFormat()); |
|
205
|
|
|
$this->assertEquals([new Iri(LinkType::HTML_PROFILE_URI, 'icon')], $items[0]->getType()); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Test the JSON-LD parser with a valid recursion |
|
210
|
|
|
*/ |
|
211
|
|
|
public function testRecursionInJsonLDParser() |
|
212
|
|
|
{ |
|
213
|
|
|
$items = $this->parseItems('json-ld/jsonld-recursion.html', JsonLD::class); |
|
214
|
|
|
$this->assertTrue(is_array($items)); |
|
215
|
|
|
$this->assertInstanceOf(Item::class, $items[0]); |
|
216
|
|
|
|
|
217
|
|
|
$url = $items[0]->getProperty('url'); |
|
218
|
|
|
$this->assertTrue(is_array($url)); |
|
219
|
|
|
$this->assertEquals(1, count($url)); |
|
220
|
|
|
$this->assertInstanceOf(StringValue::class, $url[0]); |
|
221
|
|
|
$this->assertEquals('https://www.example.com/', strval($url[0])); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|