Completed
Pull Request — master (#38)
by Richard van
03:10 queued 13s
created

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