ThingTest::validateProperty()   A
last analyzed

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
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
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\Domain;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Parser\NullVocabulary;
40
use Jkphl\RdfaLiteMicrodata\Domain\Property\Property;
41
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyInterface;
42
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyListInterface;
43
use Jkphl\RdfaLiteMicrodata\Domain\Thing\Thing;
44
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
45
use Jkphl\RdfaLiteMicrodata\Domain\Type\Type;
46
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\Vocabulary;
47
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
48
49
/**
50
 * Thing tests
51
 *
52
 * @package Jkphl\RdfaLiteMicrodata
53
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
54
 */
55
class ThingTest extends \PHPUnit_Framework_TestCase
56
{
57
    /**
58
     * schema.org vocabulary
59
     *
60
     * @var Vocabulary
61
     */
62
    protected static $schemaOrgVocabulary;
63
64
    /**
65
     * Setup all tests
66
     */
67
    public static function setUpBeforeClass()
68
    {
69
        self::$schemaOrgVocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
70
    }
71
72
    /**
73
     * Test the instantiation of a minimum thing
74
     */
75
    public function testMinimumThing()
76
    {
77
        $type = new Type('Person', self::$schemaOrgVocabulary);
78
        $thing = new Thing($type);
79
        $this->assertInstanceOf(Thing::class, $thing);
80
        $this->assertEquals([$type], $thing->getTypes());
81
        $this->assertNull($thing->getResourceId());
82
        $this->assertInstanceOf(PropertyListInterface::class, $thing->getProperties());
83
        $this->assertEquals(0, count($thing->getProperties()));
84
    }
85
86
    /**
87
     * Test the resource ID
88
     */
89
    public function testResourceId()
90
    {
91
        $type = new Type('Person', self::$schemaOrgVocabulary);
92
        $thing = new Thing($type, 'bob');
93
        $this->assertInstanceOf(Thing::class, $thing);
94
        $this->assertEquals('bob', $thing->getResourceId());
95
    }
96
97
    /**
98
     * Test the thing instantiation with an invalid type
99
     *
100
     * @expectedException \Jkphl\RdfaLiteMicrodata\Domain\Exceptions\RuntimeException
101
     * @expectedExceptionCode 1487435964
102
     */
103
    public function testInvalidTypeThing()
104
    {
105
        new Thing([null]);
106
    }
107
108
    /**
109
     * Test the instantiation of an anonymous thing
110
     */
111
    public function testAnonymousThing()
112
    {
113
        $thing = new Thing([]);
114
        $this->assertInstanceOf(Thing::class, $thing);
115
        $this->assertEquals([], $thing->getTypes());
116
    }
117
118
    /**
119
     * Test adding a property
120
     */
121
    public function testAddProperty()
122
    {
123
        $type = new Type('Person', self::$schemaOrgVocabulary);
124
        $thing = new Thing($type);
125
        $this->assertInstanceOf(Thing::class, $thing);
126
127
        $vocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
128
        $property1 = new Property('test1', $vocabulary, 'value1');
129
        $property2 = new Property('test1', $vocabulary, 'value2');
130
        $property3 = new Property('test2', $vocabulary, 'value1');
131
        $thing->addProperty($property1);
132
        $thing->addProperty($property2);
133
        $thing->addProperty($property3);
134
135
        $this->validateProperties($thing, $vocabulary);
136
        $this->validateProperty($thing, $vocabulary, $property1, $property2);
137
    }
138
139
    /**
140
     * Validate all properties
141
     *
142
     * @param ThingInterface $thing Thing
143
     * @param VocabularyInterface $vocabulary Vocabulary
144
     */
145
    protected function validateProperties(ThingInterface $thing, VocabularyInterface $vocabulary)
146
    {
147
        $properties = $thing->getProperties();
148
        $this->assertInstanceOf(PropertyListInterface::class, $properties);
149
        $this->assertTrue(isset($properties[$vocabulary->expand('test1')]));
150
        $this->assertTrue(isset($properties[$vocabulary->expand('test2')]));
151
        $this->assertEquals(2, count($properties));
152
    }
153
154
    /**
155
     * Validate a single property
156
     *
157
     * @param ThingInterface $thing Thing
158
     * @param VocabularyInterface $vocabulary Vocabulary
159
     * @param PropertyInterface $property1 First property
160
     * @param PropertyInterface $property2 Second property
161
     */
162
    protected function validateProperty(
163
        ThingInterface $thing,
164
        VocabularyInterface $vocabulary,
165
        PropertyInterface $property1,
166
        PropertyInterface $property2
167
    ) {
168
        $test1Property = $thing->getProperty('test1', $vocabulary);
169
        $this->assertTrue(is_array($test1Property));
170
        $this->assertEquals(2, count($test1Property));
171
        $this->assertEquals([$property1, $property2], $test1Property);
172
    }
173
174
    /**
175
     * Test getting an invalid property name
176
     *
177
     * @expectedException \Jkphl\RdfaLiteMicrodata\Domain\Exceptions\OutOfBoundsException
178
     * @expectedExceptionCode 1486849016
179
     */
180
    public function testGetInvalidPropertyName()
181
    {
182
        $type = new Type('Person', self::$schemaOrgVocabulary);
183
        $thing = new Thing($type);
184
        $this->assertInstanceOf(Thing::class, $thing);
185
        $thing->getProperty('invalid', new NullVocabulary());
186
    }
187
}
188