Completed
Push — master ( 86bee8...f7a629 )
by Grégoire
04:38
created

testDeleteGalleryMediaGalleryItemAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 8.8571
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Tests\Controller\Api;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Sonata\MediaBundle\Controller\Api\GalleryController;
16
use Sonata\MediaBundle\Model\GalleryItem;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class GalleryTest extends GalleryItem
20
{
21
    private $id;
22
23
    public function __construct()
24
    {
25
        parent::__construct();
26
        $this->id = rand();
27
    }
28
29
    public function getId()
30
    {
31
        return $this->id;
32
    }
33
}
34
35
/**
36
 * Class GalleryControllerTest.
37
 *
38
 *
39
 * @author Hugo Briand <[email protected]>
40
 */
41
class GalleryControllerTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
42
{
43
    public function testGetGalleriesAction()
44
    {
45
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
46
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
47
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
48
49
        $galleryManager->expects($this->once())->method('getPager')->will($this->returnValue(array()));
50
51
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
52
53
        $params = $this->getMock('FOS\RestBundle\Request\ParamFetcherInterface');
54
        $params
55
            ->expects($this->once())
56
            ->method('all')
57
            ->will($this->returnValue(array(
58
                'page' => 1,
59
                'count' => 10,
60
                'orderBy' => array('id' => 'ASC'),
61
        )));
62
        $params->expects($this->exactly(3))->method('get');
63
64
        $this->assertSame(array(), $gController->getGalleriesAction($params));
65
    }
66
67
    public function testGetGalleryAction()
68
    {
69
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
70
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
71
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
72
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
73
74
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
75
76
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
77
78
        $this->assertSame($gallery, $gController->getGalleryAction(1));
79
    }
80
81
    /**
82
     * @expectedException        \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
83
     * @expectedExceptionMessage Gallery (42) not found
84
     */
85
    public function testGetGalleryNotFoundAction()
86
    {
87
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
88
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
89
90
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
91
92
        $galleryManager->expects($this->once())->method('findOneBy');
93
94
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
95
96
        $gController->getGalleryAction(42);
97
    }
98
99
    public function testGetGalleryGalleryItemsAction()
100
    {
101
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
102
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
103
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
104
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
105
106
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue(array($galleryItem)));
107
108
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
109
110
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
111
112
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
113
114
        $this->assertSame(array($galleryItem), $gController->getGalleryGalleryItemAction(1));
115
    }
116
117
    public function testGetGalleryMediaAction()
118
    {
119
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
120
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
121
122
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
123
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media));
124
125
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
126
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue(array($galleryItem)));
127
128
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
129
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
130
131
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
132
133
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
134
135
        $this->assertSame(array($media), $gController->getGalleryMediasAction(1));
136
    }
137
138
    public function testPostGalleryMediaGalleryItemAction()
139
    {
140
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
141
142
        $media2 = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
143
        $media2->expects($this->any())->method('getId')->will($this->returnValue(1));
144
145
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
146
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media2));
147
148
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
149
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue(array($galleryItem)));
150
151
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
152
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
153
154
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
155
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
156
157
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
158
        $form->expects($this->once())->method('handleRequest');
159
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
160
        $form->expects($this->once())->method('getData')->will($this->returnValue($galleryItem));
161
162
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
163
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
164
165
        $galleryController = new GalleryController(
166
            $galleryManager,
167
            $mediaManager,
168
            $formFactory,
169
            'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest'
170
        );
171
        $view = $galleryController->postGalleryMediaGalleryItemAction(1, 2, new Request());
172
173
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
174
        $this->assertSame(200, $view->getStatusCode(), 'Should return 200');
0 ignored issues
show
Bug introduced by
The method getStatusCode does only exist in FOS\RestBundle\View\View, but not in Symfony\Component\Form\FormInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
175
    }
176
177
    public function testPostGalleryMediaGalleryItemInvalidAction()
178
    {
179
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
180
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
181
182
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
183
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media));
184
185
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
186
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue(array($galleryItem)));
187
188
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
189
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
190
191
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
192
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
193
194
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
195
196
        $galleryController = new GalleryController(
197
            $galleryManager,
198
            $mediaManager,
199
            $formFactory,
200
            'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest'
201
        );
202
        $view = $galleryController->postGalleryMediaGalleryItemAction(1, 1, new Request());
203
204
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
205
        $this->assertSame(400, $view->getStatusCode(), 'Should return 400');
0 ignored issues
show
Bug introduced by
The method getStatusCode does only exist in FOS\RestBundle\View\View, but not in Symfony\Component\Form\FormInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
206
    }
207
208
    public function testPutGalleryMediaGalleryItemAction()
209
    {
210
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
211
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
212
213
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
214
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media));
215
216
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
217
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue(array($galleryItem)));
218
219
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
220
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
221
222
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
223
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
224
225
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
226
        $form->expects($this->once())->method('handleRequest');
227
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
228
        $form->expects($this->once())->method('getData')->will($this->returnValue($galleryItem));
229
230
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
231
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
232
233
        $galleryController = new GalleryController(
234
            $galleryManager,
235
            $mediaManager,
236
            $formFactory,
237
            'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest'
238
        );
239
        $view = $galleryController->putGalleryMediaGalleryItemAction(1, 1, new Request());
240
241
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
242
        $this->assertSame(200, $view->getStatusCode(), 'Should return 200');
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not seem to exist on object<Symfony\Component\Form\FormInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
243
    }
244
245
    public function testPutGalleryMediaGalleryItemInvalidAction()
246
    {
247
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
248
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
249
250
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
251
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media));
252
253
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
254
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue(array($galleryItem)));
255
256
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
257
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
258
259
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
260
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
261
262
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
263
        $form->expects($this->once())->method('handleRequest');
264
        $form->expects($this->once())->method('isValid')->will($this->returnValue(false));
265
266
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
267
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
268
269
        $galleryController = new GalleryController(
270
            $galleryManager,
271
            $mediaManager,
272
            $formFactory,
273
            'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest'
274
        );
275
        $view = $galleryController->putGalleryMediaGalleryItemAction(1, 1, new Request());
276
277
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
278
    }
279
280
    public function testDeleteGalleryMediaGalleryItemAction()
281
    {
282
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
283
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
284
285
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
286
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media));
287
288
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
289
        $gallery
290
            ->expects($this->any())
291
            ->method('getGalleryItems')
292
            ->will($this->returnValue(new ArrayCollection(array($galleryItem))));
293
294
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
295
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
296
297
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
298
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
299
300
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
301
302
        $galleryController = new GalleryController(
303
            $galleryManager,
304
            $mediaManager,
305
            $formFactory,
306
            'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest'
307
        );
308
        $view = $galleryController->deleteGalleryMediaGalleryItemAction(1, 1);
309
310
        $this->assertSame(array('deleted' => true), $view);
311
    }
312
313
    public function testDeleteGalleryMediaGalleryItemInvalidAction()
314
    {
315
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
316
317
        $media2 = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
318
        $media2->expects($this->any())->method('getId')->will($this->returnValue(2));
319
320
        $galleryItem = $this->getMock('Sonata\MediaBundle\Model\GalleryItemInterface');
321
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media2));
322
323
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
324
        $gallery
325
            ->expects($this->any())
326
            ->method('getGalleryItems')
327
            ->will($this->returnValue(new ArrayCollection(array($galleryItem))));
328
329
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
330
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
331
332
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
333
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
334
335
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
336
337
        $galleryController = new GalleryController(
338
            $galleryManager,
339
            $mediaManager,
340
            $formFactory,
341
            'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest'
342
        );
343
        $view = $galleryController->deleteGalleryMediaGalleryItemAction(1, 1);
344
345
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
346
        $this->assertSame(400, $view->getStatusCode(), 'Should return 400');
347
    }
348
}
349