testViewActionWithNotFoundMedia()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 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;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Prophecy\ObjectProphecy;
18
use Sonata\Doctrine\Entity\BaseEntityManager;
19
use Sonata\MediaBundle\Controller\MediaController;
20
use Sonata\MediaBundle\Model\Media;
21
use Sonata\MediaBundle\Provider\MediaProviderInterface;
22
use Sonata\MediaBundle\Provider\Pool;
23
use Sonata\MediaBundle\Security\DownloadStrategyInterface;
24
use Symfony\Component\DependencyInjection\Container;
25
use Symfony\Component\HttpFoundation\BinaryFileResponse;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
30
use Twig\Environment;
31
32
class MediaControllerTest extends TestCase
33
{
34
    /**
35
     * @var Container
36
     */
37
    protected $container;
38
39
    /**
40
     * @var MediaController
41
     */
42
    protected $controller;
43
44
    protected function setUp(): void
45
    {
46
        $this->container = new Container();
47
48
        $this->controller = new MediaController();
49
        $this->controller->setContainer($this->container);
50
    }
51
52
    public function testDownloadActionWithNotFoundMedia(): void
53
    {
54
        $this->expectException(NotFoundHttpException::class);
55
56
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
57
58
        $this->configureGetMedia(1, null);
59
60
        $this->controller->downloadAction($request->reveal(), 1);
61
    }
62
63
    public function testDownloadActionAccessDenied(): void
64
    {
65
        $this->expectException(AccessDeniedException::class);
66
67
        $request = $this->prophesize(Request::class);
68
        $media = $this->prophesize(Media::class);
69
        $pool = $this->prophesize(Pool::class);
70
71
        $this->configureGetMedia(1, $media->reveal());
72
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), false);
73
        $this->container->set('sonata.media.pool', $pool->reveal());
74
75
        $this->controller->downloadAction($request->reveal(), 1);
76
    }
77
78
    public function testDownloadActionBinaryFile(): void
79
    {
80
        $media = $this->prophesize(Media::class);
81
        $pool = $this->prophesize(Pool::class);
82
        $provider = $this->prophesize(MediaProviderInterface::class);
83
        $request = $this->prophesize(Request::class);
84
        $response = $this->prophesize(BinaryFileResponse::class);
85
86
        $this->configureGetMedia(1, $media->reveal());
87
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), true);
88
        $this->configureGetProvider($pool, $media, $provider->reveal());
89
        $this->container->set('sonata.media.pool', $pool->reveal());
90
        $pool->getDownloadMode($media->reveal())->willReturn('mode');
91
        $provider->getDownloadResponse($media->reveal(), 'format', 'mode')->willReturn($response->reveal());
92
        $response->prepare($request->reveal())->shouldBeCalled();
93
94
        $result = $this->controller->downloadAction($request->reveal(), 1, 'format');
95
96
        $this->assertSame($response->reveal(), $result);
97
    }
98
99
    public function testViewActionWithNotFoundMedia(): void
100
    {
101
        $this->expectException(NotFoundHttpException::class);
102
103
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
104
105
        $this->configureGetMedia(1, null);
106
107
        $this->controller->viewAction($request->reveal(), 1);
108
    }
109
110
    public function testViewActionAccessDenied(): void
111
    {
112
        $this->expectException(AccessDeniedException::class);
113
114
        $media = $this->prophesize(Media::class);
115
        $pool = $this->prophesize(Pool::class);
116
        $request = $this->prophesize(Request::class);
117
118
        $this->configureGetMedia(1, $media->reveal());
119
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), false);
120
        $this->container->set('sonata.media.pool', $pool->reveal());
121
122
        $this->controller->viewAction($request->reveal(), 1);
123
    }
124
125
    public function testViewActionRendersView(): void
126
    {
127
        $media = $this->prophesize(Media::class);
128
        $pool = $this->prophesize(Pool::class);
129
        $request = $this->prophesize(Request::class);
130
131
        $this->configureGetMedia(1, $media->reveal());
132
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), true);
133
        $this->configureRender('@SonataMedia/Media/view.html.twig', [
134
            'media' => $media->reveal(),
135
            'formats' => ['format'],
136
            'format' => 'format',
137
        ], 'renderResponse');
138
        $this->container->set('sonata.media.pool', $pool->reveal());
139
        $media->getContext()->willReturn('context');
140
        $pool->getFormatNamesByContext('context')->willReturn(['format']);
141
142
        $response = $this->controller->viewAction($request->reveal(), 1, 'format');
143
144
        $this->assertInstanceOf(Response::class, $response);
145
        $this->assertSame('renderResponse', $response->getContent());
146
    }
147
148
    private function configureDownloadSecurity(
149
        ObjectProphecy $pool,
150
        Media $media,
151
        Request $request,
152
        bool $isGranted
153
    ): void {
154
        $strategy = $this->prophesize(DownloadStrategyInterface::class);
155
156
        $pool->getDownloadSecurity($media)->willReturn($strategy->reveal());
157
        $strategy->isGranted($media, $request)->willReturn($isGranted);
158
    }
159
160
    private function configureGetMedia(int $id, ?Media $media): void
161
    {
162
        $mediaManager = $this->prophesize(BaseEntityManager::class);
163
164
        $this->container->set('sonata.media.manager.media', $mediaManager->reveal());
165
        $mediaManager->find($id)->willReturn($media);
166
    }
167
168
    private function configureGetProvider(
169
        ObjectProphecy $pool,
170
        ObjectProphecy $media,
171
        MediaProviderInterface $provider
172
    ): void {
173
        $pool->getProvider('provider')->willReturn($provider);
174
        $media->getProviderName()->willReturn('provider');
175
    }
176
177
    private function configureRender(
178
        string $template,
179
        array $data,
180
        string $rendered
181
    ): void {
182
        $twig = $this->prophesize(Environment::class);
183
        $response = $this->prophesize(Response::class);
184
185
        $this->container->set('twig', $twig->reveal());
186
        $response->getContent()->willReturn($rendered);
187
        $twig->render($template, $data)->willReturn($rendered);
188
    }
189
}
190