1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014-2016 Ryan Parman. |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* http://opensource.org/licenses/MIT |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Skyzyx\Tests\StrongTypes; |
27
|
|
|
|
28
|
|
|
use Skyzyx\StrongTypes\BooleanType; |
29
|
|
|
use Skyzyx\StrongTypes\Collection; |
30
|
|
|
use Skyzyx\StrongTypes\FloatType; |
31
|
|
|
use Skyzyx\StrongTypes\IntegerType; |
32
|
|
|
use Skyzyx\StrongTypes\StringType; |
33
|
|
|
|
34
|
|
|
class CollectionTest extends \PHPUnit_Framework_TestCase |
35
|
|
|
{ |
36
|
|
|
public $type; |
37
|
|
|
|
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
$this->type = new Collection([ |
41
|
|
|
'abc' => 'xyz', |
42
|
|
|
'def' => 123, |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testCollectionType() |
47
|
|
|
{ |
48
|
|
|
$this->assertEquals('Skyzyx\StrongTypes\Collection', get_class($this->type)); |
49
|
|
|
$this->assertEquals([ |
50
|
|
|
'abc' => 'xyz', |
51
|
|
|
'def' => 123, |
52
|
|
|
], $this->type->getValue()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCollectionCount() |
56
|
|
|
{ |
57
|
|
|
$this->assertEquals(2, count($this->type)); |
58
|
|
|
$this->assertEquals(2, $this->type->count()); |
59
|
|
|
$this->assertEquals(2, $this->type->length()); |
60
|
|
|
$this->assertEquals(2, $this->type->size()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testCollectionAppend() |
64
|
|
|
{ |
65
|
|
|
$this->type['xyz'] = 123; |
66
|
|
|
$this->type->set('uvw', 456); |
67
|
|
|
|
68
|
|
|
$this->assertEquals(123, $this->type['xyz']); |
69
|
|
|
$this->assertEquals(456, $this->type->get('uvw')); |
70
|
|
|
$this->assertEquals(4, count($this->type)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testCollectionGetInvalid() |
74
|
|
|
{ |
75
|
|
|
$this->assertEquals(null, $this->type->get('aaa')); |
76
|
|
|
$this->assertEquals(null, $this->type['aaa']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testCollectionRemove() |
80
|
|
|
{ |
81
|
|
|
unset($this->type['abc']); |
82
|
|
|
$this->type->remove('def'); |
83
|
|
|
|
84
|
|
|
$this->assertEquals(0, count($this->type)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testCollectionSeek() |
88
|
|
|
{ |
89
|
|
|
$set = []; |
90
|
|
|
|
91
|
|
|
$this->type->rewind(); |
92
|
|
|
while ($this->type->valid()) { |
93
|
|
|
$set[$this->type->key()] = $this->type->current(); |
94
|
|
|
$this->type->next(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->assertEquals([ |
98
|
|
|
'abc' => 'xyz', |
99
|
|
|
'def' => 123, |
100
|
|
|
], $set); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testCollectionToString() |
104
|
|
|
{ |
105
|
|
|
$this->assertEquals(json_encode([ |
106
|
|
|
'abc' => 'xyz', |
107
|
|
|
'def' => 123, |
108
|
|
|
]), |
109
|
|
|
(string) $this->type); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testCollectionDeepArray() |
113
|
|
|
{ |
114
|
|
|
$collection = new Collection([ |
115
|
|
|
'abc' => 'xyz', |
116
|
|
|
'def' => 123, |
117
|
|
|
'key' => [ |
118
|
|
|
'zyx' => 'aaa', |
119
|
|
|
'wvut' => 'bbb', |
120
|
|
|
'srqp' => 'ccc', |
121
|
|
|
], |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
$this->assertEquals(json_encode([ |
125
|
|
|
'abc' => 'xyz', |
126
|
|
|
'def' => 123, |
127
|
|
|
'key' => [ |
128
|
|
|
'zyx' => 'aaa', |
129
|
|
|
'wvut' => 'bbb', |
130
|
|
|
'srqp' => 'ccc', |
131
|
|
|
], |
132
|
|
|
]), |
133
|
|
|
(string) $collection); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testCollectionDeepArrayFail() |
137
|
|
|
{ |
138
|
|
|
$collection = new Collection([ |
139
|
|
|
'abc' => 'xyz', |
140
|
|
|
'def' => 123, |
141
|
|
|
'key' => [ |
142
|
|
|
'zyx' => 'aaa', |
143
|
|
|
'wvut' => 'bbb', |
144
|
|
|
'srqp' => 'ccc', |
145
|
|
|
], |
146
|
|
|
], true); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$this->assertNotEquals(json_encode([ |
149
|
|
|
'abc' => 'xyz', |
150
|
|
|
'def' => 123, |
151
|
|
|
'key' => [], |
152
|
|
|
]), |
153
|
|
|
(string) $collection); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testCollectionExists() |
157
|
|
|
{ |
158
|
|
|
$this->assertEquals(true, isset($this->type['abc'])); |
159
|
|
|
$this->assertEquals(true, $this->type->exists('abc')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testCollectionSeekable() |
163
|
|
|
{ |
164
|
|
|
$this->assertEquals('xyz', $this->type->seek(0)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testCollectionForeach() |
168
|
|
|
{ |
169
|
|
|
$set = []; |
170
|
|
|
|
171
|
|
|
foreach ($this->type->getIterator() as $k => $v) { |
|
|
|
|
172
|
|
|
$set[$k] = $v; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->assertEquals([ |
176
|
|
|
'abc' => 'xyz', |
177
|
|
|
'def' => 123, |
178
|
|
|
], $set); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testCollectionValidation() |
182
|
|
|
{ |
183
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
184
|
|
|
new TestCollection([ |
185
|
|
|
'key1' => new StringType('abc'), |
186
|
|
|
'key2' => new IntegerType(123), |
187
|
|
|
'key3' => new FloatType(123.456), |
188
|
|
|
'key4' => new BooleanType(true), |
189
|
|
|
'key5' => new TestEnum(), |
190
|
|
|
'key6' => new Collection([]), |
191
|
|
|
]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testCollectionValidationStringException() |
195
|
|
|
{ |
196
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
197
|
|
|
new TestCollection([ |
198
|
|
|
'key1' => 'abc', |
199
|
|
|
'key2' => new IntegerType(123), |
200
|
|
|
'key3' => new FloatType(123.456), |
201
|
|
|
'key4' => new BooleanType(true), |
202
|
|
|
'key5' => new TestEnum(), |
203
|
|
|
'key6' => new Collection([]), |
204
|
|
|
]); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testCollectionValidationIntegerException() |
208
|
|
|
{ |
209
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
210
|
|
|
new TestCollection([ |
211
|
|
|
'key1' => new StringType('abc'), |
212
|
|
|
'key2' => 123, |
213
|
|
|
'key3' => new FloatType(123.456), |
214
|
|
|
'key4' => new BooleanType(true), |
215
|
|
|
'key5' => new TestEnum(), |
216
|
|
|
'key6' => new Collection([]), |
217
|
|
|
]); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @expectedException InvalidArgumentException |
222
|
|
|
* @expectedExceptionMessage The Skyzyx\Tests\StrongTypes\TestCollection shape expects the key2 key |
223
|
|
|
* to be of type Skyzyx\StrongTypes\Integer. |
224
|
|
|
*/ |
225
|
|
|
public function testCollectionValidationIntegerException2() |
226
|
|
|
{ |
227
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
228
|
|
|
new TestCollection([ |
229
|
|
|
'key1' => new StringType('abc'), |
230
|
|
|
'key2' => 123.45, |
231
|
|
|
'key3' => new FloatType(123.456), |
232
|
|
|
'key4' => new BooleanType(true), |
233
|
|
|
'key5' => new TestEnum(), |
234
|
|
|
'key6' => new Collection([]), |
235
|
|
|
]); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @expectedException DomainException |
240
|
|
|
* @expectedExceptionMessage The Skyzyx\Tests\StrongTypes\TestCollection collection is missing one or |
241
|
|
|
* more required keys: key1, key2. |
242
|
|
|
*/ |
243
|
|
|
public function testCollectionValidationMissingRequiredException() |
244
|
|
|
{ |
245
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
246
|
|
|
new TestCollection([ |
247
|
|
|
'key3' => new FloatType(123.456), |
248
|
|
|
'key4' => new BooleanType(true), |
249
|
|
|
'key5' => new TestEnum(), |
250
|
|
|
'key6' => new Collection([]), |
251
|
|
|
]); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function testCollectionValidationDisallowedException() |
255
|
|
|
{ |
256
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
257
|
|
|
new TestCollection([ |
258
|
|
|
'key1' => new StringType('abc'), |
259
|
|
|
'key2' => new IntegerType(123), |
260
|
|
|
'key3' => new FloatType(123.456), |
261
|
|
|
'key4' => new BooleanType(true), |
262
|
|
|
'key5' => new TestEnum(), |
263
|
|
|
'key6' => new Collection([]), |
264
|
|
|
'key7' => null, |
265
|
|
|
]); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @expectedException OutOfBoundsException |
270
|
|
|
* @expectedExceptionMessage Seek position 99 is out of range |
271
|
|
|
*/ |
272
|
|
|
public function testCollectionSeekableException() |
273
|
|
|
{ |
274
|
|
|
$this->assertEquals('fail', $this->type->seek(99)); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @expectedException UnexpectedValueException |
279
|
|
|
* @expectedExceptionMessage The Skyzyx\StrongTypes\Collection class expects a value of type array. |
280
|
|
|
* Received a value of type string instead. |
281
|
|
|
*/ |
282
|
|
|
public function testCollectionException() |
283
|
|
|
{ |
284
|
|
|
$this->assertEquals('', ''); // Shut-up, test runner |
285
|
|
|
new Collection('abc'); |
|
|
|
|
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.