1 | <?php |
||
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) |
||
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'); |
||
194 | |||
195 | /** |
||
196 | * Test the LinkType parser |
||
197 | */ |
||
198 | public function testLinkTypeParser() |
||
207 | |||
208 | /** |
||
209 | * Test the JSON-LD parser with a valid recursion |
||
210 | */ |
||
211 | public function testRecursionInJsonLDParser() |
||
223 | } |
||
224 |