Completed
Push — v2 ( 9de304...bf6a94 )
by Joschi
05:08
created

ParserTest::testMultipleJsonLDParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
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 © 2017 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 © 2017 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\LinkRel;
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::$logger);
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
     * Test the JSON-LD parser with multiple documents and file cache
83
     */
84
    public function testMultipleJsonLDParser()
85
    {
86
        list($uri, $dom) = $this->getUriFixture('json-ld/jsonld-examples.html');
87
        $parser = new JsonLD($uri, new ExceptionLogger(0));
88
        $items = $parser->parseDom($dom)->getItems();
89
        $this->assertTrue(is_array($items));
90
        $this->assertEquals(4, count($items));
91
        $this->assertInstanceOf(Item::class, $items[0]);
92
        $this->assertEquals(JsonLD::FORMAT, $items[0]->getFormat());
93
        $this->assertEquals('https://jsonld-examples.com/#header_website', $items[0]->getId());
94
    }
95
96
    /**
97
     * Test the JSON-LD parser with an invalid document
98
     */
99
    public function testInvalidJsonLDParser()
100
    {
101
        list($uri, $dom) = $this->getUriFixture('json-ld/jsonld-invalid.html');
102
        $parser = new JsonLD($uri, new ExceptionLogger(0));
103
        $items = $parser->parseDom($dom)->getItems();
104
        $this->assertTrue(is_array($items));
105
        $this->assertEquals(0, count($items));
106
    }
107
108
    /**
109
     * Test the Microformats parser
110
     */
111
    public function testMicroformatsParser()
112
    {
113
        list($uri, $dom) = $this->getUriFixture('microformats/entry.html');
114
        $parser = new Microformats($uri, self::$logger);
115
        $items = $parser->parseDom($dom)->getItems();
116
        $this->assertTrue(is_array($items));
117
        $this->assertEquals(1, count($items));
118
        $this->assertInstanceOf(Item::class, $items[0]);
119
        $this->assertEquals(Microformats::FORMAT, $items[0]->getFormat());
120
        $this->assertEquals('en', $items[0]->getLanguage());
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::$logger);
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::$logger);
145
        $items = $parser->parseDom($dom)->getItems();
146
        $this->assertTrue(is_array($items));
147
        $this->assertEquals(1, count($items));
148
        $this->assertInstanceOf(Item::class, $items[0]);
149
        $this->assertEquals(Microdata::FORMAT, $items[0]->getFormat());
150
        $this->assertEquals([new Iri('http://schema.org/', 'NewsArticle')], $items[0]->getType());
151
    }
152
153
    /**
154
     * Test the RDFa Lite 1.1 parser
155
     */
156
    public function testRdfaLiteParser()
157
    {
158
        list($uri, $dom) = $this->getUriFixture('rdfa-lite/article-rdfa-lite.html');
159
        $parser = new RdfaLite($uri, self::$logger);
160
        $items = $parser->parseDom($dom)->getItems();
161
        $this->assertTrue(is_array($items));
162
        $this->assertEquals(1, count($items));
163
        $this->assertInstanceOf(Item::class, $items[0]);
164
        $this->assertEquals(RdfaLite::FORMAT, $items[0]->getFormat());
165
        $this->assertEquals([new Iri('http://schema.org/', 'NewsArticle')], $items[0]->getType());
166
    }
167
168
    /**
169
     * Test the LinkRel parser
170
     */
171
    public function testLinkRelParser()
172
    {
173
        list($uri, $dom) = $this->getUriFixture('link-rel/valid-test.html');
174
        $parser = new LinkRel($uri, self::$logger);
175
        $items = $parser->parseDom($dom)->getItems();
176
        $this->assertTrue(is_array($items));
177
        $this->assertEquals(4, count($items));
178
        $this->assertInstanceOf(Item::class, $items[0]);
179
        $this->assertEquals(LinkRel::FORMAT, $items[0]->getFormat());
180
        $this->assertEquals([new Iri(LinkRel::HTML_PROFILE_URI, 'icon')], $items[0]->getType());
181
    }
182
}
183