1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nord\Lumen\Fractal\Tests; |
4
|
|
|
|
5
|
|
|
use League\Fractal\Pagination\Cursor; |
6
|
|
|
use League\Fractal\Serializer\ArraySerializer; |
7
|
|
|
use League\Fractal\Serializer\DataArraySerializer; |
8
|
|
|
use Nord\Lumen\Fractal\FractalService; |
9
|
|
|
use Nord\Lumen\Tests\Files\Book; |
10
|
|
|
use Nord\Lumen\Tests\Files\BookPaginator; |
11
|
|
|
use Nord\Lumen\Tests\Files\BookTransformer; |
12
|
|
|
use Nord\Lumen\Tests\Files\Author; |
13
|
|
|
|
14
|
|
|
class FractalServiceTest extends \Codeception\Test\Unit |
15
|
|
|
{ |
16
|
|
|
use \Codeception\Specify; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Nord\Lumen\Fractal\FractalService |
20
|
|
|
*/ |
21
|
|
|
protected $service; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Author |
25
|
|
|
*/ |
26
|
|
|
private $author; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Book |
30
|
|
|
*/ |
31
|
|
|
private $book; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var BookTransformer |
35
|
|
|
*/ |
36
|
|
|
private $transformer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
protected function setup() |
42
|
|
|
{ |
43
|
|
|
$this->service = new FractalService(); |
44
|
|
|
$this->service->setDefaultSerializer(new DataArraySerializer()); |
45
|
|
|
|
46
|
|
|
$this->author = new Author('Test', 'Author'); |
47
|
|
|
$this->book = new Book('Test Book', 'Test Publisher', $this->author); |
48
|
|
|
$this->transformer = new BookTransformer(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Tests the fractal item. |
53
|
|
|
*/ |
54
|
|
|
public function testItem() |
55
|
|
|
{ |
56
|
|
|
$this->specify('The key "data" is required.', function () { |
57
|
|
|
$item = $this->service->item($this->book, $this->transformer)->toArray(); |
58
|
|
|
verify($item)->hasKey('data'); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
View Code Duplication |
$this->specify('The book title must equal "Test Book".', function () { |
|
|
|
|
62
|
|
|
$item = $this->service->item($this->book, $this->transformer)->toArray(); |
63
|
|
|
verify($item['data']['title'])->equals('Test Book'); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Tests the fractal collection. |
69
|
|
|
*/ |
70
|
|
|
public function testCollection() |
71
|
|
|
{ |
72
|
|
|
$this->specify('Array does not contain specified amount of items', function () { |
73
|
|
|
$collection = $this->service->collection([$this->book], $this->transformer)->toArray(); |
74
|
|
|
verify(count($collection['data']))->equals(1); |
75
|
|
|
verify(count($collection['data']))->equals(1); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Tests the Author include. |
81
|
|
|
*/ |
82
|
|
|
public function testAuthorInclude() |
83
|
|
|
{ |
84
|
|
|
$this->specify('The key "data" is required.', function () { |
85
|
|
|
$item = $this->service->item($this->book, $this->transformer)->toArray(); |
86
|
|
|
verify($item)->hasKey('data'); |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
$this->specify('Author last name must be "Author".', function () { |
90
|
|
|
$item = $this->service->item($this->book, $this->transformer)->toArray(); |
91
|
|
|
verify($item['data']['author'])->hasKey('data'); |
92
|
|
|
verify($item['data']['author']['data'])->hasKey('lastName'); |
93
|
|
|
verify($item['data']['author']['data']['lastName'])->equals('Author'); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tests the item&collection meta support. |
99
|
|
|
*/ |
100
|
|
|
public function testAssertHasMeta() |
101
|
|
|
{ |
102
|
|
|
$meta = [ |
103
|
|
|
'foo' => 'bar' |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
View Code Duplication |
$this->specify('The key "meta" is required for item.', function () use ($meta) { |
|
|
|
|
107
|
|
|
$item = $this->service->item($this->book, $this->transformer) |
108
|
|
|
->setMeta($meta) |
109
|
|
|
->toArray(); |
110
|
|
|
verify($item)->hasKey('meta'); |
111
|
|
|
verify($item['meta'])->equals($meta); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
View Code Duplication |
$this->specify('The key "meta" is required for collection.', function () use ($meta) { |
|
|
|
|
115
|
|
|
$collection = $this->service->collection([$this->book], $this->transformer) |
116
|
|
|
->setMeta($meta) |
117
|
|
|
->toArray(); |
118
|
|
|
verify($collection)->hasKey('meta'); |
119
|
|
|
verify($collection['meta'])->equals($meta); |
120
|
|
|
}); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* |
125
|
|
|
*/ |
126
|
|
|
public function testAssertJson() |
127
|
|
|
{ |
128
|
|
View Code Duplication |
$this->specify('The item has to be in JSON format.', function () { |
|
|
|
|
129
|
|
|
$item = $this->service->item($this->book, $this->transformer)->toJson(); |
130
|
|
|
verify($item)->equals('{"data":{"title":"Test Book","publisher":"Test Publisher","author":{"data":{"firstName":"Test","lastName":"Author"}}}}'); |
131
|
|
|
}); |
132
|
|
|
|
133
|
|
View Code Duplication |
$this->specify('The collection has to be in JSON format.', function () { |
|
|
|
|
134
|
|
|
$collection = $this->service->collection([$this->book], $this->transformer)->toJson(); |
135
|
|
|
verify($collection)->equals('{"data":[{"title":"Test Book","publisher":"Test Publisher","author":{"data":{"firstName":"Test","lastName":"Author"}}}]}'); |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* |
141
|
|
|
*/ |
142
|
|
|
public function testAssertSerializer() |
143
|
|
|
{ |
144
|
|
|
$this->specify('The serializer is array serializer', function () { |
145
|
|
|
$builder = $this->service->item($this->book, $this->transformer); |
146
|
|
|
$builder->setSerializer(new ArraySerializer()); |
147
|
|
|
$item = $builder->toArray(); |
148
|
|
|
verify($item)->equals([ |
149
|
|
|
'title' => 'Test Book', |
150
|
|
|
'publisher' => 'Test Publisher', |
151
|
|
|
'author' => [ |
152
|
|
|
'firstName' => 'Test', |
153
|
|
|
'lastName' => 'Author', |
154
|
|
|
] |
155
|
|
|
]); |
156
|
|
|
}); |
157
|
|
|
|
158
|
|
|
$this->specify('The serializer is data array serializer', function () { |
159
|
|
|
$builder = $this->service->item($this->book, $this->transformer); |
160
|
|
|
$builder->setSerializer(new DataArraySerializer()); |
161
|
|
|
$item = $builder->toArray(); |
162
|
|
|
verify($item)->equals([ |
163
|
|
|
'data' => [ |
164
|
|
|
'title' => 'Test Book', |
165
|
|
|
'publisher' => 'Test Publisher', |
166
|
|
|
'author' => [ |
167
|
|
|
'data' => [ |
168
|
|
|
'firstName' => 'Test', |
169
|
|
|
'lastName' => 'Author', |
170
|
|
|
] |
171
|
|
|
] |
172
|
|
|
] |
173
|
|
|
]); |
174
|
|
|
}); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* |
179
|
|
|
*/ |
180
|
|
|
public function testAssertResourceKey() |
181
|
|
|
{ |
182
|
|
View Code Duplication |
$this->specify('The fractal builder resource key can be set.', function () { |
|
|
|
|
183
|
|
|
$item = $this->service->item($this->book, $this->transformer); |
184
|
|
|
$item->setResourceKey('book'); |
185
|
|
|
$array = $item->toArray(); |
186
|
|
|
verify($array)->hasKey('data'); |
187
|
|
|
}); |
188
|
|
|
|
189
|
|
|
$this->specify('The fractal service can set the builder resource key.', function () { |
190
|
|
|
$item = $this->service->item($this->book, $this->transformer, 'book')->toArray(); |
191
|
|
|
verify($item)->hasKey('data'); |
192
|
|
|
}); |
193
|
|
|
|
194
|
|
|
$this->specify('The fractal builder resource key has to be valid.', function () { |
195
|
|
|
$item = $this->service->item($this->book, $this->transformer); |
196
|
|
|
$item->setResourceKey(123); |
197
|
|
|
}, ['throws' => 'Nord\Lumen\Fractal\Exceptions\InvalidArgument']); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* |
202
|
|
|
*/ |
203
|
|
|
public function testAssertPaginator() |
204
|
|
|
{ |
205
|
|
View Code Duplication |
$this->specify('The collection can have a paginator', function () { |
|
|
|
|
206
|
|
|
$books = [$this->book]; |
207
|
|
|
$collection = $this->service->collection($books, $this->transformer) |
208
|
|
|
->setPaginator(new BookPaginator($books)) |
209
|
|
|
->toArray(); |
210
|
|
|
verify($collection)->hasKey('meta'); |
211
|
|
|
verify($collection['meta'])->hasKey('pagination'); |
212
|
|
|
}); |
213
|
|
|
|
214
|
|
|
$this->specify('The item cannot have a paginator', function () { |
215
|
|
|
$this->service->item($this->book, $this->transformer) |
216
|
|
|
->setPaginator(new BookPaginator([])) |
217
|
|
|
->toArray(); |
218
|
|
|
}, ['throws' => 'Nord\Lumen\Fractal\Exceptions\NotApplicable']); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* |
223
|
|
|
*/ |
224
|
|
|
public function testAssertCursor() |
225
|
|
|
{ |
226
|
|
|
$this->specify('The collection can have a cursor', function () { |
227
|
|
|
$collection = $this->service->collection([$this->book], $this->transformer) |
228
|
|
|
->setCursor(new Cursor()) |
229
|
|
|
->toArray(); |
230
|
|
|
verify(count($collection['data']))->equals(1); |
231
|
|
|
}); |
232
|
|
|
|
233
|
|
|
$this->specify('The item cannot have a cursor', function () { |
234
|
|
|
$this->service->item($this->book, $this->transformer) |
235
|
|
|
->setCursor(new Cursor()) |
236
|
|
|
->toArray(); |
237
|
|
|
}, ['throws' => 'Nord\Lumen\Fractal\Exceptions\NotApplicable']); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
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.