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; |
38
|
|
|
|
39
|
|
|
use Jkphl\Rdfalite\Domain\Thing; |
40
|
|
|
use Jkphl\Rdfalite\Domain\Vocabulary; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Thing tests |
44
|
|
|
* |
45
|
|
|
* @package Jkphl\Rdfalite |
46
|
|
|
* @subpackage Jkphl\Rdfalite\Tests |
47
|
|
|
*/ |
48
|
|
|
class ThingTest extends \PHPUnit_Framework_TestCase |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* schema.org vocabulary |
52
|
|
|
* |
53
|
|
|
* @var Vocabulary |
54
|
|
|
*/ |
55
|
|
|
protected static $schemaOrgVocabulary; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Setup all tests |
59
|
|
|
*/ |
60
|
|
|
public static function setUpBeforeClass() |
61
|
|
|
{ |
62
|
|
|
self::$schemaOrgVocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test the instantiation of a minimum thing |
67
|
|
|
*/ |
68
|
|
|
public function testMinimumThing() |
69
|
|
|
{ |
70
|
|
|
$type = 'Person'; |
71
|
|
|
$thing = new Thing($type, self::$schemaOrgVocabulary); |
72
|
|
|
$this->assertInstanceOf(Thing::class, $thing); |
73
|
|
|
$this->assertEquals(VocabularyTest::SCHEMA_ORG.'/'.$type, $thing->getType()); |
74
|
|
|
$this->assertEquals(self::$schemaOrgVocabulary, $thing->getVocabulary()); |
75
|
|
|
$this->assertNull($thing->getId()); |
76
|
|
|
$this->assertTrue(is_array($thing->getChildren())); |
77
|
|
|
$this->assertEquals(0, count($thing->getChildren())); |
78
|
|
|
$this->assertTrue(is_array($thing->getProperties())); |
79
|
|
|
$this->assertEquals(0, count($thing->getProperties())); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test the resource ID |
84
|
|
|
*/ |
85
|
|
|
public function testResourceId() |
86
|
|
|
{ |
87
|
|
|
$thing = new Thing('Person', self::$schemaOrgVocabulary, 'bob'); |
88
|
|
|
$this->assertInstanceOf(Thing::class, $thing); |
89
|
|
|
$this->assertEquals('bob', $thing->getId()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test the thing instantiation with an invalid type |
94
|
|
|
* |
95
|
|
|
* @expectedException \Jkphl\Rdfalite\Domain\RuntimeException |
96
|
|
|
* @expectedExceptionCode 1486823588 |
97
|
|
|
*/ |
98
|
|
|
public function testInvalidTypeThing() |
99
|
|
|
{ |
100
|
|
|
new Thing('', self::$schemaOrgVocabulary); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test adding a property |
105
|
|
|
*/ |
106
|
|
|
public function testAddProperty() |
107
|
|
|
{ |
108
|
|
|
$thing = new Thing('Person', self::$schemaOrgVocabulary); |
109
|
|
|
$this->assertInstanceOf(Thing::class, $thing); |
110
|
|
|
|
111
|
|
|
$thing->addProperty('test1', 'value1'); |
112
|
|
|
$thing->addProperty('test1', 'value2'); |
113
|
|
|
$thing->addProperty('test2', 'value1'); |
114
|
|
|
|
115
|
|
|
$properties = $thing->getProperties(); |
116
|
|
|
$this->assertTrue(is_array($properties)); |
117
|
|
|
$this->assertTrue(array_key_exists('test1', $properties)); |
118
|
|
|
$this->assertTrue(array_key_exists('test2', $properties)); |
119
|
|
|
$this->assertEquals(2, count($properties)); |
120
|
|
|
|
121
|
|
|
$test1Property = $thing->getProperty('test1'); |
122
|
|
|
$this->assertTrue(is_array($test1Property)); |
123
|
|
|
$this->assertEquals(2, count($test1Property)); |
124
|
|
|
$this->assertEquals(['value1', 'value2'], $test1Property); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Test setting an invalid property name |
129
|
|
|
* |
130
|
|
|
* @expectedException \Jkphl\Rdfalite\Domain\RuntimeException |
131
|
|
|
* @expectedExceptionCode 1486848618 |
132
|
|
|
*/ |
133
|
|
|
public function testSetInvalidPropertyName() |
134
|
|
|
{ |
135
|
|
|
$thing = new Thing('Person', self::$schemaOrgVocabulary); |
136
|
|
|
$this->assertInstanceOf(Thing::class, $thing); |
137
|
|
|
$thing->addProperty('', null); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Test getting an invalid property name |
142
|
|
|
* |
143
|
|
|
* @expectedException \Jkphl\Rdfalite\Domain\OutOfBoundsException |
144
|
|
|
* @expectedExceptionCode 1486849016 |
145
|
|
|
*/ |
146
|
|
|
public function testGetInvalidPropertyName() |
147
|
|
|
{ |
148
|
|
|
$thing = new Thing('Person', self::$schemaOrgVocabulary); |
149
|
|
|
$this->assertInstanceOf(Thing::class, $thing); |
150
|
|
|
$thing->getProperty('invalid'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Test adding children |
155
|
|
|
*/ |
156
|
|
|
public function testAddChild() |
157
|
|
|
{ |
158
|
|
|
$thing = new Thing('Person', self::$schemaOrgVocabulary); |
159
|
|
|
$this->assertInstanceOf(Thing::class, $thing); |
160
|
|
|
|
161
|
|
|
$child1 = new Thing('Person', self::$schemaOrgVocabulary); |
162
|
|
|
$child2 = new Thing('Person', self::$schemaOrgVocabulary); |
163
|
|
|
$thing->addChild($child1)->addChild($child2); |
164
|
|
|
|
165
|
|
|
$children = $thing->getChildren(); |
166
|
|
|
$this->assertTrue(is_array($children)); |
167
|
|
|
$this->assertEquals(2, count($children)); |
168
|
|
|
$this->assertEquals([$child1, $child2], $children); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|