Completed
Push — master ( a29a95...dded20 )
by
unknown
03:05
created

MediaControllerTest::configureGetCurrentRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
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
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
35
36
        $this->configureGetMedia(1, null);
37
38
        $this->controller->downloadAction($request->reveal(), 1);
39
    }
40
41
    public function testDownloadActionAccessDenied()
42
    {
43
        $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
44
45
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
46
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
47
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
48
49
        $this->configureGetMedia(1, $media->reveal());
50
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), false);
51
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
52
53
        $this->controller->downloadAction($request->reveal(), 1);
54
    }
55
56
    public function testDownloadActionBinaryFile()
57
    {
58
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
59
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
60
        $provider = $this->prophesize('Sonata\MediaBundle\Provider\MediaProviderInterface');
61
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
62
        $response = $this->prophesize('Symfony\Component\HttpFoundation\BinaryFileResponse');
63
64
        $this->configureGetMedia(1, $media->reveal());
65
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), true);
66
        $this->configureGetProvider($pool, $media, $provider->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($request->reveal(), 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
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
82
83
        $this->configureGetMedia(1, null);
84
85
        $this->controller->viewAction($request->reveal(), 1);
86
    }
87
88
    public function testViewActionAccessDenied()
89
    {
90
        $this->expectException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
91
92
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
93
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
94
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
95
96
        $this->configureGetMedia(1, $media->reveal());
97
        $this->configureDownloadSecurity($pool, $media->reveal(), $request->reveal(), false);
98
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
99
100
        $this->controller->viewAction($request->reveal(), 1);
101
    }
102
103
    public function testViewActionRendersView()
104
    {
105
        $media = $this->prophesize('Sonata\MediaBundle\Model\Media');
106
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
107
        $request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
108
109
        $this->configureGetMedia(1, $media->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($request->reveal(), 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 configureRender($template, $data, $rendered)
148
    {
149
        $templating = $this->prophesize('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
150
151
        $this->container->has('templating')->willReturn(true);
152
        $this->container->get('templating')->willReturn($templating->reveal());
153
        $templating->renderResponse($template, $data, null)->willReturn($rendered);
154
    }
155
}
156