Completed
Push — master ( 645bbf...3704b8 )
by Grégoire
10s
created

GalleryControllerTest::testDeleteGalleryMediaGalleryItemAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
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 FOS\RestBundle\Request\ParamFetcher;
17
use FOS\RestBundle\View\View;
18
use PHPUnit\Framework\TestCase;
19
use Sonata\MediaBundle\Controller\Api\GalleryController;
20
use Sonata\MediaBundle\Model\GalleryInterface;
21
use Sonata\MediaBundle\Model\GalleryItem;
22
use Sonata\MediaBundle\Model\GalleryItemInterface;
23
use Sonata\MediaBundle\Model\GalleryManagerInterface;
24
use Sonata\MediaBundle\Model\MediaInterface;
25
use Sonata\MediaBundle\Model\MediaManagerInterface;
26
use Symfony\Component\Form\FormFactoryInterface;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
29
class GalleryTest extends GalleryItem
30
{
31
    private $id;
32
33
    public function __construct()
34
    {
35
        parent::__construct();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Sonata\MediaBundle\Model\GalleryItem as the method __construct() does only exist in the following sub-classes of Sonata\MediaBundle\Model\GalleryItem: Sonata\MediaBundle\Tests...troller\Api\GalleryTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
36
        $this->id = random_int(0, getrandmax());
37
    }
38
39
    public function getId()
40
    {
41
        return $this->id;
42
    }
43
}
44
45
/**
46
 * @author Hugo Briand <[email protected]>
47
 */
48
class GalleryControllerTest extends 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...
49
{
50
    public function testGetGalleriesAction(): void
51
    {
52
        $galleryManager = $this->createMock(GalleryManagerInterface::class);
53
        $mediaManager = $this->createMock(MediaManagerInterface::class);
54
        $formFactory = $this->createMock(FormFactoryInterface::class);
55
56
        $galleryManager->expects($this->once())->method('getPager')->will($this->returnValue([]));
57
58
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
59
60
        $paramFetcher = $this->createMock(ParamFetcher::class);
61
        $paramFetcher->expects($this->exactly(3))->method('get');
62
        $paramFetcher
63
            ->expects($this->once())
64
            ->method('all')
65
            ->will($this->returnValue([
66
                'page' => 1,
67
                'count' => 10,
68
                'orderBy' => ['id' => 'ASC'],
69
            ]));
70
71
        $this->assertSame([], $gController->getGalleriesAction($paramFetcher));
72
    }
73
74
    public function testGetGalleryAction(): void
75
    {
76
        $galleryManager = $this->createMock(GalleryManagerInterface::class);
77
        $mediaManager = $this->createMock(MediaManagerInterface::class);
78
        $gallery = $this->createMock(GalleryInterface::class);
79
        $formFactory = $this->createMock(FormFactoryInterface::class);
80
81
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
82
83
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
84
85
        $this->assertSame($gallery, $gController->getGalleryAction(1));
86
    }
87
88
    public function testGetGalleryNotFoundAction(): void
89
    {
90
        $this->expectException(NotFoundHttpException::class);
91
        $this->expectExceptionMessage('Gallery (42) not found');
92
93
        $galleryManager = $this->createMock(GalleryManagerInterface::class);
94
        $mediaManager = $this->createMock(MediaManagerInterface::class);
95
96
        $formFactory = $this->createMock(FormFactoryInterface::class);
97
98
        $galleryManager->expects($this->once())->method('findOneBy');
99
100
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
101
102
        $gController->getGalleryAction(42);
103
    }
104
105
    public function testGetGalleryGalleryItemsAction(): void
106
    {
107
        $galleryManager = $this->createMock(GalleryManagerInterface::class);
108
        $galleryItem = $this->createMock(GalleryItemInterface::class);
109
        $gallery = $this->createMock(GalleryInterface::class);
110
        $formFactory = $this->createMock(FormFactoryInterface::class);
111
112
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem]));
113
114
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
115
116
        $mediaManager = $this->createMock(MediaManagerInterface::class);
117
118
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
119
120
        $this->assertSame([$galleryItem], $gController->getGalleryGalleryItemAction(1));
121
    }
122
123
    public function testGetGalleryMediaAction(): void
124
    {
125
        $media = $this->createMock(MediaInterface::class);
126
        $formFactory = $this->createMock(FormFactoryInterface::class);
127
        $galleryItem = $this->createMock(GalleryItemInterface::class);
128
        $gallery = $this->createMock(GalleryInterface::class);
129
        $galleryManager = $this->createMock(GalleryManagerInterface::class);
130
        $mediaManager = $this->createMock(MediaManagerInterface::class);
131
132
        $galleryItem->expects($this->once())->method('getMedia')->will($this->returnValue($media));
133
        $gallery->expects($this->once())->method('getGalleryItems')->will($this->returnValue([$galleryItem]));
134
        $galleryManager->expects($this->once())->method('findOneBy')->will($this->returnValue($gallery));
135
136
        $gController = new GalleryController($galleryManager, $mediaManager, $formFactory, 'test');
137
138
        $this->assertSame([$media], $gController->getGalleryMediasAction(1));
139
    }
140
}
141