|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* micrometa |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\Micrometa |
|
8
|
|
|
* @subpackage Jkphl\Micrometa\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\Micrometa\Tests\Domain; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\Micrometa\Application\Value\StringValue; |
|
40
|
|
|
use Jkphl\Micrometa\Domain\Item\Iri; |
|
41
|
|
|
use Jkphl\Micrometa\Domain\Item\Item; |
|
42
|
|
|
use Jkphl\Micrometa\Domain\Item\PropertyListInterface; |
|
43
|
|
|
use Jkphl\Micrometa\Domain\Value\ValueInterface; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Item tests |
|
47
|
|
|
* |
|
48
|
|
|
* @package Jkphl\Micrometa |
|
49
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
|
50
|
|
|
*/ |
|
51
|
|
|
class ItemTest extends \PHPUnit_Framework_TestCase |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Public function test the item creation |
|
55
|
|
|
* |
|
56
|
|
|
* @param string|\stdClass|\stdClass[] $type Item type(s) |
|
57
|
|
|
* @param array $properties Item properties |
|
58
|
|
|
* @param $itemId Item id |
|
59
|
|
|
* @param $itemLanguage Item language |
|
60
|
|
|
* @param array $expectedTypes Expected item types |
|
61
|
|
|
* @param array $expectedProperties Expected item properties |
|
62
|
|
|
* @param string $expectedId Expected item id |
|
63
|
|
|
* @param string $expectedLanguage Expected language |
|
64
|
|
|
* @dataProvider creationArgumentProvider |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testItemCreation( |
|
67
|
|
|
$type, |
|
68
|
|
|
array $properties, |
|
69
|
|
|
$itemId, |
|
70
|
|
|
$itemLanguage, |
|
71
|
|
|
array $expectedTypes, |
|
72
|
|
|
array $expectedProperties, |
|
73
|
|
|
$expectedId, |
|
74
|
|
|
$expectedLanguage = null |
|
75
|
|
|
) { |
|
76
|
|
|
$item = new Item($type, $properties, $itemId, $itemLanguage); |
|
77
|
|
|
$this->assertInstanceOf(Item::class, $item); |
|
78
|
|
|
$this->assertEquals($expectedTypes, $item->getType()); |
|
79
|
|
|
$this->assertEquals($expectedProperties, $this->convertPropertyListToArray($item->getProperties())); |
|
80
|
|
|
$this->assertEquals($expectedId, $item->getId()); |
|
81
|
|
|
$this->assertEquals($expectedLanguage, $item->getLanguage()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Convert a property list to a plain array |
|
86
|
|
|
* |
|
87
|
|
|
* @param PropertyListInterface $propertyList Property list |
|
88
|
|
|
* @return array Property list array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function convertPropertyListToArray(PropertyListInterface $propertyList) |
|
91
|
|
|
{ |
|
92
|
|
|
$propertyListValues = []; |
|
93
|
|
|
foreach ($propertyList as $iri => $values) { |
|
94
|
|
|
$propertyListValues[$iri->profile.$iri->name] = $values; |
|
95
|
|
|
} |
|
96
|
|
|
return $propertyListValues; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Data provider for item creation tests |
|
101
|
|
|
* |
|
102
|
|
|
* @return array Item creation arguments |
|
103
|
|
|
*/ |
|
104
|
|
|
public function creationArgumentProvider() |
|
105
|
|
|
{ |
|
106
|
|
|
$item = new Item('test'); |
|
107
|
|
|
return [ |
|
108
|
|
|
['test', [], null, null, [$this->t('test')], [], null], |
|
109
|
|
|
[$this->t('test', 'a'), [], null, null, [$this->t('test', 'a')], [], null], |
|
110
|
|
|
[['test'], [], null, null, [$this->t('test')], [], null], |
|
111
|
|
|
[['test', 'lorem'], [], null, null, [$this->t('test'), $this->t('lorem')], [], null], |
|
112
|
|
|
[['test', '', 'lorem'], [], null, null, [$this->t('test'), $this->t('lorem')], [], null], |
|
113
|
|
|
[ |
|
114
|
|
|
'test', |
|
115
|
|
|
[$this->p('name1', 'value1')], |
|
116
|
|
|
null, |
|
117
|
|
|
null, |
|
118
|
|
|
[$this->t('test')], |
|
119
|
|
|
['name1' => [$this->s('value1')]], |
|
120
|
|
|
null |
|
121
|
|
|
], |
|
122
|
|
|
[ |
|
123
|
|
|
'test', |
|
124
|
|
|
[$this->p('name1', '')], |
|
125
|
|
|
null, |
|
126
|
|
|
null, |
|
127
|
|
|
[$this->t('test')], |
|
128
|
|
|
[], |
|
129
|
|
|
null |
|
130
|
|
|
], |
|
131
|
|
|
[ |
|
132
|
|
|
'test', |
|
133
|
|
|
[$this->p('name1', [])], |
|
134
|
|
|
null, |
|
135
|
|
|
null, |
|
136
|
|
|
[$this->t('test')], |
|
137
|
|
|
[], |
|
138
|
|
|
null |
|
139
|
|
|
], |
|
140
|
|
|
[ |
|
141
|
|
|
'test', |
|
142
|
|
|
[$this->p('name1', 'value1', 'profile1/')], |
|
143
|
|
|
null, |
|
144
|
|
|
null, |
|
145
|
|
|
[$this->t('test')], |
|
146
|
|
|
['profile1/name1' => [$this->s('value1')]], |
|
147
|
|
|
null |
|
148
|
|
|
], |
|
149
|
|
|
[ |
|
150
|
|
|
'test', |
|
151
|
|
|
[$this->p('name1', 'value1')], |
|
152
|
|
|
null, |
|
153
|
|
|
null, |
|
154
|
|
|
[$this->t('test')], |
|
155
|
|
|
['name1' => [$this->s('value1')]], |
|
156
|
|
|
null |
|
157
|
|
|
], |
|
158
|
|
|
[ |
|
159
|
|
|
'test', |
|
160
|
|
|
[$this->p('name1', 'value1'), $this->p('name1', 'value2')], |
|
161
|
|
|
null, |
|
162
|
|
|
null, |
|
163
|
|
|
[$this->t('test')], |
|
164
|
|
|
['name1' => [$this->s('value1'), $this->s('value2')]], |
|
165
|
|
|
null |
|
166
|
|
|
], |
|
167
|
|
|
[ |
|
168
|
|
|
'test', |
|
169
|
|
|
[$this->p('name1', 'value1'), $this->p('name2', 'value2')], |
|
170
|
|
|
null, |
|
171
|
|
|
null, |
|
172
|
|
|
[$this->t('test')], |
|
173
|
|
|
['name1' => [$this->s('value1')], 'name2' => [$this->s('value2')]], |
|
174
|
|
|
null |
|
175
|
|
|
], |
|
176
|
|
|
[ |
|
177
|
|
|
'test', |
|
178
|
|
|
[$this->p('name', [$item])], |
|
179
|
|
|
null, |
|
180
|
|
|
null, |
|
181
|
|
|
[$this->t('test')], |
|
182
|
|
|
['name' => [$item]], |
|
183
|
|
|
null |
|
184
|
|
|
], |
|
185
|
|
|
['test', [], 'id', null, [$this->t('test')], [], 'id'], |
|
186
|
|
|
['test', [], null, 'en', [$this->t('test')], [], null, 'en'], |
|
187
|
|
|
]; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Create a type object |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $n Type name |
|
194
|
|
|
* @param string $p Type profile |
|
195
|
|
|
* @return object Type object |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function t($n, $p = '') |
|
198
|
|
|
{ |
|
199
|
|
|
return new Iri($p, $n); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Create a property object |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $n Property name |
|
206
|
|
|
* @param mixed $s Property value(s) |
|
207
|
|
|
* @param string $p Property profile |
|
208
|
|
|
* @return \stdClass Property object |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function p($n, $s, $p = '') |
|
211
|
|
|
{ |
|
212
|
|
|
$values = array_map([$this, 's'], (array)$s); |
|
213
|
|
|
return (object)['profile' => $p, 'name' => $n, 'values' => $values]; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Create a string value |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $s Value |
|
220
|
|
|
* @return ValueInterface String value |
|
221
|
|
|
*/ |
|
222
|
|
|
protected function s($s) |
|
223
|
|
|
{ |
|
224
|
|
|
return ($s instanceof ValueInterface) ? $s : new StringValue($s); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Test the item creation with an empty types list |
|
229
|
|
|
* |
|
230
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
|
231
|
|
|
* @expectedExceptionCode 1490814631 |
|
232
|
|
|
*/ |
|
233
|
|
|
public function testEmptyTypesList() |
|
234
|
|
|
{ |
|
235
|
|
|
new Item(null); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Test the item creation with an empty types list |
|
240
|
|
|
* |
|
241
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
|
242
|
|
|
* @expectedExceptionCode 1488314667 |
|
243
|
|
|
*/ |
|
244
|
|
|
public function testEmptyTypeName() |
|
245
|
|
|
{ |
|
246
|
|
|
new Item(''); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Test the item creation with an empty property name |
|
251
|
|
|
* |
|
252
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
|
253
|
|
|
* @expectedExceptionCode 1488314921 |
|
254
|
|
|
*/ |
|
255
|
|
|
public function testEmptyPropertyName() |
|
256
|
|
|
{ |
|
257
|
|
|
new Item('type', [$this->p('', 'value')]); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Test empty property value list |
|
262
|
|
|
* |
|
263
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
|
264
|
|
|
* @expectedExceptionCode 1490814554 |
|
265
|
|
|
*/ |
|
266
|
|
|
public function testInvalidPropertyStructure() |
|
267
|
|
|
{ |
|
268
|
|
|
new Item('type', [(object)['invalid' => 'structure']]); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Test the item creation with an invalid property value |
|
273
|
|
|
* |
|
274
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
|
275
|
|
|
* @expectedExceptionCode 1488315339 |
|
276
|
|
|
*/ |
|
277
|
|
|
public function testInvalidPropertyValue() |
|
278
|
|
|
{ |
|
279
|
|
|
new Item('type', [(object)['profile' => '', 'name' => 'test', 'values' => [123]]]); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Test the item creation with an invalid property value |
|
284
|
|
|
* |
|
285
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException |
|
286
|
|
|
* @expectedExceptionCode 1488315604 |
|
287
|
|
|
*/ |
|
288
|
|
|
public function testUnknownPropertyName() |
|
289
|
|
|
{ |
|
290
|
|
|
$item = new Item('type'); |
|
291
|
|
|
$item->getProperty('name'); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Test the item property getter with an unprofiled property |
|
296
|
|
|
*/ |
|
297
|
|
|
public function testItemUnprofiledProperty() |
|
298
|
|
|
{ |
|
299
|
|
|
$item = new Item('type', [$this->p('name', 123)]); |
|
300
|
|
|
$this->assertEquals([new StringValue('123')], $item->getProperty('name')); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Test the item property getter with a profiled property |
|
305
|
|
|
*/ |
|
306
|
|
|
public function testItemProfiledProperty() |
|
307
|
|
|
{ |
|
308
|
|
|
$item = new Item('type', [$this->p('name', 123, 'profile')]); |
|
309
|
|
|
$value = [new StringValue('123')]; |
|
310
|
|
|
$this->assertEquals($value, $item->getProperty('name')); |
|
311
|
|
|
$this->assertEquals($value, $item->getProperty('name', 'profile')); |
|
312
|
|
|
$this->assertEquals($value, $item->getProperty((object)['name' => 'name', 'profile' => 'profile'])); |
|
313
|
|
|
$this->assertEquals($value, $item->getProperty(new Iri('profile', 'name'))); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
|