|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the php-utilities package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Asm\Tests\Data; |
|
11
|
|
|
|
|
12
|
|
|
use Asm\Data\Data; |
|
13
|
|
|
use Asm\Exception\InvalidParameterSetException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class DataTest |
|
17
|
|
|
* |
|
18
|
|
|
* @package Asm\Tests\Data |
|
19
|
|
|
* @author marc aschmann <[email protected]> |
|
20
|
|
|
* @uses Asm\Data\Data |
|
21
|
|
|
*/ |
|
22
|
|
|
class DataTest extends \PHPUnit_Framework_TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @return Data |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testConstruct() |
|
28
|
|
|
{ |
|
29
|
|
|
$data = new Data(); |
|
30
|
|
|
$this->assertInstanceOf('\Asm\Data\Data', $data); |
|
31
|
|
|
|
|
32
|
|
|
return $data; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* test basic set() |
|
37
|
|
|
* |
|
38
|
|
|
* @depends testConstruct |
|
39
|
|
|
* @covers \Asm\Data\Data::set |
|
40
|
|
|
* @covers \Asm\Data\Data::get |
|
41
|
|
|
* @param Data $data |
|
42
|
|
|
* @return Data |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testSet(Data $data) |
|
45
|
|
|
{ |
|
46
|
|
|
// test setters |
|
47
|
|
|
|
|
48
|
|
|
// test initial setting of value |
|
49
|
|
|
$data->set('test_key_1', 'test_value'); |
|
50
|
|
|
$this->assertEquals('test_value', $data->get('test_key_1'), 'ist value key1 equal to value'); |
|
51
|
|
|
|
|
52
|
|
|
// test overwrite |
|
53
|
|
|
$data->set('test_key_1', 'test_value_blah'); |
|
54
|
|
|
$this->assertEquals('test_value_blah', $data->get('test_key_1'), 'can key be overwritten'); |
|
55
|
|
|
|
|
56
|
|
|
$data->set('test_key_1', 'test_key_2', 'test_value3'); |
|
57
|
|
|
$this->assertEquals('test_value3', $data->get('test_key_1', 'test_key_2'), 'multidim key get'); |
|
58
|
|
|
|
|
59
|
|
|
// test initial setting of value |
|
60
|
|
|
$data->set('test_key_4', ['test_key_3' => 'testValue4']); |
|
61
|
|
|
$data->set('test_key_5', ['test_key_4' => 'testValue5']); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals('testValue4', $data->get('test_key_4', 'test_key_3'), 'merge arrays on set'); |
|
64
|
|
|
$this->assertEquals('testValue5', $data->get('test_key_5', 'test_key_4'), 'merge arrays on set'); |
|
65
|
|
|
|
|
66
|
|
|
return $data; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @depends testSet |
|
71
|
|
|
* @expectedException \Asm\Exception\InvalidParameterSetException |
|
72
|
|
|
* @param Data $data |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testSetException(Data $data) |
|
75
|
|
|
{ |
|
76
|
|
|
$data->set(); |
|
77
|
|
|
$data->set('blah'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* test basic get |
|
82
|
|
|
* |
|
83
|
|
|
* @depends testSet |
|
84
|
|
|
* @covers \Asm\Data\Data::get |
|
85
|
|
|
* @covers \Asm\Data\Data::searchArray |
|
86
|
|
|
* @param Data $data |
|
87
|
|
|
* @return Data |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testGet(Data $data) |
|
90
|
|
|
{ |
|
91
|
|
|
// single value get |
|
92
|
|
|
$data->set('testKey1', 'test_value'); |
|
93
|
|
|
$this->assertEquals('test_value', $data->get('testKey1')); |
|
94
|
|
|
|
|
95
|
|
|
$data->set( |
|
96
|
|
|
'testKey2', |
|
97
|
|
|
[ |
|
98
|
|
|
'test' => 'test_value', |
|
99
|
|
|
'sub_test' => [ |
|
100
|
|
|
'subsubtest' => 'subvalue', |
|
101
|
|
|
] |
|
102
|
|
|
] |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
// single dimension array getter |
|
106
|
|
|
$mixResult = $data->get('testKey2'); |
|
107
|
|
|
$this->assertTrue(is_array($mixResult)); |
|
108
|
|
|
$this->assertArrayHasKey('test', $mixResult); |
|
109
|
|
|
|
|
110
|
|
|
// multi dimension array single value getter |
|
111
|
|
|
$mixResult = $data->get('testKey2', 'test'); |
|
112
|
|
|
$this->assertEquals('test_value', $mixResult); |
|
113
|
|
|
|
|
114
|
|
|
// multi dimension array single value getter |
|
115
|
|
|
$mixResult = $data->get('testKey2', 'sub_test', 'subsubtest'); |
|
116
|
|
|
$this->assertEquals('subvalue', $mixResult); |
|
117
|
|
|
|
|
118
|
|
|
// non existent keys |
|
119
|
|
|
$this->assertFalse($data->get('nothing')); |
|
120
|
|
|
$this->assertFalse($data->get('testKey2', 'nothing')); |
|
121
|
|
|
$this->assertFalse($data->get('testKey2', 'test', 'nothing')); |
|
122
|
|
|
$this->assertFalse($data->get('testKey2', 'sub_test', 'nothing')); |
|
123
|
|
|
$this->assertTrue(is_array($data->get('testKey2', 'sub_test', ''))); |
|
124
|
|
|
|
|
125
|
|
|
return $data; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @depends testSet |
|
130
|
|
|
* @covers \Asm\Data\Data::setByArray |
|
131
|
|
|
* @param Data $data |
|
132
|
|
|
* @return Data |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testSetByArray(Data $data) |
|
135
|
|
|
{ |
|
136
|
|
|
$data->setByArray([]); |
|
137
|
|
|
$this->assertTrue(empty($data->toArray())); |
|
138
|
|
|
|
|
139
|
|
|
$data->setByArray( |
|
140
|
|
|
[ |
|
141
|
|
|
'testKey3' => 'test_value_3', |
|
142
|
|
|
'testKey4' => 'test_value_4', |
|
143
|
|
|
'testKey5' => ['subKey1' => 'sub_val_1'], |
|
144
|
|
|
'testKey6' => new \stdClass(), |
|
145
|
|
|
] |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals('test_value_3', $data->get('testKey3')); |
|
149
|
|
|
$this->assertEquals('test_value_4', $data->get('testKey4')); |
|
150
|
|
|
$this->assertArrayHasKey('subKey1', $data->get('testKey5')); |
|
151
|
|
|
$this->assertInstanceOf('stdClass', $data->get('testKey6')); |
|
152
|
|
|
|
|
153
|
|
|
return $data; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @depends testConstruct |
|
158
|
|
|
* @covers \Asm\Data\Data::setByObject |
|
159
|
|
|
* @param Data $data |
|
160
|
|
|
* @return Data |
|
161
|
|
|
*/ |
|
162
|
|
|
public function testSetByObject(Data $data) |
|
163
|
|
|
{ |
|
164
|
|
|
$objParam = new \stdClass(); |
|
165
|
|
|
$objParam->testProperty1 = 'test_property_value_1'; |
|
166
|
|
|
$objParam->testProperty2 = 'test_property_value_2'; |
|
167
|
|
|
$objParam->testProperty3 = ['subkeyPropertyTest' => 'property_value']; |
|
168
|
|
|
$objParam->testProperty4 = new \stdClass(); |
|
169
|
|
|
|
|
170
|
|
|
$data->setByObject($objParam); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertEquals('test_property_value_1', $data->get('testProperty1')); |
|
173
|
|
|
$this->assertEquals('test_property_value_2', $data->get('testProperty2')); |
|
174
|
|
|
$this->assertArrayHasKey('subkeyPropertyTest', $data->get('testProperty3')); |
|
175
|
|
|
$this->assertInstanceOf('stdClass', $data->get('testProperty4')); |
|
176
|
|
|
|
|
177
|
|
|
$fromData = new Data(); |
|
178
|
|
|
$fromData->setByObject($data); |
|
179
|
|
|
|
|
180
|
|
|
$this->assertEquals('test_property_value_1', $fromData->get('testProperty1')); |
|
181
|
|
|
$this->assertEquals('test_property_value_2', $fromData->get('testProperty2')); |
|
182
|
|
|
$this->assertArrayHasKey('subkeyPropertyTest', $fromData->get('testProperty3')); |
|
183
|
|
|
$this->assertInstanceOf('stdClass', $fromData->get('testProperty4')); |
|
184
|
|
|
|
|
185
|
|
|
return $data; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @depends testConstruct |
|
190
|
|
|
* @expectedException \Asm\Exception\InvalidParameterException |
|
191
|
|
|
* @param Data $data |
|
192
|
|
|
*/ |
|
193
|
|
|
public function testSetByObjectException(Data $data) |
|
194
|
|
|
{ |
|
195
|
|
|
$data->setByObject(null); |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @depends testConstruct |
|
200
|
|
|
* @covers \Asm\Data\Data::setByJson |
|
201
|
|
|
* @param Data $data |
|
202
|
|
|
* @return Data |
|
203
|
|
|
*/ |
|
204
|
|
|
public function testSetByJson(Data $data) |
|
205
|
|
|
{ |
|
206
|
|
|
$data->setByJson( |
|
207
|
|
|
json_encode( |
|
208
|
|
|
[ |
|
209
|
|
|
'testKey3' => 'test_value_3', |
|
210
|
|
|
'testKey4' => 'test_value_4', |
|
211
|
|
|
'testKey5' => ['subKey1' => 'sub_val_1'], |
|
212
|
|
|
] |
|
213
|
|
|
) |
|
214
|
|
|
); |
|
215
|
|
|
|
|
216
|
|
|
$this->assertEquals('test_value_3', $data->get('testKey3')); |
|
217
|
|
|
$this->assertEquals('test_value_4', $data->get('testKey4')); |
|
218
|
|
|
$this->assertArrayHasKey('subKey1', $data->get('testKey5')); |
|
219
|
|
|
|
|
220
|
|
|
return $data; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @depends testSetByArray |
|
225
|
|
|
* @covers \Asm\Data\Data::getKeys |
|
226
|
|
|
* @param Data $data |
|
227
|
|
|
*/ |
|
228
|
|
|
public function testGetKeys(Data $data) |
|
229
|
|
|
{ |
|
230
|
|
|
$temp = $data->getKeys(); |
|
231
|
|
|
$this->assertNotEmpty($temp, "empty array"); |
|
232
|
|
|
$this->assertNotEmpty($temp[0]); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @depends testSetByArray |
|
237
|
|
|
* @covers \Asm\Data\Data::remove |
|
238
|
|
|
* @param Data $data |
|
239
|
|
|
*/ |
|
240
|
|
|
public function testRemove(Data $data) |
|
241
|
|
|
{ |
|
242
|
|
|
$data->set('removal_test', 'xyz'); |
|
243
|
|
|
$this->assertEquals('xyz', $data->get('removal_test')); |
|
244
|
|
|
$data->remove('removal_test'); |
|
245
|
|
|
$this->assertFalse($data->get('removal_test')); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @depends testSetByArray |
|
250
|
|
|
* @covers \Asm\Data\Data::toArray |
|
251
|
|
|
* @param Data $data |
|
252
|
|
|
*/ |
|
253
|
|
|
public function testToArray(Data $data) |
|
254
|
|
|
{ |
|
255
|
|
|
$temp = $data->toArray(); |
|
256
|
|
|
$this->assertArrayHasKey('testKey5', $temp); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @depends testSetByArray |
|
261
|
|
|
* @covers \Asm\Data\Data::toJson |
|
262
|
|
|
* @param Data $data |
|
263
|
|
|
*/ |
|
264
|
|
|
public function testToJson(Data $data) |
|
265
|
|
|
{ |
|
266
|
|
|
$temp = json_decode($data->toJson()); |
|
267
|
|
|
|
|
268
|
|
|
$this->assertNotFalse($temp); |
|
269
|
|
|
$this->assertNotEmpty($temp->testKey5); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @depends testSetByArray |
|
274
|
|
|
* @covers \Asm\Data\Data::count |
|
275
|
|
|
* @param Data $data |
|
276
|
|
|
*/ |
|
277
|
|
|
public function testCount(Data $data) |
|
278
|
|
|
{ |
|
279
|
|
|
$temp = $data->toArray(); |
|
280
|
|
|
$this->assertEquals(count($temp), $data->count()); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @depends testSetByArray |
|
285
|
|
|
* @covers \Asm\Data\Data::findInArray |
|
286
|
|
|
* @covers \Asm\Data\Data::searchArray |
|
287
|
|
|
* @param Data $data |
|
288
|
|
|
*/ |
|
289
|
|
|
public function testFindInArray(Data $data) |
|
290
|
|
|
{ |
|
291
|
|
|
$temp = $data->toArray(); |
|
292
|
|
|
|
|
293
|
|
|
$this->assertNotFalse(Data::findInArray($temp, 'testKey5')); |
|
294
|
|
|
$this->assertNotFalse(Data::findInArray($temp, 'testProperty3', 'subkeyPropertyTest')); |
|
295
|
|
|
$this->assertFalse(Data::findInArray($temp, 'testKey35', false)); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @covers \Asm\Data\Data::normalize |
|
300
|
|
|
*/ |
|
301
|
|
|
public function testNormalize() |
|
302
|
|
|
{ |
|
303
|
|
|
$key = Data::normalize('this_is_my_key'); |
|
304
|
|
|
$this->assertEquals('thisIsMyKey', $key); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @depends testSetByArray |
|
309
|
|
|
* @covers \Asm\Data\Data::setByArray |
|
310
|
|
|
* @covers \Asm\Data\Data::clear |
|
311
|
|
|
* @covers \Asm\Data\Data::toArray |
|
312
|
|
|
* @param Data $data |
|
313
|
|
|
*/ |
|
314
|
|
|
public function testClear(Data $data) |
|
315
|
|
|
{ |
|
316
|
|
|
$data->clear(); |
|
317
|
|
|
$this->assertArrayNotHasKey('testKey3', $data->toArray()); |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
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: