Completed
Push — master ( 2d3fae...0d820b )
by Joschi
02:45
created

ParserIteratorTestBase::validateProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 4
1
<?php
2
3
/**
4
 * rdfa-lite
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Rdfalite
8
 * @subpackage Jkphl\Rdfalite\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\Rdfalite\Tests\Application;
38
39
use Jkphl\Rdfalite\Domain\Property\Property;
40
use Jkphl\Rdfalite\Domain\Property\PropertyInterface;
41
use Jkphl\Rdfalite\Domain\Thing\ThingInterface;
42
use Jkphl\Rdfalite\Domain\Vocabulary\Vocabulary;
43
use Jkphl\Rdfalite\Domain\Vocabulary\VocabularyInterface;
44
use Jkphl\Rdfalite\Tests\Domain\VocabularyTest;
45
46
/**
47
 * Base class for parser & iterator tests
48
 *
49
 * @package Jkphl\Rdfalite
50
 * @subpackage Jkphl\Rdfalite\Tests
51
 */
52
abstract class ParserIteratorTestBase extends \PHPUnit_Framework_TestCase
53
{
54
    /**
55
     * Test HTML with person RDFa Lite 1.1
56
     *
57
     * @var string
58
     */
59
    protected static $personRdfa;
60
61
    /**
62
     * Setup all tests
63
     */
64
    public static function setUpBeforeClass()
65
    {
66
        self::$personRdfa = file_get_contents(
67
            dirname(__DIR__).DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'person-rdfa-lite.html'
68
        );
69
    }
70
71
    /**
72
     * Validate parsing results
73
     *
74
     * @param ThingInterface[] $things Parsing Results
75
     */
76
    protected function validatePersonResult(array $things)
77
    {
78
        $schemaOrgVocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG);
79
80
        $this->assertTrue(is_array($things));
81
        $this->assertEquals(1, count($things));
82
        $thing = $things[0];
83
84
        $this->assertInstanceOf(ThingInterface::class, $thing);
85
        $this->assertEquals(VocabularyTest::SCHEMA_ORG.'Person', $thing->getType());
86
        $this->assertEquals($schemaOrgVocabulary, $thing->getVocabulary());
87
        $this->assertEquals('#joschi', $thing->getResourceId());
88
89
        $properties = $thing->getProperties();
90
        $this->assertTrue(is_array($properties));
91
        $this->assertEquals(4, count($properties));
92
93
        $this->validateProperty($thing, 'name', $schemaOrgVocabulary, 'Joschi Kuphal');
94
        $this->validateProperty($thing, 'telephone', $schemaOrgVocabulary, '+49 911 9593945');
95
        $this->validateProperty($thing, 'image', $schemaOrgVocabulary, 'https://jkphl.is/avatar.jpg');
96
        $this->validateProperty($thing, 'preferredAnimal', new Vocabulary('http://open.vocab.org/terms/'), 'Unicorn');
97
    }
98
99
    /**
100
     * Validate a single property
101
     *
102
     * @param ThingInterface $thing Thing
103
     * @param string $name Property name
104
     * @param VocabularyInterface $vocabulary Property vocabulary
105
     * @param string $value Property value
106
     */
107
    protected function validateProperty(ThingInterface $thing, $name, VocabularyInterface $vocabulary, $value)
108
    {
109
        $property = $thing->getProperty($name);
110
        $this->assertTrue(is_array($property));
111
        $this->assertEquals(1, count($property));
112
        $this->assertInstanceOf(PropertyInterface::class, $property[0]);
113
        $this->assertEquals(new Property($name, $vocabulary, $value), $property[0]);
114
    }
115
}
116