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