Completed
Pull Request — master (#41)
by Richard van
03:02
created

ParserTest::testFixOnSemicolonForJsonLDParser()   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 0
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 JSON-LD parser with an invalid document
105
     */
106
    public function testFixOnSemicolonForJsonLDParser()
107
    {
108
        list($uri, $dom) = $this->getUriFixture('json-ld/jsonld-ending-semicolon.html');
109
        $parser = new JsonLD($uri, new ExceptionLogger(0));
110
        $items  = $parser->parseDom($dom)->getItems();
111
        $this->assertTrue(is_array($items));
112
        $this->assertEquals(1, count($items));
113
    }
114
115
    /**
116
     * Test the Microformats parser
117
     */
118
    public function testMicroformatsParser()
119
    {
120
        $items = $this->parseItems('microformats/entry.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
    }
126
127
    /**
128
     * Test the Microformats parser with nested items
129
     */
130
    public function testNestedMicroformatsParser()
131
    {
132
        $items = $this->parseItems('microformats/nested-events.html', Microformats::class);
133
        $this->assertTrue(is_array($items));
134
        $this->assertEquals(1, count($items));
135
        $this->assertInstanceOf(Item::class, $items[0]);
136
        $this->assertEquals(Microformats::FORMAT, $items[0]->getFormat());
137
        $this->assertEquals(2, count($items[0]->getChildren()));
138
    }
139
140
    /**
141
     * Test the HTML Microdata parser
142
     */
143
    public function testMicrodataParser()
144
    {
145
        $items = $this->parseItems('html-microdata/article-microdata.html', Microdata::class);
146
        $expectedItemFormat = Microdata::FORMAT;
147
        $expectedItemIri = new Iri('http://schema.org/', 'NewsArticle');
148
        $this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
149
    }
150
151
    /**
152
     * Test the RDFa Lite 1.1 parser
153
     */
154
    public function testRdfaLiteParser()
155
    {
156
        $items = $this->parseItems('rdfa-lite/article-rdfa-lite.html', RdfaLite::class);
157
        $expectedItemFormat = RdfaLite::FORMAT;
158
        $expectedItemIri = new Iri('http://schema.org/', 'NewsArticle');
159
        $this->assertItemParsedAs($items, $expectedItemFormat, $expectedItemIri);
160
    }
161
162
    /**
163
     * Test the LinkType parser
164
     */
165
    public function testLinkTypeParser()
166
    {
167
        $items = $this->parseItems('link-type/valid-test.html', LinkType::class);
168
        $this->assertTrue(is_array($items));
169
        $this->assertEquals(4, count($items));
170
        $this->assertInstanceOf(Item::class, $items[0]);
171
        $this->assertEquals(LinkType::FORMAT, $items[0]->getFormat());
172
        $this->assertEquals([new Iri(LinkType::HTML_PROFILE_URI, 'icon')], $items[0]->getType());
173
    }
174
175
    private 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
    private function parseItems(string $fixture, string $parser, int $errorThreshold = 400)
185
    {
186
        list($uri, $dom) = $this->getUriFixture($fixture);
187
        $parser = new $parser($uri, self::getLogger($errorThreshold));
188
        return $parser->parseDom($dom)->getItems();
189
    }
190
}
191