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\Item; |
41
|
|
|
use Jkphl\Micrometa\Domain\Value\ValueInterface; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Item tests |
45
|
|
|
* |
46
|
|
|
* @package Jkphl\Micrometa |
47
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
48
|
|
|
*/ |
49
|
|
|
class ItemTest extends \PHPUnit_Framework_TestCase |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Public function test the item creation |
53
|
|
|
* |
54
|
|
|
* @param string|array $type Item type(s) |
55
|
|
|
* @param array $properties Item properties |
56
|
|
|
* @param $itemId Item id |
57
|
|
|
* @param array $expectedTypes Expected item types |
58
|
|
|
* @param array $expectedProperties Expected item properties |
59
|
|
|
* @param string $expectedId Expected item id |
60
|
|
|
* @dataProvider creationArgumentProvider |
61
|
|
|
*/ |
62
|
|
|
public function testItemCreation( |
63
|
|
|
$type, |
64
|
|
|
array $properties, |
65
|
|
|
$itemId, |
66
|
|
|
array $expectedTypes, |
67
|
|
|
array $expectedProperties, |
68
|
|
|
$expectedId |
69
|
|
|
) { |
70
|
|
|
$item = new Item($type, $properties, $itemId); |
|
|
|
|
71
|
|
|
$this->assertInstanceOf(Item::class, $item); |
72
|
|
|
$this->assertEquals($expectedTypes, $item->getType()); |
73
|
|
|
$this->assertEquals($expectedProperties, $item->getProperties()->toArray()); |
74
|
|
|
$this->assertEquals($expectedId, $item->getId()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Data provider for item creation tests |
79
|
|
|
* |
80
|
|
|
* @return array Item creation arguments |
81
|
|
|
*/ |
82
|
|
|
public function creationArgumentProvider() |
83
|
|
|
{ |
84
|
|
|
$item = new Item('test'); |
85
|
|
|
return [ |
86
|
|
|
['test', [], null, [$this->t('test')], [], null], |
87
|
|
|
[$this->t('test', 'a'), [], null, [$this->t('test', 'a')], [], null], |
88
|
|
|
[['test'], [], null, [$this->t('test')], [], null], |
89
|
|
|
[['test', 'lorem'], [], null, [$this->t('test'), $this->t('lorem')], [], null], |
90
|
|
|
[['test', '', 'lorem'], [], null, [$this->t('test'), $this->t('lorem')], [], null], |
91
|
|
|
[ |
92
|
|
|
'test', |
93
|
|
|
[$this->p('name1', 'value1')], |
94
|
|
|
null, |
95
|
|
|
[$this->t('test')], |
96
|
|
|
['name1' => [$this->s('value1')]], |
97
|
|
|
null |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
'test', |
101
|
|
|
[$this->p('name1', '')], |
102
|
|
|
null, |
103
|
|
|
[$this->t('test')], |
104
|
|
|
[], |
105
|
|
|
null |
106
|
|
|
], |
107
|
|
|
[ |
108
|
|
|
'test', |
109
|
|
|
[$this->p('name1', [])], |
110
|
|
|
null, |
111
|
|
|
[$this->t('test')], |
112
|
|
|
[], |
113
|
|
|
null |
114
|
|
|
], |
115
|
|
|
[ |
116
|
|
|
'test', |
117
|
|
|
[$this->p('name1', 'value1', 'profile1/')], |
118
|
|
|
null, |
119
|
|
|
[$this->t('test')], |
120
|
|
|
['profile1/name1' => [$this->s('value1')]], |
121
|
|
|
null |
122
|
|
|
], |
123
|
|
|
[ |
124
|
|
|
'test', |
125
|
|
|
[$this->p('name1', 'value1')], |
126
|
|
|
null, |
127
|
|
|
[$this->t('test')], |
128
|
|
|
['name1' => [$this->s('value1')]], |
129
|
|
|
null |
130
|
|
|
], |
131
|
|
|
[ |
132
|
|
|
'test', |
133
|
|
|
[$this->p('name1', 'value1'), $this->p('name1', 'value2')], |
134
|
|
|
null, |
135
|
|
|
[$this->t('test')], |
136
|
|
|
['name1' => [$this->s('value1'), $this->s('value2')]], |
137
|
|
|
null |
138
|
|
|
], |
139
|
|
|
[ |
140
|
|
|
'test', |
141
|
|
|
[$this->p('name1', 'value1'), $this->p('name2', 'value2')], |
142
|
|
|
null, |
143
|
|
|
[$this->t('test')], |
144
|
|
|
['name1' => [$this->s('value1')], 'name2' => [$this->s('value2')]], |
145
|
|
|
null |
146
|
|
|
], |
147
|
|
|
[ |
148
|
|
|
'test', |
149
|
|
|
[$this->p('name', [$item])], |
150
|
|
|
null, |
151
|
|
|
[$this->t('test')], |
152
|
|
|
['name' => [$item]], |
153
|
|
|
null |
154
|
|
|
], |
155
|
|
|
['test', [], 'id', [$this->t('test')], [], 'id'], |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Create a type object |
161
|
|
|
* |
162
|
|
|
* @param string $n Type name |
163
|
|
|
* @param string $p Type profile |
164
|
|
|
* @return object Type object |
165
|
|
|
*/ |
166
|
|
|
protected function t($n, $p = '') |
167
|
|
|
{ |
168
|
|
|
return (object)['profile' => $p, 'name' => $n]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Create a property object |
173
|
|
|
* |
174
|
|
|
* @param string $n Property name |
175
|
|
|
* @param mixed $s Property value(s) |
176
|
|
|
* @param string $p Property profiles |
177
|
|
|
* @return object Property object |
178
|
|
|
*/ |
179
|
|
|
protected function p($n, $s, $p = '') |
180
|
|
|
{ |
181
|
|
|
$values = array_map([$this, 's'], (array)$s); |
182
|
|
|
return (object)['profile' => $p, 'name' => $n, 'values' => $values]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Create a string value |
187
|
|
|
* |
188
|
|
|
* @param string $s Value |
189
|
|
|
* @return ValueInterface String value |
190
|
|
|
*/ |
191
|
|
|
protected function s($s) |
192
|
|
|
{ |
193
|
|
|
return ($s instanceof ValueInterface) ? $s : new StringValue($s); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Test the item creation with an empty types list |
198
|
|
|
* |
199
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
200
|
|
|
* @expectedExceptionCode 1490814631 |
201
|
|
|
*/ |
202
|
|
|
public function testEmptyTypesList() |
203
|
|
|
{ |
204
|
|
|
new Item(null); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Test the item creation with an empty types list |
209
|
|
|
* |
210
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
211
|
|
|
* @expectedExceptionCode 1488314667 |
212
|
|
|
*/ |
213
|
|
|
public function testEmptyTypeName() |
214
|
|
|
{ |
215
|
|
|
new Item(''); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Test the item creation with an empty property name |
220
|
|
|
* |
221
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
222
|
|
|
* @expectedExceptionCode 1488314921 |
223
|
|
|
*/ |
224
|
|
|
public function testEmptyPropertyName() |
225
|
|
|
{ |
226
|
|
|
new Item('type', [$this->p('', 'value')]); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Test empty property value list |
231
|
|
|
* |
232
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
233
|
|
|
* @expectedExceptionCode 1490814554 |
234
|
|
|
*/ |
235
|
|
|
public function testInvalidPropertyStructure() |
236
|
|
|
{ |
237
|
|
|
new Item('type', [(object)['invalid' => 'structure']]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Test the item creation with an invalid property value |
242
|
|
|
* |
243
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
244
|
|
|
* @expectedExceptionCode 1488315339 |
245
|
|
|
*/ |
246
|
|
|
public function testInvalidPropertyValue() |
247
|
|
|
{ |
248
|
|
|
new Item('type', [(object)['profile' => '', 'name' => 'test', 'values' => [123]]]); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Test the item creation with an invalid property value |
253
|
|
|
* |
254
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException |
255
|
|
|
* @expectedExceptionCode 1488315604 |
256
|
|
|
*/ |
257
|
|
|
public function testUnknownPropertyName() |
258
|
|
|
{ |
259
|
|
|
$item = new Item('type'); |
260
|
|
|
$item->getProperty('name'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Test the item property getter |
265
|
|
|
*/ |
266
|
|
|
public function testItemPropertyGetter() |
267
|
|
|
{ |
268
|
|
|
$item = new Item('type', [$this->p('name', 123)]); |
|
|
|
|
269
|
|
|
$this->assertEquals([new StringValue('123')], $item->getProperty('name')); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.