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

configureSetCsrfToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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