1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace spec\Sylius\Bundle\ResourceBundle\Controller; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
17
|
|
|
use FOS\RestBundle\View\View; |
18
|
|
|
use PhpSpec\ObjectBehavior; |
19
|
|
|
use PhpSpec\Wrapper\Collaborator; |
20
|
|
|
use Prophecy\Argument; |
21
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\AuthorizationCheckerInterface; |
22
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface; |
23
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\FlashHelperInterface; |
24
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface; |
25
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\RedirectHandlerInterface; |
26
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; |
27
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface; |
28
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandlerInterface; |
29
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface; |
30
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface; |
31
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandlerInterface; |
32
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface; |
33
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface; |
34
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface; |
35
|
|
|
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
36
|
|
|
use Sylius\Component\Resource\Exception\DeleteHandlingException; |
37
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
38
|
|
|
use Sylius\Component\Resource\Metadata\MetadataInterface; |
39
|
|
|
use Sylius\Component\Resource\Model\ResourceInterface; |
40
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
41
|
|
|
use Sylius\Component\Resource\ResourceActions; |
42
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
43
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
44
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
45
|
|
|
use Symfony\Component\Form\Form; |
46
|
|
|
use Symfony\Component\Form\FormView; |
47
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
48
|
|
|
use Symfony\Component\HttpFoundation\Request; |
49
|
|
|
use Symfony\Component\HttpFoundation\Response; |
50
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
51
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
52
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
53
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
54
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken; |
55
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
56
|
|
|
|
57
|
|
|
final class ResourceControllerSpec extends ObjectBehavior |
58
|
|
|
{ |
59
|
|
|
function let( |
60
|
|
|
MetadataInterface $metadata, |
61
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
62
|
|
|
ViewHandlerInterface $viewHandler, |
63
|
|
|
RepositoryInterface $repository, |
64
|
|
|
FactoryInterface $factory, |
65
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
66
|
|
|
ObjectManager $manager, |
67
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
68
|
|
|
ResourcesCollectionProviderInterface $resourcesCollectionProvider, |
69
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
70
|
|
|
RedirectHandlerInterface $redirectHandler, |
71
|
|
|
FlashHelperInterface $flashHelper, |
72
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
73
|
|
|
EventDispatcherInterface $eventDispatcher, |
74
|
|
|
StateMachineInterface $stateMachine, |
75
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
76
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
77
|
|
|
ContainerInterface $container |
78
|
|
|
): void { |
79
|
|
|
$this->beConstructedWith( |
80
|
|
|
$metadata, |
81
|
|
|
$requestConfigurationFactory, |
82
|
|
|
$viewHandler, |
83
|
|
|
$repository, |
84
|
|
|
$factory, |
85
|
|
|
$newResourceFactory, |
86
|
|
|
$manager, |
87
|
|
|
$singleResourceProvider, |
88
|
|
|
$resourcesCollectionProvider, |
89
|
|
|
$resourceFormFactory, |
90
|
|
|
$redirectHandler, |
91
|
|
|
$flashHelper, |
92
|
|
|
$authorizationChecker, |
93
|
|
|
$eventDispatcher, |
94
|
|
|
$stateMachine, |
95
|
|
|
$resourceUpdateHandler, |
96
|
|
|
$resourceDeleteHandler |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->setContainer($container); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
function it_extends_base_Symfony_controller(): void |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$this->shouldHaveType(Controller::class); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
function it_throws_a_403_exception_if_user_is_unauthorized_to_view_a_single_resource( |
108
|
|
|
MetadataInterface $metadata, |
109
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
110
|
|
|
RequestConfiguration $configuration, |
111
|
|
|
Request $request, |
112
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
113
|
|
|
): void { |
114
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
115
|
|
|
$configuration->hasPermission()->willReturn(true); |
116
|
|
|
$configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show'); |
117
|
|
|
|
118
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(false); |
119
|
|
|
|
120
|
|
|
$this |
121
|
|
|
->shouldThrow(new AccessDeniedException()) |
122
|
|
|
->during('showAction', [$request]) |
123
|
|
|
; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
function it_throws_a_404_exception_if_resource_is_not_found_based_on_configuration( |
127
|
|
|
MetadataInterface $metadata, |
128
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
129
|
|
|
RequestConfiguration $configuration, |
130
|
|
|
Request $request, |
131
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
132
|
|
|
RepositoryInterface $repository, |
133
|
|
|
SingleResourceProviderInterface $singleResourceProvider |
134
|
|
|
): void { |
135
|
|
|
$metadata->getHumanizedName()->willReturn('product'); |
136
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
137
|
|
|
$configuration->hasPermission()->willReturn(true); |
138
|
|
|
$configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show'); |
139
|
|
|
|
140
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true); |
141
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn(null); |
|
|
|
|
142
|
|
|
|
143
|
|
|
$this |
144
|
|
|
->shouldThrow(new NotFoundHttpException('The "product" has not been found')) |
145
|
|
|
->during('showAction', [$request]) |
146
|
|
|
; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function it_returns_a_response_for_html_view_of_a_single_resource( |
150
|
|
|
MetadataInterface $metadata, |
151
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
152
|
|
|
RequestConfiguration $configuration, |
153
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
154
|
|
|
RepositoryInterface $repository, |
155
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
156
|
|
|
ResourceInterface $resource, |
157
|
|
|
ViewHandlerInterface $viewHandler, |
158
|
|
|
EventDispatcherInterface $eventDispatcher, |
159
|
|
|
Request $request, |
160
|
|
|
Response $response |
161
|
|
|
): void { |
162
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
163
|
|
|
$metadata->getName()->willReturn('product'); |
164
|
|
|
|
165
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
166
|
|
|
$configuration->hasPermission()->willReturn(true); |
167
|
|
|
$configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show'); |
168
|
|
|
|
169
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true); |
170
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
171
|
|
|
|
172
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
173
|
|
|
$configuration->getTemplate(ResourceActions::SHOW . '.html')->willReturn('SyliusShopBundle:Product:show.html.twig'); |
174
|
|
|
|
175
|
|
|
$eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled(); |
176
|
|
|
|
177
|
|
|
$expectedView = View::create() |
178
|
|
|
->setData([ |
179
|
|
|
'configuration' => $configuration, |
180
|
|
|
'metadata' => $metadata, |
181
|
|
|
'resource' => $resource, |
182
|
|
|
'product' => $resource, |
183
|
|
|
]) |
184
|
|
|
->setTemplateVar('product') |
185
|
|
|
->setTemplate('SyliusShopBundle:Product:show.html.twig') |
186
|
|
|
; |
187
|
|
|
|
188
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
189
|
|
|
|
190
|
|
|
$this->showAction($request)->shouldReturn($response); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
function it_returns_a_response_for_non_html_view_of_single_resource( |
194
|
|
|
MetadataInterface $metadata, |
195
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
196
|
|
|
RequestConfiguration $configuration, |
197
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
198
|
|
|
RepositoryInterface $repository, |
199
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
200
|
|
|
ResourceInterface $resource, |
201
|
|
|
ViewHandlerInterface $viewHandler, |
202
|
|
|
EventDispatcherInterface $eventDispatcher, |
203
|
|
|
Request $request, |
204
|
|
|
Response $response |
205
|
|
|
): void { |
206
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
207
|
|
|
$metadata->getName()->willReturn('product'); |
208
|
|
|
|
209
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
210
|
|
|
$configuration->hasPermission()->willReturn(true); |
211
|
|
|
$configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show'); |
212
|
|
|
|
213
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true); |
214
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
215
|
|
|
|
216
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
217
|
|
|
|
218
|
|
|
$eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled(); |
219
|
|
|
|
220
|
|
|
$expectedView = View::create($resource); |
221
|
|
|
|
222
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
223
|
|
|
|
224
|
|
|
$this->showAction($request)->shouldReturn($response); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
function it_throws_a_403_exception_if_user_is_unauthorized_to_view_an_index_of_resources( |
228
|
|
|
MetadataInterface $metadata, |
229
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
230
|
|
|
RequestConfiguration $configuration, |
231
|
|
|
Request $request, |
232
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
233
|
|
|
): void { |
234
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
235
|
|
|
$configuration->hasPermission()->willReturn(true); |
236
|
|
|
$configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index'); |
237
|
|
|
|
238
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(false); |
239
|
|
|
|
240
|
|
|
$this |
241
|
|
|
->shouldThrow(new AccessDeniedException()) |
242
|
|
|
->during('indexAction', [$request]) |
243
|
|
|
; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
function it_returns_a_response_for_html_view_of_paginated_resources( |
247
|
|
|
MetadataInterface $metadata, |
248
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
249
|
|
|
RequestConfiguration $configuration, |
250
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
251
|
|
|
RepositoryInterface $repository, |
252
|
|
|
ResourcesCollectionProviderInterface $resourcesCollectionProvider, |
253
|
|
|
EventDispatcherInterface $eventDispatcher, |
254
|
|
|
ResourceInterface $resource1, |
255
|
|
|
ResourceInterface $resource2, |
256
|
|
|
ViewHandlerInterface $viewHandler, |
257
|
|
|
Request $request, |
258
|
|
|
Response $response |
259
|
|
|
): void { |
260
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
261
|
|
|
$metadata->getName()->willReturn('product'); |
262
|
|
|
$metadata->getPluralName()->willReturn('products'); |
263
|
|
|
|
264
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
265
|
|
|
$configuration->hasPermission()->willReturn(true); |
266
|
|
|
$configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index'); |
267
|
|
|
|
268
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(true); |
269
|
|
|
|
270
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
271
|
|
|
$configuration->getTemplate(ResourceActions::INDEX . '.html')->willReturn('SyliusShopBundle:Product:index.html.twig'); |
272
|
|
|
$resourcesCollectionProvider->get($configuration, $repository)->willReturn([$resource1, $resource2]); |
273
|
|
|
|
274
|
|
|
$eventDispatcher->dispatchMultiple(ResourceActions::INDEX, $configuration, [$resource1, $resource2])->shouldBeCalled(); |
275
|
|
|
|
276
|
|
|
$expectedView = View::create() |
277
|
|
|
->setData([ |
278
|
|
|
'configuration' => $configuration, |
279
|
|
|
'metadata' => $metadata, |
280
|
|
|
'resources' => [$resource1, $resource2], |
281
|
|
|
'products' => [$resource1, $resource2], |
282
|
|
|
]) |
283
|
|
|
->setTemplateVar('products') |
284
|
|
|
->setTemplate('SyliusShopBundle:Product:index.html.twig') |
285
|
|
|
; |
286
|
|
|
|
287
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
288
|
|
|
|
289
|
|
|
$this->indexAction($request)->shouldReturn($response); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
function it_throws_a_403_exception_if_user_is_unauthorized_to_create_a_new_resource( |
293
|
|
|
MetadataInterface $metadata, |
294
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
295
|
|
|
RequestConfiguration $configuration, |
296
|
|
|
Request $request, |
297
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
298
|
|
|
): void { |
299
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
300
|
|
|
$configuration->hasPermission()->willReturn(true); |
301
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
302
|
|
|
|
303
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(false); |
304
|
|
|
|
305
|
|
|
$this |
306
|
|
|
->shouldThrow(new AccessDeniedException()) |
307
|
|
|
->during('createAction', [$request]) |
308
|
|
|
; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
function it_returns_a_html_response_for_creating_new_resource_form( |
312
|
|
|
MetadataInterface $metadata, |
313
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
314
|
|
|
RequestConfiguration $configuration, |
315
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
316
|
|
|
ViewHandlerInterface $viewHandler, |
317
|
|
|
FactoryInterface $factory, |
318
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
319
|
|
|
ResourceInterface $newResource, |
320
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
321
|
|
|
EventDispatcherInterface $eventDispatcher, |
322
|
|
|
ResourceControllerEvent $event, |
323
|
|
|
Form $form, |
324
|
|
|
FormView $formView, |
325
|
|
|
Request $request, |
326
|
|
|
Response $response |
327
|
|
|
): void { |
328
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
329
|
|
|
$metadata->getName()->willReturn('product'); |
330
|
|
|
|
331
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
332
|
|
|
$configuration->hasPermission()->willReturn(true); |
333
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
334
|
|
|
|
335
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
336
|
|
|
|
337
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
338
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
339
|
|
|
|
340
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
341
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
342
|
|
|
|
343
|
|
|
$eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
344
|
|
|
$event->isStopped()->willReturn(false); |
345
|
|
|
$event->hasResponse()->willReturn(false); |
346
|
|
|
|
347
|
|
|
$request->isMethod('POST')->willReturn(false); |
348
|
|
|
$form->createView()->willReturn($formView); |
349
|
|
|
|
350
|
|
|
$expectedView = View::create() |
351
|
|
|
->setData([ |
352
|
|
|
'configuration' => $configuration, |
353
|
|
|
'metadata' => $metadata, |
354
|
|
|
'resource' => $newResource, |
355
|
|
|
'product' => $newResource, |
356
|
|
|
'form' => $formView, |
357
|
|
|
]) |
358
|
|
|
->setTemplate('SyliusShopBundle:Product:create.html.twig') |
359
|
|
|
; |
360
|
|
|
|
361
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
362
|
|
|
|
363
|
|
|
$this->createAction($request)->shouldReturn($response); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
function it_returns_a_html_response_for_invalid_form_during_resource_creation( |
367
|
|
|
MetadataInterface $metadata, |
368
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
369
|
|
|
RequestConfiguration $configuration, |
370
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
371
|
|
|
ViewHandlerInterface $viewHandler, |
372
|
|
|
FactoryInterface $factory, |
373
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
374
|
|
|
ResourceInterface $newResource, |
375
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
376
|
|
|
EventDispatcherInterface $eventDispatcher, |
377
|
|
|
ResourceControllerEvent $event, |
378
|
|
|
Form $form, |
379
|
|
|
FormView $formView, |
380
|
|
|
Request $request, |
381
|
|
|
Response $response |
382
|
|
|
): void { |
383
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
384
|
|
|
$metadata->getName()->willReturn('product'); |
385
|
|
|
|
386
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
387
|
|
|
$configuration->hasPermission()->willReturn(true); |
388
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
389
|
|
|
|
390
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
391
|
|
|
|
392
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
393
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
394
|
|
|
|
395
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
396
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
397
|
|
|
|
398
|
|
|
$eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
399
|
|
|
$event->isStopped()->willReturn(false); |
400
|
|
|
$event->hasResponse()->willReturn(false); |
401
|
|
|
|
402
|
|
|
$request->isMethod('POST')->willReturn(true); |
403
|
|
|
$form->handleRequest($request)->willReturn($form); |
404
|
|
|
$form->isValid()->willReturn(false); |
405
|
|
|
$form->createView()->willReturn($formView); |
406
|
|
|
|
407
|
|
|
$expectedView = View::create() |
408
|
|
|
->setData([ |
409
|
|
|
'configuration' => $configuration, |
410
|
|
|
'metadata' => $metadata, |
411
|
|
|
'resource' => $newResource, |
412
|
|
|
'product' => $newResource, |
413
|
|
|
'form' => $formView, |
414
|
|
|
]) |
415
|
|
|
->setTemplate('SyliusShopBundle:Product:create.html.twig') |
416
|
|
|
; |
417
|
|
|
|
418
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
419
|
|
|
|
420
|
|
|
$this->createAction($request)->shouldReturn($response); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
function it_returns_a_non_html_response_for_invalid_form_during_resource_creation( |
424
|
|
|
MetadataInterface $metadata, |
425
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
426
|
|
|
RequestConfiguration $configuration, |
427
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
428
|
|
|
ViewHandlerInterface $viewHandler, |
429
|
|
|
FactoryInterface $factory, |
430
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
431
|
|
|
ResourceInterface $newResource, |
432
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
433
|
|
|
Form $form, |
434
|
|
|
Request $request, |
435
|
|
|
Response $response |
436
|
|
|
): void { |
437
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
438
|
|
|
$metadata->getName()->willReturn('product'); |
439
|
|
|
|
440
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
441
|
|
|
$configuration->hasPermission()->willReturn(true); |
442
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
443
|
|
|
|
444
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
445
|
|
|
|
446
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
447
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
448
|
|
|
|
449
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
450
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
451
|
|
|
|
452
|
|
|
$request->isMethod('POST')->willReturn(true); |
453
|
|
|
$form->handleRequest($request)->willReturn($form); |
454
|
|
|
$form->isValid()->willReturn(false); |
455
|
|
|
|
456
|
|
|
$expectedView = View::create($form, 400); |
457
|
|
|
|
458
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
459
|
|
|
|
460
|
|
|
$this->createAction($request)->shouldReturn($response); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
function it_does_not_create_the_resource_and_redirects_to_index_for_html_requests_stopped_via_events( |
464
|
|
|
MetadataInterface $metadata, |
465
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
466
|
|
|
RequestConfiguration $configuration, |
467
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
468
|
|
|
ViewHandlerInterface $viewHandler, |
469
|
|
|
FactoryInterface $factory, |
470
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
471
|
|
|
RepositoryInterface $repository, |
472
|
|
|
ResourceInterface $newResource, |
473
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
474
|
|
|
Form $form, |
475
|
|
|
RedirectHandlerInterface $redirectHandler, |
476
|
|
|
FlashHelperInterface $flashHelper, |
477
|
|
|
EventDispatcherInterface $eventDispatcher, |
478
|
|
|
ResourceControllerEvent $event, |
479
|
|
|
Request $request, |
480
|
|
|
Response $redirectResponse |
481
|
|
|
): void { |
482
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
483
|
|
|
$metadata->getName()->willReturn('product'); |
484
|
|
|
|
485
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
486
|
|
|
$configuration->hasPermission()->willReturn(true); |
487
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
488
|
|
|
|
489
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
490
|
|
|
|
491
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
492
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
493
|
|
|
|
494
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
495
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
496
|
|
|
|
497
|
|
|
$request->isMethod('POST')->willReturn(true); |
498
|
|
|
$form->handleRequest($request)->willReturn($form); |
499
|
|
|
$form->isValid()->willReturn(true); |
500
|
|
|
$form->getData()->willReturn($newResource); |
501
|
|
|
|
502
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
503
|
|
|
$event->isStopped()->willReturn(true); |
504
|
|
|
|
505
|
|
|
$flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled(); |
506
|
|
|
|
507
|
|
|
$event->hasResponse()->willReturn(false); |
508
|
|
|
|
509
|
|
|
$repository->add($newResource)->shouldNotBeCalled(); |
510
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled(); |
511
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
512
|
|
|
|
513
|
|
|
$redirectHandler->redirectToIndex($configuration, $newResource)->willReturn($redirectResponse); |
514
|
|
|
|
515
|
|
|
$this->createAction($request)->shouldReturn($redirectResponse); |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
function it_does_not_create_the_resource_and_return_response_for_html_requests_stopped_via_events( |
519
|
|
|
MetadataInterface $metadata, |
520
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
521
|
|
|
RequestConfiguration $configuration, |
522
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
523
|
|
|
FactoryInterface $factory, |
524
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
525
|
|
|
RepositoryInterface $repository, |
526
|
|
|
ResourceInterface $newResource, |
527
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
528
|
|
|
Form $form, |
529
|
|
|
FlashHelperInterface $flashHelper, |
530
|
|
|
EventDispatcherInterface $eventDispatcher, |
531
|
|
|
ResourceControllerEvent $event, |
532
|
|
|
Request $request, |
533
|
|
|
Response $response |
534
|
|
|
): void { |
535
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
536
|
|
|
$metadata->getName()->willReturn('product'); |
537
|
|
|
|
538
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
539
|
|
|
$configuration->hasPermission()->willReturn(true); |
540
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
541
|
|
|
|
542
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
543
|
|
|
|
544
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
545
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
546
|
|
|
|
547
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
548
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
549
|
|
|
|
550
|
|
|
$request->isMethod('POST')->willReturn(true); |
551
|
|
|
$form->handleRequest($request)->willReturn($form); |
552
|
|
|
$form->isValid()->willReturn(true); |
553
|
|
|
$form->getData()->willReturn($newResource); |
554
|
|
|
|
555
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
556
|
|
|
$event->isStopped()->willReturn(true); |
557
|
|
|
|
558
|
|
|
$flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled(); |
559
|
|
|
|
560
|
|
|
$event->hasResponse()->willReturn(true); |
561
|
|
|
$event->getResponse()->willReturn($response); |
562
|
|
|
|
563
|
|
|
$repository->add($newResource)->shouldNotBeCalled(); |
564
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled(); |
565
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
566
|
|
|
|
567
|
|
|
$this->createAction($request)->shouldReturn($response); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
function it_redirects_to_newly_created_resource( |
571
|
|
|
MetadataInterface $metadata, |
572
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
573
|
|
|
RequestConfiguration $configuration, |
574
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
575
|
|
|
ViewHandlerInterface $viewHandler, |
576
|
|
|
FactoryInterface $factory, |
577
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
578
|
|
|
RepositoryInterface $repository, |
579
|
|
|
ResourceInterface $newResource, |
580
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
581
|
|
|
StateMachineInterface $stateMachine, |
582
|
|
|
Form $form, |
583
|
|
|
RedirectHandlerInterface $redirectHandler, |
584
|
|
|
FlashHelperInterface $flashHelper, |
585
|
|
|
EventDispatcherInterface $eventDispatcher, |
586
|
|
|
ResourceControllerEvent $event, |
587
|
|
|
ResourceControllerEvent $postEvent, |
588
|
|
|
Request $request, |
589
|
|
|
Response $redirectResponse |
590
|
|
|
): void { |
591
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
592
|
|
|
$metadata->getName()->willReturn('product'); |
593
|
|
|
|
594
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
595
|
|
|
$configuration->hasPermission()->willReturn(true); |
596
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
597
|
|
|
$configuration->hasStateMachine()->willReturn(true); |
598
|
|
|
|
599
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
600
|
|
|
|
601
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
602
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
603
|
|
|
|
604
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
605
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
606
|
|
|
|
607
|
|
|
$request->isMethod('POST')->willReturn(true); |
608
|
|
|
$form->handleRequest($request)->willReturn($form); |
609
|
|
|
$form->isValid()->willReturn(true); |
610
|
|
|
$form->getData()->willReturn($newResource); |
611
|
|
|
|
612
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
613
|
|
|
$event->isStopped()->willReturn(false); |
614
|
|
|
|
615
|
|
|
$stateMachine->apply($configuration, $newResource)->shouldBeCalled(); |
616
|
|
|
|
617
|
|
|
$repository->add($newResource)->shouldBeCalled(); |
618
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($postEvent); |
619
|
|
|
|
620
|
|
|
$postEvent->hasResponse()->willReturn(false); |
621
|
|
|
|
622
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled(); |
623
|
|
|
$redirectHandler->redirectToResource($configuration, $newResource)->willReturn($redirectResponse); |
624
|
|
|
|
625
|
|
|
$this->createAction($request)->shouldReturn($redirectResponse); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
function it_uses_response_from_post_create_event_if_defined( |
629
|
|
|
MetadataInterface $metadata, |
630
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
631
|
|
|
RequestConfiguration $configuration, |
632
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
633
|
|
|
FactoryInterface $factory, |
634
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
635
|
|
|
RepositoryInterface $repository, |
636
|
|
|
ResourceInterface $newResource, |
637
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
638
|
|
|
StateMachineInterface $stateMachine, |
639
|
|
|
Form $form, |
640
|
|
|
FlashHelperInterface $flashHelper, |
641
|
|
|
EventDispatcherInterface $eventDispatcher, |
642
|
|
|
ResourceControllerEvent $event, |
643
|
|
|
ResourceControllerEvent $postEvent, |
644
|
|
|
Request $request, |
645
|
|
|
Response $redirectResponse |
646
|
|
|
): void { |
647
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
648
|
|
|
$metadata->getName()->willReturn('product'); |
649
|
|
|
|
650
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
651
|
|
|
$configuration->hasPermission()->willReturn(true); |
652
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
653
|
|
|
$configuration->hasStateMachine()->willReturn(true); |
654
|
|
|
|
655
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
656
|
|
|
|
657
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
658
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
659
|
|
|
|
660
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
661
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
662
|
|
|
|
663
|
|
|
$request->isMethod('POST')->willReturn(true); |
664
|
|
|
$form->handleRequest($request)->willReturn($form); |
665
|
|
|
$form->isValid()->willReturn(true); |
666
|
|
|
$form->getData()->willReturn($newResource); |
667
|
|
|
|
668
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
669
|
|
|
$event->isStopped()->willReturn(false); |
670
|
|
|
|
671
|
|
|
$stateMachine->apply($configuration, $newResource)->shouldBeCalled(); |
672
|
|
|
|
673
|
|
|
$repository->add($newResource)->shouldBeCalled(); |
674
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($postEvent); |
675
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled(); |
676
|
|
|
|
677
|
|
|
$postEvent->hasResponse()->willReturn(true); |
678
|
|
|
$postEvent->getResponse()->willReturn($redirectResponse); |
679
|
|
|
|
680
|
|
|
$this->createAction($request)->shouldReturn($redirectResponse); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
function it_returns_a_non_html_response_for_correctly_created_resources( |
684
|
|
|
MetadataInterface $metadata, |
685
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
686
|
|
|
RequestConfiguration $configuration, |
687
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
688
|
|
|
ViewHandlerInterface $viewHandler, |
689
|
|
|
FactoryInterface $factory, |
690
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
691
|
|
|
RepositoryInterface $repository, |
692
|
|
|
ResourceInterface $newResource, |
693
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
694
|
|
|
FlashHelperInterface $flashHelper, |
695
|
|
|
EventDispatcherInterface $eventDispatcher, |
696
|
|
|
ResourceControllerEvent $event, |
697
|
|
|
StateMachineInterface $stateMachine, |
698
|
|
|
Form $form, |
699
|
|
|
Request $request, |
700
|
|
|
Response $response |
701
|
|
|
): void { |
702
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
703
|
|
|
$metadata->getName()->willReturn('product'); |
704
|
|
|
|
705
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
706
|
|
|
$configuration->hasPermission()->willReturn(true); |
707
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
708
|
|
|
$configuration->hasStateMachine()->willReturn(true); |
709
|
|
|
|
710
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
711
|
|
|
|
712
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
713
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
714
|
|
|
|
715
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
716
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
717
|
|
|
|
718
|
|
|
$request->isMethod('POST')->willReturn(true); |
719
|
|
|
$form->handleRequest($request)->willReturn($form); |
720
|
|
|
$form->isValid()->willReturn(true); |
721
|
|
|
$form->getData()->willReturn($newResource); |
722
|
|
|
|
723
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
724
|
|
|
$event->isStopped()->willReturn(false); |
725
|
|
|
|
726
|
|
|
$stateMachine->apply($configuration, $newResource)->shouldBeCalled(); |
727
|
|
|
|
728
|
|
|
$repository->add($newResource)->shouldBeCalled(); |
729
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldBeCalled(); |
730
|
|
|
|
731
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
732
|
|
|
|
733
|
|
|
$expectedView = View::create($newResource, 201); |
734
|
|
|
|
735
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
736
|
|
|
|
737
|
|
|
$this->createAction($request)->shouldReturn($response); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
function it_does_not_create_the_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event( |
741
|
|
|
MetadataInterface $metadata, |
742
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
743
|
|
|
RequestConfiguration $configuration, |
744
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
745
|
|
|
FactoryInterface $factory, |
746
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
747
|
|
|
RepositoryInterface $repository, |
748
|
|
|
ResourceInterface $newResource, |
749
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
750
|
|
|
FlashHelperInterface $flashHelper, |
751
|
|
|
EventDispatcherInterface $eventDispatcher, |
752
|
|
|
Form $form, |
753
|
|
|
Request $request, |
754
|
|
|
ResourceControllerEvent $event |
755
|
|
|
): void { |
756
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
757
|
|
|
$metadata->getName()->willReturn('product'); |
758
|
|
|
|
759
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
760
|
|
|
$configuration->hasPermission()->willReturn(true); |
761
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
762
|
|
|
|
763
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
764
|
|
|
|
765
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
766
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
767
|
|
|
|
768
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
769
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
770
|
|
|
|
771
|
|
|
$request->isMethod('POST')->willReturn(true); |
772
|
|
|
$form->handleRequest($request)->willReturn($form); |
773
|
|
|
$form->isValid()->willReturn(true); |
774
|
|
|
$form->getData()->willReturn($newResource); |
775
|
|
|
|
776
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event); |
777
|
|
|
$event->isStopped()->willReturn(true); |
778
|
|
|
$event->getMessage()->willReturn('You cannot add a new product right now.'); |
779
|
|
|
$event->getErrorCode()->willReturn(500); |
780
|
|
|
|
781
|
|
|
$repository->add($newResource)->shouldNotBeCalled(); |
782
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled(); |
783
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
784
|
|
|
|
785
|
|
|
$this |
786
|
|
|
->shouldThrow(new HttpException(500, 'You cannot add a new product right now.')) |
787
|
|
|
->during('createAction', [$request]) |
788
|
|
|
; |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
function it_throws_a_403_exception_if_user_is_unauthorized_to_edit_a_single_resource( |
792
|
|
|
MetadataInterface $metadata, |
793
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
794
|
|
|
RequestConfiguration $configuration, |
795
|
|
|
Request $request, |
796
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
797
|
|
|
): void { |
798
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
799
|
|
|
$configuration->hasPermission()->willReturn(true); |
800
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
801
|
|
|
|
802
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false); |
803
|
|
|
|
804
|
|
|
$this |
805
|
|
|
->shouldThrow(new AccessDeniedException()) |
806
|
|
|
->during('updateAction', [$request]) |
807
|
|
|
; |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
function it_throws_a_404_exception_if_resource_to_update_is_not_found_based_on_configuration( |
811
|
|
|
MetadataInterface $metadata, |
812
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
813
|
|
|
RequestConfiguration $configuration, |
814
|
|
|
Request $request, |
815
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
816
|
|
|
RepositoryInterface $repository, |
817
|
|
|
SingleResourceProviderInterface $singleResourceProvider |
818
|
|
|
): void { |
819
|
|
|
$metadata->getHumanizedName()->willReturn('product'); |
820
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
821
|
|
|
$configuration->hasPermission()->willReturn(true); |
822
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
823
|
|
|
|
824
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
825
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn(null); |
|
|
|
|
826
|
|
|
|
827
|
|
|
$this |
828
|
|
|
->shouldThrow(new NotFoundHttpException('The "product" has not been found')) |
829
|
|
|
->during('updateAction', [$request]) |
830
|
|
|
; |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
function it_returns_a_html_response_for_updating_resource_form( |
834
|
|
|
MetadataInterface $metadata, |
835
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
836
|
|
|
RequestConfiguration $configuration, |
837
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
838
|
|
|
ViewHandlerInterface $viewHandler, |
839
|
|
|
RepositoryInterface $repository, |
840
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
841
|
|
|
ResourceInterface $resource, |
842
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
843
|
|
|
EventDispatcherInterface $eventDispatcher, |
844
|
|
|
ResourceControllerEvent $event, |
845
|
|
|
Form $form, |
846
|
|
|
FormView $formView, |
847
|
|
|
Request $request, |
848
|
|
|
Response $response |
849
|
|
|
): void { |
850
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
851
|
|
|
$metadata->getName()->willReturn('product'); |
852
|
|
|
|
853
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
854
|
|
|
$configuration->hasPermission()->willReturn(true); |
855
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
856
|
|
|
$configuration->hasStateMachine()->willReturn(false); |
857
|
|
|
|
858
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
859
|
|
|
|
860
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
861
|
|
|
$configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig'); |
862
|
|
|
|
863
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
864
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
865
|
|
|
|
866
|
|
|
$eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
867
|
|
|
$event->isStopped()->willReturn(false); |
868
|
|
|
$event->hasResponse()->willReturn(false); |
869
|
|
|
|
870
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
871
|
|
|
$request->getMethod()->willReturn('GET'); |
872
|
|
|
|
873
|
|
|
$form->handleRequest($request)->willReturn($form); |
874
|
|
|
$form->createView()->willReturn($formView); |
875
|
|
|
|
876
|
|
|
$expectedView = View::create() |
877
|
|
|
->setData([ |
878
|
|
|
'configuration' => $configuration, |
879
|
|
|
'metadata' => $metadata, |
880
|
|
|
'resource' => $resource, |
881
|
|
|
'product' => $resource, |
882
|
|
|
'form' => $formView, |
883
|
|
|
]) |
884
|
|
|
->setTemplate('SyliusShopBundle:Product:update.html.twig') |
885
|
|
|
; |
886
|
|
|
|
887
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
888
|
|
|
|
889
|
|
|
$this->updateAction($request)->shouldReturn($response); |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
function it_returns_a_html_response_for_invalid_form_during_resource_update( |
893
|
|
|
MetadataInterface $metadata, |
894
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
895
|
|
|
RequestConfiguration $configuration, |
896
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
897
|
|
|
ViewHandlerInterface $viewHandler, |
898
|
|
|
RepositoryInterface $repository, |
899
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
900
|
|
|
ResourceInterface $resource, |
901
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
902
|
|
|
EventDispatcherInterface $eventDispatcher, |
903
|
|
|
ResourceControllerEvent $event, |
904
|
|
|
Form $form, |
905
|
|
|
FormView $formView, |
906
|
|
|
Request $request, |
907
|
|
|
Response $response |
908
|
|
|
): void { |
909
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
910
|
|
|
$metadata->getName()->willReturn('product'); |
911
|
|
|
|
912
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
913
|
|
|
$configuration->hasPermission()->willReturn(true); |
914
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
915
|
|
|
|
916
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
917
|
|
|
|
918
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
919
|
|
|
$configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig'); |
920
|
|
|
|
921
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
922
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
923
|
|
|
|
924
|
|
|
$eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
925
|
|
|
$event->isStopped()->willReturn(false); |
926
|
|
|
$event->hasResponse()->willReturn(false); |
927
|
|
|
|
928
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
929
|
|
|
$request->getMethod()->willReturn('PUT'); |
930
|
|
|
|
931
|
|
|
$form->handleRequest($request)->willReturn($form); |
932
|
|
|
|
933
|
|
|
$form->isValid()->willReturn(false); |
934
|
|
|
$form->createView()->willReturn($formView); |
935
|
|
|
|
936
|
|
|
$expectedView = View::create() |
937
|
|
|
->setData([ |
938
|
|
|
'configuration' => $configuration, |
939
|
|
|
'metadata' => $metadata, |
940
|
|
|
'resource' => $resource, |
941
|
|
|
'product' => $resource, |
942
|
|
|
'form' => $formView, |
943
|
|
|
]) |
944
|
|
|
->setTemplate('SyliusShopBundle:Product:update.html.twig') |
945
|
|
|
; |
946
|
|
|
|
947
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
948
|
|
|
|
949
|
|
|
$this->updateAction($request)->shouldReturn($response); |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
function it_returns_a_non_html_response_for_invalid_form_during_resource_update( |
953
|
|
|
MetadataInterface $metadata, |
954
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
955
|
|
|
RequestConfiguration $configuration, |
956
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
957
|
|
|
ViewHandlerInterface $viewHandler, |
958
|
|
|
RepositoryInterface $repository, |
959
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
960
|
|
|
ResourceInterface $resource, |
961
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
962
|
|
|
Form $form, |
963
|
|
|
Request $request, |
964
|
|
|
Response $response |
965
|
|
|
): void { |
966
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
967
|
|
|
$metadata->getName()->willReturn('product'); |
968
|
|
|
|
969
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
970
|
|
|
$configuration->hasPermission()->willReturn(true); |
971
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
972
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
973
|
|
|
|
974
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
975
|
|
|
|
976
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
977
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
978
|
|
|
|
979
|
|
|
$request->isMethod('PATCH')->willReturn(true); |
980
|
|
|
$request->getMethod()->willReturn('PATCH'); |
981
|
|
|
|
982
|
|
|
$form->handleRequest($request)->willReturn($form); |
983
|
|
|
$form->isValid()->willReturn(false); |
984
|
|
|
|
985
|
|
|
$expectedView = View::create($form, 400); |
986
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
987
|
|
|
|
988
|
|
|
$this->updateAction($request)->shouldReturn($response); |
989
|
|
|
} |
990
|
|
|
|
991
|
|
|
function it_does_not_update_the_resource_and_redirects_to_resource_for_html_request_if_stopped_via_event( |
992
|
|
|
MetadataInterface $metadata, |
993
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
994
|
|
|
RequestConfiguration $configuration, |
995
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
996
|
|
|
ObjectManager $manager, |
997
|
|
|
RepositoryInterface $repository, |
998
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
999
|
|
|
ResourceInterface $resource, |
1000
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1001
|
|
|
Form $form, |
1002
|
|
|
EventDispatcherInterface $eventDispatcher, |
1003
|
|
|
RedirectHandlerInterface $redirectHandler, |
1004
|
|
|
FlashHelperInterface $flashHelper, |
1005
|
|
|
ResourceControllerEvent $event, |
1006
|
|
|
Request $request, |
1007
|
|
|
Response $redirectResponse |
1008
|
|
|
): void { |
1009
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1010
|
|
|
$metadata->getName()->willReturn('product'); |
1011
|
|
|
|
1012
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1013
|
|
|
$configuration->hasPermission()->willReturn(true); |
1014
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
1015
|
|
|
|
1016
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
1017
|
|
|
|
1018
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1019
|
|
|
|
1020
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1021
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
1022
|
|
|
|
1023
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
1024
|
|
|
$request->getMethod()->willReturn('PUT'); |
1025
|
|
|
|
1026
|
|
|
$form->handleRequest($request)->willReturn($form); |
1027
|
|
|
|
1028
|
|
|
$form->isSubmitted()->willReturn(true); |
1029
|
|
|
$form->isValid()->willReturn(true); |
1030
|
|
|
$form->getData()->willReturn($resource); |
1031
|
|
|
|
1032
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
1033
|
|
|
$event->isStopped()->willReturn(true); |
1034
|
|
|
$event->hasResponse()->willReturn(false); |
1035
|
|
|
$flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled(); |
1036
|
|
|
|
1037
|
|
|
$manager->flush()->shouldNotBeCalled(); |
1038
|
|
|
$eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1039
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1040
|
|
|
|
1041
|
|
|
$redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse); |
1042
|
|
|
|
1043
|
|
|
$this->updateAction($request)->shouldReturn($redirectResponse); |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
|
|
function it_redirects_to_updated_resource( |
1047
|
|
|
MetadataInterface $metadata, |
1048
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1049
|
|
|
RepositoryInterface $repository, |
1050
|
|
|
ObjectManager $manager, |
1051
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1052
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1053
|
|
|
RedirectHandlerInterface $redirectHandler, |
1054
|
|
|
FlashHelperInterface $flashHelper, |
1055
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1056
|
|
|
EventDispatcherInterface $eventDispatcher, |
1057
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
1058
|
|
|
RequestConfiguration $configuration, |
1059
|
|
|
ResourceInterface $resource, |
1060
|
|
|
Form $form, |
1061
|
|
|
ResourceControllerEvent $preEvent, |
1062
|
|
|
ResourceControllerEvent $postEvent, |
1063
|
|
|
Request $request, |
1064
|
|
|
Response $redirectResponse |
1065
|
|
|
): void { |
1066
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1067
|
|
|
$metadata->getName()->willReturn('product'); |
1068
|
|
|
|
1069
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1070
|
|
|
$configuration->hasPermission()->willReturn(true); |
1071
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
1072
|
|
|
$configuration->hasStateMachine()->willReturn(false); |
1073
|
|
|
|
1074
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
1075
|
|
|
|
1076
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1077
|
|
|
$configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig'); |
1078
|
|
|
|
1079
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1080
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
1081
|
|
|
|
1082
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
1083
|
|
|
$request->getMethod()->willReturn('PUT'); |
1084
|
|
|
|
1085
|
|
|
$form->handleRequest($request)->willReturn($form); |
1086
|
|
|
|
1087
|
|
|
$form->isSubmitted()->willReturn(true); |
1088
|
|
|
$form->isValid()->willReturn(true); |
1089
|
|
|
$form->getData()->willReturn($resource); |
1090
|
|
|
|
1091
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent); |
1092
|
|
|
$preEvent->isStopped()->willReturn(false); |
1093
|
|
|
|
1094
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
1095
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent); |
1096
|
|
|
|
1097
|
|
|
$postEvent->hasResponse()->willReturn(false); |
1098
|
|
|
|
1099
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled(); |
1100
|
|
|
$redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse); |
1101
|
|
|
|
1102
|
|
|
$this->updateAction($request)->shouldReturn($redirectResponse); |
1103
|
|
|
} |
1104
|
|
|
|
1105
|
|
|
function it_uses_response_from_post_update_event_if_defined( |
1106
|
|
|
MetadataInterface $metadata, |
1107
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1108
|
|
|
RepositoryInterface $repository, |
1109
|
|
|
ObjectManager $manager, |
1110
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1111
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1112
|
|
|
RedirectHandlerInterface $redirectHandler, |
1113
|
|
|
FlashHelperInterface $flashHelper, |
1114
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1115
|
|
|
EventDispatcherInterface $eventDispatcher, |
1116
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
1117
|
|
|
RequestConfiguration $configuration, |
1118
|
|
|
ResourceInterface $resource, |
1119
|
|
|
Form $form, |
1120
|
|
|
ResourceControllerEvent $preEvent, |
1121
|
|
|
ResourceControllerEvent $postEvent, |
1122
|
|
|
Request $request, |
1123
|
|
|
Response $redirectResponse |
1124
|
|
|
): void { |
1125
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1126
|
|
|
$metadata->getName()->willReturn('product'); |
1127
|
|
|
|
1128
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1129
|
|
|
$configuration->hasPermission()->willReturn(true); |
1130
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
1131
|
|
|
$configuration->hasStateMachine()->willReturn(false); |
1132
|
|
|
|
1133
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
1134
|
|
|
|
1135
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1136
|
|
|
$configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig'); |
1137
|
|
|
|
1138
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1139
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
1140
|
|
|
|
1141
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
1142
|
|
|
$request->getMethod()->willReturn('PUT'); |
1143
|
|
|
|
1144
|
|
|
$form->handleRequest($request)->willReturn($form); |
1145
|
|
|
|
1146
|
|
|
$form->isSubmitted()->willReturn(true); |
1147
|
|
|
$form->isValid()->willReturn(true); |
1148
|
|
|
$form->getData()->willReturn($resource); |
1149
|
|
|
|
1150
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent); |
1151
|
|
|
$preEvent->isStopped()->willReturn(false); |
1152
|
|
|
|
1153
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
1154
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled(); |
1155
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent); |
1156
|
|
|
|
1157
|
|
|
$postEvent->hasResponse()->willReturn(true); |
1158
|
|
|
$postEvent->getResponse()->willReturn($redirectResponse); |
1159
|
|
|
|
1160
|
|
|
$redirectHandler->redirectToResource($configuration, $resource)->shouldNotBeCalled(); |
1161
|
|
|
|
1162
|
|
|
$this->updateAction($request)->shouldReturn($redirectResponse); |
1163
|
|
|
} |
1164
|
|
|
|
1165
|
|
|
function it_uses_response_from_initialize_create_event_if_defined( |
1166
|
|
|
MetadataInterface $metadata, |
1167
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1168
|
|
|
RequestConfiguration $configuration, |
1169
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1170
|
|
|
ViewHandlerInterface $viewHandler, |
1171
|
|
|
RedirectHandlerInterface $redirectHandler, |
1172
|
|
|
FactoryInterface $factory, |
1173
|
|
|
NewResourceFactoryInterface $newResourceFactory, |
1174
|
|
|
ResourceInterface $newResource, |
1175
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1176
|
|
|
EventDispatcherInterface $eventDispatcher, |
1177
|
|
|
ResourceControllerEvent $initializeEvent, |
1178
|
|
|
Form $form, |
1179
|
|
|
FormView $formView, |
1180
|
|
|
Request $request, |
1181
|
|
|
Response $response |
1182
|
|
|
): void { |
1183
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1184
|
|
|
$metadata->getName()->willReturn('product'); |
1185
|
|
|
|
1186
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1187
|
|
|
$configuration->hasPermission()->willReturn(true); |
1188
|
|
|
$configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create'); |
1189
|
|
|
|
1190
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true); |
1191
|
|
|
|
1192
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1193
|
|
|
$configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig'); |
1194
|
|
|
|
1195
|
|
|
$newResourceFactory->create($configuration, $factory)->willReturn($newResource); |
|
|
|
|
1196
|
|
|
$resourceFormFactory->create($configuration, $newResource)->willReturn($form); |
1197
|
|
|
|
1198
|
|
|
$request->isMethod('POST')->willReturn(false); |
1199
|
|
|
$form->createView()->shouldNotBeCalled(); |
1200
|
|
|
|
1201
|
|
|
$eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($initializeEvent); |
1202
|
|
|
$initializeEvent->hasResponse()->willReturn(true); |
1203
|
|
|
$initializeEvent->getResponse()->willReturn($response); |
1204
|
|
|
|
1205
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled(); |
1206
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled(); |
1207
|
|
|
$redirectHandler->redirectToResource($configuration, $newResource)->shouldNotBeCalled(); |
1208
|
|
|
|
1209
|
|
|
$expectedView = View::create() |
1210
|
|
|
->setData([ |
1211
|
|
|
'configuration' => $configuration, |
1212
|
|
|
'metadata' => $metadata, |
1213
|
|
|
'resource' => $newResource, |
1214
|
|
|
'product' => $newResource, |
1215
|
|
|
'form' => $formView, |
1216
|
|
|
]) |
1217
|
|
|
->setTemplate('SyliusShopBundle:Product:create.html.twig') |
1218
|
|
|
; |
1219
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->shouldNotBeCalled(); |
1220
|
|
|
|
1221
|
|
|
$this->createAction($request)->shouldReturn($response); |
1222
|
|
|
} |
1223
|
|
|
|
1224
|
|
|
function it_uses_response_from_initialize_update_event_if_defined( |
1225
|
|
|
MetadataInterface $metadata, |
1226
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1227
|
|
|
RepositoryInterface $repository, |
1228
|
|
|
ObjectManager $manager, |
1229
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1230
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1231
|
|
|
RedirectHandlerInterface $redirectHandler, |
1232
|
|
|
FlashHelperInterface $flashHelper, |
1233
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1234
|
|
|
EventDispatcherInterface $eventDispatcher, |
1235
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
1236
|
|
|
RequestConfiguration $configuration, |
1237
|
|
|
ResourceInterface $resource, |
1238
|
|
|
Form $form, |
1239
|
|
|
ResourceControllerEvent $initializeEvent, |
1240
|
|
|
Request $request, |
1241
|
|
|
Response $response |
1242
|
|
|
): void { |
1243
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1244
|
|
|
$metadata->getName()->willReturn('product'); |
1245
|
|
|
|
1246
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1247
|
|
|
$configuration->hasPermission()->willReturn(true); |
1248
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
1249
|
|
|
$configuration->hasStateMachine()->willReturn(false); |
1250
|
|
|
|
1251
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
1252
|
|
|
|
1253
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1254
|
|
|
$configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig'); |
1255
|
|
|
|
1256
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1257
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
1258
|
|
|
|
1259
|
|
|
$request->getMethod()->willReturn('GET'); |
1260
|
|
|
|
1261
|
|
|
$form->handleRequest($request)->willReturn($form); |
1262
|
|
|
$form->isSubmitted()->willReturn(false); |
1263
|
|
|
$form->isValid()->willReturn(false); |
1264
|
|
|
|
1265
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled(); |
1266
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldNotBeCalled(); |
1267
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled(); |
1268
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled(); |
1269
|
|
|
$redirectHandler->redirectToResource($configuration, $resource)->shouldNotBeCalled(); |
1270
|
|
|
|
1271
|
|
|
$eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($initializeEvent); |
1272
|
|
|
$initializeEvent->hasResponse()->willReturn(true); |
1273
|
|
|
$initializeEvent->getResponse()->willReturn($response); |
1274
|
|
|
|
1275
|
|
|
$this->updateAction($request)->shouldReturn($response); |
1276
|
|
|
} |
1277
|
|
|
|
1278
|
|
|
function it_returns_a_non_html_response_for_correctly_updated_resource( |
1279
|
|
|
MetadataInterface $metadata, |
1280
|
|
|
ParameterBagInterface $parameterBag, |
1281
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1282
|
|
|
ViewHandlerInterface $viewHandler, |
1283
|
|
|
RepositoryInterface $repository, |
1284
|
|
|
ObjectManager $manager, |
1285
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1286
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1287
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1288
|
|
|
EventDispatcherInterface $eventDispatcher, |
1289
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
1290
|
|
|
RequestConfiguration $configuration, |
1291
|
|
|
ResourceInterface $resource, |
1292
|
|
|
ResourceControllerEvent $event, |
1293
|
|
|
Form $form, |
1294
|
|
|
Request $request, |
1295
|
|
|
Response $response |
1296
|
|
|
): void { |
1297
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1298
|
|
|
$metadata->getName()->willReturn('product'); |
1299
|
|
|
|
1300
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1301
|
|
|
$configuration->hasPermission()->willReturn(true); |
1302
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
1303
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
1304
|
|
|
$configuration->hasStateMachine()->willReturn(false); |
1305
|
|
|
|
1306
|
|
|
$configuration->getParameters()->willReturn($parameterBag); |
1307
|
|
|
$parameterBag->get('return_content', false)->willReturn(false); |
1308
|
|
|
|
1309
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
1310
|
|
|
|
1311
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1312
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
1313
|
|
|
|
1314
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
1315
|
|
|
$request->getMethod()->willReturn('PUT'); |
1316
|
|
|
|
1317
|
|
|
$form->handleRequest($request)->willReturn($form); |
1318
|
|
|
$form->isValid()->willReturn(true); |
1319
|
|
|
$form->getData()->willReturn($resource); |
1320
|
|
|
|
1321
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
1322
|
|
|
$event->isStopped()->willReturn(false); |
1323
|
|
|
|
1324
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
1325
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled(); |
1326
|
|
|
|
1327
|
|
|
$expectedView = View::create(null, 204); |
1328
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
1329
|
|
|
|
1330
|
|
|
$this->updateAction($request)->shouldReturn($response); |
1331
|
|
|
} |
1332
|
|
|
|
1333
|
|
|
function it_does_not_update_the_resource_throws_a_http_exception_for_non_html_requests_stopped_via_event( |
1334
|
|
|
MetadataInterface $metadata, |
1335
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1336
|
|
|
RequestConfiguration $configuration, |
1337
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1338
|
|
|
ObjectManager $manager, |
1339
|
|
|
RepositoryInterface $repository, |
1340
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1341
|
|
|
ResourceInterface $resource, |
1342
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1343
|
|
|
EventDispatcherInterface $eventDispatcher, |
1344
|
|
|
ResourceControllerEvent $event, |
1345
|
|
|
Form $form, |
1346
|
|
|
Request $request |
1347
|
|
|
): void { |
1348
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1349
|
|
|
$metadata->getName()->willReturn('product'); |
1350
|
|
|
|
1351
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1352
|
|
|
$configuration->hasPermission()->willReturn(true); |
1353
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
1354
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
1355
|
|
|
|
1356
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
1357
|
|
|
|
1358
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1359
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
1360
|
|
|
|
1361
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
1362
|
|
|
$request->getMethod()->willReturn('PUT'); |
1363
|
|
|
|
1364
|
|
|
$form->handleRequest($request)->willReturn($form); |
1365
|
|
|
$form->isValid()->willReturn(true); |
1366
|
|
|
$form->getData()->willReturn($resource); |
1367
|
|
|
|
1368
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
1369
|
|
|
$event->isStopped()->willReturn(true); |
1370
|
|
|
$event->getMessage()->willReturn('Cannot update this channel.'); |
1371
|
|
|
$event->getErrorCode()->willReturn(500); |
1372
|
|
|
|
1373
|
|
|
$manager->flush()->shouldNotBeCalled(); |
1374
|
|
|
$eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1375
|
|
|
|
1376
|
|
|
$this |
1377
|
|
|
->shouldThrow(new HttpException(500, 'Cannot update this channel.')) |
1378
|
|
|
->during('updateAction', [$request]) |
1379
|
|
|
; |
1380
|
|
|
} |
1381
|
|
|
|
1382
|
|
|
function it_applies_state_machine_transition_to_updated_resource_if_configured( |
1383
|
|
|
MetadataInterface $metadata, |
1384
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1385
|
|
|
RepositoryInterface $repository, |
1386
|
|
|
ObjectManager $manager, |
1387
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1388
|
|
|
ResourceFormFactoryInterface $resourceFormFactory, |
1389
|
|
|
RedirectHandlerInterface $redirectHandler, |
1390
|
|
|
FlashHelperInterface $flashHelper, |
1391
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1392
|
|
|
EventDispatcherInterface $eventDispatcher, |
1393
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
1394
|
|
|
RequestConfiguration $configuration, |
1395
|
|
|
ResourceInterface $resource, |
1396
|
|
|
Form $form, |
1397
|
|
|
ResourceControllerEvent $preEvent, |
1398
|
|
|
ResourceControllerEvent $postEvent, |
1399
|
|
|
Request $request, |
1400
|
|
|
Response $redirectResponse |
1401
|
|
|
): void { |
1402
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1403
|
|
|
$metadata->getName()->willReturn('product'); |
1404
|
|
|
|
1405
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1406
|
|
|
$configuration->hasPermission()->willReturn(true); |
1407
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
1408
|
|
|
$configuration->hasStateMachine()->willReturn(true); |
1409
|
|
|
|
1410
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
1411
|
|
|
|
1412
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1413
|
|
|
$configuration->getTemplate(ResourceActions::UPDATE)->willReturn('SyliusShopBundle:Product:update.html.twig'); |
1414
|
|
|
|
1415
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1416
|
|
|
$resourceFormFactory->create($configuration, $resource)->willReturn($form); |
1417
|
|
|
|
1418
|
|
|
$request->isMethod('PATCH')->willReturn(false); |
1419
|
|
|
$request->getMethod()->willReturn('PUT'); |
1420
|
|
|
|
1421
|
|
|
$form->handleRequest($request)->willReturn($form); |
1422
|
|
|
|
1423
|
|
|
$form->isSubmitted()->willReturn(true); |
1424
|
|
|
$form->isValid()->willReturn(true); |
1425
|
|
|
$form->getData()->willReturn($resource); |
1426
|
|
|
|
1427
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent); |
1428
|
|
|
$preEvent->isStopped()->willReturn(false); |
1429
|
|
|
|
1430
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
1431
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent); |
1432
|
|
|
|
1433
|
|
|
$postEvent->hasResponse()->willReturn(false); |
1434
|
|
|
|
1435
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled(); |
1436
|
|
|
$redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse); |
1437
|
|
|
|
1438
|
|
|
$this->updateAction($request)->shouldReturn($redirectResponse); |
1439
|
|
|
} |
1440
|
|
|
|
1441
|
|
|
function it_throws_a_403_exception_if_user_is_unauthorized_to_delete_multiple_resources( |
1442
|
|
|
MetadataInterface $metadata, |
1443
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1444
|
|
|
RequestConfiguration $configuration, |
1445
|
|
|
Request $request, |
1446
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
1447
|
|
|
): void { |
1448
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1449
|
|
|
$configuration->hasPermission()->willReturn(true); |
1450
|
|
|
$configuration->getPermission(ResourceActions::BULK_DELETE)->willReturn('sylius.product.bulk_delete'); |
1451
|
|
|
|
1452
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.bulk_delete')->willReturn(false); |
1453
|
|
|
|
1454
|
|
|
$this |
1455
|
|
|
->shouldThrow(new AccessDeniedException()) |
1456
|
|
|
->during('bulkDeleteAction', [$request]) |
1457
|
|
|
; |
1458
|
|
|
} |
1459
|
|
|
|
1460
|
|
|
function it_deletes_multiple_resources_and_redirects_to_index_for_html_request( |
1461
|
|
|
MetadataInterface $metadata, |
1462
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1463
|
|
|
RequestConfiguration $configuration, |
1464
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1465
|
|
|
RepositoryInterface $repository, |
1466
|
|
|
ResourcesCollectionProviderInterface $resourcesCollectionProvider, |
1467
|
|
|
ResourceInterface $firstResource, |
1468
|
|
|
ResourceInterface $secondResource, |
1469
|
|
|
RedirectHandlerInterface $redirectHandler, |
1470
|
|
|
FlashHelperInterface $flashHelper, |
1471
|
|
|
EventDispatcherInterface $eventDispatcher, |
1472
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1473
|
|
|
ContainerInterface $container, |
1474
|
|
|
ResourceControllerEvent $firstPreEvent, |
1475
|
|
|
ResourceControllerEvent $secondPreEvent, |
1476
|
|
|
ResourceControllerEvent $firstPostEvent, |
1477
|
|
|
ResourceControllerEvent $secondPostEvent, |
1478
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1479
|
|
|
Request $request, |
1480
|
|
|
Response $redirectResponse |
1481
|
|
|
): void { |
1482
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1483
|
|
|
$metadata->getName()->willReturn('product'); |
1484
|
|
|
|
1485
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1486
|
|
|
$configuration->hasPermission()->willReturn(true); |
1487
|
|
|
$configuration->getPermission(ResourceActions::BULK_DELETE)->willReturn('sylius.product.bulk_delete'); |
1488
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1489
|
|
|
|
1490
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1491
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1492
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('bulk_delete', 'xyz'))->willReturn(true); |
1493
|
|
|
|
1494
|
|
|
$eventDispatcher |
1495
|
|
|
->dispatchMultiple(ResourceActions::BULK_DELETE, $configuration, [$firstResource, $secondResource]) |
1496
|
|
|
->shouldBeCalled() |
1497
|
|
|
; |
1498
|
|
|
|
1499
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.bulk_delete')->willReturn(true); |
1500
|
|
|
$resourcesCollectionProvider->get($configuration, $repository)->willReturn([$firstResource, $secondResource]); |
1501
|
|
|
|
1502
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1503
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1504
|
|
|
|
1505
|
|
|
$eventDispatcher |
1506
|
|
|
->dispatchPreEvent(ResourceActions::DELETE, $configuration, $firstResource) |
1507
|
|
|
->willReturn($firstPreEvent) |
1508
|
|
|
; |
1509
|
|
|
$firstPreEvent->isStopped()->willReturn(false); |
1510
|
|
|
|
1511
|
|
|
$resourceDeleteHandler->handle($firstResource, $repository)->shouldBeCalled(); |
1512
|
|
|
|
1513
|
|
|
$eventDispatcher |
1514
|
|
|
->dispatchPostEvent(ResourceActions::DELETE, $configuration, $firstResource) |
1515
|
|
|
->willReturn($firstPostEvent) |
1516
|
|
|
; |
1517
|
|
|
$firstPostEvent->hasResponse()->willReturn(false); |
1518
|
|
|
|
1519
|
|
|
$eventDispatcher |
1520
|
|
|
->dispatchPreEvent(ResourceActions::DELETE, $configuration, $secondResource) |
1521
|
|
|
->willReturn($secondPreEvent) |
1522
|
|
|
; |
1523
|
|
|
$secondPreEvent->isStopped()->willReturn(false); |
1524
|
|
|
|
1525
|
|
|
$resourceDeleteHandler->handle($secondResource, $repository)->shouldBeCalled(); |
1526
|
|
|
|
1527
|
|
|
$eventDispatcher |
1528
|
|
|
->dispatchPostEvent(ResourceActions::DELETE, $configuration, $secondResource) |
1529
|
|
|
->willReturn($secondPostEvent) |
1530
|
|
|
; |
1531
|
|
|
$secondPostEvent->hasResponse()->willReturn(false); |
1532
|
|
|
|
1533
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::BULK_DELETE)->shouldBeCalled(); |
1534
|
|
|
|
1535
|
|
|
$redirectHandler->redirectToIndex($configuration)->willReturn($redirectResponse); |
1536
|
|
|
|
1537
|
|
|
$this->bulkDeleteAction($request)->shouldReturn($redirectResponse); |
1538
|
|
|
} |
1539
|
|
|
|
1540
|
|
|
function it_throws_a_403_exception_if_user_is_unauthorized_to_delete_a_single_resource( |
1541
|
|
|
MetadataInterface $metadata, |
1542
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1543
|
|
|
RequestConfiguration $configuration, |
1544
|
|
|
Request $request, |
1545
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
1546
|
|
|
): void { |
1547
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1548
|
|
|
$configuration->hasPermission()->willReturn(true); |
1549
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1550
|
|
|
|
1551
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(false); |
1552
|
|
|
|
1553
|
|
|
$this |
1554
|
|
|
->shouldThrow(new AccessDeniedException()) |
1555
|
|
|
->during('deleteAction', [$request]) |
1556
|
|
|
; |
1557
|
|
|
} |
1558
|
|
|
|
1559
|
|
|
function it_throws_a_404_exception_if_resource_for_deletion_is_not_found_based_on_configuration( |
1560
|
|
|
MetadataInterface $metadata, |
1561
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1562
|
|
|
RequestConfiguration $configuration, |
1563
|
|
|
Request $request, |
1564
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1565
|
|
|
RepositoryInterface $repository, |
1566
|
|
|
SingleResourceProviderInterface $singleResourceProvider |
1567
|
|
|
): void { |
1568
|
|
|
$metadata->getHumanizedName()->willReturn('product'); |
1569
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1570
|
|
|
$configuration->hasPermission()->willReturn(true); |
1571
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1572
|
|
|
|
1573
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1574
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn(null); |
|
|
|
|
1575
|
|
|
|
1576
|
|
|
$this |
1577
|
|
|
->shouldThrow(new NotFoundHttpException('The "product" has not been found')) |
1578
|
|
|
->during('deleteAction', [$request]) |
1579
|
|
|
; |
1580
|
|
|
} |
1581
|
|
|
|
1582
|
|
|
function it_deletes_a_resource_and_redirects_to_index_by_for_html_request( |
1583
|
|
|
MetadataInterface $metadata, |
1584
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1585
|
|
|
RequestConfiguration $configuration, |
1586
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1587
|
|
|
RepositoryInterface $repository, |
1588
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1589
|
|
|
ResourceInterface $resource, |
1590
|
|
|
RedirectHandlerInterface $redirectHandler, |
1591
|
|
|
FlashHelperInterface $flashHelper, |
1592
|
|
|
EventDispatcherInterface $eventDispatcher, |
1593
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1594
|
|
|
ContainerInterface $container, |
1595
|
|
|
ResourceControllerEvent $event, |
1596
|
|
|
ResourceControllerEvent $postEvent, |
1597
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1598
|
|
|
Request $request, |
1599
|
|
|
Response $redirectResponse |
1600
|
|
|
): void { |
1601
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1602
|
|
|
$metadata->getName()->willReturn('product'); |
1603
|
|
|
|
1604
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1605
|
|
|
$configuration->hasPermission()->willReturn(true); |
1606
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1607
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1608
|
|
|
|
1609
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1610
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1611
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(true); |
1612
|
|
|
|
1613
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1614
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1615
|
|
|
$resource->getId()->willReturn(1); |
1616
|
|
|
|
1617
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1618
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1619
|
|
|
|
1620
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1621
|
|
|
$event->isStopped()->willReturn(false); |
1622
|
|
|
|
1623
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled(); |
1624
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($postEvent); |
1625
|
|
|
|
1626
|
|
|
$postEvent->hasResponse()->willReturn(false); |
1627
|
|
|
|
1628
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled(); |
1629
|
|
|
$redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse); |
1630
|
|
|
|
1631
|
|
|
$this->deleteAction($request)->shouldReturn($redirectResponse); |
1632
|
|
|
} |
1633
|
|
|
|
1634
|
|
|
function it_uses_response_from_post_delete_event_if_defined( |
1635
|
|
|
MetadataInterface $metadata, |
1636
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1637
|
|
|
RequestConfiguration $configuration, |
1638
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1639
|
|
|
RepositoryInterface $repository, |
1640
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1641
|
|
|
ResourceInterface $resource, |
1642
|
|
|
FlashHelperInterface $flashHelper, |
1643
|
|
|
EventDispatcherInterface $eventDispatcher, |
1644
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1645
|
|
|
ContainerInterface $container, |
1646
|
|
|
ResourceControllerEvent $event, |
1647
|
|
|
ResourceControllerEvent $postEvent, |
1648
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1649
|
|
|
Request $request, |
1650
|
|
|
Response $redirectResponse |
1651
|
|
|
): void { |
1652
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1653
|
|
|
$metadata->getName()->willReturn('product'); |
1654
|
|
|
|
1655
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1656
|
|
|
$configuration->hasPermission()->willReturn(true); |
1657
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1658
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1659
|
|
|
|
1660
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1661
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1662
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(true); |
1663
|
|
|
|
1664
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1665
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1666
|
|
|
$resource->getId()->willReturn(1); |
1667
|
|
|
|
1668
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1669
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1670
|
|
|
|
1671
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1672
|
|
|
$event->isStopped()->willReturn(false); |
1673
|
|
|
|
1674
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled(); |
1675
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($postEvent); |
1676
|
|
|
|
1677
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled(); |
1678
|
|
|
|
1679
|
|
|
$postEvent->hasResponse()->willReturn(true); |
1680
|
|
|
$postEvent->getResponse()->willReturn($redirectResponse); |
1681
|
|
|
|
1682
|
|
|
$this->deleteAction($request)->shouldReturn($redirectResponse); |
1683
|
|
|
} |
1684
|
|
|
|
1685
|
|
|
function it_does_not_delete_a_resource_and_redirects_to_index_for_html_requests_stopped_via_event( |
1686
|
|
|
MetadataInterface $metadata, |
1687
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1688
|
|
|
RequestConfiguration $configuration, |
1689
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1690
|
|
|
RepositoryInterface $repository, |
1691
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1692
|
|
|
ResourceInterface $resource, |
1693
|
|
|
RedirectHandlerInterface $redirectHandler, |
1694
|
|
|
FlashHelperInterface $flashHelper, |
1695
|
|
|
EventDispatcherInterface $eventDispatcher, |
1696
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1697
|
|
|
ContainerInterface $container, |
1698
|
|
|
ResourceControllerEvent $event, |
1699
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1700
|
|
|
Request $request, |
1701
|
|
|
Response $redirectResponse |
1702
|
|
|
): void { |
1703
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1704
|
|
|
$metadata->getName()->willReturn('product'); |
1705
|
|
|
|
1706
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1707
|
|
|
$configuration->hasPermission()->willReturn(true); |
1708
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1709
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1710
|
|
|
|
1711
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1712
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1713
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(true); |
1714
|
|
|
|
1715
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1716
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1717
|
|
|
$resource->getId()->willReturn(1); |
1718
|
|
|
|
1719
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1720
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1721
|
|
|
|
1722
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1723
|
|
|
$event->isStopped()->willReturn(true); |
1724
|
|
|
$event->hasResponse()->willReturn(false); |
1725
|
|
|
|
1726
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled(); |
1727
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled(); |
1728
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled(); |
1729
|
|
|
|
1730
|
|
|
$flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled(); |
1731
|
|
|
$redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse); |
1732
|
|
|
|
1733
|
|
|
$this->deleteAction($request)->shouldReturn($redirectResponse); |
1734
|
|
|
} |
1735
|
|
|
|
1736
|
|
|
function it_does_not_delete_a_resource_and_uses_response_from_event_if_defined( |
1737
|
|
|
MetadataInterface $metadata, |
1738
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1739
|
|
|
RequestConfiguration $configuration, |
1740
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1741
|
|
|
RepositoryInterface $repository, |
1742
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1743
|
|
|
ResourceInterface $resource, |
1744
|
|
|
RedirectHandlerInterface $redirectHandler, |
1745
|
|
|
FlashHelperInterface $flashHelper, |
1746
|
|
|
EventDispatcherInterface $eventDispatcher, |
1747
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1748
|
|
|
ContainerInterface $container, |
1749
|
|
|
ResourceControllerEvent $event, |
1750
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1751
|
|
|
Request $request, |
1752
|
|
|
Response $redirectResponse |
1753
|
|
|
): void { |
1754
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1755
|
|
|
$metadata->getName()->willReturn('product'); |
1756
|
|
|
|
1757
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1758
|
|
|
$configuration->hasPermission()->willReturn(true); |
1759
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1760
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1761
|
|
|
|
1762
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1763
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1764
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(true); |
1765
|
|
|
|
1766
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1767
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1768
|
|
|
$resource->getId()->willReturn(1); |
1769
|
|
|
|
1770
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1771
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1772
|
|
|
|
1773
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1774
|
|
|
$event->isStopped()->willReturn(true); |
1775
|
|
|
|
1776
|
|
|
$flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled(); |
1777
|
|
|
|
1778
|
|
|
$event->hasResponse()->willReturn(true); |
1779
|
|
|
$event->getResponse()->willReturn($redirectResponse); |
1780
|
|
|
|
1781
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled(); |
1782
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled(); |
1783
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled(); |
1784
|
|
|
|
1785
|
|
|
$redirectHandler->redirectToIndex($configuration, $resource)->shouldNotBeCalled(); |
1786
|
|
|
|
1787
|
|
|
$this->deleteAction($request)->shouldReturn($redirectResponse); |
1788
|
|
|
} |
1789
|
|
|
|
1790
|
|
|
function it_does_not_correctly_delete_a_resource_and_returns_500_for_not_html_response( |
1791
|
|
|
MetadataInterface $metadata, |
1792
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1793
|
|
|
RequestConfiguration $configuration, |
1794
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1795
|
|
|
ViewHandlerInterface $viewHandler, |
1796
|
|
|
RepositoryInterface $repository, |
1797
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1798
|
|
|
ResourceInterface $resource, |
1799
|
|
|
EventDispatcherInterface $eventDispatcher, |
1800
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1801
|
|
|
ContainerInterface $container, |
1802
|
|
|
ResourceControllerEvent $event, |
1803
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1804
|
|
|
Request $request, |
1805
|
|
|
Response $response |
1806
|
|
|
): void { |
1807
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1808
|
|
|
$metadata->getName()->willReturn('product'); |
1809
|
|
|
|
1810
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1811
|
|
|
$configuration->hasPermission()->willReturn(true); |
1812
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1813
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1814
|
|
|
|
1815
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1816
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1817
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(true); |
1818
|
|
|
|
1819
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1820
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1821
|
|
|
$resource->getId()->willReturn(1); |
1822
|
|
|
|
1823
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
1824
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1825
|
|
|
|
1826
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1827
|
|
|
$event->isStopped()->willReturn(false); |
1828
|
|
|
|
1829
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->willThrow(new DeleteHandlingException()); |
1830
|
|
|
|
1831
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled(); |
1832
|
|
|
|
1833
|
|
|
$expectedView = View::create(null, 500); |
1834
|
|
|
|
1835
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
1836
|
|
|
|
1837
|
|
|
$this->deleteAction($request)->shouldReturn($response); |
1838
|
|
|
} |
1839
|
|
|
|
1840
|
|
|
function it_deletes_a_resource_and_returns_204_for_non_html_requests( |
1841
|
|
|
MetadataInterface $metadata, |
1842
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1843
|
|
|
RequestConfiguration $configuration, |
1844
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1845
|
|
|
ViewHandlerInterface $viewHandler, |
1846
|
|
|
RepositoryInterface $repository, |
1847
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1848
|
|
|
ResourceInterface $resource, |
1849
|
|
|
EventDispatcherInterface $eventDispatcher, |
1850
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1851
|
|
|
ContainerInterface $container, |
1852
|
|
|
ResourceControllerEvent $event, |
1853
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1854
|
|
|
Request $request, |
1855
|
|
|
Response $response |
1856
|
|
|
): void { |
1857
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1858
|
|
|
$metadata->getName()->willReturn('product'); |
1859
|
|
|
|
1860
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1861
|
|
|
$configuration->hasPermission()->willReturn(true); |
1862
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1863
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1864
|
|
|
|
1865
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1866
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1867
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(true); |
1868
|
|
|
|
1869
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1870
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1871
|
|
|
$resource->getId()->willReturn(1); |
1872
|
|
|
|
1873
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
1874
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1875
|
|
|
|
1876
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1877
|
|
|
$event->isStopped()->willReturn(false); |
1878
|
|
|
|
1879
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled(); |
1880
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldBeCalled(); |
1881
|
|
|
|
1882
|
|
|
$expectedView = View::create(null, 204); |
1883
|
|
|
|
1884
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
1885
|
|
|
|
1886
|
|
|
$this->deleteAction($request)->shouldReturn($response); |
1887
|
|
|
} |
1888
|
|
|
|
1889
|
|
|
function it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event( |
1890
|
|
|
MetadataInterface $metadata, |
1891
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1892
|
|
|
RequestConfiguration $configuration, |
1893
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1894
|
|
|
RepositoryInterface $repository, |
1895
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1896
|
|
|
ResourceInterface $resource, |
1897
|
|
|
FlashHelperInterface $flashHelper, |
1898
|
|
|
EventDispatcherInterface $eventDispatcher, |
1899
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1900
|
|
|
ContainerInterface $container, |
1901
|
|
|
ResourceControllerEvent $event, |
1902
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1903
|
|
|
Request $request |
1904
|
|
|
): void { |
1905
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1906
|
|
|
$metadata->getName()->willReturn('product'); |
1907
|
|
|
|
1908
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1909
|
|
|
$configuration->hasPermission()->willReturn(true); |
1910
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1911
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1912
|
|
|
|
1913
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1914
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1915
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(true); |
1916
|
|
|
|
1917
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1918
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1919
|
|
|
$resource->getId()->willReturn(1); |
1920
|
|
|
|
1921
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
1922
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1923
|
|
|
|
1924
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1925
|
|
|
$event->isStopped()->willReturn(true); |
1926
|
|
|
$event->getMessage()->willReturn('Cannot delete this product.'); |
1927
|
|
|
$event->getErrorCode()->willReturn(500); |
1928
|
|
|
|
1929
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled(); |
1930
|
|
|
|
1931
|
|
|
$eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1932
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1933
|
|
|
$flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1934
|
|
|
|
1935
|
|
|
$this |
1936
|
|
|
->shouldThrow(new HttpException(500, 'Cannot delete this product.')) |
1937
|
|
|
->during('deleteAction', [$request]) |
1938
|
|
|
; |
1939
|
|
|
} |
1940
|
|
|
|
1941
|
|
|
function it_throws_a_403_exception_if_csrf_token_is_invalid_during_delete_action( |
1942
|
|
|
MetadataInterface $metadata, |
1943
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1944
|
|
|
RequestConfiguration $configuration, |
1945
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
1946
|
|
|
RepositoryInterface $repository, |
1947
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
1948
|
|
|
ResourceInterface $resource, |
1949
|
|
|
FlashHelperInterface $flashHelper, |
1950
|
|
|
EventDispatcherInterface $eventDispatcher, |
1951
|
|
|
CsrfTokenManagerInterface $csrfTokenManager, |
1952
|
|
|
ContainerInterface $container, |
1953
|
|
|
ResourceControllerEvent $event, |
1954
|
|
|
ResourceDeleteHandlerInterface $resourceDeleteHandler, |
1955
|
|
|
Request $request |
1956
|
|
|
): void { |
1957
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
1958
|
|
|
$metadata->getName()->willReturn('product'); |
1959
|
|
|
|
1960
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1961
|
|
|
$configuration->hasPermission()->willReturn(true); |
1962
|
|
|
$configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete'); |
1963
|
|
|
$request->request = new ParameterBag(['_csrf_token' => 'xyz']); |
1964
|
|
|
|
1965
|
|
|
$container->has('security.csrf.token_manager')->willReturn(true); |
1966
|
|
|
$container->get('security.csrf.token_manager')->willReturn($csrfTokenManager); |
1967
|
|
|
$csrfTokenManager->isTokenValid(new CsrfToken('1', 'xyz'))->willReturn(false); |
1968
|
|
|
|
1969
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true); |
1970
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
1971
|
|
|
$resource->getId()->willReturn(1); |
1972
|
|
|
|
1973
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
1974
|
|
|
$configuration->isCsrfProtectionEnabled()->willReturn(true); |
1975
|
|
|
|
1976
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event); |
1977
|
|
|
$event->isStopped()->shouldNotBeCalled(); |
1978
|
|
|
|
1979
|
|
|
$resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled(); |
1980
|
|
|
|
1981
|
|
|
$eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1982
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1983
|
|
|
$flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
1984
|
|
|
|
1985
|
|
|
$this |
1986
|
|
|
->shouldThrow(new HttpException(403, 'Invalid csrf token.')) |
1987
|
|
|
->during('deleteAction', [$request]) |
1988
|
|
|
; |
1989
|
|
|
} |
1990
|
|
|
|
1991
|
|
|
function it_throws_a_403_exception_if_user_is_unauthorized_to_apply_state_machine_transition_on_resource( |
1992
|
|
|
MetadataInterface $metadata, |
1993
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
1994
|
|
|
RequestConfiguration $configuration, |
1995
|
|
|
Request $request, |
1996
|
|
|
AuthorizationCheckerInterface $authorizationChecker |
1997
|
|
|
): void { |
1998
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
1999
|
|
|
$configuration->hasPermission()->willReturn(true); |
2000
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2001
|
|
|
|
2002
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false); |
2003
|
|
|
|
2004
|
|
|
$this |
2005
|
|
|
->shouldThrow(new AccessDeniedException()) |
2006
|
|
|
->during('applyStateMachineTransitionAction', [$request]) |
2007
|
|
|
; |
2008
|
|
|
} |
2009
|
|
|
|
2010
|
|
|
function it_throws_a_404_exception_if_resource_is_not_found_when_trying_to_apply_state_machine_transition( |
2011
|
|
|
MetadataInterface $metadata, |
2012
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2013
|
|
|
RequestConfiguration $configuration, |
2014
|
|
|
Request $request, |
2015
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2016
|
|
|
RepositoryInterface $repository, |
2017
|
|
|
SingleResourceProviderInterface $singleResourceProvider |
2018
|
|
|
): void { |
2019
|
|
|
$metadata->getHumanizedName()->willReturn('product'); |
2020
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2021
|
|
|
$configuration->hasPermission()->willReturn(true); |
2022
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2023
|
|
|
|
2024
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2025
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn(null); |
|
|
|
|
2026
|
|
|
|
2027
|
|
|
$this |
2028
|
|
|
->shouldThrow(new NotFoundHttpException('The "product" has not been found')) |
2029
|
|
|
->during('applyStateMachineTransitionAction', [$request]) |
2030
|
|
|
; |
2031
|
|
|
} |
2032
|
|
|
|
2033
|
|
|
function it_does_not_apply_state_machine_transition_on_resource_if_not_applicable_and_returns_400_bad_request( |
2034
|
|
|
MetadataInterface $metadata, |
2035
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2036
|
|
|
RequestConfiguration $configuration, |
2037
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2038
|
|
|
RepositoryInterface $repository, |
2039
|
|
|
ObjectManager $objectManager, |
2040
|
|
|
StateMachineInterface $stateMachine, |
2041
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2042
|
|
|
ResourceInterface $resource, |
2043
|
|
|
FlashHelperInterface $flashHelper, |
2044
|
|
|
EventDispatcherInterface $eventDispatcher, |
2045
|
|
|
ResourceControllerEvent $event, |
2046
|
|
|
Request $request |
2047
|
|
|
): void { |
2048
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2049
|
|
|
$metadata->getName()->willReturn('product'); |
2050
|
|
|
|
2051
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2052
|
|
|
$configuration->hasPermission()->willReturn(true); |
2053
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2054
|
|
|
|
2055
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2056
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2057
|
|
|
|
2058
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
2059
|
|
|
|
2060
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2061
|
|
|
$event->isStopped()->willReturn(false); |
2062
|
|
|
|
2063
|
|
|
$stateMachine->can($configuration, $resource)->willReturn(false); |
2064
|
|
|
|
2065
|
|
|
$stateMachine->apply($configuration, $resource)->shouldNotBeCalled(); |
2066
|
|
|
$objectManager->flush()->shouldNotBeCalled(); |
2067
|
|
|
|
2068
|
|
|
$eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
2069
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
2070
|
|
|
$flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
2071
|
|
|
|
2072
|
|
|
$this |
2073
|
|
|
->shouldThrow(new BadRequestHttpException()) |
2074
|
|
|
->during('applyStateMachineTransitionAction', [$request]) |
2075
|
|
|
; |
2076
|
|
|
} |
2077
|
|
|
|
2078
|
|
|
function it_applies_state_machine_transition_to_resource_and_redirects_for_html_request( |
2079
|
|
|
MetadataInterface $metadata, |
2080
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2081
|
|
|
RepositoryInterface $repository, |
2082
|
|
|
ObjectManager $manager, |
2083
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2084
|
|
|
RedirectHandlerInterface $redirectHandler, |
2085
|
|
|
FlashHelperInterface $flashHelper, |
2086
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2087
|
|
|
EventDispatcherInterface $eventDispatcher, |
2088
|
|
|
StateMachineInterface $stateMachine, |
2089
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
2090
|
|
|
RequestConfiguration $configuration, |
2091
|
|
|
ResourceInterface $resource, |
2092
|
|
|
ResourceControllerEvent $event, |
2093
|
|
|
ResourceControllerEvent $postEvent, |
2094
|
|
|
Request $request, |
2095
|
|
|
Response $redirectResponse |
2096
|
|
|
): void { |
2097
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2098
|
|
|
$metadata->getName()->willReturn('product'); |
2099
|
|
|
|
2100
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2101
|
|
|
$configuration->hasPermission()->willReturn(true); |
2102
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2103
|
|
|
|
2104
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2105
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2106
|
|
|
|
2107
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
2108
|
|
|
|
2109
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2110
|
|
|
$event->isStopped()->willReturn(false); |
2111
|
|
|
|
2112
|
|
|
$stateMachine->can($configuration, $resource)->willReturn(true); |
2113
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
2114
|
|
|
|
2115
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled(); |
2116
|
|
|
|
2117
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent); |
2118
|
|
|
|
2119
|
|
|
$postEvent->hasResponse()->willReturn(false); |
2120
|
|
|
|
2121
|
|
|
$redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse); |
2122
|
|
|
|
2123
|
|
|
$this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse); |
2124
|
|
|
} |
2125
|
|
|
|
2126
|
|
|
function it_uses_response_from_post_apply_state_machine_transition_event_if_defined( |
2127
|
|
|
MetadataInterface $metadata, |
2128
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2129
|
|
|
RepositoryInterface $repository, |
2130
|
|
|
ObjectManager $manager, |
2131
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2132
|
|
|
RedirectHandlerInterface $redirectHandler, |
2133
|
|
|
FlashHelperInterface $flashHelper, |
2134
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2135
|
|
|
EventDispatcherInterface $eventDispatcher, |
2136
|
|
|
StateMachineInterface $stateMachine, |
2137
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
2138
|
|
|
RequestConfiguration $configuration, |
2139
|
|
|
ResourceInterface $resource, |
2140
|
|
|
ResourceControllerEvent $event, |
2141
|
|
|
ResourceControllerEvent $postEvent, |
2142
|
|
|
Request $request, |
2143
|
|
|
Response $redirectResponse |
2144
|
|
|
): void { |
2145
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2146
|
|
|
$metadata->getName()->willReturn('product'); |
2147
|
|
|
|
2148
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2149
|
|
|
$configuration->hasPermission()->willReturn(true); |
2150
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2151
|
|
|
|
2152
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2153
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2154
|
|
|
|
2155
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
2156
|
|
|
|
2157
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2158
|
|
|
$event->isStopped()->willReturn(false); |
2159
|
|
|
|
2160
|
|
|
$stateMachine->can($configuration, $resource)->willReturn(true); |
2161
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
2162
|
|
|
|
2163
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled(); |
2164
|
|
|
|
2165
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent); |
2166
|
|
|
|
2167
|
|
|
$postEvent->hasResponse()->willReturn(true); |
2168
|
|
|
$postEvent->getResponse()->willReturn($redirectResponse); |
2169
|
|
|
|
2170
|
|
|
$this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse); |
2171
|
|
|
} |
2172
|
|
|
|
2173
|
|
|
function it_does_not_apply_state_machine_transition_on_resource_and_redirects_for_html_requests_stopped_via_event( |
2174
|
|
|
MetadataInterface $metadata, |
2175
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2176
|
|
|
RequestConfiguration $configuration, |
2177
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2178
|
|
|
StateMachineInterface $stateMachine, |
2179
|
|
|
ObjectManager $manager, |
2180
|
|
|
RepositoryInterface $repository, |
2181
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2182
|
|
|
ResourceInterface $resource, |
2183
|
|
|
RedirectHandlerInterface $redirectHandler, |
2184
|
|
|
FlashHelperInterface $flashHelper, |
2185
|
|
|
EventDispatcherInterface $eventDispatcher, |
2186
|
|
|
ResourceControllerEvent $event, |
2187
|
|
|
Request $request, |
2188
|
|
|
Response $redirectResponse |
2189
|
|
|
): void { |
2190
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2191
|
|
|
$metadata->getName()->willReturn('product'); |
2192
|
|
|
|
2193
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2194
|
|
|
$configuration->hasPermission()->willReturn(true); |
2195
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2196
|
|
|
|
2197
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2198
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2199
|
|
|
|
2200
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
2201
|
|
|
|
2202
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2203
|
|
|
$event->isStopped()->willReturn(true); |
2204
|
|
|
|
2205
|
|
|
$manager->flush()->shouldNotBeCalled(); |
2206
|
|
|
$stateMachine->apply($resource)->shouldNotBeCalled(); |
|
|
|
|
2207
|
|
|
|
2208
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled(); |
2209
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled(); |
2210
|
|
|
|
2211
|
|
|
$event->hasResponse()->willReturn(false); |
2212
|
|
|
|
2213
|
|
|
$flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled(); |
2214
|
|
|
$redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse); |
2215
|
|
|
|
2216
|
|
|
$this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse); |
2217
|
|
|
} |
2218
|
|
|
|
2219
|
|
|
function it_does_not_apply_state_machine_transition_on_resource_and_return_event_response_for_html_requests_stopped_via_event( |
2220
|
|
|
MetadataInterface $metadata, |
2221
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2222
|
|
|
RequestConfiguration $configuration, |
2223
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2224
|
|
|
StateMachineInterface $stateMachine, |
2225
|
|
|
ObjectManager $manager, |
2226
|
|
|
RepositoryInterface $repository, |
2227
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2228
|
|
|
ResourceInterface $resource, |
2229
|
|
|
FlashHelperInterface $flashHelper, |
2230
|
|
|
EventDispatcherInterface $eventDispatcher, |
2231
|
|
|
ResourceControllerEvent $event, |
2232
|
|
|
Request $request, |
2233
|
|
|
Response $response |
2234
|
|
|
): void { |
2235
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2236
|
|
|
$metadata->getName()->willReturn('product'); |
2237
|
|
|
|
2238
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2239
|
|
|
$configuration->hasPermission()->willReturn(true); |
2240
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2241
|
|
|
|
2242
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2243
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2244
|
|
|
|
2245
|
|
|
$configuration->isHtmlRequest()->willReturn(true); |
2246
|
|
|
|
2247
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2248
|
|
|
$event->isStopped()->willReturn(true); |
2249
|
|
|
|
2250
|
|
|
$manager->flush()->shouldNotBeCalled(); |
2251
|
|
|
$stateMachine->apply($resource)->shouldNotBeCalled(); |
|
|
|
|
2252
|
|
|
|
2253
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled(); |
2254
|
|
|
$flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled(); |
2255
|
|
|
|
2256
|
|
|
$flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled(); |
2257
|
|
|
|
2258
|
|
|
$event->hasResponse()->willReturn(true); |
2259
|
|
|
$event->getResponse()->willReturn($response); |
2260
|
|
|
|
2261
|
|
|
$this->applyStateMachineTransitionAction($request)->shouldReturn($response); |
2262
|
|
|
} |
2263
|
|
|
|
2264
|
|
|
function it_applies_state_machine_transition_on_resource_and_returns_200_for_non_html_requests( |
2265
|
|
|
MetadataInterface $metadata, |
2266
|
|
|
ParameterBagInterface $parameterBag, |
2267
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2268
|
|
|
ViewHandlerInterface $viewHandler, |
2269
|
|
|
RepositoryInterface $repository, |
2270
|
|
|
ObjectManager $manager, |
2271
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2272
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2273
|
|
|
EventDispatcherInterface $eventDispatcher, |
2274
|
|
|
StateMachineInterface $stateMachine, |
2275
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
2276
|
|
|
RequestConfiguration $configuration, |
2277
|
|
|
ResourceInterface $resource, |
2278
|
|
|
ResourceControllerEvent $event, |
2279
|
|
|
Request $request, |
2280
|
|
|
Response $response |
2281
|
|
|
): void { |
2282
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2283
|
|
|
$metadata->getName()->willReturn('product'); |
2284
|
|
|
|
2285
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2286
|
|
|
$configuration->getParameters()->willReturn($parameterBag); |
2287
|
|
|
$configuration->hasPermission()->willReturn(true); |
2288
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2289
|
|
|
|
2290
|
|
|
$parameterBag->get('return_content', true)->willReturn(true); |
2291
|
|
|
|
2292
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2293
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2294
|
|
|
|
2295
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
2296
|
|
|
|
2297
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2298
|
|
|
$event->isStopped()->willReturn(false); |
2299
|
|
|
|
2300
|
|
|
$stateMachine->can($configuration, $resource)->willReturn(true); |
2301
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
2302
|
|
|
|
2303
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled(); |
2304
|
|
|
|
2305
|
|
|
$expectedView = View::create($resource, 200); |
2306
|
|
|
|
2307
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
2308
|
|
|
|
2309
|
|
|
$this->applyStateMachineTransitionAction($request)->shouldReturn($response); |
2310
|
|
|
} |
2311
|
|
|
|
2312
|
|
|
function it_applies_state_machine_transition_on_resource_and_returns_204_for_non_html_requests_if_additional_option_added( |
2313
|
|
|
MetadataInterface $metadata, |
2314
|
|
|
ParameterBagInterface $parameterBag, |
2315
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2316
|
|
|
ViewHandlerInterface $viewHandler, |
2317
|
|
|
RepositoryInterface $repository, |
2318
|
|
|
ObjectManager $manager, |
2319
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2320
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2321
|
|
|
EventDispatcherInterface $eventDispatcher, |
2322
|
|
|
StateMachineInterface $stateMachine, |
2323
|
|
|
ResourceUpdateHandlerInterface $resourceUpdateHandler, |
2324
|
|
|
RequestConfiguration $configuration, |
2325
|
|
|
ResourceInterface $resource, |
2326
|
|
|
ResourceControllerEvent $event, |
2327
|
|
|
Request $request, |
2328
|
|
|
Response $response |
2329
|
|
|
): void { |
2330
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2331
|
|
|
$metadata->getName()->willReturn('product'); |
2332
|
|
|
|
2333
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2334
|
|
|
$configuration->getParameters()->willReturn($parameterBag); |
2335
|
|
|
$configuration->hasPermission()->willReturn(true); |
2336
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2337
|
|
|
|
2338
|
|
|
$parameterBag->get('return_content', true)->willReturn(false); |
2339
|
|
|
|
2340
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2341
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2342
|
|
|
|
2343
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
2344
|
|
|
|
2345
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2346
|
|
|
$event->isStopped()->willReturn(false); |
2347
|
|
|
|
2348
|
|
|
$stateMachine->can($configuration, $resource)->willReturn(true); |
2349
|
|
|
$resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled(); |
2350
|
|
|
|
2351
|
|
|
$eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled(); |
2352
|
|
|
|
2353
|
|
|
$expectedView = View::create(null, 204); |
2354
|
|
|
|
2355
|
|
|
$viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response); |
2356
|
|
|
|
2357
|
|
|
$this->applyStateMachineTransitionAction($request)->shouldReturn($response); |
2358
|
|
|
} |
2359
|
|
|
|
2360
|
|
|
function it_does_not_apply_state_machine_transition_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event( |
2361
|
|
|
MetadataInterface $metadata, |
2362
|
|
|
RequestConfigurationFactoryInterface $requestConfigurationFactory, |
2363
|
|
|
RequestConfiguration $configuration, |
2364
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
2365
|
|
|
RepositoryInterface $repository, |
2366
|
|
|
ObjectManager $objectManager, |
2367
|
|
|
StateMachineInterface $stateMachine, |
2368
|
|
|
SingleResourceProviderInterface $singleResourceProvider, |
2369
|
|
|
ResourceInterface $resource, |
2370
|
|
|
FlashHelperInterface $flashHelper, |
2371
|
|
|
EventDispatcherInterface $eventDispatcher, |
2372
|
|
|
ResourceControllerEvent $event, |
2373
|
|
|
Request $request |
2374
|
|
|
): void { |
2375
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
2376
|
|
|
$metadata->getName()->willReturn('product'); |
2377
|
|
|
|
2378
|
|
|
$requestConfigurationFactory->create($metadata, $request)->willReturn($configuration); |
|
|
|
|
2379
|
|
|
$configuration->hasPermission()->willReturn(true); |
2380
|
|
|
$configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update'); |
2381
|
|
|
|
2382
|
|
|
$authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true); |
2383
|
|
|
$singleResourceProvider->get($configuration, $repository)->willReturn($resource); |
|
|
|
|
2384
|
|
|
|
2385
|
|
|
$configuration->isHtmlRequest()->willReturn(false); |
2386
|
|
|
|
2387
|
|
|
$eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event); |
2388
|
|
|
$event->isStopped()->willReturn(true); |
2389
|
|
|
$event->getMessage()->willReturn('Cannot approve this product.'); |
2390
|
|
|
$event->getErrorCode()->willReturn(500); |
2391
|
|
|
|
2392
|
|
|
$stateMachine->apply($configuration, $resource)->shouldNotBeCalled(); |
2393
|
|
|
$objectManager->flush()->shouldNotBeCalled(); |
2394
|
|
|
|
2395
|
|
|
$eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
2396
|
|
|
$flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
2397
|
|
|
$flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled(); |
|
|
|
|
2398
|
|
|
|
2399
|
|
|
$this |
2400
|
|
|
->shouldThrow(new HttpException(500, 'Cannot approve this product.')) |
2401
|
|
|
->during('applyStateMachineTransitionAction', [$request]) |
2402
|
|
|
; |
2403
|
|
|
} |
2404
|
|
|
|
2405
|
|
|
/** |
2406
|
|
|
* {@inheritdoc} |
2407
|
|
|
*/ |
2408
|
|
|
private function getViewComparingCallback(View $expectedView) |
2409
|
|
|
{ |
2410
|
|
|
return function ($value) use ($expectedView) { |
2411
|
|
|
if (!$value instanceof View) { |
|
|
|
|
2412
|
|
|
return false; |
2413
|
|
|
} |
2414
|
|
|
|
2415
|
|
|
// Need to unwrap phpspec's Collaborators to ensure proper comparison. |
2416
|
|
|
$this->unwrapViewData($expectedView); |
2417
|
|
|
$this->nullifyDates($value); |
2418
|
|
|
$this->nullifyDates($expectedView); |
2419
|
|
|
|
2420
|
|
|
return |
2421
|
|
|
$expectedView->getStatusCode() === $value->getStatusCode() && |
2422
|
|
|
$expectedView->getHeaders() === $value->getHeaders() && |
2423
|
|
|
$expectedView->getEngine() === $value->getEngine() && |
2424
|
|
|
$expectedView->getFormat() === $value->getFormat() && |
2425
|
|
|
$expectedView->getData() === $value->getData() && |
2426
|
|
|
$expectedView->getTemplate() === $value->getTemplate() && |
2427
|
|
|
$expectedView->getTemplateVar() === $value->getTemplateVar() |
2428
|
|
|
; |
2429
|
|
|
}; |
2430
|
|
|
} |
2431
|
|
|
|
2432
|
|
|
/** |
2433
|
|
|
* @param View $view |
2434
|
|
|
*/ |
2435
|
|
|
private function unwrapViewData(View $view) |
2436
|
|
|
{ |
2437
|
|
|
$view->setData($this->unwrapIfCollaborator($view->getData())); |
2438
|
|
|
} |
2439
|
|
|
|
2440
|
|
|
/** |
2441
|
|
|
* @param mixed $value |
2442
|
|
|
* |
2443
|
|
|
* @return mixed |
2444
|
|
|
*/ |
2445
|
|
|
private function unwrapIfCollaborator($value) |
2446
|
|
|
{ |
2447
|
|
|
if (null === $value) { |
2448
|
|
|
return null; |
2449
|
|
|
} |
2450
|
|
|
|
2451
|
|
|
if ($value instanceof Collaborator) { |
|
|
|
|
2452
|
|
|
return $value->getWrappedObject(); |
2453
|
|
|
} |
2454
|
|
|
|
2455
|
|
|
if (is_array($value)) { |
2456
|
|
|
foreach ($value as $key => $childValue) { |
2457
|
|
|
$value[$key] = $this->unwrapIfCollaborator($childValue); |
2458
|
|
|
} |
2459
|
|
|
} |
2460
|
|
|
|
2461
|
|
|
return $value; |
2462
|
|
|
} |
2463
|
|
|
|
2464
|
|
|
/** |
2465
|
|
|
* @param View $view |
2466
|
|
|
*/ |
2467
|
|
|
private function nullifyDates(View $view) |
2468
|
|
|
{ |
2469
|
|
|
$headers = $view->getHeaders(); |
2470
|
|
|
unset($headers['date']); |
2471
|
|
|
$view->setHeaders($headers); |
2472
|
|
|
} |
2473
|
|
|
} |
2474
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.