1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Tests\Controller\Api; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use FOS\RestBundle\Request\ParamFetcher; |
18
|
|
|
use FOS\RestBundle\View\View; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use Sonata\MediaBundle\Controller\Api\GalleryController; |
21
|
|
|
use Sonata\MediaBundle\Model\GalleryInterface; |
22
|
|
|
use Sonata\MediaBundle\Model\GalleryItem; |
23
|
|
|
use Sonata\MediaBundle\Model\GalleryItemInterface; |
24
|
|
|
use Sonata\MediaBundle\Model\GalleryManagerInterface; |
25
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
26
|
|
|
use Sonata\MediaBundle\Model\MediaManagerInterface; |
27
|
|
|
use Symfony\Component\Form\Form; |
28
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
29
|
|
|
use Symfony\Component\Form\FormInterface; |
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
31
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
32
|
|
|
|
33
|
|
|
class GalleryTest extends GalleryItem |
34
|
|
|
{ |
35
|
|
|
private $id; |
36
|
|
|
|
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
|
|
|
|
40
|
|
|
$this->id = random_int(0, getrandmax()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getId() |
44
|
|
|
{ |
45
|
|
|
return $this->id; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @author Hugo Briand <[email protected]> |
51
|
|
|
*/ |
52
|
|
|
class GalleryControllerTest extends TestCase |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
public function testGetGalleriesAction(): void |
55
|
|
|
{ |
56
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
57
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
58
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
59
|
|
|
|
60
|
|
|
$galleryManager->expects($this->once())->method('getPager')->will($this->returnValue([])); |
61
|
|
|
|
62
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
63
|
|
|
|
64
|
|
|
$paramFetcher = $this->createMock(ParamFetcher::class); |
65
|
|
|
$paramFetcher->expects($this->exactly(3))->method('get'); |
66
|
|
|
$paramFetcher |
67
|
|
|
->expects($this->once()) |
68
|
|
|
->method('all') |
69
|
|
|
->will($this->returnValue([ |
70
|
|
|
'page' => 1, |
71
|
|
|
'count' => 10, |
72
|
|
|
'orderBy' => ['id' => 'ASC'], |
73
|
|
|
])); |
74
|
|
|
|
75
|
|
|
$this->assertSame([], $gController->getGalleriesAction($paramFetcher)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetGalleryAction(): void |
79
|
|
|
{ |
80
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
81
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
82
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
83
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
84
|
|
|
|
85
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
86
|
|
|
|
87
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
88
|
|
|
|
89
|
|
|
$this->assertSame($gallery, $gController->getGalleryAction(1)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGetGalleryNotFoundAction(): void |
93
|
|
|
{ |
94
|
|
|
$this->expectException(NotFoundHttpException::class); |
95
|
|
|
$this->expectExceptionMessage('Gallery (42) not found'); |
96
|
|
|
|
97
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
98
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
99
|
|
|
|
100
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
101
|
|
|
|
102
|
|
|
$galleryManager->expects($this->once())->method('findOneBy'); |
103
|
|
|
|
104
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
105
|
|
|
|
106
|
|
|
$gController->getGalleryAction(42); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testGetGalleryGalleryItemsAction(): void |
110
|
|
|
{ |
111
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
112
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
113
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
114
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
115
|
|
|
|
116
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
117
|
|
|
|
118
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
119
|
|
|
|
120
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
121
|
|
|
|
122
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
123
|
|
|
|
124
|
|
|
$this->assertSame([$galleryItem], $gController->getGalleryGalleryItemAction(1)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testGetGalleryMediaAction(): void |
128
|
|
|
{ |
129
|
|
|
$media = $this->createMock(MediaInterface::class); |
130
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
131
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
132
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
133
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
134
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
135
|
|
|
|
136
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media)); |
137
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
138
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
139
|
|
|
|
140
|
|
|
$gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test'); |
141
|
|
|
|
142
|
|
|
$this->assertSame([$media], $gController->getGalleryMediasAction(1)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testPostGalleryMediaGalleryItemAction(): void |
146
|
|
|
{ |
147
|
|
|
$media = $this->createMock(MediaInterface::class); |
148
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
149
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
150
|
|
|
$media2 = $this->createMock(MediaInterface::class); |
151
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
152
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
153
|
|
|
|
154
|
|
|
$media2->expects($this->any())->method('getId')->will($this->returnValue(1)); |
155
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media2)); |
156
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
157
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
158
|
|
|
$mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media)); |
159
|
|
|
|
160
|
|
|
$form = $this->createMock(Form::class); |
161
|
|
|
$form->expects($this->once())->method('handleRequest'); |
162
|
|
|
$form->expects($this->once())->method('isValid')->will($this->returnValue(true)); |
163
|
|
|
$form->expects($this->once())->method('getData')->will($this->returnValue($galleryItem)); |
164
|
|
|
|
165
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
166
|
|
|
$formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form)); |
167
|
|
|
|
168
|
|
|
$galleryController = new GalleryController( |
169
|
|
|
$galleryManager, |
170
|
|
|
$mediaManager, |
171
|
|
|
$formFactory, |
172
|
|
|
GalleryTest::class |
173
|
|
|
); |
174
|
|
|
$view = $galleryController->postGalleryMediaGalleryItemAction(1, 2, new Request()); |
175
|
|
|
|
176
|
|
|
$this->assertInstanceOf(View::class, $view); |
177
|
|
|
$this->assertSame(200, $view->getResponse()->getStatusCode(), 'Should return 200'); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testPostGalleryMediaGalleryItemInvalidAction(): void |
181
|
|
|
{ |
182
|
|
|
$media = $this->createMock(MediaInterface::class); |
183
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
184
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
185
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
186
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
187
|
|
|
|
188
|
|
|
$media->expects($this->any())->method('getId')->will($this->returnValue(1)); |
189
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media)); |
190
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
191
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
192
|
|
|
$mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media)); |
193
|
|
|
|
194
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
195
|
|
|
|
196
|
|
|
$galleryController = new GalleryController( |
197
|
|
|
$galleryManager, |
198
|
|
|
$mediaManager, |
199
|
|
|
$formFactory, |
200
|
|
|
GalleryTest::class |
201
|
|
|
); |
202
|
|
|
$view = $galleryController->postGalleryMediaGalleryItemAction(1, 1, new Request()); |
203
|
|
|
|
204
|
|
|
$this->assertInstanceOf(View::class, $view); |
205
|
|
|
$this->assertSame(400, $view->getResponse()->getStatusCode(), 'Should return 400'); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testPutGalleryMediaGalleryItemAction(): void |
209
|
|
|
{ |
210
|
|
|
$media = $this->createMock(MediaInterface::class); |
211
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
212
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
213
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
214
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
215
|
|
|
|
216
|
|
|
$media->expects($this->any())->method('getId')->will($this->returnValue(1)); |
217
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media)); |
218
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
219
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
220
|
|
|
$mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media)); |
221
|
|
|
|
222
|
|
|
$form = $this->createMock(Form::class); |
223
|
|
|
$form->expects($this->once())->method('handleRequest'); |
224
|
|
|
$form->expects($this->once())->method('isValid')->will($this->returnValue(true)); |
225
|
|
|
$form->expects($this->once())->method('getData')->will($this->returnValue($galleryItem)); |
226
|
|
|
|
227
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
228
|
|
|
$formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form)); |
229
|
|
|
|
230
|
|
|
$galleryController = new GalleryController( |
231
|
|
|
$galleryManager, |
232
|
|
|
$mediaManager, |
233
|
|
|
$formFactory, |
234
|
|
|
GalleryTest::class |
235
|
|
|
); |
236
|
|
|
$view = $galleryController->putGalleryMediaGalleryItemAction(1, 1, new Request()); |
237
|
|
|
|
238
|
|
|
$this->assertInstanceOf(View::class, $view); |
239
|
|
|
$this->assertSame(200, $view->getResponse()->getStatusCode(), 'Should return 200'); |
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testPutGalleryMediaGalleryItemInvalidAction(): void |
243
|
|
|
{ |
244
|
|
|
$media = $this->createMock(MediaInterface::class); |
245
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
246
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
247
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
248
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
249
|
|
|
|
250
|
|
|
$media->expects($this->any())->method('getId')->will($this->returnValue(1)); |
251
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media)); |
252
|
|
|
$gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem])); |
253
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
254
|
|
|
$mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media)); |
255
|
|
|
|
256
|
|
|
$form = $this->createMock(Form::class); |
257
|
|
|
$form->expects($this->once())->method('handleRequest'); |
258
|
|
|
$form->expects($this->once())->method('isValid')->will($this->returnValue(false)); |
259
|
|
|
|
260
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
261
|
|
|
$formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form)); |
262
|
|
|
|
263
|
|
|
$galleryController = new GalleryController( |
264
|
|
|
$galleryManager, |
265
|
|
|
$mediaManager, |
266
|
|
|
$formFactory, |
267
|
|
|
GalleryTest::class |
268
|
|
|
); |
269
|
|
|
$view = $galleryController->putGalleryMediaGalleryItemAction(1, 1, new Request()); |
270
|
|
|
|
271
|
|
|
$this->assertInstanceOf(FormInterface::class, $view); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function testDeleteGalleryMediaGalleryItemAction(): void |
275
|
|
|
{ |
276
|
|
|
$media = $this->createMock(MediaInterface::class); |
277
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
278
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
279
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
280
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
281
|
|
|
|
282
|
|
|
$media->expects($this->any())->method('getId')->will($this->returnValue(1)); |
283
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media)); |
284
|
|
|
$gallery |
285
|
|
|
->expects($this->any()) |
286
|
|
|
->method('getGalleryItems') |
287
|
|
|
->will($this->returnValue(new ArrayCollection([$galleryItem]))); |
288
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
289
|
|
|
$mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media)); |
290
|
|
|
|
291
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
292
|
|
|
|
293
|
|
|
$galleryController = new GalleryController( |
294
|
|
|
$galleryManager, |
295
|
|
|
$mediaManager, |
296
|
|
|
$formFactory, |
297
|
|
|
GalleryTest::class |
298
|
|
|
); |
299
|
|
|
$view = $galleryController->deleteGalleryMediaGalleryItemAction(1, 1); |
300
|
|
|
|
301
|
|
|
$this->assertSame(['deleted' => true], $view); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
public function testDeleteGalleryMediaGalleryItemInvalidAction(): void |
305
|
|
|
{ |
306
|
|
|
$media = $this->createMock(MediaInterface::class); |
307
|
|
|
$media2 = $this->createMock(MediaInterface::class); |
308
|
|
|
$galleryItem = $this->createMock(GalleryItemInterface::class); |
309
|
|
|
$gallery = $this->createMock(GalleryInterface::class); |
310
|
|
|
$galleryManager = $this->createMock(GalleryManagerInterface::class); |
311
|
|
|
$mediaManager = $this->createMock(MediaManagerInterface::class); |
312
|
|
|
|
313
|
|
|
$media2->expects($this->any())->method('getId')->will($this->returnValue(2)); |
314
|
|
|
$galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media2)); |
315
|
|
|
$gallery |
316
|
|
|
->expects($this->any()) |
317
|
|
|
->method('getGalleryItems') |
318
|
|
|
->will($this->returnValue(new ArrayCollection([$galleryItem]))); |
319
|
|
|
$galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery)); |
320
|
|
|
$mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media)); |
321
|
|
|
|
322
|
|
|
$formFactory = $this->createMock(FormFactoryInterface::class); |
323
|
|
|
|
324
|
|
|
$galleryController = new GalleryController( |
325
|
|
|
$galleryManager, |
326
|
|
|
$mediaManager, |
327
|
|
|
$formFactory, |
328
|
|
|
GalleryTest::class |
329
|
|
|
); |
330
|
|
|
$view = $galleryController->deleteGalleryMediaGalleryItemAction(1, 1); |
331
|
|
|
|
332
|
|
|
$this->assertInstanceOf(View::class, $view); |
333
|
|
|
$this->assertSame(400, $view->getResponse()->getStatusCode(), 'Should return 400'); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: