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

MediaAdminControllerTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 204
rs 10
c 1
b 0
f 1
wmc 13
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testCreateActionToSelectProvider() 0 21 1
A testCreateAction() 0 12 1
B testListAction() 0 42 1
A configureCRUDController() 0 17 1
A configureCreateAction() 0 19 1
A configureGetCurrentRequest() 0 14 2
A configureSetFormTheme() 0 20 2
A configureSetCsrfToken() 0 20 2
A configureRender() 0 11 1
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 Prophecy\Argument;
15
use Sonata\MediaBundle\Controller\MediaAdminController;
16
17
class EntityWithGetId
18
{
19
    public function getId()
20
    {
21
    }
22
}
23
24
class MediaAdminControllerTest 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...
25
{
26
    private $container;
27
    private $admin;
28
    private $request;
29
    private $controller;
30
31
    protected function setUp()
32
    {
33
        $this->container = $this->prophesize('Symfony\Component\DependencyInjection\ContainerInterface');
34
        $this->admin = $this->prophesize('Sonata\MediaBundle\Admin\BaseMediaAdmin');
35
        $this->request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
36
37
        $this->configureCRUDController();
38
39
        $this->controller = new MediaAdminController();
40
        $this->controller->setContainer($this->container->reveal());
41
    }
42
43
    public function testCreateActionToSelectProvider()
44
    {
45
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
46
47
        $this->configureRender(
48
            'SonataMediaBundle:MediaAdmin:select_provider.html.twig',
49
            Argument::type('array'),
50
            'renderResponse'
51
        );
52
        $pool->getProvidersByContext('context')->willReturn(array('provider'));
53
        $pool->getDefaultContext()->willReturn('default_context');
54
        $this->admin->checkAccess('create')->shouldBeCalled();
55
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
56
        $this->request->get('provider')->willReturn(false);
57
        $this->request->isMethod('get')->willReturn(true);
58
        $this->request->get('context', 'default_context')->willReturn('context');
59
60
        $response = $this->controller->createAction($this->request->reveal());
61
62
        $this->assertSame('renderResponse', $response);
63
    }
64
65
    public function testCreateAction()
66
    {
67
        $this->configureCreateAction('Sonata\MediaBundle\Tests\Entity\Media');
68
        $this->configureRender('template', Argument::type('array'), 'renderResponse');
69
        $this->admin->checkAccess('create')->shouldBeCalled();
70
        $this->request->get('provider')->willReturn(true);
71
        $this->request->isMethod('get')->willReturn(true);
72
73
        $response = $this->controller->createAction($this->request->reveal());
74
75
        $this->assertSame('renderResponse', $response);
76
    }
77
78
    public function testListAction()
79
    {
80
        $datagrid = $this->prophesize('Sonata\AdminBundle\Datagrid\DatagridInterface');
81
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
82
        $categoryManager = $this->prophesize('Sonata\MediaBundle\Model\CategoryManagerInterface');
83
        $category = $this->prophesize();
84
        $category->willExtend('Sonata\MediaBundle\Tests\Controller\EntityWithGetId');
85
        $category->willImplement('Sonata\ClassificationBundle\Model\CategoryInterface');
86
        $form = $this->prophesize('Symfony\Component\Form\Form');
87
        $formView = $this->prophesize('Symfony\Component\Form\FormView');
88
89
        $this->configureSetFormTheme($formView->reveal(), 'filterTheme');
90
        $this->configureSetCsrfToken('sonata.batch');
91
        $this->configureRender('templateList', Argument::type('array'), 'renderResponse');
92
        $datagrid->setValue('context', null, 'another_context')->shouldBeCalled();
93
        $datagrid->setValue('category', null, 1)->shouldBeCalled();
94
        $datagrid->getForm()->willReturn($form->reveal());
95
        $pool->getDefaultContext()->willReturn('context');
96
        $categoryManager->getRootCategory('another_context')->willReturn($category->reveal());
97
        $categoryManager->findOneBy(array(
98
            'id' => 2,
99
            'context' => 'another_context',
100
        ))->willReturn($category->reveal());
101
        $category->getId()->willReturn(1);
102
        $form->createView()->willReturn($formView->reveal());
103
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
104
        $this->container->has('sonata.media.manager.category')->willReturn(true);
105
        $this->container->get('sonata.media.manager.category')->willReturn($categoryManager->reveal());
106
        $this->admin->checkAccess('list')->shouldBeCalled();
107
        $this->admin->setListMode('mosaic')->shouldBeCalled();
108
        $this->admin->getDatagrid()->willReturn($datagrid->reveal());
109
        $this->admin->getPersistentParameter('context', 'context')->willReturn('another_context');
110
        $this->admin->getFilterTheme()->willReturn('filterTheme');
111
        $this->admin->getTemplate('list')->willReturn('templateList');
112
        $this->request->get('_list_mode', 'mosaic')->willReturn('mosaic');
113
        $this->request->get('filter')->willReturn(array());
114
        $this->request->get('category')->willReturn(2);
115
116
        $response = $this->controller->listAction($this->request->reveal());
117
118
        $this->assertSame('renderResponse', $response);
119
    }
120
121
    private function configureCRUDController()
122
    {
123
        $pool = $this->prophesize('Sonata\AdminBundle\Admin\Pool');
124
        $breadcrumbsBuilder = $this->prophesize('Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface');
125
126
        $this->configureGetCurrentRequest($this->request->reveal());
127
        $pool->getAdminByAdminCode('admin_code')->willReturn($this->admin->reveal());
128
        $this->request->isXmlHttpRequest()->willReturn(false);
129
        $this->request->get('_xml_http_request')->willReturn(false);
130
        $this->request->get('_sonata_admin')->willReturn('admin_code');
131
        $this->request->get('uniqid')->shouldBeCalled();
132
        $this->container->get('sonata.admin.pool')->willReturn($pool->reveal());
133
        $this->container->get('sonata.admin.breadcrumbs_builder')->willReturn($breadcrumbsBuilder->reveal());
134
        $this->admin->getTemplate('layout')->willReturn('layout.html.twig');
135
        $this->admin->isChild()->willReturn(false);
136
        $this->admin->setRequest($this->request->reveal())->shouldBeCalled();
137
    }
138
139
    private function configureCreateAction($class)
140
    {
141
        $object = $this->prophesize('Sonata\MediaBundle\Tests\Entity\Media');
142
        $form = $this->prophesize('Symfony\Component\Form\Form');
143
        $formView = $this->prophesize('Symfony\Component\Form\FormView');
144
145
        $this->configureSetFormTheme($formView->reveal(), 'formTheme');
146
        $this->admin->hasActiveSubClass()->willReturn(false);
147
        $this->admin->getClass()->willReturn($class);
148
        $this->admin->getNewInstance()->willReturn($object->reveal());
149
        $this->admin->setSubject($object->reveal())->shouldBeCalled();
150
        $this->admin->getForm()->willReturn($form->reveal());
151
        $this->admin->getFormTheme()->willReturn('formTheme');
152
        $this->admin->getTemplate('edit')->willReturn('template');
153
        $form->createView()->willReturn($formView->reveal());
154
        $form->setData($object->reveal())->shouldBeCalled();
155
        $form->handleRequest($this->request->reveal())->shouldBeCalled();
156
        $form->isSubmitted()->willReturn(false);
157
    }
158
159
    private function configureGetCurrentRequest($request)
160
    {
161
        // NEXT_MAJOR: Remove this trick when bumping Symfony requirement to 2.8+.
162
        if (class_exists('Symfony\Component\HttpFoundation\RequestStack')) {
163
            $requestStack = $this->prophesize('Symfony\Component\HttpFoundation\RequestStack');
164
165
            $this->container->has('request_stack')->willReturn(true);
166
            $this->container->get('request_stack')->willReturn($requestStack->reveal());
167
            $requestStack->getCurrentRequest()->willReturn($request);
168
        } else {
169
            $this->container->has('request_stack')->willReturn(false);
170
            $this->container->get('request')->willReturn($request);
171
        }
172
    }
173
174
    private function configureSetFormTheme($formView, $formTheme)
175
    {
176
        $twig = $this->prophesize('\Twig_Environment');
177
        $twigRenderer = $this->prophesize('Symfony\Bridge\Twig\Form\TwigRenderer');
178
179
        $this->container->get('twig')->willReturn($twig->reveal());
180
181
        // Remove this trick when bumping Symfony requirement to 3.2+.
182
        if (method_exists('Symfony\Bridge\Twig\AppVariable', 'getToken')) {
183
            $twig->getRuntime('Symfony\Bridge\Twig\Form\TwigRenderer')->willReturn($twigRenderer->reveal());
184
        } else {
185
            $formExtension = $this->prophesize('Symfony\Bridge\Twig\Extension\FormExtension');
186
            $formExtension->renderer = $twigRenderer->reveal();
187
188
            // This Throw is for the CRUDController::setFormTheme()
189
            $twig->getRuntime('Symfony\Bridge\Twig\Form\TwigRenderer')->willThrow('\Twig_Error_Runtime');
190
            $twig->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')->willReturn($formExtension->reveal());
191
        }
192
        $twigRenderer->setTheme($formView, $formTheme)->shouldBeCalled();
193
    }
194
195
    private function configureSetCsrfToken($intention)
196
    {
197
        // NEXT_MAJOR: Remove this trick when bumping Symfony requirement to 2.8+.
198
        if (interface_exists('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface')) {
199
            $tokenManager = $this->prophesize('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface');
200
            $token = $this->prophesize('Symfony\Component\Security\Csrf\CsrfToken');
201
202
            $tokenManager->getToken($intention)->willReturn($token->reveal());
203
            $token->getValue()->willReturn('token');
204
            $this->container->has('security.csrf.token_manager')->willReturn(true);
205
            $this->container->get('security.csrf.token_manager')->willReturn($tokenManager->reveal());
206
        } else {
207
            $csrfProvider = $this->prophesize('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
208
209
            $csrfProvider->generateCsrfToken($intention)->shouldBeCalled('token');
210
            $this->container->has('security.csrf.token_manager')->willReturn(false);
211
            $this->container->has('form.csrf_provider')->willReturn(true);
212
            $this->container->get('form.csrf_provider')->willReturn($csrfProvider->reveal());
213
        }
214
    }
215
216
    private function configureRender($template, $data, $rendered)
217
    {
218
        $templating = $this->prophesize('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
219
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
220
221
        $this->admin->getPersistentParameters()->willReturn(array('param' => 'param'));
222
        $this->container->has('templating')->willReturn(true);
223
        $this->container->get('templating')->willReturn($templating->reveal());
224
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
225
        $templating->renderResponse($template, $data, null)->willReturn($rendered);
226
    }
227
}
228