Completed
Push — master ( c78c30...f35f13 )
by Joschi
02:42
created

ParserTest::testArticle()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
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\RdfaLiteMicrodata\Tests\Application;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Context\RdfaLiteContext;
40
use Jkphl\RdfaLiteMicrodata\Application\Parser\Parser;
41
use Jkphl\RdfaLiteMicrodata\Infrastructure\Factories\HtmlDocumentFactory;
42
use Jkphl\RdfaLiteMicrodata\Infrastructure\Parser\RdfaLiteElementProcessor;
43
use Jkphl\RdfaLiteMicrodata\Infrastructure\Service\ThingGateway;
44
45
/**
46
 * Parser tests
47
 *
48
 * @package Jkphl\RdfaLiteMicrodata
49
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
50
 */
51
class ParserTest extends ParserIteratorTestBase
52
{
53
    /**
54
     * Test person parsing
55
     */
56
    public function testPerson()
57
    {
58
        $htmlDocumentFactory = new HtmlDocumentFactory();
59
        $rdfaElementProcessor = new RdfaLiteElementProcessor(true);
60
        $rdfaContext = new RdfaLiteContext();
61
        $parser = new Parser($htmlDocumentFactory, $rdfaElementProcessor, $rdfaContext);
62
        $this->assertInstanceOf(Parser::class, $parser);
63
64
        $things = $parser->parse(self::$personRdfa);
65
        $this->validatePersonResult($things);
66
    }
67
68
    /**
69
     * Test article parsing
70
     */
71
    public function testArticle()
72
    {
73
        $htmlDocumentFactory = new HtmlDocumentFactory();
74
        $rdfaElementProcessor = new RdfaLiteElementProcessor(true);
75
        $rdfaContext = new RdfaLiteContext();
76
        $parser = new Parser($htmlDocumentFactory, $rdfaElementProcessor, $rdfaContext);
77
        $this->assertInstanceOf(Parser::class, $parser);
78
79
        $things = $parser->parse(
80
            file_get_contents(
81
                dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'article-rdfa-lite.html'
82
            )
83
        );
84
        $this->assertArrayEquals(
85
            $this->castArray(
86
                json_decode(
87
                    file_get_contents(
88
                        dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'article-rdfa-lite.json'
89
                    )
90
                )
91
            ),
92
            $this->castArray((new ThingGateway())->export($things))
93
        );
94
    }
95
96
    /**
97
     * Test empty default vocabulary parsing
98
     *
99
     * @expectedException \Jkphl\RdfaLiteMicrodata\Infrastructure\Exceptions\RuntimeException
100
     * @expectedExceptionCode 1487030264
101
     */
102
    public function testEmptyDefaultVocabulary()
103
    {
104
        $htmlDocumentFactory = new HtmlDocumentFactory();
105
        $rdfaElementProcessor = new RdfaLiteElementProcessor(true);
106
        $rdfaContext = new RdfaLiteContext();
107
        $parser = new Parser($htmlDocumentFactory, $rdfaElementProcessor, $rdfaContext);
108
        $this->assertInstanceOf(Parser::class, $parser);
109
110
        $parser->parse(
111
            file_get_contents(
112
                dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'empty-default-vocab-rdfa-lite.html'
113
            )
114
        );
115
    }
116
117
    /**
118
     * Test unknown vocabulary prefix parsing
119
     *
120
     * @expectedException \Jkphl\RdfaLiteMicrodata\Infrastructure\Exceptions\OutOfBoundsException
121
     * @expectedExceptionCode 1486928423
122
     */
123
    public function testUnknownVocabularyPrefix()
124
    {
125
        $htmlDocumentFactory = new HtmlDocumentFactory();
126
        $rdfaElementProcessor = new RdfaLiteElementProcessor(true);
127
        $rdfaContext = new RdfaLiteContext();
128
        $parser = new Parser($htmlDocumentFactory, $rdfaElementProcessor, $rdfaContext);
129
        $this->assertInstanceOf(Parser::class, $parser);
130
131
        $parser->parse(
132
            file_get_contents(
133
                dirname(
134
                    __DIR__
135
                ).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'unknown-vocab-prefix-rdfa-lite.html'
136
            )
137
        );
138
    }
139
}
140