Completed
Push — master ( 8c0a67...1fafee )
by Grégoire
9s
created

MediaControllerTest::configureGetProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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;
13
14
use Sonata\MediaBundle\Controller\MediaController;
15
use Sonata\MediaBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
16
17
class MediaControllerTest extends PHPUnit_Framework_TestCase
18
{
19
    protected $container;
20
    protected $controller;
21
22
    protected function setUp()
23
    {
24
        $this->container = $this->prophesize('Symfony\Component\DependencyInjection\Container');
25
26
        $this->controller = new MediaController();
27
        $this->controller->setContainer($this->container->reveal());
28
    }
29
30
    public function testDownloadActionWithNotFoundMedia()
31
    {
32
        $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
33
34
        $this->configureGetMedia(1, null);
35
36
        $this->controller->downloadAction(1);
37
    }
38
39
    public function testDownloadActionAccessDenied()
40
    {
41
        $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
42
43
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
44
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
45
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
46
47
        $this->configureGetCurrentRequest($request->reveal());
48
        $this->configureGetMedia(1, $media->reveal());
49
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), false);
50
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
51
52
        $this->controller->downloadAction(1);
53
    }
54
55
    public function testDownloadActionBinaryFile()
56
    {
57
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
58
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
59
        $provider = $this->prophesize('Sonata\MediaBundle\Provider\MediaProviderInterface');
60
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
61
        $response = $this->prophesize('Symfony\Component\HttpFoundation\BinaryFileResponse');
62
63
        $this->configureGetMedia(1, $media->reveal());
64
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), true);
65
        $this->configureGetProvider($pool, $media, $provider->reveal());
66
        $this->configureGetCurrentRequest($request->reveal());
67
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
68
        $pool->getDownloadMode($media->reveal())->willReturn('mode');
69
        $provider->getDownloadResponse($media->reveal(), 'format', 'mode')->willReturn($response->reveal());
70
        $response->prepare($request->reveal())->shouldBeCalled();
71
72
        $result = $this->controller->downloadAction(1, 'format');
73
74
        $this->assertSame($response->reveal(), $result);
75
    }
76
77
    public function testViewActionWithNotFoundMedia()
78
    {
79
        $this->expectException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
80
81
        $this->configureGetMedia(1, null);
82
83
        $this->controller->viewAction(1);
84
    }
85
86
    public function testViewActionAccessDenied()
87
    {
88
        $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
89
90
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
91
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
92
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
93
94
        $this->configureGetMedia(1, $media->reveal());
95
        $this->configureGetCurrentRequest($request->reveal());
96
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), false);
97
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
98
99
        $this->controller->viewAction(1);
100
    }
101
102
    public function testViewActionRendersView()
103
    {
104
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
105
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
106
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
107
108
        $this->configureGetMedia(1, $media->reveal());
109
        $this->configureGetCurrentRequest($request->reveal());
110
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), true);
111
        $this->configureRender('SonataMediaBundle:Media:view.html.twig', array(
112
            'media' => $media->reveal(),
113
            'formats' => array('format'),
114
            'format' => 'format',
115
        ), 'renderResponse');
116
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
117
        $media->getContext()->willReturn('context');
118
        $pool->getFormatNamesByContext('context')->willReturn(array('format'));
119
120
        $response = $this->controller->viewAction(1, 'format');
121
122
        $this->assertSame('renderResponse', $response);
123
    }
124
125
    private function configureDownloadSecurity($pool, $media, $request, $isGranted)
126
    {
127
        $strategy = $this->prophesize('Sonata\MediaBundle\Security\DownloadStrategyInterface');
128
129
        $pool->getDownloadSecurity($media)->willReturn($strategy->reveal());
130
        $strategy->isGranted($media, $request)->willReturn($isGranted);
131
    }
132
133
    private function configureGetMedia($id, $media)
134
    {
135
        $mediaManager = $this->prophesize('Sonata\CoreBundle\Model\BaseEntityManager');
136
137
        $this->container->get('sonata.media.manager.media')->willReturn($mediaManager->reveal());
138
        $mediaManager->find($id)->willReturn($media);
139
    }
140
141
    private function configureGetProvider($pool, $media, $provider)
142
    {
143
        $pool->getProvider('provider')->willReturn($provider);
144
        $media->getProviderName()->willReturn('provider');
145
    }
146
147
    private function configureGetCurrentRequest($request)
148
    {
149
        // NEXT_MAJOR: Remove this trick when bumping Symfony requirement to 2.8+.
150
        if (class_exists('Symfony\Component\HttpFoundation\RequestStack')) {
151
            $requestStack = $this->prophesize('Symfony\Component\HttpFoundation\RequestStack');
152
153
            $this->container->has('request_stack')->willReturn(true);
154
            $this->container->get('request_stack')->willReturn($requestStack->reveal());
155
            $requestStack->getCurrentRequest()->willReturn($request);
156
        } else {
157
            $this->container->has('request_stack')->willReturn(false);
158
            $this->container->get('request')->willReturn($request);
159
        }
160
    }
161
162
    private function configureRender($template, $data, $rendered)
163
    {
164
        $templating = $this->prophesize('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
165
166
        $this->container->has('templating')->willReturn(true);
167
        $this->container->get('templating')->willReturn($templating->reveal());
168
        $templating->renderResponse($template, $data, null)->willReturn($rendered);
169
    }
170
}
171