Completed
Push — master ( 002125...a57c7d )
by Joschi
02:48
created

ParserIteratorTestBase::validateProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

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 8
nc 1
nop 2
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
        $this->validateProperties($thing, $schemaOrgVocabulary);
99
    }
100
101
    /**
102
     * Validate properties
103
     *
104
     * @param ThingInterface $thing Thing
105
     * @param VocabularyInterface $schemaOrgVocabulary schema.org vocabulary
106
     */
107
    protected function validateProperties(ThingInterface $thing, VocabularyInterface $schemaOrgVocabulary)
108
    {
109
        $properties = $thing->getProperties();
110
        $this->assertTrue(is_array($properties));
111
        $this->assertEquals(4, count($properties));
112
113
        $this->validateProperty($thing, 'name', $schemaOrgVocabulary, 'Joschi Kuphal');
114
        $this->validateProperty($thing, 'telephone', $schemaOrgVocabulary, '+49 911 9593945');
115
        $this->validateProperty($thing, 'image', $schemaOrgVocabulary, 'https://jkphl.is/avatar.jpg');
116
        $this->validateProperty($thing, 'preferredAnimal', new Vocabulary('http://open.vocab.org/terms/'), 'Unicorn');
117
    }
118
119
    /**
120
     * Validate a single property
121
     *
122
     * @param ThingInterface $thing Thing
123
     * @param string $name Property name
124
     * @param VocabularyInterface $vocabulary Property vocabulary
125
     * @param string $value Property value
126
     */
127
    protected function validateProperty(ThingInterface $thing, $name, VocabularyInterface $vocabulary, $value)
128
    {
129
        $property = $thing->getProperty($name, $vocabulary);
130
        $this->assertTrue(is_array($property));
131
        $this->assertEquals(1, count($property));
132
        $this->assertInstanceOf(PropertyInterface::class, $property[0]);
133
        $this->assertEquals(new Property($name, $vocabulary, $value), $property[0]);
134
    }
135
}
136