|
1
|
|
|
<?php |
|
2
|
|
|
namespace Chadicus\Marvel\Api; |
|
3
|
|
|
|
|
4
|
|
|
use Chadicus\Marvel\Api\Assets\CollectionAdapter; |
|
5
|
|
|
use Chadicus\Marvel\Api\Assets\EmptyAdapter; |
|
6
|
|
|
use Chadicus\Marvel\Api\Assets\SingleAdapter; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Unit tests for the Collection class. |
|
10
|
|
|
* |
|
11
|
|
|
* @coversDefaultClass \Chadicus\Marvel\Api\Collection |
|
12
|
|
|
* @covers ::<private> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class CollectionTest extends \PHPUnit_Framework_TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Set up for all tests. |
|
18
|
|
|
* |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
|
|
public function setUp() |
|
22
|
|
|
{ |
|
23
|
|
|
\Chadicus\FunctionRegistry::reset(__NAMESPACE__, ['date']); |
|
24
|
|
|
\Chadicus\FunctionRegistry::set( |
|
25
|
|
|
__NAMESPACE__, |
|
26
|
|
|
'time', |
|
27
|
|
|
function () { |
|
28
|
|
|
return 1; |
|
29
|
|
|
} |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Tear down for all tests. |
|
35
|
|
|
* |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function tearDown() |
|
39
|
|
|
{ |
|
40
|
|
|
\Chadicus\FunctionRegistry::reset(__NAMESPACE__, ['date']); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Verifies basic usage of the collection. |
|
45
|
|
|
* |
|
46
|
|
|
* @test |
|
47
|
|
|
* @covers ::__construct |
|
48
|
|
|
* @covers ::rewind |
|
49
|
|
|
* @covers ::valid |
|
50
|
|
|
* @covers ::key |
|
51
|
|
|
* @covers ::current |
|
52
|
|
|
* @covers ::next |
|
53
|
|
|
* @covers ::count |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function directUsage() |
|
58
|
|
|
{ |
|
59
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
60
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
61
|
|
|
$collection->rewind(); |
|
62
|
|
|
$iterations = 0; |
|
63
|
|
|
while ($collection->valid()) { |
|
64
|
|
|
$key = $collection->key(); |
|
65
|
|
|
$this->assertSame($key, $collection->current()->id); |
|
66
|
|
|
$this->assertSame("a title for comic {$key}", $collection->current()->title); |
|
67
|
|
|
$collection->next(); |
|
68
|
|
|
++$iterations; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame($collection->count(), $iterations); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Verifies code does not explode when rewind() consectutively. |
|
76
|
|
|
* |
|
77
|
|
|
* @test |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function consecutiveRewind() |
|
82
|
|
|
{ |
|
83
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
84
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
85
|
|
|
$collection->rewind(); |
|
86
|
|
|
$collection->rewind(); |
|
87
|
|
|
$iterations = 0; |
|
88
|
|
|
foreach ($collection as $key => $actual) { |
|
89
|
|
|
$this->assertSame($key, $collection->current()->id); |
|
90
|
|
|
$this->assertSame("a title for comic {$key}", $collection->current()->title); |
|
91
|
|
|
++$iterations; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->assertSame(5, $iterations); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Verifies code does not explode when current() consectutively. |
|
99
|
|
|
* |
|
100
|
|
|
* @test |
|
101
|
|
|
* @covers ::current |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
View Code Duplication |
public function consecutiveCurrent() |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
108
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
109
|
|
|
$this->assertSame(0, $collection->current()->id); |
|
110
|
|
|
$this->assertSame(0, $collection->current()->id); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Verifies code does not explode when next() consectutively. |
|
115
|
|
|
* |
|
116
|
|
|
* @test |
|
117
|
|
|
* @covers ::next |
|
118
|
|
|
* |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
View Code Duplication |
public function consecutiveNext() |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
124
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
125
|
|
|
$collection->next(); |
|
126
|
|
|
$collection->next(); |
|
127
|
|
|
$this->assertSame(1, $collection->current()->id); |
|
128
|
|
|
$this->assertSame('a title for comic 1', $collection->current()->title); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Verifies count() lazy loads the next result. |
|
133
|
|
|
* |
|
134
|
|
|
* @test |
|
135
|
|
|
* @covers ::count |
|
136
|
|
|
* |
|
137
|
|
|
* @return void |
|
138
|
|
|
*/ |
|
139
|
|
|
public function countBasic() |
|
140
|
|
|
{ |
|
141
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
142
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
143
|
|
|
$this->assertSame(5, $collection->count()); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Verifies key() lazy loads the next result. |
|
148
|
|
|
* |
|
149
|
|
|
* @test |
|
150
|
|
|
* @covers ::key |
|
151
|
|
|
* |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
public function key() |
|
155
|
|
|
{ |
|
156
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
157
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
158
|
|
|
$this->assertSame(0, $collection->key()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Verifies current() lazy loads the next result. |
|
163
|
|
|
* |
|
164
|
|
|
* @test |
|
165
|
|
|
* @covers ::current |
|
166
|
|
|
* |
|
167
|
|
|
* @return void |
|
168
|
|
|
*/ |
|
169
|
|
View Code Duplication |
public function current() |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
172
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
173
|
|
|
$this->assertSame(0, $collection->current()->getId()); |
|
174
|
|
|
$this->assertSame('a title for comic 0', $collection->current()->getTitle()); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Verfies current() throws when collection is empty. |
|
179
|
|
|
* |
|
180
|
|
|
* @test |
|
181
|
|
|
* @covers ::current |
|
182
|
|
|
* @expectedException \OutOfBoundsException |
|
183
|
|
|
* |
|
184
|
|
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
|
|
public function currentWithEmpty() |
|
187
|
|
|
{ |
|
188
|
|
|
$client = new Client('not under tests', 'not under test', new EmptyAdapter()); |
|
189
|
|
|
$collection = new Collection($client, 'not under tests'); |
|
190
|
|
|
$collection->current(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Verfies key() throws when collection is empty. |
|
195
|
|
|
* |
|
196
|
|
|
* @test |
|
197
|
|
|
* @covers ::key |
|
198
|
|
|
* @expectedException \OutOfBoundsException |
|
199
|
|
|
* |
|
200
|
|
|
* @return void |
|
201
|
|
|
*/ |
|
202
|
|
|
public function keyWithEmpty() |
|
203
|
|
|
{ |
|
204
|
|
|
$client = new Client('not under tests', 'not under test', new EmptyAdapter()); |
|
205
|
|
|
$collection = new Collection($client, 'not under tests'); |
|
206
|
|
|
$collection->key(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Verify Collection can iterated multiple times. |
|
211
|
|
|
* |
|
212
|
|
|
* @test |
|
213
|
|
|
* |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
public function multiIteration() |
|
217
|
|
|
{ |
|
218
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
219
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3]); |
|
220
|
|
|
|
|
221
|
|
|
$iterations = 0; |
|
222
|
|
View Code Duplication |
foreach ($collection as $key => $actual) { |
|
223
|
|
|
$this->assertSame($key, $collection->current()->getId()); |
|
224
|
|
|
$this->assertSame("a title for comic {$key}", $collection->current()->getTitle()); |
|
225
|
|
|
++$iterations; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$this->assertSame(5, $iterations); |
|
229
|
|
|
|
|
230
|
|
|
$iterations = 0; |
|
231
|
|
View Code Duplication |
foreach ($collection as $key => $actual) { |
|
232
|
|
|
$this->assertSame($key, $collection->current()->getId()); |
|
233
|
|
|
$this->assertSame("a title for comic {$key}", $collection->current()->getTitle()); |
|
234
|
|
|
++$iterations; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$this->assertSame(5, $iterations); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Verify Collection can handle an empty response. |
|
242
|
|
|
* |
|
243
|
|
|
* @test |
|
244
|
|
|
* |
|
245
|
|
|
* @return void |
|
246
|
|
|
*/ |
|
247
|
|
|
public function emptyResult() |
|
248
|
|
|
{ |
|
249
|
|
|
$client = new Client('not under tests', 'not under test', new EmptyAdapter()); |
|
250
|
|
|
$collection = new Collection($client, 'not under tests'); |
|
251
|
|
|
$this->assertFalse($collection->valid()); |
|
252
|
|
|
$this->assertSame(0, $collection->count()); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Verify Collection can handle a response with a single item. |
|
257
|
|
|
* |
|
258
|
|
|
* @test |
|
259
|
|
|
* |
|
260
|
|
|
* @return void |
|
261
|
|
|
*/ |
|
262
|
|
|
public function oneItemCollection() |
|
263
|
|
|
{ |
|
264
|
|
|
$client = new Client('not under tests', 'not under test', new SingleAdapter()); |
|
265
|
|
|
$collection = new Collection($client, 'not under tests'); |
|
266
|
|
|
foreach ($collection as $item) { |
|
267
|
|
|
$this->assertSame(0, $collection->current()->getId()); |
|
268
|
|
|
$this->assertSame('a title for comic 0', $collection->current()->getTitle()); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Verify current() returns result from the loader given in the constructor. |
|
274
|
|
|
* |
|
275
|
|
|
* @test |
|
276
|
|
|
* @covers ::current |
|
277
|
|
|
* |
|
278
|
|
|
* @return void |
|
279
|
|
|
*/ |
|
280
|
|
|
public function currentCustomLoader() |
|
281
|
|
|
{ |
|
282
|
|
|
$loader = function ($comic) { |
|
283
|
|
|
$obj = new \StdClass(); |
|
284
|
|
|
$obj->id = $comic->id; |
|
285
|
|
|
$obj->name = $comic->title; |
|
286
|
|
|
return $obj; |
|
287
|
|
|
}; |
|
288
|
|
|
|
|
289
|
|
|
$client = new Client('not under tests', 'not under test', new CollectionAdapter()); |
|
290
|
|
|
$collection = new Collection($client, 'not under tests', ['limit' => 3], $loader); |
|
291
|
|
|
|
|
292
|
|
|
$iterations = 0; |
|
293
|
|
|
foreach ($collection as $key => $actual) { |
|
294
|
|
|
$this->assertInstanceOf('\StdClass', $actual); |
|
295
|
|
|
$this->assertSame($key, $actual->id); |
|
296
|
|
|
$this->assertSame("a title for comic {$key}", $actual->name); |
|
297
|
|
|
++$iterations; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
$this->assertSame(5, $iterations); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.