Completed
Push — master ( 97e09b...3bcd53 )
by
unknown
02:36
created

configureSetFormTheme()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 15
nc 4
nop 2
dl 0
loc 26
rs 8.8571
c 1
b 0
f 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 PHPUnit\Framework\TestCase;
15
use Prophecy\Argument;
16
use Sonata\MediaBundle\Controller\GalleryAdminController;
17
use Symfony\Bridge\Twig\AppVariable;
18
use Symfony\Bridge\Twig\Command\DebugCommand;
19
use Symfony\Bridge\Twig\Extension\FormExtension;
20
use Symfony\Bridge\Twig\Form\TwigRenderer;
21
use Symfony\Component\Form\FormRenderer;
22
23
class GalleryAdminControllerTest extends TestCase
24
{
25
    private $container;
26
    private $admin;
27
    private $request;
28
    private $controller;
29
30
    protected function setUp()
31
    {
32
        $this->container = $this->prophesize('Symfony\Component\DependencyInjection\ContainerInterface');
33
        $this->admin = $this->prophesize('Sonata\MediaBundle\Admin\BaseMediaAdmin');
34
        $this->request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
35
36
        $this->configureCRUDController();
37
38
        $this->controller = new GalleryAdminController();
39
        $this->controller->setContainer($this->container->reveal());
40
    }
41
42
    public function testItIsInstantiable()
43
    {
44
        $this->assertNotNull($this->controller);
45
    }
46
47
    public function testListAction()
48
    {
49
        $datagrid = $this->prophesize('Sonata\AdminBundle\Datagrid\DatagridInterface');
50
        $form = $this->prophesize('Symfony\Component\Form\Form');
51
        $formView = $this->prophesize('Symfony\Component\Form\FormView');
52
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
53
54
        $this->configureSetFormTheme($formView->reveal(), 'filterTheme');
55
        $this->configureSetCsrfToken('sonata.batch');
56
        $this->configureRender('templateList', Argument::type('array'), 'renderResponse');
57
        $datagrid->setValue('context', null, 'context')->shouldBeCalled();
58
        $datagrid->getForm()->willReturn($form->reveal());
59
        $form->createView()->willReturn($formView->reveal());
60
        $this->admin->checkAccess('list')->shouldBeCalled();
61
        $this->admin->setListMode('list')->shouldBeCalled();
62
        $this->admin->getDatagrid()->willReturn($datagrid->reveal());
63
        $this->admin->getPersistentParameter('context')->willReturn('context');
64
        $this->admin->getFilterTheme()->willReturn('filterTheme');
65
        $this->admin->getTemplate('list')->willReturn('templateList');
66
        $this->request->get('_list_mode')->willReturn('list');
67
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
68
69
        $this->controller->listAction($this->request->reveal());
70
    }
71
72
    private function configureCRUDController()
73
    {
74
        $pool = $this->prophesize('Sonata\AdminBundle\Admin\Pool');
75
        $breadcrumbsBuilder = $this->prophesize('Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface');
76
77
        $this->configureGetCurrentRequest($this->request->reveal());
78
        $pool->getAdminByAdminCode('admin_code')->willReturn($this->admin->reveal());
79
        $this->request->isXmlHttpRequest()->willReturn(false);
80
        $this->request->get('_xml_http_request')->willReturn(false);
81
        $this->request->get('_sonata_admin')->willReturn('admin_code');
82
        $this->request->get('uniqid')->shouldBeCalled();
83
        $this->container->get('sonata.admin.pool')->willReturn($pool->reveal());
84
        $this->container->get('sonata.admin.breadcrumbs_builder')->willReturn($breadcrumbsBuilder->reveal());
85
        $this->admin->getTemplate('layout')->willReturn('layout.html.twig');
86
        $this->admin->isChild()->willReturn(false);
87
        $this->admin->setRequest($this->request->reveal())->shouldBeCalled();
88
    }
89
90
    private function configureGetCurrentRequest($request)
91
    {
92
        $requestStack = $this->prophesize('Symfony\Component\HttpFoundation\RequestStack');
93
94
        $this->container->has('request_stack')->willReturn(true);
95
        $this->container->get('request_stack')->willReturn($requestStack->reveal());
96
        $requestStack->getCurrentRequest()->willReturn($request);
97
    }
98
99
    private function configureSetCsrfToken($intention)
100
    {
101
        $tokenManager = $this->prophesize('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface');
102
        $token = $this->prophesize('Symfony\Component\Security\Csrf\CsrfToken');
103
104
        $tokenManager->getToken($intention)->willReturn($token->reveal());
105
        $token->getValue()->willReturn('token');
106
        $this->container->has('security.csrf.token_manager')->willReturn(true);
107
        $this->container->get('security.csrf.token_manager')->willReturn($tokenManager->reveal());
108
    }
109
110
    private function configureSetFormTheme($formView, $formTheme)
111
    {
112
        $twig = $this->prophesize(\Twig_Environment::class);
113
114
        // Remove this trick when bumping Symfony requirement to 3.4+
115
        if (method_exists(DebugCommand::class, 'getLoaderPaths')) {
116
            $rendererClass = FormRenderer::class;
117
        } else {
118
            $rendererClass = TwigRenderer::class;
119
        }
120
121
        $twigRenderer = $this->prophesize($rendererClass);
122
123
        $this->container->get('twig')->willReturn($twig->reveal());
124
125
        // Remove this trick when bumping Symfony requirement to 3.2+.
126
        if (method_exists(AppVariable::class, 'getToken')) {
127
            $twig->getRuntime($rendererClass)->willReturn($twigRenderer->reveal());
128
        } else {
129
            $formExtension = $this->prophesize(FormExtension::class);
130
            $formExtension->renderer = $twigRenderer->reveal();
131
132
            $twig->getExtension(FormExtension::class)->willReturn($formExtension->reveal());
133
        }
134
        $twigRenderer->setTheme($formView, $formTheme)->shouldBeCalled();
135
    }
136
137
    private function configureRender($template, $data, $rendered)
138
    {
139
        $templating = $this->prophesize('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface');
140
        $response = $this->prophesize('Symfony\Component\HttpFoundation\Response');
141
        $pool = $this->prophesize('Sonata\MediaBundle\Provider\Pool');
142
143
        $this->admin->getPersistentParameters()->willReturn(['param' => 'param']);
144
        $this->container->has('templating')->willReturn(true);
145
        $this->container->get('templating')->willReturn($templating->reveal());
146
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
147
        $response->getContent()->willReturn($rendered);
148
        $templating->renderResponse($template, $data, null)->willReturn($response->reveal());
149
        $templating->render($template, $data)->willReturn($rendered);
150
    }
151
}
152