1
|
|
|
<?php namespace League\Fractal\Test; |
2
|
|
|
|
3
|
|
|
use League\Fractal\Manager; |
4
|
|
|
use League\Fractal\Resource\Collection; |
5
|
|
|
use League\Fractal\Resource\Item; |
6
|
|
|
use League\Fractal\Scope; |
7
|
|
|
use Mockery as m; |
8
|
|
|
|
9
|
|
|
class TransformerAbstractTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @covers League\Fractal\TransformerAbstract::setAvailableIncludes |
13
|
|
|
*/ |
14
|
|
|
public function testSetAvailableIncludes() |
15
|
|
|
{ |
16
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
17
|
|
|
$this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setAvailableIncludes(['foo'])); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @covers League\Fractal\TransformerAbstract::getAvailableIncludes |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function testGetAvailableIncludes() |
24
|
|
|
{ |
25
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
26
|
|
|
|
27
|
|
|
$transformer->setAvailableIncludes(['foo', 'bar']); |
28
|
|
|
$this->assertSame(['foo', 'bar'], $transformer->getAvailableIncludes()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @covers League\Fractal\TransformerAbstract::setDefaultIncludes |
33
|
|
|
*/ |
34
|
|
|
public function testSetDefaultIncludes() |
35
|
|
|
{ |
36
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
37
|
|
|
$this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setDefaultIncludes(['foo'])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @covers League\Fractal\TransformerAbstract::getDefaultIncludes |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function testGetDefaultIncludes() |
44
|
|
|
{ |
45
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
46
|
|
|
|
47
|
|
|
$transformer->setDefaultIncludes(['foo', 'bar']); |
48
|
|
|
$this->assertSame(['foo', 'bar'], $transformer->getDefaultIncludes()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @covers League\Fractal\TransformerAbstract::setCurrentScope |
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function testSetCurrentScope() |
55
|
|
|
{ |
56
|
|
|
$transformer = $this->getMockForAbstractClass('League\Fractal\TransformerAbstract'); |
57
|
|
|
$manager = new Manager(); |
58
|
|
|
$scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
59
|
|
|
$this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setCurrentScope($scope)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @covers League\Fractal\TransformerAbstract::getCurrentScope |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function testGetCurrentScope() |
66
|
|
|
{ |
67
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
68
|
|
|
$manager = new Manager(); |
69
|
|
|
$scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
70
|
|
|
$transformer->setCurrentScope($scope); |
71
|
|
|
$this->assertSame($transformer->getCurrentScope(), $scope); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
public function testProcessEmbeddedResourcesNoAvailableIncludes() |
75
|
|
|
{ |
76
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
77
|
|
|
|
78
|
|
|
$manager = new Manager(); |
79
|
|
|
$manager->parseIncludes('foo'); |
80
|
|
|
|
81
|
|
|
$scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
82
|
|
|
$this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data'])); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
public function testProcessEmbeddedResourcesNoDefaultIncludes() |
86
|
|
|
{ |
87
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
88
|
|
|
|
89
|
|
|
$manager = new Manager(); |
90
|
|
|
$manager->parseIncludes('foo'); |
91
|
|
|
|
92
|
|
|
$scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
93
|
|
|
$this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data'])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
98
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
99
|
|
|
* @expectedException BadMethodCallException |
100
|
|
|
*/ |
101
|
|
|
public function testProcessEmbeddedResourcesInvalidAvailableEmbed() |
102
|
|
|
{ |
103
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
104
|
|
|
|
105
|
|
|
$manager = new Manager(); |
106
|
|
|
$manager->parseIncludes('book'); |
107
|
|
|
|
108
|
|
|
$scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
109
|
|
|
$transformer->setCurrentScope($scope); |
110
|
|
|
|
111
|
|
|
$transformer->setAvailableIncludes(['book']); |
112
|
|
|
$transformer->processIncludedResources($scope, []); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
117
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
118
|
|
|
* @expectedException BadMethodCallException |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function testProcessEmbeddedResourcesInvalidDefaultEmbed() |
121
|
|
|
{ |
122
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
123
|
|
|
|
124
|
|
|
$manager = new Manager(); |
125
|
|
|
|
126
|
|
|
$scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract')); |
127
|
|
|
|
128
|
|
|
$transformer->setDefaultIncludes(['book']); |
129
|
|
|
$transformer->processIncludedResources($scope, []); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
134
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function testProcessIncludedAvailableResources() |
137
|
|
|
{ |
138
|
|
|
$manager = new Manager(); |
139
|
|
|
$manager->parseIncludes('book'); |
140
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
141
|
|
|
|
142
|
|
|
$transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) { |
143
|
|
|
return new Item(['included' => 'thing'], function ($data) { |
144
|
|
|
return $data; |
145
|
|
|
}); |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
$transformer->setAvailableIncludes(['book', 'publisher']); |
149
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
150
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
151
|
|
|
$this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
156
|
|
|
* @covers League\Fractal\TransformerAbstract::figureOutWhichIncludes |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
public function testProcessExcludedAvailableResources() |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$manager = new Manager(); |
161
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
162
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
163
|
|
|
|
164
|
|
|
$transformer->shouldReceive('includeBook')->never(); |
165
|
|
|
|
166
|
|
|
$transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) { |
|
|
|
|
167
|
|
|
return new Item(['another' => 'thing'], function ($data) { |
168
|
|
|
return $data; |
169
|
|
|
}); |
170
|
|
|
}); |
171
|
|
|
|
172
|
|
|
// available includes that have been requested are excluded |
173
|
|
|
$manager->parseIncludes('book,publisher'); |
174
|
|
|
$manager->parseExcludes('book'); |
175
|
|
|
|
176
|
|
|
$transformer->setAvailableIncludes(['book', 'publisher']); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
|
|
|
|
179
|
|
|
$this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
184
|
|
|
* @covers League\Fractal\TransformerAbstract::figureOutWhichIncludes |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
public function testProcessExcludedDefaultResources() |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$manager = new Manager(); |
189
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
190
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
191
|
|
|
|
192
|
|
|
$transformer->shouldReceive('includeBook')->never(); |
193
|
|
|
|
194
|
|
|
$transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) { |
|
|
|
|
195
|
|
|
return new Item(['another' => 'thing'], function ($data) { |
196
|
|
|
return $data; |
197
|
|
|
}); |
198
|
|
|
}); |
199
|
|
|
|
200
|
|
|
$manager->parseIncludes('book,publisher'); |
201
|
|
|
$manager->parseExcludes('book'); |
202
|
|
|
|
203
|
|
|
$transformer->setDefaultIncludes(['book', 'publisher']); |
|
|
|
|
204
|
|
|
|
205
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
|
|
|
|
206
|
|
|
$this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
211
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
212
|
|
|
*/ |
213
|
|
|
public function testProcessIncludedAvailableResourcesEmptyEmbed() |
214
|
|
|
{ |
215
|
|
|
$manager = new Manager(); |
216
|
|
|
$manager->parseIncludes(['book']); |
217
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
218
|
|
|
|
219
|
|
|
$transformer->shouldReceive('includeBook')->once()->andReturn(null); |
220
|
|
|
|
221
|
|
|
$transformer->setAvailableIncludes(['book']); |
|
|
|
|
222
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
223
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
|
|
|
|
224
|
|
|
|
225
|
|
|
$this->assertFalse($included); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
230
|
|
|
* @expectedException Exception |
231
|
|
|
* @expectedExceptionMessage Invalid return value from League\Fractal\TransformerAbstract::includeBook(). |
232
|
|
|
*/ |
233
|
|
View Code Duplication |
public function testCallEmbedMethodReturnsCrap() |
234
|
|
|
{ |
235
|
|
|
$manager = new Manager(); |
236
|
|
|
$manager->parseIncludes('book'); |
237
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
238
|
|
|
|
239
|
|
|
$transformer->shouldReceive('includeBook')->once()->andReturn(new \stdClass()); |
240
|
|
|
|
241
|
|
|
$transformer->setAvailableIncludes(['book']); |
|
|
|
|
242
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
243
|
|
|
$transformer->processIncludedResources($scope, ['meh']); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
248
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
249
|
|
|
*/ |
250
|
|
View Code Duplication |
public function testProcessEmbeddedDefaultResources() |
251
|
|
|
{ |
252
|
|
|
$manager = new Manager(); |
253
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
254
|
|
|
|
255
|
|
|
$transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) { |
|
|
|
|
256
|
|
|
return new Item(['included' => 'thing'], function ($data) { |
257
|
|
|
return $data; |
258
|
|
|
}); |
259
|
|
|
}); |
260
|
|
|
|
261
|
|
|
$transformer->setDefaultIncludes(['book']); |
|
|
|
|
262
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
263
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
|
|
|
|
264
|
|
|
$this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
269
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
270
|
|
|
*/ |
271
|
|
View Code Duplication |
public function testIncludedItem() |
272
|
|
|
{ |
273
|
|
|
$manager = new Manager(); |
274
|
|
|
$manager->parseIncludes('book'); |
275
|
|
|
|
276
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
277
|
|
|
$transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) { |
|
|
|
|
278
|
|
|
return new Item(['included' => 'thing'], function ($data) { |
279
|
|
|
return $data; |
280
|
|
|
}); |
281
|
|
|
}); |
282
|
|
|
|
283
|
|
|
$transformer->setAvailableIncludes(['book']); |
|
|
|
|
284
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
285
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
|
|
|
|
286
|
|
|
$this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public function testParamBagIsProvidedForIncludes() |
290
|
|
|
{ |
291
|
|
|
$manager = new Manager(); |
292
|
|
|
$manager->parseIncludes('book:foo(bar)'); |
293
|
|
|
|
294
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial(); |
295
|
|
|
|
296
|
|
|
$transformer->shouldReceive('includeBook') |
297
|
|
|
->with(m::any(), m::type('\League\Fractal\ParamBag')) |
298
|
|
|
->once(); |
299
|
|
|
|
300
|
|
|
$transformer->setAvailableIncludes(['book']); |
301
|
|
|
$scope = new Scope($manager, new Item([], $transformer)); |
302
|
|
|
$included = $transformer->processIncludedResources($scope, []); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
307
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
308
|
|
|
*/ |
309
|
|
|
public function testIncludedCollection() |
310
|
|
|
{ |
311
|
|
|
$manager = new Manager(); |
312
|
|
|
$manager->parseIncludes('book'); |
313
|
|
|
|
314
|
|
|
$collectionData = [ |
315
|
|
|
['included' => 'thing'], |
316
|
|
|
['another' => 'thing'], |
317
|
|
|
]; |
318
|
|
|
|
319
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
320
|
|
|
$transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) use ($collectionData) { |
321
|
|
|
return new Collection($collectionData, function ($data) { |
322
|
|
|
return $data; |
323
|
|
|
}); |
324
|
|
|
}); |
325
|
|
|
|
326
|
|
|
$transformer->setAvailableIncludes(['book']); |
|
|
|
|
327
|
|
|
$scope = new Scope($manager, new Collection([], $transformer)); |
328
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
|
|
|
|
329
|
|
|
$this->assertSame(['book' => ['data' => $collectionData]], $included); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @covers League\Fractal\TransformerAbstract::processIncludedResources |
334
|
|
|
* @covers League\Fractal\TransformerAbstract::callIncludeMethod |
335
|
|
|
*/ |
336
|
|
View Code Duplication |
public function testProcessEmbeddedDefaultResourcesEmptyEmbed() |
337
|
|
|
{ |
338
|
|
|
$transformer = m::mock('League\Fractal\TransformerAbstract[transform]'); |
339
|
|
|
$transformer->shouldReceive('includeBook')->once()->andReturn(null); |
340
|
|
|
|
341
|
|
|
$transformer->setDefaultIncludes(['book']); |
|
|
|
|
342
|
|
|
$scope = new Scope(new Manager(), new Item([], $transformer)); |
343
|
|
|
$included = $transformer->processIncludedResources($scope, ['meh']); |
|
|
|
|
344
|
|
|
|
345
|
|
|
$this->assertFalse($included); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @covers League\Fractal\TransformerAbstract::item |
350
|
|
|
*/ |
351
|
|
|
public function testItem() |
352
|
|
|
{ |
353
|
|
|
$mock = m::mock('League\Fractal\TransformerAbstract'); |
354
|
|
|
$item = $mock->item([], function () { |
355
|
|
|
}); |
356
|
|
|
$this->assertInstanceOf('League\Fractal\Resource\Item', $item); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @covers League\Fractal\TransformerAbstract::collection |
361
|
|
|
*/ |
362
|
|
|
public function testCollection() |
363
|
|
|
{ |
364
|
|
|
$mock = m::mock('League\Fractal\TransformerAbstract'); |
365
|
|
|
$collection = $mock->collection([], function () { |
366
|
|
|
}); |
367
|
|
|
$this->assertInstanceOf('League\Fractal\Resource\Collection', $collection); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function tearDown() |
371
|
|
|
{ |
372
|
|
|
m::close(); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
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.