Completed
Push — master ( 29cf85...ef4b76 )
by Joschi
03:03
created

ParserIteratorTestBase   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 5
dl 0
loc 71
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 5 1
B validatePersonResult() 0 24 1
A validateProperty() 0 8 1
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\Domain\Property\Property;
40
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyInterface;
41
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
42
use Jkphl\RdfaLiteMicrodata\Domain\Type\Type;
43
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\Vocabulary;
44
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
45
use Jkphl\RdfaLiteMicrodata\Tests\AbstractTest;
46
use Jkphl\RdfaLiteMicrodata\Tests\Domain\VocabularyTest;
47
48
/**
49
 * Base class for parser & iterator tests
50
 *
51
 * @package Jkphl\RdfaLiteMicrodata
52
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
53
 */
54
abstract class ParserIteratorTestBase extends AbstractTest
55
{
56
    /**
57
     * Test HTML with person RDFa Lite 1.1
58
     *
59
     * @var string
60
     */
61
    protected static $personRdfa;
62
    /**
63
     * Fixture directory
64
     *
65
     * @var string
66
     */
67
    protected static $fixtures;
68
69
    /**
70
     * Setup all tests
71
     */
72
    public static function setUpBeforeClass()
73
    {
74
        self::$fixtures = dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR;
75
        self::$personRdfa = file_get_contents(self::$fixtures.'person-rdfa-lite.html');
76
    }
77
78
    /**
79
     * Validate parsing results
80
     *
81
     * @param ThingInterface[] $things Parsing Results
82
     */
83
    protected function validatePersonResult(array $things)
84
    {
85
        $schemaOrgVocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
86
        $foafVocabulary = new Vocabulary('http://xmlns.com/foaf/0.1/');
87
88
        $this->assertTrue(is_array($things));
89
        $this->assertEquals(1, count($things));
90
        $thing = $things[0];
91
        $type = new Type('Person', $schemaOrgVocabulary);
92
        $foafType = new Type('Person', $foafVocabulary);
93
94
        $this->assertInstanceOf(ThingInterface::class, $thing);
95
        $this->assertEquals([$type, $foafType], $thing->getTypes());
96
        $this->assertEquals('#joschi', $thing->getResourceId());
97
98
        $properties = $thing->getProperties();
99
        $this->assertTrue(is_array($properties));
100
        $this->assertEquals(4, count($properties));
101
102
        $this->validateProperty($thing, 'name', $schemaOrgVocabulary, 'Joschi Kuphal');
103
        $this->validateProperty($thing, 'telephone', $schemaOrgVocabulary, '+49 911 9593945');
104
        $this->validateProperty($thing, 'image', $schemaOrgVocabulary, 'https://jkphl.is/avatar.jpg');
105
        $this->validateProperty($thing, 'preferredAnimal', new Vocabulary('http://open.vocab.org/terms/'), 'Unicorn');
106
    }
107
108
    /**
109
     * Validate a single property
110
     *
111
     * @param ThingInterface $thing Thing
112
     * @param string $name Property name
113
     * @param VocabularyInterface $vocabulary Property vocabulary
114
     * @param string $value Property value
115
     */
116
    protected function validateProperty(ThingInterface $thing, $name, VocabularyInterface $vocabulary, $value)
117
    {
118
        $property = $thing->getProperty($name, $vocabulary);
119
        $this->assertTrue(is_array($property));
120
        $this->assertEquals(1, count($property));
121
        $this->assertInstanceOf(PropertyInterface::class, $property[0]);
122
        $this->assertEquals(new Property($name, $vocabulary, $value), $property[0]);
123
    }
124
}
125