Completed
Push — master ( bbca02...86bee8 )
by Grégoire
05:01
created

GalleryTest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
cc 1
eloc 3
nc 1
nop 0
rs 9.4285
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\GalleryHasMedia;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class GalleryTest extends GalleryHasMedia
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
        $gManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
46
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
47
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
48
49
        $gManager->expects($this->once())->method('getPager')->will($this->returnValue(array()));
50
51
        $gController = new GalleryController($gManager, $mediaManager, $formFactory, 'test');
52
53
        $params = $this->getMock('FOS\RestBundle\Request\ParamFetcherInterface');
54
        $params->expects($this->once())->method('all')->will($this->returnValue(array('page' => 1, 'count' => 10, 'orderBy' => array('id' => 'ASC'))));
55
        $params->expects($this->exactly(3))->method('get');
56
57
        $this->assertSame(array(), $gController->getGalleriesAction($params));
58
    }
59
60
    public function testGetGalleryAction()
61
    {
62
        $gManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
63
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
64
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
65
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
66
67
        $gManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
68
69
        $gController = new GalleryController($gManager, $mediaManager, $formFactory, 'test');
70
71
        $this->assertSame($gallery, $gController->getGalleryAction(1));
72
    }
73
74
    /**
75
     * @expectedException        \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
76
     * @expectedExceptionMessage Gallery (42) not found
77
     */
78
    public function testGetGalleryNotFoundAction()
79
    {
80
        $gManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
81
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
82
83
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
84
85
        $gManager->expects($this->once())->method('findOneBy');
86
87
        $gController = new GalleryController($gManager, $mediaManager, $formFactory, 'test');
88
89
        $gController->getGalleryAction(42);
90
    }
91
92
    public function testGetGalleryGalleryhasmediasAction()
93
    {
94
        $gManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
95
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
96
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
97
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
98
99
        $gallery->expects($this->once())->method('getGalleryHasMedias')->will($this->returnValue(array($galleryHasMedia)));
100
101
        $gManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
102
103
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
104
105
        $gController = new GalleryController($gManager, $mediaManager, $formFactory, 'test');
106
107
        $this->assertSame(array($galleryHasMedia), $gController->getGalleryGalleryhasmediasAction(1));
108
    }
109
110
    public function testGetGalleryMediaAction()
111
    {
112
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
113
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
114
115
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
116
        $galleryHasMedia->expects($this->once())->method('getMedia')->will($this->returnValue($media));
117
118
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
119
        $gallery->expects($this->once())->method('getGalleryHasMedias')->will($this->returnValue(array($galleryHasMedia)));
120
121
        $gManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
122
        $gManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
123
124
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
125
126
        $gController = new GalleryController($gManager, $mediaManager, $formFactory, 'test');
127
128
        $this->assertSame(array($media), $gController->getGalleryMediasAction(1));
129
    }
130
131
    public function testPostGalleryMediaGalleryhasmediaAction()
132
    {
133
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
134
135
        $media2 = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
136
        $media2->expects($this->any())->method('getId')->will($this->returnValue(1));
137
138
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
139
        $galleryHasMedia->expects($this->once())->method('getMedia')->will($this->returnValue($media2));
140
141
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
142
        $gallery->expects($this->once())->method('getGalleryHasMedias')->will($this->returnValue(array($galleryHasMedia)));
143
144
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
145
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
146
147
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
148
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
149
150
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
151
        $form->expects($this->once())->method('handleRequest');
152
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
153
        $form->expects($this->once())->method('getData')->will($this->returnValue($galleryHasMedia));
154
155
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
156
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
157
158
        $galleryController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest');
159
        $view = $galleryController->postGalleryMediaGalleryhasmediaAction(1, 2, new Request());
160
161
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
162
        $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...
163
    }
164
165
    public function testPostGalleryMediaGalleryhasmediaInvalidAction()
166
    {
167
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
168
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
169
170
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
171
        $galleryHasMedia->expects($this->once())->method('getMedia')->will($this->returnValue($media));
172
173
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
174
        $gallery->expects($this->once())->method('getGalleryHasMedias')->will($this->returnValue(array($galleryHasMedia)));
175
176
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
177
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
178
179
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
180
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
181
182
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
183
184
        $galleryController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest');
185
        $view = $galleryController->postGalleryMediaGalleryhasmediaAction(1, 1, new Request());
186
187
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
188
        $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...
189
    }
190
191
    public function testPutGalleryMediaGalleryhasmediaAction()
192
    {
193
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
194
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
195
196
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
197
        $galleryHasMedia->expects($this->once())->method('getMedia')->will($this->returnValue($media));
198
199
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
200
        $gallery->expects($this->once())->method('getGalleryHasMedias')->will($this->returnValue(array($galleryHasMedia)));
201
202
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
203
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
204
205
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
206
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
207
208
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
209
        $form->expects($this->once())->method('handleRequest');
210
        $form->expects($this->once())->method('isValid')->will($this->returnValue(true));
211
        $form->expects($this->once())->method('getData')->will($this->returnValue($galleryHasMedia));
212
213
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
214
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
215
216
        $galleryController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest');
217
        $view = $galleryController->putGalleryMediaGalleryhasmediaAction(1, 1, new Request());
218
219
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
220
        $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...
221
    }
222
223
    public function testPutGalleryMediaGalleryhasmediaInvalidAction()
224
    {
225
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
226
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
227
228
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
229
        $galleryHasMedia->expects($this->once())->method('getMedia')->will($this->returnValue($media));
230
231
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
232
        $gallery->expects($this->once())->method('getGalleryHasMedias')->will($this->returnValue(array($galleryHasMedia)));
233
234
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
235
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
236
237
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
238
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
239
240
        $form = $this->getMockBuilder('Symfony\Component\Form\Form')->disableOriginalConstructor()->getMock();
241
        $form->expects($this->once())->method('handleRequest');
242
        $form->expects($this->once())->method('isValid')->will($this->returnValue(false));
243
244
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
245
        $formFactory->expects($this->once())->method('createNamed')->will($this->returnValue($form));
246
247
        $galleryController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest');
248
        $view = $galleryController->putGalleryMediaGalleryhasmediaAction(1, 1, new Request());
249
250
        $this->assertInstanceOf('Symfony\Component\Form\FormInterface', $view);
251
    }
252
253
    public function testDeleteGalleryMediaGalleryhasmediaAction()
254
    {
255
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
256
        $media->expects($this->any())->method('getId')->will($this->returnValue(1));
257
258
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
259
        $galleryHasMedia->expects($this->once())->method('getMedia')->will($this->returnValue($media));
260
261
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
262
        $gallery->expects($this->any())->method('getGalleryHasMedias')->will($this->returnValue(new ArrayCollection(array($galleryHasMedia))));
263
264
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
265
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
266
267
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
268
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
269
270
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
271
272
        $galleryController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest');
273
        $view = $galleryController->deleteGalleryMediaGalleryhasmediaAction(1, 1);
274
275
        $this->assertSame(array('deleted' => true), $view);
276
    }
277
278
    public function testDeleteGalleryMediaGalleryhasmediaInvalidAction()
279
    {
280
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
281
282
        $media2 = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
283
        $media2->expects($this->any())->method('getId')->will($this->returnValue(2));
284
285
        $galleryHasMedia = $this->getMock('Sonata\MediaBundle\Model\GalleryHasMediaInterface');
286
        $galleryHasMedia->expects($this->once())->method('getMedia')->will($this->returnValue($media2));
287
288
        $gallery = $this->getMock('Sonata\MediaBundle\Model\GalleryInterface');
289
        $gallery->expects($this->any())->method('getGalleryHasMedias')->will($this->returnValue(new ArrayCollection(array($galleryHasMedia))));
290
291
        $galleryManager = $this->getMock('Sonata\MediaBundle\Model\GalleryManagerInterface');
292
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
293
294
        $mediaManager = $this->getMock('Sonata\MediaBundle\Model\MediaManagerInterface');
295
        $mediaManager->expects($this->once())->method('findOneBy')->will($this->returnValue($media));
296
297
        $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
298
299
        $galleryController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'Sonata\MediaBundle\Tests\Controller\Api\GalleryTest');
300
        $view = $galleryController->deleteGalleryMediaGalleryhasmediaAction(1, 1);
301
302
        $this->assertInstanceOf('FOS\RestBundle\View\View', $view);
303
        $this->assertSame(400, $view->getStatusCode(), 'Should return 400');
304
    }
305
}
306