1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* micrometa |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Micrometa |
8
|
|
|
* @subpackage Jkphl\Micrometa\Tests\Domain |
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\Domain\Item\Item; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Item tests |
43
|
|
|
* |
44
|
|
|
* @package Jkphl\Micrometa |
45
|
|
|
* @subpackage Jkphl\Micrometa\Tests |
46
|
|
|
*/ |
47
|
|
|
class ItemTest extends \PHPUnit_Framework_TestCase |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Public function test the item creation |
51
|
|
|
* |
52
|
|
|
* @param string|array $type Item type(s) |
53
|
|
|
* @param array $properties Item properties |
54
|
|
|
* @param $itemId Item id |
55
|
|
|
* @param array $expectedTypes Expected item types |
56
|
|
|
* @param array $expectedProperties Expected item properties |
57
|
|
|
* @param string $expectedId Expected item id |
58
|
|
|
* @dataProvider creationArgumentProvider |
59
|
|
|
*/ |
60
|
|
|
public function testItemCreation( |
61
|
|
|
$type, |
62
|
|
|
array $properties, |
63
|
|
|
$itemId, |
64
|
|
|
array $expectedTypes, |
65
|
|
|
array $expectedProperties, |
66
|
|
|
$expectedId |
67
|
|
|
) { |
68
|
|
|
$item = new Item($type, $properties, $itemId); |
69
|
|
|
$this->assertInstanceOf(Item::class, $item); |
70
|
|
|
$this->assertEquals($expectedTypes, $item->getType()); |
71
|
|
|
$this->assertEquals($expectedProperties, $item->getProperties()); |
72
|
|
|
$this->assertEquals($expectedId, $item->getId()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Data provider for item creation tests |
77
|
|
|
* |
78
|
|
|
* @return array Item creation arguments |
79
|
|
|
*/ |
80
|
|
|
public function creationArgumentProvider() |
81
|
|
|
{ |
82
|
|
|
$item = new Item('test'); |
83
|
|
|
return [ |
84
|
|
|
['test', [], null, ['test'], [], null], |
85
|
|
|
[['test'], [], null, ['test'], [], null], |
86
|
|
|
[['test', 'lorem'], [], null, ['test', 'lorem'], [], null], |
87
|
|
|
[['test', '', 'lorem'], [], null, ['test', 'lorem'], [], null], |
88
|
|
|
['test', ['name1' => 'value1'], null, ['test'], ['name1' => ['value1']], null], |
89
|
|
|
['test', ['name1' => ['value1']], null, ['test'], ['name1' => ['value1']], null], |
90
|
|
|
['test', ['name1' => ['value1', 'value2']], null, ['test'], ['name1' => ['value1', 'value2']], null], |
91
|
|
|
['test', ['name1' => ['value1', '', 'value2']], null, ['test'], ['name1' => ['value1', 'value2']], null], |
92
|
|
|
[ |
93
|
|
|
'test', |
94
|
|
|
['name1' => 'value1', 'name2' => ['value2']], |
95
|
|
|
null, |
96
|
|
|
['test'], |
97
|
|
|
['name1' => ['value1'], 'name2' => ['value2']], |
98
|
|
|
null |
99
|
|
|
], |
100
|
|
|
['test', ['name' => $item], null, ['test'], ['name' => [$item]], null], |
101
|
|
|
['test', [], 'id', ['test'], [], 'id'], |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test the item creation with an empty types list |
107
|
|
|
* |
108
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
109
|
|
|
* @expectedExceptionCode 1488314667 |
110
|
|
|
*/ |
111
|
|
|
public function testEmptyTypesList() |
112
|
|
|
{ |
113
|
|
|
new Item(null); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Test the item creation with an empty property name |
118
|
|
|
* |
119
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
120
|
|
|
* @expectedExceptionCode 1488314921 |
121
|
|
|
*/ |
122
|
|
|
public function testEmptyPropertyName() |
123
|
|
|
{ |
124
|
|
|
new Item('type', ['' => 'value']); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Test the item creation with an invalid property value |
129
|
|
|
* |
130
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException |
131
|
|
|
* @expectedExceptionCode 1488315339 |
132
|
|
|
*/ |
133
|
|
|
public function testInvalidPropertyValue() |
134
|
|
|
{ |
135
|
|
|
new Item('type', ['name' => 123]); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Test the item creation with an invalid property value |
140
|
|
|
* |
141
|
|
|
* @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException |
142
|
|
|
* @expectedExceptionCode 1488315604 |
143
|
|
|
*/ |
144
|
|
|
public function testUnknownPropertyName() |
145
|
|
|
{ |
146
|
|
|
$item = new Item('type'); |
147
|
|
|
$item->getProperty('name'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Test the item property getter |
152
|
|
|
*/ |
153
|
|
|
public function testItemPropertyGetter() |
154
|
|
|
{ |
155
|
|
|
$item = new Item('type', ['name' => '123']); |
|
|
|
|
156
|
|
|
$this->assertEquals(['123'], $item->getProperty('name')); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: