Completed
Push — master ( 52b06e...08177a )
by
unknown
04:10 queued 10s
created

EntityWithGetId   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Tests\Controller;
15
16
use PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
19
use Sonata\AdminBundle\Admin\Pool as AdminPool;
20
use Sonata\AdminBundle\Datagrid\DatagridInterface;
21
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
22
use Sonata\ClassificationBundle\Model\CategoryInterface;
23
use Sonata\MediaBundle\Admin\BaseMediaAdmin;
24
use Sonata\MediaBundle\Controller\MediaAdminController;
25
use Sonata\MediaBundle\Model\CategoryManagerInterface;
26
use Sonata\MediaBundle\Provider\Pool;
27
use Sonata\MediaBundle\Tests\Entity\Media;
28
use Sonata\MediaBundle\Tests\Fixtures\EntityWithGetId;
29
use Symfony\Bridge\Twig\AppVariable;
30
use Symfony\Bridge\Twig\Command\DebugCommand;
31
use Symfony\Bridge\Twig\Extension\FormExtension;
32
use Symfony\Bridge\Twig\Form\TwigRenderer;
33
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
34
use Symfony\Component\DependencyInjection\ContainerInterface;
35
use Symfony\Component\Form\Form;
36
use Symfony\Component\Form\FormRenderer;
37
use Symfony\Component\Form\FormView;
38
use Symfony\Component\HttpFoundation\Request;
39
use Symfony\Component\HttpFoundation\RequestStack;
40
use Symfony\Component\HttpFoundation\Response;
41
use Symfony\Component\Security\Csrf\CsrfToken;
42
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
43
44
class MediaAdminControllerTest extends TestCase
45
{
46
    private $container;
47
    private $admin;
48
    private $request;
49
    private $controller;
50
51
    protected function setUp(): void
52
    {
53
        $this->container = $this->prophesize(ContainerInterface::class);
54
        $this->admin = $this->prophesize(BaseMediaAdmin::class);
55
        $this->request = $this->prophesize(Request::class);
56
57
        $this->configureCRUDController();
58
59
        $this->controller = new MediaAdminController();
60
        $this->controller->setContainer($this->container->reveal());
61
    }
62
63
    public function testCreateActionToSelectProvider(): void
64
    {
65
        $pool = $this->prophesize(Pool::class);
66
67
        $this->configureRender(
68
            '@SonataMedia/MediaAdmin/select_provider.html.twig',
69
            Argument::type('array'),
70
            'renderResponse'
71
        );
72
        $pool->getProvidersByContext('context')->willReturn(['provider']);
73
        $pool->getDefaultContext()->willReturn('default_context');
74
        $this->admin->checkAccess('create')->shouldBeCalled();
75
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
76
        $this->request->get('provider')->willReturn(false);
77
        $this->request->isMethod('get')->willReturn(true);
78
        $this->request->get('context', 'default_context')->willReturn('context');
79
80
        $response = $this->controller->createAction($this->request->reveal());
81
82
        $this->assertInstanceOf(Response::class, $response);
83
        $this->assertSame('renderResponse', $response->getContent());
84
    }
85
86
    public function testCreateAction(): void
87
    {
88
        $this->configureCreateAction(Media::class);
89
        $this->configureRender('template', Argument::type('array'), 'renderResponse');
90
        $this->admin->checkAccess('create')->shouldBeCalled();
91
        $this->request->get('provider')->willReturn(true);
92
        $this->request->isMethod('get')->willReturn(true);
93
        $response = $this->controller->createAction($this->request->reveal());
94
95
        $this->assertInstanceOf(Response::class, $response);
96
        $this->assertSame('renderResponse', $response->getContent());
97
    }
98
99
    public function testListAction(): void
100
    {
101
        $datagrid = $this->prophesize(DatagridInterface::class);
102
        $pool = $this->prophesize(Pool::class);
103
        $categoryManager = $this->prophesize(CategoryManagerInterface::class);
104
        $category = $this->prophesize();
105
        $category->willExtend(EntityWithGetId::class);
106
        $category->willImplement(CategoryInterface::class);
107
        $form = $this->prophesize(Form::class);
108
        $formView = $this->prophesize(FormView::class);
109
110
        $this->configureSetFormTheme($formView->reveal(), 'filterTheme');
111
        $this->configureSetCsrfToken('sonata.batch');
112
        $this->configureRender('templateList', Argument::type('array'), 'renderResponse');
113
        $datagrid->setValue('context', null, 'another_context')->shouldBeCalled();
114
        $datagrid->setValue('category', null, 1)->shouldBeCalled();
115
        $datagrid->getForm()->willReturn($form->reveal());
116
        $pool->getDefaultContext()->willReturn('context');
117
        $categoryManager->getRootCategory('another_context')->willReturn($category->reveal());
118
        $categoryManager->findOneBy([
119
            'id' => 2,
120
            'context' => 'another_context',
121
        ])->willReturn($category->reveal());
122
        $category->getId()->willReturn(1);
123
        $form->createView()->willReturn($formView->reveal());
124
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
125
        $this->container->has('sonata.media.manager.category')->willReturn(true);
126
        $this->container->get('sonata.media.manager.category')->willReturn($categoryManager->reveal());
127
        $this->admin->checkAccess('list')->shouldBeCalled();
128
        $this->admin->setListMode('mosaic')->shouldBeCalled();
129
        $this->admin->getDatagrid()->willReturn($datagrid->reveal());
130
        $this->admin->getPersistentParameter('context', 'context')->willReturn('another_context');
131
        $this->admin->getFilterTheme()->willReturn('filterTheme');
132
        $this->admin->getTemplate('list')->willReturn('templateList');
133
        $this->request->get('_list_mode', 'mosaic')->willReturn('mosaic');
134
        $this->request->get('filter')->willReturn([]);
135
        $this->request->get('category')->willReturn(2);
136
137
        $response = $this->controller->listAction($this->request->reveal());
138
139
        $this->assertInstanceOf(Response::class, $response);
140
        $this->assertSame('renderResponse', $response->getContent());
141
    }
142
143
    private function configureCRUDController(): void
144
    {
145
        $pool = $this->prophesize(AdminPool::class);
146
        $breadcrumbsBuilder = $this->prophesize(BreadcrumbsBuilderInterface::class);
147
        $templateRegistry = $this->prophesize(TemplateRegistryInterface::class);
148
149
        $this->configureGetCurrentRequest($this->request->reveal());
150
        $pool->getAdminByAdminCode('admin_code')->willReturn($this->admin->reveal());
151
        $this->request->isXmlHttpRequest()->willReturn(false);
152
        $this->request->get('_xml_http_request')->willReturn(false);
153
        $this->request->get('_sonata_admin')->willReturn('admin_code');
154
        $this->request->get('uniqid')->shouldBeCalled();
155
        $this->container->get('sonata.admin.pool')->willReturn($pool->reveal());
156
        $this->container->get('sonata.admin.breadcrumbs_builder')->willReturn($breadcrumbsBuilder->reveal());
157
        $this->container->get('admin_code.template_registry')->willReturn($templateRegistry);
158
        $this->admin->getTemplate('layout')->willReturn('layout.html.twig');
159
        $this->admin->isChild()->willReturn(false);
160
        $this->admin->setRequest($this->request->reveal())->shouldBeCalled();
161
        $this->admin->getCode()->willReturn('admin_code');
162
    }
163
164
    private function configureCreateAction($class): void
165
    {
166
        $object = $this->prophesize(Media::class);
167
        $form = $this->prophesize(Form::class);
168
        $formView = $this->prophesize(FormView::class);
169
170
        $this->configureSetFormTheme($formView->reveal(), ['formTheme']);
171
        $this->admin->hasActiveSubClass()->willReturn(false);
172
        $this->admin->getClass()->willReturn($class);
173
        $this->admin->getNewInstance()->willReturn($object->reveal());
174
        $this->admin->setSubject($object->reveal())->shouldBeCalled();
175
        $this->admin->getForm()->willReturn($form->reveal());
176
        $this->admin->getFormTheme()->willReturn(['formTheme']);
177
        $this->admin->getTemplate('edit')->willReturn('template');
178
        $form->createView()->willReturn($formView->reveal());
179
        $form->setData($object->reveal())->shouldBeCalled();
180
        $form->handleRequest($this->request->reveal())->shouldBeCalled();
181
        $form->isSubmitted()->willReturn(false);
182
        $form->all()->willReturn(['field' => null]);
183
    }
184
185
    private function configureGetCurrentRequest($request): void
186
    {
187
        $requestStack = $this->prophesize(RequestStack::class);
188
189
        $this->container->has('request_stack')->willReturn(true);
190
        $this->container->get('request_stack')->willReturn($requestStack->reveal());
191
        $requestStack->getCurrentRequest()->willReturn($request);
192
    }
193
194
    private function configureSetFormTheme($formView, $formTheme): void
195
    {
196
        $twig = $this->prophesize(\Twig_Environment::class);
197
198
        // Remove this trick when bumping Symfony requirement to 3.4+
199
        if (method_exists(DebugCommand::class, 'getLoaderPaths')) {
200
            $rendererClass = FormRenderer::class;
201
        } else {
202
            $rendererClass = TwigRenderer::class;
203
        }
204
205
        $twigRenderer = $this->prophesize($rendererClass);
206
207
        $this->container->get('twig')->willReturn($twig->reveal());
208
209
        // Remove this trick when bumping Symfony requirement to 3.2+.
210
        if (method_exists(AppVariable::class, 'getToken')) {
211
            $twig->getRuntime($rendererClass)->willReturn($twigRenderer->reveal());
212
        } else {
213
            $formExtension = $this->prophesize(FormExtension::class);
214
            $formExtension->renderer = $twigRenderer->reveal();
215
216
            // This Throw is for the CRUDController::setFormTheme()
217
            $twig->getRuntime($rendererClass)->willThrow(\Twig_Error_Runtime::class);
218
            $twig->getExtension(FormExtension::class)->willReturn($formExtension->reveal());
219
        }
220
        $twigRenderer->setTheme($formView, $formTheme)->shouldBeCalled();
221
    }
222
223
    private function configureSetCsrfToken($intention): void
224
    {
225
        $tokenManager = $this->prophesize(CsrfTokenManagerInterface::class);
226
        $token = $this->prophesize(CsrfToken::class);
227
228
        $tokenManager->getToken($intention)->willReturn($token->reveal());
229
        $token->getValue()->willReturn('token');
230
        $this->container->has('security.csrf.token_manager')->willReturn(true);
231
        $this->container->get('security.csrf.token_manager')->willReturn($tokenManager->reveal());
232
    }
233
234
    private function configureRender($template, $data, $rendered): void
235
    {
236
        $templating = $this->prophesize(EngineInterface::class);
237
        $response = $this->prophesize(Response::class);
238
        $pool = $this->prophesize(Pool::class);
239
240
        $this->admin->getPersistentParameters()->willReturn(['param' => 'param']);
241
        $this->container->has('templating')->willReturn(true);
242
        $this->container->get('templating')->willReturn($templating->reveal());
243
        $this->container->get('sonata.media.pool')->willReturn($pool->reveal());
244
        $response->getContent()->willReturn($rendered);
245
        $templating->renderResponse($template, $data, null)->willReturn($response->reveal());
246
        $templating->render($template, $data)->willReturn($rendered);
247
    }
248
}
249