Completed
Pull Request — master (#38)
by Richard van
03:08
created

ParserTest::parseItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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\Item\Item;
40
use Jkphl\Micrometa\Application\Value\StringValue;
41
use Jkphl\Micrometa\Domain\Item\Iri;
42
use Jkphl\Micrometa\Infrastructure\Logger\ExceptionLogger;
43
use Jkphl\Micrometa\Infrastructure\Parser\JsonLD;
44
use Jkphl\Micrometa\Infrastructure\Parser\LinkType;
45
use Jkphl\Micrometa\Infrastructure\Parser\Microdata;
46
use Jkphl\Micrometa\Infrastructure\Parser\Microformats;
47
use Jkphl\Micrometa\Infrastructure\Parser\RdfaLite;
48
use Jkphl\Micrometa\Tests\AbstractTestBase;
49
50
/**
51
 * Parser tests
52
 *
53
 * @package    Jkphl\Micrometa
54
 * @subpackage Jkphl\Micrometa\Tests
55
 */
56
class ParserTest extends AbstractTestBase
57
{
58
    /**
59
     * Test the JSON-LD parser with multiple languages
60
     */
61
    public function testLanguageJsonLDParser()
62
    {
63
        $items = $this->parseItems('json-ld/jsonld-languages.html', JsonLD::class);
64
        $this->assertTrue(is_array($items));
65
        $this->assertEquals(1, count($items));
66
        $this->assertInstanceOf(Item::class, $items[0]);
67
        $this->assertEquals(JsonLD::FORMAT, $items[0]->getFormat());
68
        $this->assertEquals('http://example.com/id1', $items[0]->getId());
69
70
        /** @var StringValue[] $propertyValues */
71
        $propertyValues = $items[0]->getProperty('http://example.com/term6');
72
        $this->assertTrue(is_array($propertyValues));
73
        foreach ([null, null, 'en', 'de'] as $index => $language) {
74
            $this->assertInstanceOf(StringValue::class, $propertyValues[$index]);
75
            $this->assertEquals(strval($index + 1), strval($propertyValues[$index]));
76
            $this->assertEquals($language, $propertyValues[$index]->getLanguage());
77
        }
78
    }
79
80
    /**
81
     * Test the JSON-LD parser with multiple documents and file cache
82
     */
83
    public function testMultipleJsonLDParser()
84
    {
85
        $items = $this->parseItems('json-ld/jsonld-examples.html', JsonLD::class, 0);
86
        $this->assertTrue(is_array($items));
87
        $this->assertEquals(5, count($items));
88
        $this->assertInstanceOf(Item::class, $items[0]);
89
        $this->assertEquals(JsonLD::FORMAT, $items[0]->getFormat());
90
        $this->assertEquals('https://jsonld-examples.com/#header_website', $items[0]->getId());
91
    }
92
93
    /**
94
     * Test the JSON-LD parser with an invalid document
95
     */
96
    public function testInvalidJsonLDParser()
97
    {
98
        $items = $this->parseItems('json-ld/jsonld-invalid.html', JsonLD::class, 0);
99
        $this->assertTrue(is_array($items));
100
        $this->assertEquals(0, count($items));
101
    }
102
103
    /**
104
     * Test the Microformats parser
105
     */
106
    public function testMicroformatsParser()
107
    {
108
        $items = $this->parseItems('microformats/entry.html', Microformats::class);
109
        $this->assertTrue(is_array($items));
110
        $this->assertEquals(1, count($items));
111
        $this->assertInstanceOf(Item::class, $items[0]);
112
        $this->assertEquals(Microformats::FORMAT, $items[0]->getFormat());
113
    }
114
115
    /**
116
     * Test the Microformats parser with nested items
117
     */
118
    public function testNestedMicroformatsParser()
119
    {
120
        $items = $this->parseItems('microformats/nested-events.html', Microformats::class);
121
        $this->assertTrue(is_array($items));
122
        $this->assertEquals(1, count($items));
123
        $this->assertInstanceOf(Item::class, $items[0]);
124
        $this->assertEquals(Microformats::FORMAT, $items[0]->getFormat());
125
        $this->assertEquals(2, count($items[0]->getChildren()));
126
    }
127
128
    /**
129
     * Test the HTML Microdata parser
130
     */
131
    public function testMicrodataParser()
132
    {
133
        $items = $this->parseItems('html-microdata/article-microdata.html', Microdata::class);
134
        $expectedItemFormat = Microdata::FORMAT;
135
        $expectedItemIri = new Iri('http://schema.org/', 'NewsArticle');
136
        $this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
137
    }
138
139
    /**
140
     * Test the RDFa Lite 1.1 parser
141
     */
142
    public function testRdfaLiteParser()
143
    {
144
        $items = $this->parseItems('rdfa-lite/article-rdfa-lite.html', RdfaLite::class);
145
        $expectedItemFormat = RdfaLite::FORMAT;
146
        $expectedItemIri = new Iri('http://schema.org/', 'NewsArticle');
147
        $this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
148
    }
149
150
    /**
151
     * Test the LinkType parser
152
     */
153
    public function testLinkTypeParser()
154
    {
155
        $items = $this->parseItems('link-type/valid-test.html', LinkType::class);
156
        $this->assertTrue(is_array($items));
157
        $this->assertEquals(4, count($items));
158
        $this->assertInstanceOf(Item::class, $items[0]);
159
        $this->assertEquals(LinkType::FORMAT, $items[0]->getFormat());
160
        $this->assertEquals([new Iri(LinkType::HTML_PROFILE_URI, 'icon')], $items[0]->getType());
161
    }
162
163
    private function assertItemParsedAs(array $items, int $expectedItemFormat, Iri $expectedItemIri)
164
    {
165
        $this->assertIsArray($items);
166
        $this->assertCount(1, $items);
167
        $this->assertInstanceOf(Item::class, $items[0]);
168
        $this->assertEquals($expectedItemFormat, $items[0]->getFormat());
169
        $this->assertEquals([$expectedItemIri], $items[0]->getType());
170
    }
171
172
    private function parseItems(string $fixture, string $parser, int $errorThreshold = 400)
173
    {
174
        list($uri, $dom) = $this->getUriFixture($fixture);
175
        $parser = new $parser($uri, self::getLogger($errorThreshold));
176
        return $parser->parseDom($dom)->getItems();
177
    }
178
}
179