Completed
Push — 1.0-phpstan ( 5ae83f )
by Kamil
27:50
created

ResourceControllerSpec   F

Complexity

Total Complexity 62

Size/Duplication

Total Lines 2096
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 36

Importance

Changes 0
Metric Value
wmc 62
lcom 2
cbo 36
dl 0
loc 2096
rs 0.5217
c 0
b 0
f 0

51 Methods

Rating   Name   Duplication   Size   Complexity  
A it_extends_base_Symfony_controller() 0 4 1
B let() 0 42 1
A it_throws_a_403_exception_if_user_is_unauthorized_to_view_a_single_resource() 0 18 1
A it_throws_a_404_exception_if_resource_is_not_found_based_on_configuration() 0 22 1
B it_returns_a_response_for_html_view_of_a_single_resource() 0 43 1
B it_returns_a_response_for_non_html_view_of_single_resource() 0 33 1
A it_throws_a_403_exception_if_user_is_unauthorized_to_view_an_index_of_resources() 0 18 1
B it_returns_a_response_for_html_view_of_paginated_resources() 0 45 1
A it_throws_a_403_exception_if_user_is_unauthorized_to_create_a_new_resource() 0 18 1
A it_returns_a_html_response_for_creating_new_resource_form() 0 52 1
A it_returns_a_html_response_for_invalid_form_during_resource_creation() 0 54 1
B it_returns_a_non_html_response_for_invalid_form_during_resource_creation() 0 39 1
A it_does_not_create_the_resource_and_redirects_to_index_for_html_requests_stopped_via_events() 0 52 1
A it_redirects_to_newly_created_resource() 0 57 1
A it_uses_response_from_post_create_event_if_defined() 0 54 1
A it_returns_a_non_html_response_for_correctly_created_resources() 0 56 1
A it_does_not_create_the_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event() 0 50 1
A it_throws_a_403_exception_if_user_is_unauthorized_to_edit_a_single_resource() 0 18 1
A it_throws_a_404_exception_if_resource_to_update_is_not_found_based_on_configuration() 0 22 1
A it_returns_a_html_response_for_updating_resource_form() 0 56 1
A it_returns_a_html_response_for_invalid_form_during_resource_update() 0 57 1
B it_returns_a_non_html_response_for_invalid_form_during_resource_update() 0 38 1
A it_does_not_update_the_resource_and_redirects_to_resource_for_html_request_if_stopped_via_event() 0 54 1
A it_redirects_to_updated_resource() 0 58 1
A it_uses_response_from_post_update_event_if_defined() 0 59 1
A it_returns_a_non_html_response_for_correctly_updated_resource() 0 54 1
A it_does_not_update_the_resource_throws_a_http_exception_for_non_html_requests_stopped_via_event() 0 48 1
A it_applies_state_machine_transition_to_updated_resource_if_configured() 0 58 1
A it_throws_a_403_exception_if_user_is_unauthorized_to_delete_a_single_resource() 0 18 1
A it_throws_a_404_exception_if_resource_for_deletion_is_not_found_based_on_configuration() 0 22 1
A it_deletes_a_resource_and_redirects_to_index_by_for_html_request() 0 51 1
A it_uses_response_from_post_delete_event_if_defined() 0 50 1
A it_does_not_delete_a_resource_and_redirects_to_index_for_html_requests_stopped_via_event() 0 50 1
A it_does_not_delete_a_resource_and_uses_response_from_event_if_defined() 0 53 1
A it_does_not_correctly_delete_a_resource_and_returns_500_for_not_html_response() 0 49 1
A it_deletes_a_resource_and_returns_204_for_non_html_requests() 0 48 1
A it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event() 0 51 1
A it_throws_a_403_exception_if_csrf_token_is_invalid_during_delete_action() 0 49 1
A it_throws_a_403_exception_if_user_is_unauthorized_to_apply_state_machine_transition_on_resource() 0 18 1
A it_throws_a_404_exception_if_resource_is_not_found_when_trying_to_apply_state_machine_transition() 0 22 1
B it_does_not_apply_state_machine_transition_on_resource_if_not_applicable_and_returns_400_bad_request() 0 44 1
A it_applies_state_machine_transition_to_resource_and_redirects_for_html_request() 0 47 1
B it_uses_response_from_post_apply_state_machine_transition_event_if_defined() 0 46 1
B it_does_not_apply_state_machine_transition_on_resource_and_redirects_for_html_requests_stopped_via_event() 0 43 1
A it_applies_state_machine_transition_on_resource_and_returns_200_for_non_html_requests() 0 47 1
A it_applies_state_machine_transition_on_resource_and_returns_204_for_non_html_requests_if_additional_option_added() 0 47 1
B it_does_not_apply_state_machine_transition_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event() 0 44 1
C getViewComparingCallback() 0 23 8
A unwrapViewData() 0 4 1
B unwrapIfCollaborator() 0 18 5
A nullifyDates() 0 6 1

How to fix   Complexity   

Complex Class

Complex classes like ResourceControllerSpec often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ResourceControllerSpec, and based on these observations, apply Extract Interface, too.

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
/**
58
 * @author Paweł Jędrzejewski <[email protected]>
59
 */
60
final class ResourceControllerSpec extends ObjectBehavior
61
{
62
    function let(
63
        MetadataInterface $metadata,
64
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
65
        ViewHandlerInterface $viewHandler,
66
        RepositoryInterface $repository,
67
        FactoryInterface $factory,
68
        NewResourceFactoryInterface $newResourceFactory,
69
        ObjectManager $manager,
70
        SingleResourceProviderInterface $singleResourceProvider,
71
        ResourcesCollectionProviderInterface $resourcesCollectionProvider,
72
        ResourceFormFactoryInterface $resourceFormFactory,
73
        RedirectHandlerInterface $redirectHandler,
74
        FlashHelperInterface $flashHelper,
75
        AuthorizationCheckerInterface $authorizationChecker,
76
        EventDispatcherInterface $eventDispatcher,
77
        StateMachineInterface $stateMachine,
78
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
79
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
80
        ContainerInterface $container
81
    ): void {
82
        $this->beConstructedWith(
83
            $metadata,
84
            $requestConfigurationFactory,
85
            $viewHandler,
86
            $repository,
87
            $factory,
88
            $newResourceFactory,
89
            $manager,
90
            $singleResourceProvider,
91
            $resourcesCollectionProvider,
92
            $resourceFormFactory,
93
            $redirectHandler,
94
            $flashHelper,
95
            $authorizationChecker,
96
            $eventDispatcher,
97
            $stateMachine,
98
            $resourceUpdateHandler,
99
            $resourceDeleteHandler
100
        );
101
102
        $this->setContainer($container);
103
    }
104
105
    function it_extends_base_Symfony_controller(): void
0 ignored issues
show
Coding Style introduced by
function it_extends_base_Symfony_controller() does not seem to conform to the naming convention (^(?:(?:[a-z]|__)[a-zA-Z0-9]*|[a-z][a-z0-9_]*)$).

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.

Loading history...
106
    {
107
        $this->shouldHaveType(Controller::class);
108
    }
109
110
    function it_throws_a_403_exception_if_user_is_unauthorized_to_view_a_single_resource(
111
        MetadataInterface $metadata,
112
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
113
        RequestConfiguration $configuration,
114
        Request $request,
115
        AuthorizationCheckerInterface $authorizationChecker
116
    ): void {
117
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
118
        $configuration->hasPermission()->willReturn(true);
119
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
120
121
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(false);
122
123
        $this
124
            ->shouldThrow(new AccessDeniedException())
125
            ->during('showAction', [$request])
126
        ;
127
    }
128
129
    function it_throws_a_404_exception_if_resource_is_not_found_based_on_configuration(
130
        MetadataInterface $metadata,
131
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
132
        RequestConfiguration $configuration,
133
        Request $request,
134
        AuthorizationCheckerInterface $authorizationChecker,
135
        RepositoryInterface $repository,
136
        SingleResourceProviderInterface $singleResourceProvider
137
    ): void {
138
        $metadata->getHumanizedName()->willReturn('product');
139
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
140
        $configuration->hasPermission()->willReturn(true);
141
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
142
143
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
144
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
145
146
        $this
147
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
148
            ->during('showAction', [$request])
149
        ;
150
    }
151
152
    function it_returns_a_response_for_html_view_of_a_single_resource(
153
        MetadataInterface $metadata,
154
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
155
        RequestConfiguration $configuration,
156
        AuthorizationCheckerInterface $authorizationChecker,
157
        RepositoryInterface $repository,
158
        SingleResourceProviderInterface $singleResourceProvider,
159
        ResourceInterface $resource,
160
        ViewHandlerInterface $viewHandler,
161
        EventDispatcherInterface $eventDispatcher,
162
        Request $request,
163
        Response $response
164
    ): void {
165
        $metadata->getApplicationName()->willReturn('sylius');
166
        $metadata->getName()->willReturn('product');
167
168
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
169
        $configuration->hasPermission()->willReturn(true);
170
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
171
172
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
173
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
174
175
        $configuration->isHtmlRequest()->willReturn(true);
176
        $configuration->getTemplate(ResourceActions::SHOW . '.html')->willReturn('SyliusShopBundle:Product:show.html.twig');
177
178
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
179
180
        $expectedView = View::create()
181
            ->setData([
182
                'configuration' => $configuration,
183
                'metadata' => $metadata,
184
                'resource' => $resource,
185
                'product' => $resource,
186
            ])
187
            ->setTemplateVar('product')
188
            ->setTemplate('SyliusShopBundle:Product:show.html.twig')
189
        ;
190
191
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
192
193
        $this->showAction($request)->shouldReturn($response);
194
    }
195
196
    function it_returns_a_response_for_non_html_view_of_single_resource(
197
        MetadataInterface $metadata,
198
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
199
        RequestConfiguration $configuration,
200
        AuthorizationCheckerInterface $authorizationChecker,
201
        RepositoryInterface $repository,
202
        SingleResourceProviderInterface $singleResourceProvider,
203
        ResourceInterface $resource,
204
        ViewHandlerInterface $viewHandler,
205
        EventDispatcherInterface $eventDispatcher,
206
        Request $request,
207
        Response $response
208
    ): void {
209
        $metadata->getApplicationName()->willReturn('sylius');
210
        $metadata->getName()->willReturn('product');
211
212
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
213
        $configuration->hasPermission()->willReturn(true);
214
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
215
216
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
217
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
218
219
        $configuration->isHtmlRequest()->willReturn(false);
220
221
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
222
223
        $expectedView = View::create($resource);
224
225
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
226
227
        $this->showAction($request)->shouldReturn($response);
228
    }
229
230
    function it_throws_a_403_exception_if_user_is_unauthorized_to_view_an_index_of_resources(
231
        MetadataInterface $metadata,
232
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
233
        RequestConfiguration $configuration,
234
        Request $request,
235
        AuthorizationCheckerInterface $authorizationChecker
236
    ): void {
237
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
238
        $configuration->hasPermission()->willReturn(true);
239
        $configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index');
240
241
        $authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(false);
242
243
        $this
244
            ->shouldThrow(new AccessDeniedException())
245
            ->during('indexAction', [$request])
246
        ;
247
    }
248
249
    function it_returns_a_response_for_html_view_of_paginated_resources(
250
        MetadataInterface $metadata,
251
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
252
        RequestConfiguration $configuration,
253
        AuthorizationCheckerInterface $authorizationChecker,
254
        RepositoryInterface $repository,
255
        ResourcesCollectionProviderInterface $resourcesCollectionProvider,
256
        EventDispatcherInterface $eventDispatcher,
257
        ResourceInterface $resource1,
258
        ResourceInterface $resource2,
259
        ViewHandlerInterface $viewHandler,
260
        Request $request,
261
        Response $response
262
    ): void {
263
        $metadata->getApplicationName()->willReturn('sylius');
264
        $metadata->getName()->willReturn('product');
265
        $metadata->getPluralName()->willReturn('products');
266
267
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
268
        $configuration->hasPermission()->willReturn(true);
269
        $configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index');
270
271
        $authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(true);
272
273
        $configuration->isHtmlRequest()->willReturn(true);
274
        $configuration->getTemplate(ResourceActions::INDEX . '.html')->willReturn('SyliusShopBundle:Product:index.html.twig');
275
        $resourcesCollectionProvider->get($configuration, $repository)->willReturn([$resource1, $resource2]);
276
277
        $eventDispatcher->dispatchMultiple(ResourceActions::INDEX, $configuration, [$resource1, $resource2])->shouldBeCalled();
278
279
        $expectedView = View::create()
280
            ->setData([
281
                'configuration' => $configuration,
282
                'metadata' => $metadata,
283
                'resources' => [$resource1, $resource2],
284
                'products' => [$resource1, $resource2],
285
            ])
286
            ->setTemplateVar('products')
287
            ->setTemplate('SyliusShopBundle:Product:index.html.twig')
288
        ;
289
290
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
291
292
        $this->indexAction($request)->shouldReturn($response);
293
    }
294
295
    function it_throws_a_403_exception_if_user_is_unauthorized_to_create_a_new_resource(
296
        MetadataInterface $metadata,
297
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
298
        RequestConfiguration $configuration,
299
        Request $request,
300
        AuthorizationCheckerInterface $authorizationChecker
301
    ): void {
302
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
303
        $configuration->hasPermission()->willReturn(true);
304
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
305
306
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(false);
307
308
        $this
309
            ->shouldThrow(new AccessDeniedException())
310
            ->during('createAction', [$request])
311
        ;
312
    }
313
314
    function it_returns_a_html_response_for_creating_new_resource_form(
315
        MetadataInterface $metadata,
316
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
317
        RequestConfiguration $configuration,
318
        AuthorizationCheckerInterface $authorizationChecker,
319
        ViewHandlerInterface $viewHandler,
320
        FactoryInterface $factory,
321
        NewResourceFactoryInterface $newResourceFactory,
322
        ResourceInterface $newResource,
323
        ResourceFormFactoryInterface $resourceFormFactory,
324
        EventDispatcherInterface $eventDispatcher,
325
        ResourceControllerEvent $event,
326
        Form $form,
327
        FormView $formView,
328
        Request $request,
329
        Response $response
330
    ): void {
331
        $metadata->getApplicationName()->willReturn('sylius');
332
        $metadata->getName()->willReturn('product');
333
334
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
335
        $configuration->hasPermission()->willReturn(true);
336
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
337
338
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
339
340
        $configuration->isHtmlRequest()->willReturn(true);
341
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
342
343
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
344
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
345
346
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
347
348
        $request->isMethod('POST')->willReturn(false);
349
        $form->createView()->willReturn($formView);
350
351
        $expectedView = View::create()
352
            ->setData([
353
                'configuration' => $configuration,
354
                'metadata' => $metadata,
355
                'resource' => $newResource,
356
                'product' => $newResource,
357
                'form' => $formView,
358
            ])
359
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
360
        ;
361
362
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
363
364
        $this->createAction($request)->shouldReturn($response);
365
    }
366
367
    function it_returns_a_html_response_for_invalid_form_during_resource_creation(
368
        MetadataInterface $metadata,
369
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
370
        RequestConfiguration $configuration,
371
        AuthorizationCheckerInterface $authorizationChecker,
372
        ViewHandlerInterface $viewHandler,
373
        FactoryInterface $factory,
374
        NewResourceFactoryInterface $newResourceFactory,
375
        ResourceInterface $newResource,
376
        ResourceFormFactoryInterface $resourceFormFactory,
377
        EventDispatcherInterface $eventDispatcher,
378
        ResourceControllerEvent $event,
379
        Form $form,
380
        FormView $formView,
381
        Request $request,
382
        Response $response
383
    ): void {
384
        $metadata->getApplicationName()->willReturn('sylius');
385
        $metadata->getName()->willReturn('product');
386
387
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
388
        $configuration->hasPermission()->willReturn(true);
389
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
390
391
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
392
393
        $configuration->isHtmlRequest()->willReturn(true);
394
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
395
396
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
397
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
398
399
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
400
401
        $request->isMethod('POST')->willReturn(true);
402
        $form->handleRequest($request)->willReturn($form);
403
        $form->isValid()->willReturn(false);
404
        $form->createView()->willReturn($formView);
405
406
        $expectedView = View::create()
407
            ->setData([
408
                'configuration' => $configuration,
409
                'metadata' => $metadata,
410
                'resource' => $newResource,
411
                'product' => $newResource,
412
                'form' => $formView,
413
            ])
414
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
415
        ;
416
417
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
418
419
        $this->createAction($request)->shouldReturn($response);
420
    }
421
422
    function it_returns_a_non_html_response_for_invalid_form_during_resource_creation(
423
        MetadataInterface $metadata,
424
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
425
        RequestConfiguration $configuration,
426
        AuthorizationCheckerInterface $authorizationChecker,
427
        ViewHandlerInterface $viewHandler,
428
        FactoryInterface $factory,
429
        NewResourceFactoryInterface $newResourceFactory,
430
        ResourceInterface $newResource,
431
        ResourceFormFactoryInterface $resourceFormFactory,
432
        Form $form,
433
        Request $request,
434
        Response $response
435
    ): void {
436
        $metadata->getApplicationName()->willReturn('sylius');
437
        $metadata->getName()->willReturn('product');
438
439
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
440
        $configuration->hasPermission()->willReturn(true);
441
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
442
443
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
444
445
        $configuration->isHtmlRequest()->willReturn(false);
446
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
447
448
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
449
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
450
451
        $request->isMethod('POST')->willReturn(true);
452
        $form->handleRequest($request)->willReturn($form);
453
        $form->isValid()->willReturn(false);
454
455
        $expectedView = View::create($form, 400);
456
457
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
458
459
        $this->createAction($request)->shouldReturn($response);
460
    }
461
462
    function it_does_not_create_the_resource_and_redirects_to_index_for_html_requests_stopped_via_events(
463
        MetadataInterface $metadata,
464
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
465
        RequestConfiguration $configuration,
466
        AuthorizationCheckerInterface $authorizationChecker,
467
        ViewHandlerInterface $viewHandler,
0 ignored issues
show
Unused Code introduced by
The parameter $viewHandler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
468
        FactoryInterface $factory,
469
        NewResourceFactoryInterface $newResourceFactory,
470
        RepositoryInterface $repository,
471
        ResourceInterface $newResource,
472
        ResourceFormFactoryInterface $resourceFormFactory,
473
        Form $form,
474
        RedirectHandlerInterface $redirectHandler,
475
        FlashHelperInterface $flashHelper,
476
        EventDispatcherInterface $eventDispatcher,
477
        ResourceControllerEvent $event,
478
        Request $request,
479
        Response $redirectResponse
480
    ): void {
481
        $metadata->getApplicationName()->willReturn('sylius');
482
        $metadata->getName()->willReturn('product');
483
484
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
485
        $configuration->hasPermission()->willReturn(true);
486
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
487
488
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
489
490
        $configuration->isHtmlRequest()->willReturn(true);
491
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
492
493
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
494
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
495
496
        $request->isMethod('POST')->willReturn(true);
497
        $form->handleRequest($request)->willReturn($form);
498
        $form->isValid()->willReturn(true);
499
        $form->getData()->willReturn($newResource);
500
501
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
502
        $event->isStopped()->willReturn(true);
503
504
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
505
506
        $repository->add($newResource)->shouldNotBeCalled();
507
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
508
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
509
510
        $redirectHandler->redirectToIndex($configuration, $newResource)->willReturn($redirectResponse);
511
512
        $this->createAction($request)->shouldReturn($redirectResponse);
513
    }
514
515
    function it_redirects_to_newly_created_resource(
516
        MetadataInterface $metadata,
517
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
518
        RequestConfiguration $configuration,
519
        AuthorizationCheckerInterface $authorizationChecker,
520
        ViewHandlerInterface $viewHandler,
0 ignored issues
show
Unused Code introduced by
The parameter $viewHandler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
521
        FactoryInterface $factory,
522
        NewResourceFactoryInterface $newResourceFactory,
523
        RepositoryInterface $repository,
524
        ResourceInterface $newResource,
525
        ResourceFormFactoryInterface $resourceFormFactory,
526
        StateMachineInterface $stateMachine,
527
        Form $form,
528
        RedirectHandlerInterface $redirectHandler,
529
        FlashHelperInterface $flashHelper,
530
        EventDispatcherInterface $eventDispatcher,
531
        ResourceControllerEvent $event,
532
        ResourceControllerEvent $postEvent,
533
        Request $request,
534
        Response $redirectResponse
535
    ): void {
536
        $metadata->getApplicationName()->willReturn('sylius');
537
        $metadata->getName()->willReturn('product');
538
539
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
540
        $configuration->hasPermission()->willReturn(true);
541
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
542
        $configuration->hasStateMachine()->willReturn(true);
543
544
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
545
546
        $configuration->isHtmlRequest()->willReturn(true);
547
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
548
549
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
550
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
551
552
        $request->isMethod('POST')->willReturn(true);
553
        $form->handleRequest($request)->willReturn($form);
554
        $form->isValid()->willReturn(true);
555
        $form->getData()->willReturn($newResource);
556
557
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
558
        $event->isStopped()->willReturn(false);
559
560
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
561
562
        $repository->add($newResource)->shouldBeCalled();
563
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($postEvent);
564
565
        $postEvent->hasResponse()->willReturn(false);
566
567
        $flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled();
568
        $redirectHandler->redirectToResource($configuration, $newResource)->willReturn($redirectResponse);
569
570
        $this->createAction($request)->shouldReturn($redirectResponse);
571
    }
572
573
    function it_uses_response_from_post_create_event_if_defined(
574
        MetadataInterface $metadata,
575
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
576
        RequestConfiguration $configuration,
577
        AuthorizationCheckerInterface $authorizationChecker,
578
        FactoryInterface $factory,
579
        NewResourceFactoryInterface $newResourceFactory,
580
        RepositoryInterface $repository,
581
        ResourceInterface $newResource,
582
        ResourceFormFactoryInterface $resourceFormFactory,
583
        StateMachineInterface $stateMachine,
584
        Form $form,
585
        FlashHelperInterface $flashHelper,
586
        EventDispatcherInterface $eventDispatcher,
587
        ResourceControllerEvent $event,
588
        ResourceControllerEvent $postEvent,
589
        Request $request,
590
        Response $redirectResponse
591
    ): void {
592
        $metadata->getApplicationName()->willReturn('sylius');
593
        $metadata->getName()->willReturn('product');
594
595
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
596
        $configuration->hasPermission()->willReturn(true);
597
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
598
        $configuration->hasStateMachine()->willReturn(true);
599
600
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
601
602
        $configuration->isHtmlRequest()->willReturn(true);
603
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
604
605
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
606
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
607
608
        $request->isMethod('POST')->willReturn(true);
609
        $form->handleRequest($request)->willReturn($form);
610
        $form->isValid()->willReturn(true);
611
        $form->getData()->willReturn($newResource);
612
613
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
614
        $event->isStopped()->willReturn(false);
615
616
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
617
618
        $repository->add($newResource)->shouldBeCalled();
619
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($postEvent);
620
        $flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled();
621
622
        $postEvent->hasResponse()->willReturn(true);
623
        $postEvent->getResponse()->willReturn($redirectResponse);
624
625
        $this->createAction($request)->shouldReturn($redirectResponse);
626
    }
627
628
    function it_returns_a_non_html_response_for_correctly_created_resources(
629
        MetadataInterface $metadata,
630
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
631
        RequestConfiguration $configuration,
632
        AuthorizationCheckerInterface $authorizationChecker,
633
        ViewHandlerInterface $viewHandler,
634
        FactoryInterface $factory,
635
        NewResourceFactoryInterface $newResourceFactory,
636
        RepositoryInterface $repository,
637
        ResourceInterface $newResource,
638
        ResourceFormFactoryInterface $resourceFormFactory,
639
        FlashHelperInterface $flashHelper,
640
        EventDispatcherInterface $eventDispatcher,
641
        ResourceControllerEvent $event,
642
        StateMachineInterface $stateMachine,
643
        Form $form,
644
        Request $request,
645
        Response $response
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(false);
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)->shouldBeCalled();
675
676
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
677
678
        $expectedView = View::create($newResource, 201);
679
680
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
681
682
        $this->createAction($request)->shouldReturn($response);
683
    }
684
685
    function it_does_not_create_the_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
686
        MetadataInterface $metadata,
687
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
688
        RequestConfiguration $configuration,
689
        AuthorizationCheckerInterface $authorizationChecker,
690
        FactoryInterface $factory,
691
        NewResourceFactoryInterface $newResourceFactory,
692
        RepositoryInterface $repository,
693
        ResourceInterface $newResource,
694
        ResourceFormFactoryInterface $resourceFormFactory,
695
        FlashHelperInterface $flashHelper,
696
        EventDispatcherInterface $eventDispatcher,
697
        Form $form,
698
        Request $request,
699
        ResourceControllerEvent $event
700
    ): void {
701
        $metadata->getApplicationName()->willReturn('sylius');
702
        $metadata->getName()->willReturn('product');
703
704
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
705
        $configuration->hasPermission()->willReturn(true);
706
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
707
708
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
709
710
        $configuration->isHtmlRequest()->willReturn(false);
711
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
712
713
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
714
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
715
716
        $request->isMethod('POST')->willReturn(true);
717
        $form->handleRequest($request)->willReturn($form);
718
        $form->isValid()->willReturn(true);
719
        $form->getData()->willReturn($newResource);
720
721
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
722
        $event->isStopped()->willReturn(true);
723
        $event->getMessage()->willReturn('You cannot add a new product right now.');
724
        $event->getErrorCode()->willReturn(500);
725
726
        $repository->add($newResource)->shouldNotBeCalled();
727
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
728
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
729
730
        $this
731
            ->shouldThrow(new HttpException(500, 'You cannot add a new product right now.'))
732
            ->during('createAction', [$request])
733
        ;
734
    }
735
736
    function it_throws_a_403_exception_if_user_is_unauthorized_to_edit_a_single_resource(
737
        MetadataInterface $metadata,
738
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
739
        RequestConfiguration $configuration,
740
        Request $request,
741
        AuthorizationCheckerInterface $authorizationChecker
742
    ): void {
743
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
744
        $configuration->hasPermission()->willReturn(true);
745
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
746
747
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
748
749
        $this
750
            ->shouldThrow(new AccessDeniedException())
751
            ->during('updateAction', [$request])
752
        ;
753
    }
754
755
    function it_throws_a_404_exception_if_resource_to_update_is_not_found_based_on_configuration(
756
        MetadataInterface $metadata,
757
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
758
        RequestConfiguration $configuration,
759
        Request $request,
760
        AuthorizationCheckerInterface $authorizationChecker,
761
        RepositoryInterface $repository,
762
        SingleResourceProviderInterface $singleResourceProvider
763
    ): void {
764
        $metadata->getHumanizedName()->willReturn('product');
765
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
766
        $configuration->hasPermission()->willReturn(true);
767
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
768
769
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
770
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
771
772
        $this
773
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
774
            ->during('updateAction', [$request])
775
        ;
776
    }
777
778
    function it_returns_a_html_response_for_updating_resource_form(
779
        MetadataInterface $metadata,
780
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
781
        RequestConfiguration $configuration,
782
        AuthorizationCheckerInterface $authorizationChecker,
783
        ViewHandlerInterface $viewHandler,
784
        RepositoryInterface $repository,
785
        SingleResourceProviderInterface $singleResourceProvider,
786
        ResourceInterface $resource,
787
        ResourceFormFactoryInterface $resourceFormFactory,
788
        EventDispatcherInterface $eventDispatcher,
789
        ResourceControllerEvent $event,
790
        Form $form,
791
        FormView $formView,
792
        Request $request,
793
        Response $response
794
    ): void {
795
        $metadata->getApplicationName()->willReturn('sylius');
796
        $metadata->getName()->willReturn('product');
797
798
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
799
        $configuration->hasPermission()->willReturn(true);
800
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
801
        $configuration->hasStateMachine()->willReturn(false);
802
803
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
804
805
        $configuration->isHtmlRequest()->willReturn(true);
806
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
807
808
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
809
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
810
811
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
812
813
        $request->isMethod('PATCH')->willReturn(false);
814
        $request->getMethod()->willReturn('GET');
815
816
        $form->handleRequest($request)->willReturn($form);
817
        $form->createView()->willReturn($formView);
818
819
        $expectedView = View::create()
820
            ->setData([
821
                'configuration' => $configuration,
822
                'metadata' => $metadata,
823
                'resource' => $resource,
824
                'product' => $resource,
825
                'form' => $formView,
826
            ])
827
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
828
        ;
829
830
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
831
832
        $this->updateAction($request)->shouldReturn($response);
833
    }
834
835
    function it_returns_a_html_response_for_invalid_form_during_resource_update(
836
        MetadataInterface $metadata,
837
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
838
        RequestConfiguration $configuration,
839
        AuthorizationCheckerInterface $authorizationChecker,
840
        ViewHandlerInterface $viewHandler,
841
        RepositoryInterface $repository,
842
        SingleResourceProviderInterface $singleResourceProvider,
843
        ResourceInterface $resource,
844
        ResourceFormFactoryInterface $resourceFormFactory,
845
        EventDispatcherInterface $eventDispatcher,
846
        ResourceControllerEvent $event,
847
        Form $form,
848
        FormView $formView,
849
        Request $request,
850
        Response $response
851
    ): void {
852
        $metadata->getApplicationName()->willReturn('sylius');
853
        $metadata->getName()->willReturn('product');
854
855
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
856
        $configuration->hasPermission()->willReturn(true);
857
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
858
859
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
860
861
        $configuration->isHtmlRequest()->willReturn(true);
862
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
863
864
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
865
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
866
867
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
868
869
        $request->isMethod('PATCH')->willReturn(false);
870
        $request->getMethod()->willReturn('PUT');
871
872
        $form->handleRequest($request)->willReturn($form);
873
874
        $form->isValid()->willReturn(false);
875
        $form->createView()->willReturn($formView);
876
877
        $expectedView = View::create()
878
            ->setData([
879
                'configuration' => $configuration,
880
                'metadata' => $metadata,
881
                'resource' => $resource,
882
                'product' => $resource,
883
                'form' => $formView,
884
            ])
885
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
886
        ;
887
888
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
889
890
        $this->updateAction($request)->shouldReturn($response);
891
    }
892
893
    function it_returns_a_non_html_response_for_invalid_form_during_resource_update(
894
        MetadataInterface $metadata,
895
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
896
        RequestConfiguration $configuration,
897
        AuthorizationCheckerInterface $authorizationChecker,
898
        ViewHandlerInterface $viewHandler,
899
        RepositoryInterface $repository,
900
        SingleResourceProviderInterface $singleResourceProvider,
901
        ResourceInterface $resource,
902
        ResourceFormFactoryInterface $resourceFormFactory,
903
        Form $form,
904
        Request $request,
905
        Response $response
906
    ): void {
907
        $metadata->getApplicationName()->willReturn('sylius');
908
        $metadata->getName()->willReturn('product');
909
910
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
911
        $configuration->hasPermission()->willReturn(true);
912
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
913
        $configuration->isHtmlRequest()->willReturn(false);
914
915
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
916
917
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
918
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
919
920
        $request->isMethod('PATCH')->willReturn(true);
921
        $request->getMethod()->willReturn('PATCH');
922
923
        $form->handleRequest($request)->willReturn($form);
924
        $form->isValid()->willReturn(false);
925
926
        $expectedView = View::create($form, 400);
927
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
928
929
        $this->updateAction($request)->shouldReturn($response);
930
    }
931
932
    function it_does_not_update_the_resource_and_redirects_to_resource_for_html_request_if_stopped_via_event(
933
        MetadataInterface $metadata,
934
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
935
        RequestConfiguration $configuration,
936
        AuthorizationCheckerInterface $authorizationChecker,
937
        ObjectManager $manager,
938
        RepositoryInterface $repository,
939
        SingleResourceProviderInterface $singleResourceProvider,
940
        ResourceInterface $resource,
941
        ResourceFormFactoryInterface $resourceFormFactory,
942
        Form $form,
943
        EventDispatcherInterface $eventDispatcher,
944
        RedirectHandlerInterface $redirectHandler,
945
        FlashHelperInterface $flashHelper,
946
        ResourceControllerEvent $event,
947
        Request $request,
948
        Response $redirectResponse
949
    ): void {
950
        $metadata->getApplicationName()->willReturn('sylius');
951
        $metadata->getName()->willReturn('product');
952
953
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
954
        $configuration->hasPermission()->willReturn(true);
955
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
956
957
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
958
959
        $configuration->isHtmlRequest()->willReturn(true);
960
961
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
962
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
963
964
        $request->isMethod('PATCH')->willReturn(false);
965
        $request->getMethod()->willReturn('PUT');
966
967
        $form->handleRequest($request)->willReturn($form);
968
969
        $form->isSubmitted()->willReturn(true);
970
        $form->isValid()->willReturn(true);
971
        $form->getData()->willReturn($resource);
972
973
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
974
        $event->isStopped()->willReturn(true);
975
        $event->hasResponse()->willReturn(false);
976
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
977
978
        $manager->flush()->shouldNotBeCalled();
979
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
980
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
981
982
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
983
984
        $this->updateAction($request)->shouldReturn($redirectResponse);
985
    }
986
987
    function it_redirects_to_updated_resource(
988
        MetadataInterface $metadata,
989
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
990
        RepositoryInterface $repository,
991
        ObjectManager $manager,
992
        SingleResourceProviderInterface $singleResourceProvider,
993
        ResourceFormFactoryInterface $resourceFormFactory,
994
        RedirectHandlerInterface $redirectHandler,
995
        FlashHelperInterface $flashHelper,
996
        AuthorizationCheckerInterface $authorizationChecker,
997
        EventDispatcherInterface $eventDispatcher,
998
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
999
        RequestConfiguration $configuration,
1000
        ResourceInterface $resource,
1001
        Form $form,
1002
        ResourceControllerEvent $preEvent,
1003
        ResourceControllerEvent $postEvent,
1004
        Request $request,
1005
        Response $redirectResponse
1006
    ): void {
1007
        $metadata->getApplicationName()->willReturn('sylius');
1008
        $metadata->getName()->willReturn('product');
1009
1010
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1011
        $configuration->hasPermission()->willReturn(true);
1012
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1013
        $configuration->hasStateMachine()->willReturn(false);
1014
1015
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1016
1017
        $configuration->isHtmlRequest()->willReturn(true);
1018
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
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($preEvent);
1033
        $preEvent->isStopped()->willReturn(false);
1034
1035
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1036
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1037
1038
        $postEvent->hasResponse()->willReturn(false);
1039
1040
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1041
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1042
1043
        $this->updateAction($request)->shouldReturn($redirectResponse);
1044
    }
1045
1046
    function it_uses_response_from_post_update_event_if_defined(
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
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1096
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1097
1098
        $postEvent->hasResponse()->willReturn(true);
1099
        $postEvent->getResponse()->willReturn($redirectResponse);
1100
1101
        $redirectHandler->redirectToResource($configuration, $resource)->shouldNotBeCalled();
1102
1103
        $this->updateAction($request)->shouldReturn($redirectResponse);
1104
    }
1105
1106
    function it_returns_a_non_html_response_for_correctly_updated_resource(
1107
        MetadataInterface $metadata,
1108
        ParameterBagInterface $parameterBag,
1109
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1110
        ViewHandlerInterface $viewHandler,
1111
        RepositoryInterface $repository,
1112
        ObjectManager $manager,
1113
        SingleResourceProviderInterface $singleResourceProvider,
1114
        ResourceFormFactoryInterface $resourceFormFactory,
1115
        AuthorizationCheckerInterface $authorizationChecker,
1116
        EventDispatcherInterface $eventDispatcher,
1117
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1118
        RequestConfiguration $configuration,
1119
        ResourceInterface $resource,
1120
        ResourceControllerEvent $event,
1121
        Form $form,
1122
        Request $request,
1123
        Response $response
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->isHtmlRequest()->willReturn(false);
1132
        $configuration->hasStateMachine()->willReturn(false);
1133
1134
        $configuration->getParameters()->willReturn($parameterBag);
1135
        $parameterBag->get('return_content', false)->willReturn(false);
1136
1137
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1138
1139
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1140
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1141
1142
        $request->isMethod('PATCH')->willReturn(false);
1143
        $request->getMethod()->willReturn('PUT');
1144
1145
        $form->handleRequest($request)->willReturn($form);
1146
        $form->isValid()->willReturn(true);
1147
        $form->getData()->willReturn($resource);
1148
1149
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1150
        $event->isStopped()->willReturn(false);
1151
1152
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1153
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1154
1155
        $expectedView = View::create(null, 204);
1156
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1157
1158
        $this->updateAction($request)->shouldReturn($response);
1159
    }
1160
1161
    function it_does_not_update_the_resource_throws_a_http_exception_for_non_html_requests_stopped_via_event(
1162
        MetadataInterface $metadata,
1163
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1164
        RequestConfiguration $configuration,
1165
        AuthorizationCheckerInterface $authorizationChecker,
1166
        ObjectManager $manager,
1167
        RepositoryInterface $repository,
1168
        SingleResourceProviderInterface $singleResourceProvider,
1169
        ResourceInterface $resource,
1170
        ResourceFormFactoryInterface $resourceFormFactory,
1171
        EventDispatcherInterface $eventDispatcher,
1172
        ResourceControllerEvent $event,
1173
        Form $form,
1174
        Request $request
1175
    ): void {
1176
        $metadata->getApplicationName()->willReturn('sylius');
1177
        $metadata->getName()->willReturn('product');
1178
1179
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1180
        $configuration->hasPermission()->willReturn(true);
1181
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1182
        $configuration->isHtmlRequest()->willReturn(false);
1183
1184
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1185
1186
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1187
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1188
1189
        $request->isMethod('PATCH')->willReturn(false);
1190
        $request->getMethod()->willReturn('PUT');
1191
1192
        $form->handleRequest($request)->willReturn($form);
1193
        $form->isValid()->willReturn(true);
1194
        $form->getData()->willReturn($resource);
1195
1196
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1197
        $event->isStopped()->willReturn(true);
1198
        $event->getMessage()->willReturn('Cannot update this channel.');
1199
        $event->getErrorCode()->willReturn(500);
1200
1201
        $manager->flush()->shouldNotBeCalled();
1202
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1203
1204
        $this
1205
            ->shouldThrow(new HttpException(500, 'Cannot update this channel.'))
1206
            ->during('updateAction', [$request])
1207
        ;
1208
    }
1209
1210
    function it_applies_state_machine_transition_to_updated_resource_if_configured(
1211
        MetadataInterface $metadata,
1212
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1213
        RepositoryInterface $repository,
1214
        ObjectManager $manager,
1215
        SingleResourceProviderInterface $singleResourceProvider,
1216
        ResourceFormFactoryInterface $resourceFormFactory,
1217
        RedirectHandlerInterface $redirectHandler,
1218
        FlashHelperInterface $flashHelper,
1219
        AuthorizationCheckerInterface $authorizationChecker,
1220
        EventDispatcherInterface $eventDispatcher,
1221
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1222
        RequestConfiguration $configuration,
1223
        ResourceInterface $resource,
1224
        Form $form,
1225
        ResourceControllerEvent $preEvent,
1226
        ResourceControllerEvent $postEvent,
1227
        Request $request,
1228
        Response $redirectResponse
1229
    ): void {
1230
        $metadata->getApplicationName()->willReturn('sylius');
1231
        $metadata->getName()->willReturn('product');
1232
1233
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1234
        $configuration->hasPermission()->willReturn(true);
1235
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1236
        $configuration->hasStateMachine()->willReturn(true);
1237
1238
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1239
1240
        $configuration->isHtmlRequest()->willReturn(true);
1241
        $configuration->getTemplate(ResourceActions::UPDATE)->willReturn('SyliusShopBundle:Product:update.html.twig');
1242
1243
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1244
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1245
1246
        $request->isMethod('PATCH')->willReturn(false);
1247
        $request->getMethod()->willReturn('PUT');
1248
1249
        $form->handleRequest($request)->willReturn($form);
1250
1251
        $form->isSubmitted()->willReturn(true);
1252
        $form->isValid()->willReturn(true);
1253
        $form->getData()->willReturn($resource);
1254
1255
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent);
1256
        $preEvent->isStopped()->willReturn(false);
1257
1258
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1259
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1260
1261
        $postEvent->hasResponse()->willReturn(false);
1262
1263
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1264
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1265
1266
        $this->updateAction($request)->shouldReturn($redirectResponse);
1267
    }
1268
1269
    function it_throws_a_403_exception_if_user_is_unauthorized_to_delete_a_single_resource(
1270
        MetadataInterface $metadata,
1271
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1272
        RequestConfiguration $configuration,
1273
        Request $request,
1274
        AuthorizationCheckerInterface $authorizationChecker
1275
    ): void {
1276
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1277
        $configuration->hasPermission()->willReturn(true);
1278
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1279
1280
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(false);
1281
1282
        $this
1283
            ->shouldThrow(new AccessDeniedException())
1284
            ->during('deleteAction', [$request])
1285
        ;
1286
    }
1287
1288
    function it_throws_a_404_exception_if_resource_for_deletion_is_not_found_based_on_configuration(
1289
        MetadataInterface $metadata,
1290
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1291
        RequestConfiguration $configuration,
1292
        Request $request,
1293
        AuthorizationCheckerInterface $authorizationChecker,
1294
        RepositoryInterface $repository,
1295
        SingleResourceProviderInterface $singleResourceProvider
1296
    ): void {
1297
        $metadata->getHumanizedName()->willReturn('product');
1298
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1299
        $configuration->hasPermission()->willReturn(true);
1300
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1301
1302
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1303
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
1304
1305
        $this
1306
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
1307
            ->during('deleteAction', [$request])
1308
        ;
1309
    }
1310
1311
    function it_deletes_a_resource_and_redirects_to_index_by_for_html_request(
1312
        MetadataInterface $metadata,
1313
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1314
        RequestConfiguration $configuration,
1315
        AuthorizationCheckerInterface $authorizationChecker,
1316
        RepositoryInterface $repository,
1317
        SingleResourceProviderInterface $singleResourceProvider,
1318
        ResourceInterface $resource,
1319
        RedirectHandlerInterface $redirectHandler,
1320
        FlashHelperInterface $flashHelper,
1321
        EventDispatcherInterface $eventDispatcher,
1322
        CsrfTokenManagerInterface $csrfTokenManager,
1323
        ContainerInterface $container,
1324
        ResourceControllerEvent $event,
1325
        ResourceControllerEvent $postEvent,
1326
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1327
        Request $request,
1328
        Response $redirectResponse
1329
    ): void {
1330
        $metadata->getApplicationName()->willReturn('sylius');
1331
        $metadata->getName()->willReturn('product');
1332
1333
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1334
        $configuration->hasPermission()->willReturn(true);
1335
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1336
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1337
1338
        $container->has('security.csrf.token_manager')->willReturn(true);
1339
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1340
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1341
1342
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1343
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1344
        $resource->getId()->willReturn(1);
1345
1346
        $configuration->isHtmlRequest()->willReturn(true);
1347
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1348
1349
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1350
        $event->isStopped()->willReturn(false);
1351
1352
        $resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled();
1353
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($postEvent);
1354
1355
        $postEvent->hasResponse()->willReturn(false);
1356
1357
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled();
1358
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1359
1360
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1361
    }
1362
1363
    function it_uses_response_from_post_delete_event_if_defined(
1364
        MetadataInterface $metadata,
1365
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1366
        RequestConfiguration $configuration,
1367
        AuthorizationCheckerInterface $authorizationChecker,
1368
        RepositoryInterface $repository,
1369
        SingleResourceProviderInterface $singleResourceProvider,
1370
        ResourceInterface $resource,
1371
        FlashHelperInterface $flashHelper,
1372
        EventDispatcherInterface $eventDispatcher,
1373
        CsrfTokenManagerInterface $csrfTokenManager,
1374
        ContainerInterface $container,
1375
        ResourceControllerEvent $event,
1376
        ResourceControllerEvent $postEvent,
1377
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1378
        Request $request,
1379
        Response $redirectResponse
1380
    ): void {
1381
        $metadata->getApplicationName()->willReturn('sylius');
1382
        $metadata->getName()->willReturn('product');
1383
1384
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1385
        $configuration->hasPermission()->willReturn(true);
1386
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1387
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1388
1389
        $container->has('security.csrf.token_manager')->willReturn(true);
1390
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1391
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1392
1393
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1394
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1395
        $resource->getId()->willReturn(1);
1396
1397
        $configuration->isHtmlRequest()->willReturn(true);
1398
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1399
1400
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1401
        $event->isStopped()->willReturn(false);
1402
1403
        $resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled();
1404
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($postEvent);
1405
1406
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled();
1407
1408
        $postEvent->hasResponse()->willReturn(true);
1409
        $postEvent->getResponse()->willReturn($redirectResponse);
1410
1411
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1412
    }
1413
1414
    function it_does_not_delete_a_resource_and_redirects_to_index_for_html_requests_stopped_via_event(
1415
        MetadataInterface $metadata,
1416
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1417
        RequestConfiguration $configuration,
1418
        AuthorizationCheckerInterface $authorizationChecker,
1419
        RepositoryInterface $repository,
1420
        SingleResourceProviderInterface $singleResourceProvider,
1421
        ResourceInterface $resource,
1422
        RedirectHandlerInterface $redirectHandler,
1423
        FlashHelperInterface $flashHelper,
1424
        EventDispatcherInterface $eventDispatcher,
1425
        CsrfTokenManagerInterface $csrfTokenManager,
1426
        ContainerInterface $container,
1427
        ResourceControllerEvent $event,
1428
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1429
        Request $request,
1430
        Response $redirectResponse
1431
    ): void {
1432
        $metadata->getApplicationName()->willReturn('sylius');
1433
        $metadata->getName()->willReturn('product');
1434
1435
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1436
        $configuration->hasPermission()->willReturn(true);
1437
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1438
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1439
1440
        $container->has('security.csrf.token_manager')->willReturn(true);
1441
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1442
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1443
1444
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1445
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1446
        $resource->getId()->willReturn(1);
1447
1448
        $configuration->isHtmlRequest()->willReturn(true);
1449
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1450
1451
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1452
        $event->isStopped()->willReturn(true);
1453
        $event->hasResponse()->willReturn(false);
1454
1455
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1456
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1457
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled();
1458
1459
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1460
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1461
1462
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1463
    }
1464
1465
    function it_does_not_delete_a_resource_and_uses_response_from_event_if_defined(
1466
        MetadataInterface $metadata,
1467
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1468
        RequestConfiguration $configuration,
1469
        AuthorizationCheckerInterface $authorizationChecker,
1470
        RepositoryInterface $repository,
1471
        SingleResourceProviderInterface $singleResourceProvider,
1472
        ResourceInterface $resource,
1473
        RedirectHandlerInterface $redirectHandler,
1474
        FlashHelperInterface $flashHelper,
1475
        EventDispatcherInterface $eventDispatcher,
1476
        CsrfTokenManagerInterface $csrfTokenManager,
1477
        ContainerInterface $container,
1478
        ResourceControllerEvent $event,
1479
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1480
        Request $request,
1481
        Response $redirectResponse
1482
    ): void {
1483
        $metadata->getApplicationName()->willReturn('sylius');
1484
        $metadata->getName()->willReturn('product');
1485
1486
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1487
        $configuration->hasPermission()->willReturn(true);
1488
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1489
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1490
1491
        $container->has('security.csrf.token_manager')->willReturn(true);
1492
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1493
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1494
1495
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1496
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1497
        $resource->getId()->willReturn(1);
1498
1499
        $configuration->isHtmlRequest()->willReturn(true);
1500
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1501
1502
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1503
        $event->isStopped()->willReturn(true);
1504
1505
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1506
1507
        $event->hasResponse()->willReturn(true);
1508
        $event->getResponse()->willReturn($redirectResponse);
1509
1510
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1511
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1512
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled();
1513
1514
        $redirectHandler->redirectToIndex($configuration, $resource)->shouldNotBeCalled();
1515
1516
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1517
    }
1518
1519
    function it_does_not_correctly_delete_a_resource_and_returns_500_for_not_html_response(
1520
        MetadataInterface $metadata,
1521
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1522
        RequestConfiguration $configuration,
1523
        AuthorizationCheckerInterface $authorizationChecker,
1524
        ViewHandlerInterface $viewHandler,
1525
        RepositoryInterface $repository,
1526
        SingleResourceProviderInterface $singleResourceProvider,
1527
        ResourceInterface $resource,
1528
        EventDispatcherInterface $eventDispatcher,
1529
        CsrfTokenManagerInterface $csrfTokenManager,
1530
        ContainerInterface $container,
1531
        ResourceControllerEvent $event,
1532
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1533
        Request $request,
1534
        Response $response
1535
    ): void {
1536
        $metadata->getApplicationName()->willReturn('sylius');
1537
        $metadata->getName()->willReturn('product');
1538
1539
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1540
        $configuration->hasPermission()->willReturn(true);
1541
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1542
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1543
1544
        $container->has('security.csrf.token_manager')->willReturn(true);
1545
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1546
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1547
1548
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1549
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1550
        $resource->getId()->willReturn(1);
1551
1552
        $configuration->isHtmlRequest()->willReturn(false);
1553
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1554
1555
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1556
        $event->isStopped()->willReturn(false);
1557
1558
        $resourceDeleteHandler->handle($resource, $repository)->willThrow(new DeleteHandlingException());
1559
1560
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1561
1562
        $expectedView = View::create(null, 500);
1563
1564
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1565
1566
        $this->deleteAction($request)->shouldReturn($response);
1567
    }
1568
1569
    function it_deletes_a_resource_and_returns_204_for_non_html_requests(
1570
        MetadataInterface $metadata,
1571
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1572
        RequestConfiguration $configuration,
1573
        AuthorizationCheckerInterface $authorizationChecker,
1574
        ViewHandlerInterface $viewHandler,
1575
        RepositoryInterface $repository,
1576
        SingleResourceProviderInterface $singleResourceProvider,
1577
        ResourceInterface $resource,
1578
        EventDispatcherInterface $eventDispatcher,
1579
        CsrfTokenManagerInterface $csrfTokenManager,
1580
        ContainerInterface $container,
1581
        ResourceControllerEvent $event,
1582
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1583
        Request $request,
1584
        Response $response
1585
    ): void {
1586
        $metadata->getApplicationName()->willReturn('sylius');
1587
        $metadata->getName()->willReturn('product');
1588
1589
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1590
        $configuration->hasPermission()->willReturn(true);
1591
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1592
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1593
1594
        $container->has('security.csrf.token_manager')->willReturn(true);
1595
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1596
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1597
1598
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1599
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1600
        $resource->getId()->willReturn(1);
1601
1602
        $configuration->isHtmlRequest()->willReturn(false);
1603
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1604
1605
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1606
        $event->isStopped()->willReturn(false);
1607
1608
        $resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled();
1609
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldBeCalled();
1610
1611
        $expectedView = View::create(null, 204);
1612
1613
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1614
1615
        $this->deleteAction($request)->shouldReturn($response);
1616
    }
1617
1618
    function it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
1619
        MetadataInterface $metadata,
1620
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1621
        RequestConfiguration $configuration,
1622
        AuthorizationCheckerInterface $authorizationChecker,
1623
        RepositoryInterface $repository,
1624
        SingleResourceProviderInterface $singleResourceProvider,
1625
        ResourceInterface $resource,
1626
        FlashHelperInterface $flashHelper,
1627
        EventDispatcherInterface $eventDispatcher,
1628
        CsrfTokenManagerInterface $csrfTokenManager,
1629
        ContainerInterface $container,
1630
        ResourceControllerEvent $event,
1631
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1632
        Request $request
1633
    ): void {
1634
        $metadata->getApplicationName()->willReturn('sylius');
1635
        $metadata->getName()->willReturn('product');
1636
1637
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1638
        $configuration->hasPermission()->willReturn(true);
1639
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1640
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1641
1642
        $container->has('security.csrf.token_manager')->willReturn(true);
1643
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1644
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1645
1646
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1647
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1648
        $resource->getId()->willReturn(1);
1649
1650
        $configuration->isHtmlRequest()->willReturn(false);
1651
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1652
1653
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1654
        $event->isStopped()->willReturn(true);
1655
        $event->getMessage()->willReturn('Cannot delete this product.');
1656
        $event->getErrorCode()->willReturn(500);
1657
1658
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1659
1660
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1661
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1662
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1663
1664
        $this
1665
            ->shouldThrow(new HttpException(500, 'Cannot delete this product.'))
1666
            ->during('deleteAction', [$request])
1667
        ;
1668
    }
1669
1670
    function it_throws_a_403_exception_if_csrf_token_is_invalid_during_delete_action(
1671
        MetadataInterface $metadata,
1672
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1673
        RequestConfiguration $configuration,
1674
        AuthorizationCheckerInterface $authorizationChecker,
1675
        RepositoryInterface $repository,
1676
        SingleResourceProviderInterface $singleResourceProvider,
1677
        ResourceInterface $resource,
1678
        FlashHelperInterface $flashHelper,
1679
        EventDispatcherInterface $eventDispatcher,
1680
        CsrfTokenManagerInterface $csrfTokenManager,
1681
        ContainerInterface $container,
1682
        ResourceControllerEvent $event,
1683
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1684
        Request $request
1685
    ): void {
1686
        $metadata->getApplicationName()->willReturn('sylius');
1687
        $metadata->getName()->willReturn('product');
1688
1689
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1690
        $configuration->hasPermission()->willReturn(true);
1691
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1692
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1693
1694
        $container->has('security.csrf.token_manager')->willReturn(true);
1695
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1696
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(false);
1697
1698
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1699
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1700
        $resource->getId()->willReturn(1);
1701
1702
        $configuration->isHtmlRequest()->willReturn(true);
1703
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1704
1705
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1706
        $event->isStopped()->shouldNotBeCalled();
1707
1708
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1709
1710
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1711
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1712
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1713
1714
        $this
1715
            ->shouldThrow(new HttpException(403, 'Invalid csrf token.'))
1716
            ->during('deleteAction', [$request])
1717
        ;
1718
    }
1719
1720
    function it_throws_a_403_exception_if_user_is_unauthorized_to_apply_state_machine_transition_on_resource(
1721
        MetadataInterface $metadata,
1722
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1723
        RequestConfiguration $configuration,
1724
        Request $request,
1725
        AuthorizationCheckerInterface $authorizationChecker
1726
    ): void {
1727
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1728
        $configuration->hasPermission()->willReturn(true);
1729
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1730
1731
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
1732
1733
        $this
1734
            ->shouldThrow(new AccessDeniedException())
1735
            ->during('applyStateMachineTransitionAction', [$request])
1736
        ;
1737
    }
1738
1739
    function it_throws_a_404_exception_if_resource_is_not_found_when_trying_to_apply_state_machine_transition(
1740
        MetadataInterface $metadata,
1741
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1742
        RequestConfiguration $configuration,
1743
        Request $request,
1744
        AuthorizationCheckerInterface $authorizationChecker,
1745
        RepositoryInterface $repository,
1746
        SingleResourceProviderInterface $singleResourceProvider
1747
    ): void {
1748
        $metadata->getHumanizedName()->willReturn('product');
1749
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1750
        $configuration->hasPermission()->willReturn(true);
1751
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1752
1753
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1754
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
1755
1756
        $this
1757
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
1758
            ->during('applyStateMachineTransitionAction', [$request])
1759
        ;
1760
    }
1761
1762
    function it_does_not_apply_state_machine_transition_on_resource_if_not_applicable_and_returns_400_bad_request(
1763
        MetadataInterface $metadata,
1764
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1765
        RequestConfiguration $configuration,
1766
        AuthorizationCheckerInterface $authorizationChecker,
1767
        RepositoryInterface $repository,
1768
        ObjectManager $objectManager,
1769
        StateMachineInterface $stateMachine,
1770
        SingleResourceProviderInterface $singleResourceProvider,
1771
        ResourceInterface $resource,
1772
        FlashHelperInterface $flashHelper,
1773
        EventDispatcherInterface $eventDispatcher,
1774
        ResourceControllerEvent $event,
1775
        Request $request
1776
    ): void {
1777
        $metadata->getApplicationName()->willReturn('sylius');
1778
        $metadata->getName()->willReturn('product');
1779
1780
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1781
        $configuration->hasPermission()->willReturn(true);
1782
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1783
1784
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1785
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1786
1787
        $configuration->isHtmlRequest()->willReturn(true);
1788
1789
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1790
        $event->isStopped()->willReturn(false);
1791
1792
        $stateMachine->can($configuration, $resource)->willReturn(false);
1793
1794
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
1795
        $objectManager->flush()->shouldNotBeCalled();
1796
1797
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1798
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1799
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1800
1801
        $this
1802
            ->shouldThrow(new BadRequestHttpException())
1803
            ->during('applyStateMachineTransitionAction', [$request])
1804
        ;
1805
    }
1806
1807
    function it_applies_state_machine_transition_to_resource_and_redirects_for_html_request(
1808
        MetadataInterface $metadata,
1809
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1810
        RepositoryInterface $repository,
1811
        ObjectManager $manager,
1812
        SingleResourceProviderInterface $singleResourceProvider,
1813
        RedirectHandlerInterface $redirectHandler,
1814
        FlashHelperInterface $flashHelper,
1815
        AuthorizationCheckerInterface $authorizationChecker,
1816
        EventDispatcherInterface $eventDispatcher,
1817
        StateMachineInterface $stateMachine,
1818
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1819
        RequestConfiguration $configuration,
1820
        ResourceInterface $resource,
1821
        ResourceControllerEvent $event,
1822
        ResourceControllerEvent $postEvent,
1823
        Request $request,
1824
        Response $redirectResponse
1825
    ): void {
1826
        $metadata->getApplicationName()->willReturn('sylius');
1827
        $metadata->getName()->willReturn('product');
1828
1829
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1830
        $configuration->hasPermission()->willReturn(true);
1831
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1832
1833
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1834
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1835
1836
        $configuration->isHtmlRequest()->willReturn(true);
1837
1838
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1839
        $event->isStopped()->willReturn(false);
1840
1841
        $stateMachine->can($configuration, $resource)->willReturn(true);
1842
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1843
1844
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1845
1846
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1847
1848
        $postEvent->hasResponse()->willReturn(false);
1849
1850
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1851
1852
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1853
    }
1854
1855
    function it_uses_response_from_post_apply_state_machine_transition_event_if_defined(
1856
        MetadataInterface $metadata,
1857
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1858
        RepositoryInterface $repository,
1859
        ObjectManager $manager,
1860
        SingleResourceProviderInterface $singleResourceProvider,
1861
        RedirectHandlerInterface $redirectHandler,
0 ignored issues
show
Unused Code introduced by
The parameter $redirectHandler is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1862
        FlashHelperInterface $flashHelper,
1863
        AuthorizationCheckerInterface $authorizationChecker,
1864
        EventDispatcherInterface $eventDispatcher,
1865
        StateMachineInterface $stateMachine,
1866
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1867
        RequestConfiguration $configuration,
1868
        ResourceInterface $resource,
1869
        ResourceControllerEvent $event,
1870
        ResourceControllerEvent $postEvent,
1871
        Request $request,
1872
        Response $redirectResponse
1873
    ): void {
1874
        $metadata->getApplicationName()->willReturn('sylius');
1875
        $metadata->getName()->willReturn('product');
1876
1877
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1878
        $configuration->hasPermission()->willReturn(true);
1879
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1880
1881
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1882
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1883
1884
        $configuration->isHtmlRequest()->willReturn(true);
1885
1886
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1887
        $event->isStopped()->willReturn(false);
1888
1889
        $stateMachine->can($configuration, $resource)->willReturn(true);
1890
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1891
1892
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1893
1894
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1895
1896
        $postEvent->hasResponse()->willReturn(true);
1897
        $postEvent->getResponse()->willReturn($redirectResponse);
1898
1899
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1900
    }
1901
1902
    function it_does_not_apply_state_machine_transition_on_resource_and_redirects_for_html_requests_stopped_via_event(
1903
        MetadataInterface $metadata,
1904
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1905
        RequestConfiguration $configuration,
1906
        AuthorizationCheckerInterface $authorizationChecker,
1907
        StateMachineInterface $stateMachine,
1908
        ObjectManager $manager,
1909
        RepositoryInterface $repository,
1910
        SingleResourceProviderInterface $singleResourceProvider,
1911
        ResourceInterface $resource,
1912
        RedirectHandlerInterface $redirectHandler,
1913
        FlashHelperInterface $flashHelper,
1914
        EventDispatcherInterface $eventDispatcher,
1915
        ResourceControllerEvent $event,
1916
        Request $request,
1917
        Response $redirectResponse
1918
    ): void {
1919
        $metadata->getApplicationName()->willReturn('sylius');
1920
        $metadata->getName()->willReturn('product');
1921
1922
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1923
        $configuration->hasPermission()->willReturn(true);
1924
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1925
1926
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1927
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1928
1929
        $configuration->isHtmlRequest()->willReturn(true);
1930
1931
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1932
        $event->isStopped()->willReturn(true);
1933
1934
        $manager->flush()->shouldNotBeCalled();
1935
        $stateMachine->apply($resource)->shouldNotBeCalled();
1936
1937
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled();
1938
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled();
1939
1940
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1941
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1942
1943
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1944
    }
1945
1946
    function it_applies_state_machine_transition_on_resource_and_returns_200_for_non_html_requests(
1947
        MetadataInterface $metadata,
1948
        ParameterBagInterface $parameterBag,
1949
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1950
        ViewHandlerInterface $viewHandler,
1951
        RepositoryInterface $repository,
1952
        ObjectManager $manager,
1953
        SingleResourceProviderInterface $singleResourceProvider,
1954
        AuthorizationCheckerInterface $authorizationChecker,
1955
        EventDispatcherInterface $eventDispatcher,
1956
        StateMachineInterface $stateMachine,
1957
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1958
        RequestConfiguration $configuration,
1959
        ResourceInterface $resource,
1960
        ResourceControllerEvent $event,
1961
        Request $request,
1962
        Response $response
1963
    ): void {
1964
        $metadata->getApplicationName()->willReturn('sylius');
1965
        $metadata->getName()->willReturn('product');
1966
1967
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1968
        $configuration->getParameters()->willReturn($parameterBag);
1969
        $configuration->hasPermission()->willReturn(true);
1970
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1971
1972
        $parameterBag->get('return_content', true)->willReturn(true);
1973
1974
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1975
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1976
1977
        $configuration->isHtmlRequest()->willReturn(false);
1978
1979
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1980
        $event->isStopped()->willReturn(false);
1981
1982
        $stateMachine->can($configuration, $resource)->willReturn(true);
1983
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1984
1985
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1986
1987
        $expectedView = View::create($resource, 200);
1988
1989
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1990
1991
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
1992
    }
1993
1994
    function it_applies_state_machine_transition_on_resource_and_returns_204_for_non_html_requests_if_additional_option_added(
1995
        MetadataInterface $metadata,
1996
        ParameterBagInterface $parameterBag,
1997
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1998
        ViewHandlerInterface $viewHandler,
1999
        RepositoryInterface $repository,
2000
        ObjectManager $manager,
2001
        SingleResourceProviderInterface $singleResourceProvider,
2002
        AuthorizationCheckerInterface $authorizationChecker,
2003
        EventDispatcherInterface $eventDispatcher,
2004
        StateMachineInterface $stateMachine,
2005
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
2006
        RequestConfiguration $configuration,
2007
        ResourceInterface $resource,
2008
        ResourceControllerEvent $event,
2009
        Request $request,
2010
        Response $response
2011
    ): void {
2012
        $metadata->getApplicationName()->willReturn('sylius');
2013
        $metadata->getName()->willReturn('product');
2014
2015
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
2016
        $configuration->getParameters()->willReturn($parameterBag);
2017
        $configuration->hasPermission()->willReturn(true);
2018
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
2019
2020
        $parameterBag->get('return_content', true)->willReturn(false);
2021
2022
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
2023
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
2024
2025
        $configuration->isHtmlRequest()->willReturn(false);
2026
2027
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
2028
        $event->isStopped()->willReturn(false);
2029
2030
        $stateMachine->can($configuration, $resource)->willReturn(true);
2031
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
2032
2033
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
2034
2035
        $expectedView = View::create(null, 204);
2036
2037
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
2038
2039
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
2040
    }
2041
2042
    function it_does_not_apply_state_machine_transition_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
2043
        MetadataInterface $metadata,
2044
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
2045
        RequestConfiguration $configuration,
2046
        AuthorizationCheckerInterface $authorizationChecker,
2047
        RepositoryInterface $repository,
2048
        ObjectManager $objectManager,
2049
        StateMachineInterface $stateMachine,
2050
        SingleResourceProviderInterface $singleResourceProvider,
2051
        ResourceInterface $resource,
2052
        FlashHelperInterface $flashHelper,
2053
        EventDispatcherInterface $eventDispatcher,
2054
        ResourceControllerEvent $event,
2055
        Request $request
2056
    ): void {
2057
        $metadata->getApplicationName()->willReturn('sylius');
2058
        $metadata->getName()->willReturn('product');
2059
2060
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
2061
        $configuration->hasPermission()->willReturn(true);
2062
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
2063
2064
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
2065
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
2066
2067
        $configuration->isHtmlRequest()->willReturn(false);
2068
2069
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
2070
        $event->isStopped()->willReturn(true);
2071
        $event->getMessage()->willReturn('Cannot approve this product.');
2072
        $event->getErrorCode()->willReturn(500);
2073
2074
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
2075
        $objectManager->flush()->shouldNotBeCalled();
2076
2077
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
2078
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
2079
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
2080
2081
        $this
2082
            ->shouldThrow(new HttpException(500, 'Cannot approve this product.'))
2083
            ->during('applyStateMachineTransitionAction', [$request])
2084
        ;
2085
    }
2086
2087
    /**
2088
     * {@inheritdoc}
2089
     */
2090
    private function getViewComparingCallback(View $expectedView)
2091
    {
2092
        return function ($value) use ($expectedView) {
2093
            if (!$value instanceof View) {
2094
                return false;
2095
            }
2096
2097
            // Need to unwrap phpspec's Collaborators to ensure proper comparison.
2098
            $this->unwrapViewData($expectedView);
2099
            $this->nullifyDates($value);
2100
            $this->nullifyDates($expectedView);
2101
2102
            return
2103
                $expectedView->getStatusCode() === $value->getStatusCode() &&
2104
                $expectedView->getHeaders() === $value->getHeaders() &&
2105
                $expectedView->getEngine() === $value->getEngine() &&
2106
                $expectedView->getFormat() === $value->getFormat() &&
2107
                $expectedView->getData() === $value->getData() &&
2108
                $expectedView->getTemplate() === $value->getTemplate() &&
2109
                $expectedView->getTemplateVar() === $value->getTemplateVar()
2110
            ;
2111
        };
2112
    }
2113
2114
    /**
2115
     * @param View $view
2116
     */
2117
    private function unwrapViewData(View $view)
2118
    {
2119
        $view->setData($this->unwrapIfCollaborator($view->getData()));
2120
    }
2121
2122
    /**
2123
     * @param mixed $value
2124
     *
2125
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|object|integer|double|string|boolean|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
2126
     */
2127
    private function unwrapIfCollaborator($value)
2128
    {
2129
        if (null === $value) {
2130
            return null;
2131
        }
2132
2133
        if ($value instanceof Collaborator) {
2134
            return $value->getWrappedObject();
2135
        }
2136
2137
        if (is_array($value)) {
2138
            foreach ($value as $key => $childValue) {
2139
                $value[$key] = $this->unwrapIfCollaborator($childValue);
2140
            }
2141
        }
2142
2143
        return $value;
2144
    }
2145
2146
    /**
2147
     * @param View $view
2148
     */
2149
    private function nullifyDates(View $view)
2150
    {
2151
        $headers = $view->getHeaders();
2152
        unset($headers['date']);
2153
        $view->setHeaders($headers);
2154
    }
2155
}
2156