Completed
Push — master ( 6a41ca...195821 )
by Joschi
03:07 queued 11s
created

ParserTest::parseItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
        list($uri, $dom) = $this->getUriFixture('json-ld/jsonld-ending-semicolon.html');
128
        $parser = new JsonLD($uri, new ExceptionLogger(0));
129
        $items  = $parser->parseDom($dom)->getItems();
130
        $this->assertTrue(is_array($items));
131
        $this->assertEquals(1, count($items));
132
    }
133
134
    /**
135
     * Test the Microformats parser
136
     */
137
    public function testMicroformatsParser()
138
    {
139
        $items = $this->parseItems('microformats/entry.html', Microformats::class);
140
        $this->assertTrue(is_array($items));
141
        $this->assertEquals(1, count($items));
142
        $this->assertInstanceOf(Item::class, $items[0]);
143
        $this->assertEquals(Microformats::FORMAT, $items[0]->getFormat());
144
    }
145
146
    /**
147
     * Test the Microformats parser with nested items
148
     */
149
    public function testNestedMicroformatsParser()
150
    {
151
        $items = $this->parseItems('microformats/nested-events.html', Microformats::class);
152
        $this->assertTrue(is_array($items));
153
        $this->assertEquals(1, count($items));
154
        $this->assertInstanceOf(Item::class, $items[0]);
155
        $this->assertEquals(Microformats::FORMAT, $items[0]->getFormat());
156
        $this->assertEquals(2, count($items[0]->getChildren()));
157
    }
158
159
    /**
160
     * Test the HTML Microdata parser
161
     */
162
    public function testMicrodataParser()
163
    {
164
        $items              = $this->parseItems('html-microdata/article-microdata.html', Microdata::class);
165
        $expectedItemFormat = Microdata::FORMAT;
166
        $expectedItemIri    = new Iri('http://schema.org/', 'NewsArticle');
167
        $this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
168
    }
169
170
    /**
171
     * Assert that items are of a particular type
172
     *
173
     * @param ItemInterface[] $items  Items
174
     * @param int $expectedItemFormat Expected item format
175
     * @param Iri $expectedItemIri    Expected item IRI
176
     */
177
    protected function assertItemParsedAs(array $items, int $expectedItemFormat, Iri $expectedItemIri)
178
    {
179
        $this->assertIsArray($items);
180
        $this->assertCount(1, $items);
181
        $this->assertInstanceOf(Item::class, $items[0]);
182
        $this->assertEquals($expectedItemFormat, $items[0]->getFormat());
183
        $this->assertEquals([$expectedItemIri], $items[0]->getType());
184
    }
185
186
    /**
187
     * Test the RDFa Lite 1.1 parser
188
     */
189
    public function testRdfaLiteParser()
190
    {
191
        $items              = $this->parseItems('rdfa-lite/article-rdfa-lite.html', RdfaLite::class);
192
        $expectedItemFormat = RdfaLite::FORMAT;
193
        $expectedItemIri    = new Iri('http://schema.org/', 'NewsArticle');
194
        $this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
195
    }
196
197
    /**
198
     * Test the LinkType parser
199
     */
200
    public function testLinkTypeParser()
201
    {
202
        $items = $this->parseItems('link-type/valid-test.html', LinkType::class);
203
        $this->assertTrue(is_array($items));
204
        $this->assertEquals(4, count($items));
205
        $this->assertInstanceOf(Item::class, $items[0]);
206
        $this->assertEquals(LinkType::FORMAT, $items[0]->getFormat());
207
        $this->assertEquals([new Iri(LinkType::HTML_PROFILE_URI, 'icon')], $items[0]->getType());
208
    }
209
}
210