Completed
Push — master ( efb757...0e58f0 )
by Joschi
02:41
created

ThingTest::validateProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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