Completed
Push — theme/path-package ( f6cab5 )
by Kamil
36:15 queued 13:21
created

ResourceControllerSpec   F

Complexity

Total Complexity 58

Size/Duplication

Total Lines 1807
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 34

Importance

Changes 0
Metric Value
wmc 58
c 0
b 0
f 0
lcom 2
cbo 34
dl 0
loc 1807
rs 0.5217

47 Methods

Rating   Name   Duplication   Size   Complexity  
B let() 0 40 1
A it_is_initializable() 0 4 1
A it_extends_base_Symfony_controller() 0 4 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 42 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 48 1
A it_returns_a_html_response_for_invalid_form_during_resource_creation() 0 50 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 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 52 1
A it_returns_a_html_response_for_invalid_form_during_resource_update() 0 53 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_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 47 1
A it_does_not_delete_a_resource_and_redirects_to_index_for_html_requests_stopped_via_event() 0 48 1
A it_deletes_a_resource_and_returns_204_for_non_html_requests() 0 47 1
A it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event() 0 50 1
A it_throws_a_403_exception_if_csrf_token_is_invalid_during_delete_action() 0 48 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
B it_applies_state_machine_transition_to_resource_and_redirects_for_html_request() 0 43 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
namespace spec\Sylius\Bundle\ResourceBundle\Controller;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use FOS\RestBundle\View\View;
16
use PhpSpec\ObjectBehavior;
17
use PhpSpec\Wrapper\Collaborator;
18
use Prophecy\Argument;
19
use Sylius\Bundle\ResourceBundle\Controller\AuthorizationCheckerInterface;
20
use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
21
use Sylius\Bundle\ResourceBundle\Controller\FlashHelperInterface;
22
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
23
use Sylius\Bundle\ResourceBundle\Controller\RedirectHandlerInterface;
24
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
25
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
26
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
27
use Sylius\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface;
28
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
29
use Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandlerInterface;
30
use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface;
31
use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
32
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
33
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
34
use Sylius\Component\Resource\Factory\FactoryInterface;
35
use Sylius\Component\Resource\Metadata\MetadataInterface;
36
use Sylius\Component\Resource\Model\ResourceInterface;
37
use Sylius\Component\Resource\Repository\RepositoryInterface;
38
use Sylius\Component\Resource\ResourceActions;
39
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
40
use Symfony\Component\DependencyInjection\ContainerInterface;
41
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
42
use Symfony\Component\Form\Form;
43
use Symfony\Component\Form\FormView;
44
use Symfony\Component\HttpFoundation\ParameterBag;
45
use Symfony\Component\HttpFoundation\Request;
46
use Symfony\Component\HttpFoundation\Response;
47
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
48
use Symfony\Component\HttpKernel\Exception\HttpException;
49
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
50
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
51
use Symfony\Component\Security\Csrf\CsrfToken;
52
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
53
54
/**
55
 * @author Paweł Jędrzejewski <[email protected]>
56
 */
57
final class ResourceControllerSpec extends ObjectBehavior
58
{
59
    function let(
60
        MetadataInterface $metadata,
61
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
62
        ViewHandlerInterface $viewHandler,
63
        RepositoryInterface $repository,
64
        FactoryInterface $factory,
65
        NewResourceFactoryInterface $newResourceFactory,
66
        ObjectManager $manager,
67
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
68
        ResourcesCollectionProviderInterface $resourcesCollectionProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourcesCollectionProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
69
        ResourceFormFactoryInterface $resourceFormFactory,
70
        RedirectHandlerInterface $redirectHandler,
71
        FlashHelperInterface $flashHelper,
72
        AuthorizationCheckerInterface $authorizationChecker,
73
        EventDispatcherInterface $eventDispatcher,
74
        StateMachineInterface $stateMachine,
75
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
76
        ContainerInterface $container
77
    ) {
78
        $this->beConstructedWith(
79
            $metadata,
80
            $requestConfigurationFactory,
81
            $viewHandler,
82
            $repository,
83
            $factory,
84
            $newResourceFactory,
85
            $manager,
86
            $singleResourceProvider,
87
            $resourcesCollectionProvider,
88
            $resourceFormFactory,
89
            $redirectHandler,
90
            $flashHelper,
91
            $authorizationChecker,
92
            $eventDispatcher,
93
            $stateMachine,
94
            $resourceUpdateHandler
95
        );
96
97
        $this->setContainer($container);
98
    }
99
100
    function it_is_initializable()
101
    {
102
        $this->shouldHaveType(ResourceController::class);
103
    }
104
105
    function it_extends_base_Symfony_controller()
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
113
        RequestConfiguration $configuration,
114
        Request $request,
115
        AuthorizationCheckerInterface $authorizationChecker
116
    ) {
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
132
        RequestConfiguration $configuration,
133
        Request $request,
134
        AuthorizationCheckerInterface $authorizationChecker,
135
        RepositoryInterface $repository,
136
        SingleResourceProviderInterface $singleResourceProvider
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
137
    ) {
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
155
        RequestConfiguration $configuration,
156
        AuthorizationCheckerInterface $authorizationChecker,
157
        RepositoryInterface $repository,
158
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
159
        ResourceInterface $resource,
160
        ViewHandlerInterface $viewHandler,
161
        EventDispatcherInterface $eventDispatcher,
162
        Request $request,
163
        Response $response
164
    ) {
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
199
        RequestConfiguration $configuration,
200
        AuthorizationCheckerInterface $authorizationChecker,
201
        RepositoryInterface $repository,
202
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
203
        ResourceInterface $resource,
204
        ViewHandlerInterface $viewHandler,
205
        EventDispatcherInterface $eventDispatcher,
206
        Request $request,
207
        Response $response
208
    ) {
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
233
        RequestConfiguration $configuration,
234
        Request $request,
235
        AuthorizationCheckerInterface $authorizationChecker
236
    ) {
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,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
252
        RequestConfiguration $configuration,
253
        AuthorizationCheckerInterface $authorizationChecker,
254
        RepositoryInterface $repository,
255
        ResourcesCollectionProviderInterface $resourcesCollectionProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourcesCollectionProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
256
        ResourceInterface $resource1,
257
        ResourceInterface $resource2,
258
        ViewHandlerInterface $viewHandler,
259
        Request $request,
260
        Response $response
261
    ) {
262
        $metadata->getApplicationName()->willReturn('sylius');
263
        $metadata->getName()->willReturn('product');
264
        $metadata->getPluralName()->willReturn('products');
265
266
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
267
        $configuration->hasPermission()->willReturn(true);
268
        $configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index');
269
270
        $authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(true);
271
272
        $configuration->isHtmlRequest()->willReturn(true);
273
        $configuration->getTemplate(ResourceActions::INDEX . '.html')->willReturn('SyliusShopBundle:Product:index.html.twig');
274
        $resourcesCollectionProvider->get($configuration, $repository)->willReturn([$resource1, $resource2]);
275
276
        $expectedView = View::create()
277
            ->setData([
278
                'configuration' => $configuration,
279
                'metadata' => $metadata,
280
                'resources' => [$resource1, $resource2],
281
                'products' => [$resource1, $resource2],
282
            ])
283
            ->setTemplateVar('products')
284
            ->setTemplate('SyliusShopBundle:Product:index.html.twig')
285
        ;
286
287
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
288
289
        $this->indexAction($request)->shouldReturn($response);
290
    }
291
292
    function it_throws_a_403_exception_if_user_is_unauthorized_to_create_a_new_resource(
293
        MetadataInterface $metadata,
294
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
295
        RequestConfiguration $configuration,
296
        Request $request,
297
        AuthorizationCheckerInterface $authorizationChecker
298
    ) {
299
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
300
        $configuration->hasPermission()->willReturn(true);
301
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
302
303
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(false);
304
305
        $this
306
            ->shouldThrow(new AccessDeniedException())
307
            ->during('createAction', [$request])
308
        ;
309
    }
310
311
    function it_returns_a_html_response_for_creating_new_resource_form(
312
        MetadataInterface $metadata,
313
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
314
        RequestConfiguration $configuration,
315
        AuthorizationCheckerInterface $authorizationChecker,
316
        ViewHandlerInterface $viewHandler,
317
        FactoryInterface $factory,
318
        NewResourceFactoryInterface $newResourceFactory,
319
        ResourceInterface $newResource,
320
        ResourceFormFactoryInterface $resourceFormFactory,
321
        Form $form,
322
        FormView $formView,
323
        Request $request,
324
        Response $response
325
    ) {
326
        $metadata->getApplicationName()->willReturn('sylius');
327
        $metadata->getName()->willReturn('product');
328
329
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
330
        $configuration->hasPermission()->willReturn(true);
331
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
332
333
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
334
335
        $configuration->isHtmlRequest()->willReturn(true);
336
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
337
338
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
339
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
340
341
        $request->isMethod('POST')->willReturn(false);
342
        $form->createView()->willReturn($formView);
343
344
        $expectedView = View::create()
345
            ->setData([
346
                'configuration' => $configuration,
347
                'metadata' => $metadata,
348
                'resource' => $newResource,
349
                'product' => $newResource,
350
                'form' => $formView,
351
            ])
352
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
353
        ;
354
355
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
356
357
        $this->createAction($request)->shouldReturn($response);
358
    }
359
360
    function it_returns_a_html_response_for_invalid_form_during_resource_creation(
361
        MetadataInterface $metadata,
362
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
363
        RequestConfiguration $configuration,
364
        AuthorizationCheckerInterface $authorizationChecker,
365
        ViewHandlerInterface $viewHandler,
366
        FactoryInterface $factory,
367
        NewResourceFactoryInterface $newResourceFactory,
368
        ResourceInterface $newResource,
369
        ResourceFormFactoryInterface $resourceFormFactory,
370
        Form $form,
371
        FormView $formView,
372
        Request $request,
373
        Response $response
374
    ) {
375
        $metadata->getApplicationName()->willReturn('sylius');
376
        $metadata->getName()->willReturn('product');
377
378
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
379
        $configuration->hasPermission()->willReturn(true);
380
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
381
382
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
383
384
        $configuration->isHtmlRequest()->willReturn(true);
385
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
386
387
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
388
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
389
390
        $request->isMethod('POST')->willReturn(true);
391
        $form->handleRequest($request)->willReturn($form);
392
        $form->isValid()->willReturn(false);
393
        $form->createView()->willReturn($formView);
394
395
        $expectedView = View::create()
396
            ->setData([
397
                'configuration' => $configuration,
398
                'metadata' => $metadata,
399
                'resource' => $newResource,
400
                'product' => $newResource,
401
                'form' => $formView,
402
            ])
403
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
404
        ;
405
406
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
407
408
        $this->createAction($request)->shouldReturn($response);
409
    }
410
411
    function it_returns_a_non_html_response_for_invalid_form_during_resource_creation(
412
        MetadataInterface $metadata,
413
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
414
        RequestConfiguration $configuration,
415
        AuthorizationCheckerInterface $authorizationChecker,
416
        ViewHandlerInterface $viewHandler,
417
        FactoryInterface $factory,
418
        NewResourceFactoryInterface $newResourceFactory,
419
        ResourceInterface $newResource,
420
        ResourceFormFactoryInterface $resourceFormFactory,
421
        Form $form,
422
        Request $request,
423
        Response $response
424
    ) {
425
        $metadata->getApplicationName()->willReturn('sylius');
426
        $metadata->getName()->willReturn('product');
427
428
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
429
        $configuration->hasPermission()->willReturn(true);
430
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
431
432
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
433
434
        $configuration->isHtmlRequest()->willReturn(false);
435
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
436
437
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
438
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
439
440
        $request->isMethod('POST')->willReturn(true);
441
        $form->handleRequest($request)->willReturn($form);
442
        $form->isValid()->willReturn(false);
443
444
        $expectedView = View::create($form, 400);
445
446
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
447
448
        $this->createAction($request)->shouldReturn($response);
449
    }
450
451
    function it_does_not_create_the_resource_and_redirects_to_index_for_html_requests_stopped_via_events(
452
        MetadataInterface $metadata,
453
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
454
        RequestConfiguration $configuration,
455
        AuthorizationCheckerInterface $authorizationChecker,
456
        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...
457
        FactoryInterface $factory,
458
        NewResourceFactoryInterface $newResourceFactory,
459
        RepositoryInterface $repository,
460
        ResourceInterface $newResource,
461
        ResourceFormFactoryInterface $resourceFormFactory,
462
        Form $form,
463
        RedirectHandlerInterface $redirectHandler,
464
        FlashHelperInterface $flashHelper,
465
        EventDispatcherInterface $eventDispatcher,
466
        ResourceControllerEvent $event,
467
        Request $request,
468
        Response $redirectResponse
469
    ) {
470
        $metadata->getApplicationName()->willReturn('sylius');
471
        $metadata->getName()->willReturn('product');
472
473
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
474
        $configuration->hasPermission()->willReturn(true);
475
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
476
477
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
478
479
        $configuration->isHtmlRequest()->willReturn(true);
480
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
481
482
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
483
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
484
485
        $request->isMethod('POST')->willReturn(true);
486
        $form->handleRequest($request)->willReturn($form);
487
        $form->isValid()->willReturn(true);
488
        $form->getData()->willReturn($newResource);
489
490
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
491
        $event->isStopped()->willReturn(true);
492
493
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
494
495
        $repository->add($newResource)->shouldNotBeCalled();
496
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
497
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
498
499
        $redirectHandler->redirectToIndex($configuration, $newResource)->willReturn($redirectResponse);
500
501
        $this->createAction($request)->shouldReturn($redirectResponse);
502
    }
503
504
    function it_redirects_to_newly_created_resource(
505
        MetadataInterface $metadata,
506
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
507
        RequestConfiguration $configuration,
508
        AuthorizationCheckerInterface $authorizationChecker,
509
        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...
510
        FactoryInterface $factory,
511
        NewResourceFactoryInterface $newResourceFactory,
512
        RepositoryInterface $repository,
513
        ResourceInterface $newResource,
514
        ResourceFormFactoryInterface $resourceFormFactory,
515
        StateMachineInterface $stateMachine,
516
        Form $form,
517
        RedirectHandlerInterface $redirectHandler,
518
        FlashHelperInterface $flashHelper,
519
        EventDispatcherInterface $eventDispatcher,
520
        ResourceControllerEvent $event,
521
        Request $request,
522
        Response $redirectResponse
523
    ) {
524
        $metadata->getApplicationName()->willReturn('sylius');
525
        $metadata->getName()->willReturn('product');
526
527
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
528
        $configuration->hasPermission()->willReturn(true);
529
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
530
        $configuration->hasStateMachine()->willReturn(true);
531
532
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
533
534
        $configuration->isHtmlRequest()->willReturn(true);
535
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
536
537
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
538
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
539
540
        $request->isMethod('POST')->willReturn(true);
541
        $form->handleRequest($request)->willReturn($form);
542
        $form->isValid()->willReturn(true);
543
        $form->getData()->willReturn($newResource);
544
545
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
546
        $event->isStopped()->willReturn(false);
547
548
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
549
550
        $repository->add($newResource)->shouldBeCalled();
551
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldBeCalled();
552
553
        $flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled();
554
        $redirectHandler->redirectToResource($configuration, $newResource)->willReturn($redirectResponse);
555
556
        $this->createAction($request)->shouldReturn($redirectResponse);
557
    }
558
559
    function it_returns_a_non_html_response_for_correctly_created_resources(
560
        MetadataInterface $metadata,
561
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
562
        RequestConfiguration $configuration,
563
        AuthorizationCheckerInterface $authorizationChecker,
564
        ViewHandlerInterface $viewHandler,
565
        FactoryInterface $factory,
566
        NewResourceFactoryInterface $newResourceFactory,
567
        RepositoryInterface $repository,
568
        ResourceInterface $newResource,
569
        ResourceFormFactoryInterface $resourceFormFactory,
570
        FlashHelperInterface $flashHelper,
571
        EventDispatcherInterface $eventDispatcher,
572
        ResourceControllerEvent $event,
573
        StateMachineInterface $stateMachine,
574
        Form $form,
575
        Request $request,
576
        Response $response
577
    ) {
578
        $metadata->getApplicationName()->willReturn('sylius');
579
        $metadata->getName()->willReturn('product');
580
581
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
582
        $configuration->hasPermission()->willReturn(true);
583
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
584
        $configuration->hasStateMachine()->willReturn(true);
585
586
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
587
588
        $configuration->isHtmlRequest()->willReturn(false);
589
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
590
591
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
592
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
593
594
        $request->isMethod('POST')->willReturn(true);
595
        $form->handleRequest($request)->willReturn($form);
596
        $form->isValid()->willReturn(true);
597
        $form->getData()->willReturn($newResource);
598
599
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
600
        $event->isStopped()->willReturn(false);
601
602
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
603
604
        $repository->add($newResource)->shouldBeCalled();
605
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldBeCalled();
606
607
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
608
609
        $expectedView = View::create($newResource, 201);
610
611
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
612
613
        $this->createAction($request)->shouldReturn($response);
614
    }
615
616
    function it_does_not_create_the_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
617
        MetadataInterface $metadata,
618
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
619
        RequestConfiguration $configuration,
620
        AuthorizationCheckerInterface $authorizationChecker,
621
        FactoryInterface $factory,
622
        NewResourceFactoryInterface $newResourceFactory,
623
        RepositoryInterface $repository,
624
        ResourceInterface $newResource,
625
        ResourceFormFactoryInterface $resourceFormFactory,
626
        FlashHelperInterface $flashHelper,
627
        EventDispatcherInterface $eventDispatcher,
628
        Form $form,
629
        Request $request,
630
        ResourceControllerEvent $event
631
    ) {
632
        $metadata->getApplicationName()->willReturn('sylius');
633
        $metadata->getName()->willReturn('product');
634
635
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
636
        $configuration->hasPermission()->willReturn(true);
637
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
638
639
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
640
641
        $configuration->isHtmlRequest()->willReturn(false);
642
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
643
644
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
645
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
646
647
        $request->isMethod('POST')->willReturn(true);
648
        $form->handleRequest($request)->willReturn($form);
649
        $form->isValid()->willReturn(true);
650
        $form->getData()->willReturn($newResource);
651
652
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
653
        $event->isStopped()->willReturn(true);
654
        $event->getMessage()->willReturn('You cannot add a new product right now.');
655
        $event->getErrorCode()->willReturn(500);
656
657
        $repository->add($newResource)->shouldNotBeCalled();
658
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
659
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
660
661
        $this
662
            ->shouldThrow(new HttpException(500, 'You cannot add a new product right now.'))
663
            ->during('createAction', [$request])
664
        ;
665
    }
666
667
    function it_throws_a_403_exception_if_user_is_unauthorized_to_edit_a_single_resource(
668
        MetadataInterface $metadata,
669
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
670
        RequestConfiguration $configuration,
671
        Request $request,
672
        AuthorizationCheckerInterface $authorizationChecker
673
    ) {
674
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
675
        $configuration->hasPermission()->willReturn(true);
676
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
677
678
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
679
680
        $this
681
            ->shouldThrow(new AccessDeniedException())
682
            ->during('updateAction', [$request])
683
        ;
684
    }
685
686
    function it_throws_a_404_exception_if_resource_to_update_is_not_found_based_on_configuration(
687
        MetadataInterface $metadata,
688
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
689
        RequestConfiguration $configuration,
690
        Request $request,
691
        AuthorizationCheckerInterface $authorizationChecker,
692
        RepositoryInterface $repository,
693
        SingleResourceProviderInterface $singleResourceProvider
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
694
    ) {
695
        $metadata->getHumanizedName()->willReturn('product');
696
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
697
        $configuration->hasPermission()->willReturn(true);
698
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
699
700
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
701
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
702
703
        $this
704
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
705
            ->during('updateAction', [$request])
706
        ;
707
    }
708
709
    function it_returns_a_html_response_for_updating_resource_form(
710
        MetadataInterface $metadata,
711
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
712
        RequestConfiguration $configuration,
713
        AuthorizationCheckerInterface $authorizationChecker,
714
        ViewHandlerInterface $viewHandler,
715
        RepositoryInterface $repository,
716
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
717
        ResourceInterface $resource,
718
        ResourceFormFactoryInterface $resourceFormFactory,
719
        Form $form,
720
        FormView $formView,
721
        Request $request,
722
        Response $response
723
    ) {
724
        $metadata->getApplicationName()->willReturn('sylius');
725
        $metadata->getName()->willReturn('product');
726
727
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
728
        $configuration->hasPermission()->willReturn(true);
729
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
730
        $configuration->hasStateMachine()->willReturn(false);
731
732
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
733
734
        $configuration->isHtmlRequest()->willReturn(true);
735
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
736
737
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
738
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
739
740
        $request->isMethod('PATCH')->willReturn(false);
741
        $request->getMethod()->willReturn('GET');
742
743
        $form->handleRequest($request)->willReturn($form);
744
        $form->createView()->willReturn($formView);
745
746
        $expectedView = View::create()
747
            ->setData([
748
                'configuration' => $configuration,
749
                'metadata' => $metadata,
750
                'resource' => $resource,
751
                'product' => $resource,
752
                'form' => $formView,
753
            ])
754
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
755
        ;
756
757
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
758
759
        $this->updateAction($request)->shouldReturn($response);
760
    }
761
762
    function it_returns_a_html_response_for_invalid_form_during_resource_update(
763
        MetadataInterface $metadata,
764
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
765
        RequestConfiguration $configuration,
766
        AuthorizationCheckerInterface $authorizationChecker,
767
        ViewHandlerInterface $viewHandler,
768
        RepositoryInterface $repository,
769
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
770
        ResourceInterface $resource,
771
        ResourceFormFactoryInterface $resourceFormFactory,
772
        Form $form,
773
        FormView $formView,
774
        Request $request,
775
        Response $response
776
    ) {
777
        $metadata->getApplicationName()->willReturn('sylius');
778
        $metadata->getName()->willReturn('product');
779
780
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
781
        $configuration->hasPermission()->willReturn(true);
782
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
783
784
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
785
786
        $configuration->isHtmlRequest()->willReturn(true);
787
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
788
789
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
790
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
791
792
        $request->isMethod('PATCH')->willReturn(false);
793
        $request->getMethod()->willReturn('PUT');
794
795
        $form->handleRequest($request)->willReturn($form);
796
797
        $form->isValid()->willReturn(false);
798
        $form->createView()->willReturn($formView);
799
800
        $expectedView = View::create()
801
            ->setData([
802
                'configuration' => $configuration,
803
                'metadata' => $metadata,
804
                'resource' => $resource,
805
                'product' => $resource,
806
                'form' => $formView,
807
            ])
808
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
809
        ;
810
811
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
812
813
        $this->updateAction($request)->shouldReturn($response);
814
    }
815
816
    function it_returns_a_non_html_response_for_invalid_form_during_resource_update(
817
        MetadataInterface $metadata,
818
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
819
        RequestConfiguration $configuration,
820
        AuthorizationCheckerInterface $authorizationChecker,
821
        ViewHandlerInterface $viewHandler,
822
        RepositoryInterface $repository,
823
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
824
        ResourceInterface $resource,
825
        ResourceFormFactoryInterface $resourceFormFactory,
826
        Form $form,
827
        Request $request,
828
        Response $response
829
    ) {
830
        $metadata->getApplicationName()->willReturn('sylius');
831
        $metadata->getName()->willReturn('product');
832
833
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
834
        $configuration->hasPermission()->willReturn(true);
835
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
836
        $configuration->isHtmlRequest()->willReturn(false);
837
838
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
839
840
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
841
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
842
843
        $request->isMethod('PATCH')->willReturn(true);
844
        $request->getMethod()->willReturn('PATCH');
845
846
        $form->handleRequest($request)->willReturn($form);
847
        $form->isValid()->willReturn(false);
848
849
        $expectedView = View::create($form, 400);
850
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
851
852
        $this->updateAction($request)->shouldReturn($response);
853
    }
854
855
    function it_does_not_update_the_resource_and_redirects_to_resource_for_html_request_if_stopped_via_event(
856
        MetadataInterface $metadata,
857
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
858
        RequestConfiguration $configuration,
859
        AuthorizationCheckerInterface $authorizationChecker,
860
        ObjectManager $manager,
861
        RepositoryInterface $repository,
862
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
863
        ResourceInterface $resource,
864
        ResourceFormFactoryInterface $resourceFormFactory,
865
        Form $form,
866
        EventDispatcherInterface $eventDispatcher,
867
        RedirectHandlerInterface $redirectHandler,
868
        FlashHelperInterface $flashHelper,
869
        ResourceControllerEvent $event,
870
        Request $request,
871
        Response $redirectResponse
872
    ) {
873
        $metadata->getApplicationName()->willReturn('sylius');
874
        $metadata->getName()->willReturn('product');
875
876
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
877
        $configuration->hasPermission()->willReturn(true);
878
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
879
880
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
881
882
        $configuration->isHtmlRequest()->willReturn(true);
883
884
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
885
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
886
887
        $request->isMethod('PATCH')->willReturn(false);
888
        $request->getMethod()->willReturn('PUT');
889
890
        $form->handleRequest($request)->willReturn($form);
891
892
        $form->isSubmitted()->willReturn(true);
893
        $form->isValid()->willReturn(true);
894
        $form->getData()->willReturn($resource);
895
896
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
897
        $event->isStopped()->willReturn(true);
898
        $event->hasResponse()->willReturn(false);
899
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
900
901
        $manager->flush()->shouldNotBeCalled();
902
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
903
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
904
905
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
906
907
        $this->updateAction($request)->shouldReturn($redirectResponse);
908
    }
909
910
    function it_redirects_to_updated_resource(
911
        MetadataInterface $metadata,
912
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
913
        RepositoryInterface $repository,
914
        ObjectManager $manager,
915
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
916
        ResourceFormFactoryInterface $resourceFormFactory,
917
        RedirectHandlerInterface $redirectHandler,
918
        FlashHelperInterface $flashHelper,
919
        AuthorizationCheckerInterface $authorizationChecker,
920
        EventDispatcherInterface $eventDispatcher,
921
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
922
        RequestConfiguration $configuration,
923
        ResourceInterface $resource,
924
        Form $form,
925
        ResourceControllerEvent $preEvent,
926
        ResourceControllerEvent $postEvent,
927
        Request $request,
928
        Response $redirectResponse
929
    ) {
930
        $metadata->getApplicationName()->willReturn('sylius');
931
        $metadata->getName()->willReturn('product');
932
933
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
934
        $configuration->hasPermission()->willReturn(true);
935
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
936
        $configuration->hasStateMachine()->willReturn(false);
937
938
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
939
940
        $configuration->isHtmlRequest()->willReturn(true);
941
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
942
943
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
944
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
945
946
        $request->isMethod('PATCH')->willReturn(false);
947
        $request->getMethod()->willReturn('PUT');
948
949
        $form->handleRequest($request)->willReturn($form);
950
951
        $form->isSubmitted()->willReturn(true);
952
        $form->isValid()->willReturn(true);
953
        $form->getData()->willReturn($resource);
954
955
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent);
956
        $preEvent->isStopped()->willReturn(false);
957
958
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
959
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
960
961
        $postEvent->hasResponse()->willReturn(false);
962
963
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
964
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
965
966
        $this->updateAction($request)->shouldReturn($redirectResponse);
967
    }
968
969
    function it_uses_response_from_post_event_if_defined(
970
        MetadataInterface $metadata,
971
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
972
        RepositoryInterface $repository,
973
        ObjectManager $manager,
974
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
975
        ResourceFormFactoryInterface $resourceFormFactory,
976
        RedirectHandlerInterface $redirectHandler,
977
        FlashHelperInterface $flashHelper,
978
        AuthorizationCheckerInterface $authorizationChecker,
979
        EventDispatcherInterface $eventDispatcher,
980
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
981
        RequestConfiguration $configuration,
982
        ResourceInterface $resource,
983
        Form $form,
984
        ResourceControllerEvent $preEvent,
985
        ResourceControllerEvent $postEvent,
986
        Request $request,
987
        Response $redirectResponse
988
    ) {
989
        $metadata->getApplicationName()->willReturn('sylius');
990
        $metadata->getName()->willReturn('product');
991
992
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
993
        $configuration->hasPermission()->willReturn(true);
994
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
995
        $configuration->hasStateMachine()->willReturn(false);
996
997
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
998
999
        $configuration->isHtmlRequest()->willReturn(true);
1000
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
1001
1002
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1003
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1004
1005
        $request->isMethod('PATCH')->willReturn(false);
1006
        $request->getMethod()->willReturn('PUT');
1007
1008
        $form->handleRequest($request)->willReturn($form);
1009
1010
        $form->isSubmitted()->willReturn(true);
1011
        $form->isValid()->willReturn(true);
1012
        $form->getData()->willReturn($resource);
1013
1014
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent);
1015
        $preEvent->isStopped()->willReturn(false);
1016
1017
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1018
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1019
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1020
1021
        $postEvent->hasResponse()->willReturn(true);
1022
        $postEvent->getResponse()->willReturn($redirectResponse);
1023
1024
        $redirectHandler->redirectToResource($configuration, $resource)->shouldNotBeCalled();
1025
1026
        $this->updateAction($request)->shouldReturn($redirectResponse);
1027
    }
1028
1029
    function it_returns_a_non_html_response_for_correctly_updated_resource(
1030
        MetadataInterface $metadata,
1031
        ParameterBagInterface $parameterBag,
1032
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1033
        ViewHandlerInterface $viewHandler,
1034
        RepositoryInterface $repository,
1035
        ObjectManager $manager,
1036
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1037
        ResourceFormFactoryInterface $resourceFormFactory,
1038
        AuthorizationCheckerInterface $authorizationChecker,
1039
        EventDispatcherInterface $eventDispatcher,
1040
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1041
        RequestConfiguration $configuration,
1042
        ResourceInterface $resource,
1043
        ResourceControllerEvent $event,
1044
        Form $form,
1045
        Request $request,
1046
        Response $response
1047
    ) {
1048
        $metadata->getApplicationName()->willReturn('sylius');
1049
        $metadata->getName()->willReturn('product');
1050
1051
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1052
        $configuration->hasPermission()->willReturn(true);
1053
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1054
        $configuration->isHtmlRequest()->willReturn(false);
1055
        $configuration->hasStateMachine()->willReturn(false);
1056
1057
        $configuration->getParameters()->willReturn($parameterBag);
1058
        $parameterBag->get('return_content', false)->willReturn(false);
1059
1060
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1061
1062
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1063
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1064
1065
        $request->isMethod('PATCH')->willReturn(false);
1066
        $request->getMethod()->willReturn('PUT');
1067
1068
        $form->handleRequest($request)->willReturn($form);
1069
        $form->isValid()->willReturn(true);
1070
        $form->getData()->willReturn($resource);
1071
1072
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1073
        $event->isStopped()->willReturn(false);
1074
1075
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1076
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1077
1078
        $expectedView = View::create(null, 204);
1079
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1080
1081
        $this->updateAction($request)->shouldReturn($response);
1082
    }
1083
1084
    function it_does_not_update_the_resource_throws_a_http_exception_for_non_html_requests_stopped_via_event(
1085
        MetadataInterface $metadata,
1086
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1087
        RequestConfiguration $configuration,
1088
        AuthorizationCheckerInterface $authorizationChecker,
1089
        ObjectManager $manager,
1090
        RepositoryInterface $repository,
1091
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1092
        ResourceInterface $resource,
1093
        ResourceFormFactoryInterface $resourceFormFactory,
1094
        EventDispatcherInterface $eventDispatcher,
1095
        ResourceControllerEvent $event,
1096
        Form $form,
1097
        Request $request
1098
    ) {
1099
        $metadata->getApplicationName()->willReturn('sylius');
1100
        $metadata->getName()->willReturn('product');
1101
1102
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1103
        $configuration->hasPermission()->willReturn(true);
1104
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1105
        $configuration->isHtmlRequest()->willReturn(false);
1106
1107
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1108
1109
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1110
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1111
1112
        $request->isMethod('PATCH')->willReturn(false);
1113
        $request->getMethod()->willReturn('PUT');
1114
1115
        $form->handleRequest($request)->willReturn($form);
1116
        $form->isValid()->willReturn(true);
1117
        $form->getData()->willReturn($resource);
1118
1119
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1120
        $event->isStopped()->willReturn(true);
1121
        $event->getMessage()->willReturn('Cannot update this channel.');
1122
        $event->getErrorCode()->willReturn(500);
1123
1124
        $manager->flush()->shouldNotBeCalled();
1125
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1126
1127
        $this
1128
            ->shouldThrow(new HttpException(500, 'Cannot update this channel.'))
1129
            ->during('updateAction', [$request])
1130
        ;
1131
    }
1132
1133
    function it_applies_state_machine_transition_to_updated_resource_if_configured(
1134
        MetadataInterface $metadata,
1135
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1136
        RepositoryInterface $repository,
1137
        ObjectManager $manager,
1138
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1139
        ResourceFormFactoryInterface $resourceFormFactory,
1140
        RedirectHandlerInterface $redirectHandler,
1141
        FlashHelperInterface $flashHelper,
1142
        AuthorizationCheckerInterface $authorizationChecker,
1143
        EventDispatcherInterface $eventDispatcher,
1144
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1145
        RequestConfiguration $configuration,
1146
        ResourceInterface $resource,
1147
        Form $form,
1148
        ResourceControllerEvent $preEvent,
1149
        ResourceControllerEvent $postEvent,
1150
        Request $request,
1151
        Response $redirectResponse
1152
    ) {
1153
        $metadata->getApplicationName()->willReturn('sylius');
1154
        $metadata->getName()->willReturn('product');
1155
1156
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1157
        $configuration->hasPermission()->willReturn(true);
1158
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1159
        $configuration->hasStateMachine()->willReturn(true);
1160
1161
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1162
1163
        $configuration->isHtmlRequest()->willReturn(true);
1164
        $configuration->getTemplate(ResourceActions::UPDATE)->willReturn('SyliusShopBundle:Product:update.html.twig');
1165
1166
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1167
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1168
1169
        $request->isMethod('PATCH')->willReturn(false);
1170
        $request->getMethod()->willReturn('PUT');
1171
1172
        $form->handleRequest($request)->willReturn($form);
1173
1174
        $form->isSubmitted()->willReturn(true);
1175
        $form->isValid()->willReturn(true);
1176
        $form->getData()->willReturn($resource);
1177
1178
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent);
1179
        $preEvent->isStopped()->willReturn(false);
1180
1181
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1182
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1183
1184
        $postEvent->hasResponse()->willReturn(false);
1185
1186
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1187
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1188
1189
        $this->updateAction($request)->shouldReturn($redirectResponse);
1190
    }
1191
1192
    function it_throws_a_403_exception_if_user_is_unauthorized_to_delete_a_single_resource(
1193
        MetadataInterface $metadata,
1194
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1195
        RequestConfiguration $configuration,
1196
        Request $request,
1197
        AuthorizationCheckerInterface $authorizationChecker
1198
    ) {
1199
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1200
        $configuration->hasPermission()->willReturn(true);
1201
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1202
1203
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(false);
1204
1205
        $this
1206
            ->shouldThrow(new AccessDeniedException())
1207
            ->during('deleteAction', [$request])
1208
        ;
1209
    }
1210
1211
    function it_throws_a_404_exception_if_resource_for_deletion_is_not_found_based_on_configuration(
1212
        MetadataInterface $metadata,
1213
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1214
        RequestConfiguration $configuration,
1215
        Request $request,
1216
        AuthorizationCheckerInterface $authorizationChecker,
1217
        RepositoryInterface $repository,
1218
        SingleResourceProviderInterface $singleResourceProvider
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1219
    ) {
1220
        $metadata->getHumanizedName()->willReturn('product');
1221
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1222
        $configuration->hasPermission()->willReturn(true);
1223
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1224
1225
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1226
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
1227
1228
        $this
1229
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
1230
            ->during('deleteAction', [$request])
1231
        ;
1232
    }
1233
1234
    function it_deletes_a_resource_and_redirects_to_index_by_for_html_request(
1235
        MetadataInterface $metadata,
1236
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1237
        RequestConfiguration $configuration,
1238
        AuthorizationCheckerInterface $authorizationChecker,
1239
        RepositoryInterface $repository,
1240
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1241
        ResourceInterface $resource,
1242
        RedirectHandlerInterface $redirectHandler,
1243
        FlashHelperInterface $flashHelper,
1244
        EventDispatcherInterface $eventDispatcher,
1245
        CsrfTokenManagerInterface $csrfTokenManager,
1246
        ContainerInterface $container,
1247
        ResourceControllerEvent $event,
1248
        Request $request,
1249
        Response $redirectResponse
1250
    ) {
1251
        $metadata->getApplicationName()->willReturn('sylius');
1252
        $metadata->getName()->willReturn('product');
1253
1254
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1255
        $configuration->hasPermission()->willReturn(true);
1256
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1257
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1258
1259
        $container->has('security.csrf.token_manager')->willReturn(true);
1260
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1261
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1262
1263
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1264
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1265
        $resource->getId()->willReturn(1);
1266
1267
        $configuration->isHtmlRequest()->willReturn(true);
1268
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1269
1270
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1271
        $event->isStopped()->willReturn(false);
1272
1273
        $repository->remove($resource)->shouldBeCalled();
1274
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldBeCalled();
1275
1276
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled();
1277
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1278
1279
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1280
    }
1281
1282
    function it_does_not_delete_a_resource_and_redirects_to_index_for_html_requests_stopped_via_event(
1283
        MetadataInterface $metadata,
1284
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1285
        RequestConfiguration $configuration,
1286
        AuthorizationCheckerInterface $authorizationChecker,
1287
        RepositoryInterface $repository,
1288
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1289
        ResourceInterface $resource,
1290
        RedirectHandlerInterface $redirectHandler,
1291
        FlashHelperInterface $flashHelper,
1292
        EventDispatcherInterface $eventDispatcher,
1293
        CsrfTokenManagerInterface $csrfTokenManager,
1294
        ContainerInterface $container,
1295
        ResourceControllerEvent $event,
1296
        Request $request,
1297
        Response $redirectResponse
1298
    ) {
1299
        $metadata->getApplicationName()->willReturn('sylius');
1300
        $metadata->getName()->willReturn('product');
1301
1302
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1303
        $configuration->hasPermission()->willReturn(true);
1304
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1305
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1306
1307
        $container->has('security.csrf.token_manager')->willReturn(true);
1308
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1309
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1310
1311
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1312
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1313
        $resource->getId()->willReturn(1);
1314
1315
        $configuration->isHtmlRequest()->willReturn(true);
1316
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1317
1318
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1319
        $event->isStopped()->willReturn(true);
1320
1321
        $repository->remove($resource)->shouldNotBeCalled();
1322
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1323
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled();
1324
1325
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1326
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1327
1328
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1329
    }
1330
1331
    function it_deletes_a_resource_and_returns_204_for_non_html_requests(
1332
        MetadataInterface $metadata,
1333
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1334
        RequestConfiguration $configuration,
1335
        AuthorizationCheckerInterface $authorizationChecker,
1336
        ViewHandlerInterface $viewHandler,
1337
        RepositoryInterface $repository,
1338
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1339
        ResourceInterface $resource,
1340
        EventDispatcherInterface $eventDispatcher,
1341
        CsrfTokenManagerInterface $csrfTokenManager,
1342
        ContainerInterface $container,
1343
        ResourceControllerEvent $event,
1344
        Request $request,
1345
        Response $response
1346
    ) {
1347
        $metadata->getApplicationName()->willReturn('sylius');
1348
        $metadata->getName()->willReturn('product');
1349
1350
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1351
        $configuration->hasPermission()->willReturn(true);
1352
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1353
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1354
1355
        $container->has('security.csrf.token_manager')->willReturn(true);
1356
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1357
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1358
1359
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1360
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1361
        $resource->getId()->willReturn(1);
1362
1363
        $configuration->isHtmlRequest()->willReturn(false);
1364
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1365
1366
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1367
        $event->isStopped()->willReturn(false);
1368
1369
        $repository->remove($resource)->shouldBeCalled();
1370
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldBeCalled();
1371
1372
        $expectedView = View::create(null, 204);
1373
1374
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1375
1376
        $this->deleteAction($request)->shouldReturn($response);
1377
    }
1378
1379
    function it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
1380
        MetadataInterface $metadata,
1381
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1382
        RequestConfiguration $configuration,
1383
        AuthorizationCheckerInterface $authorizationChecker,
1384
        RepositoryInterface $repository,
1385
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1386
        ResourceInterface $resource,
1387
        FlashHelperInterface $flashHelper,
1388
        EventDispatcherInterface $eventDispatcher,
1389
        CsrfTokenManagerInterface $csrfTokenManager,
1390
        ContainerInterface $container,
1391
        ResourceControllerEvent $event,
1392
        Request $request
1393
    ) {
1394
        $metadata->getApplicationName()->willReturn('sylius');
1395
        $metadata->getName()->willReturn('product');
1396
1397
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1398
        $configuration->hasPermission()->willReturn(true);
1399
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1400
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1401
1402
        $container->has('security.csrf.token_manager')->willReturn(true);
1403
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1404
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1405
1406
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1407
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1408
        $resource->getId()->willReturn(1);
1409
1410
        $configuration->isHtmlRequest()->willReturn(false);
1411
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1412
1413
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1414
        $event->isStopped()->willReturn(true);
1415
        $event->getMessage()->willReturn('Cannot delete this product.');
1416
        $event->getErrorCode()->willReturn(500);
1417
1418
        $repository->remove($resource)->shouldNotBeCalled();
1419
1420
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1421
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1422
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1423
1424
        $this
1425
            ->shouldThrow(new HttpException(500, 'Cannot delete this product.'))
1426
            ->during('deleteAction', [$request])
1427
        ;
1428
    }
1429
1430
    function it_throws_a_403_exception_if_csrf_token_is_invalid_during_delete_action(
1431
        MetadataInterface $metadata,
1432
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1433
        RequestConfiguration $configuration,
1434
        AuthorizationCheckerInterface $authorizationChecker,
1435
        RepositoryInterface $repository,
1436
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1437
        ResourceInterface $resource,
1438
        FlashHelperInterface $flashHelper,
1439
        EventDispatcherInterface $eventDispatcher,
1440
        CsrfTokenManagerInterface $csrfTokenManager,
1441
        ContainerInterface $container,
1442
        ResourceControllerEvent $event,
1443
        Request $request
1444
    ) {
1445
        $metadata->getApplicationName()->willReturn('sylius');
1446
        $metadata->getName()->willReturn('product');
1447
1448
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1449
        $configuration->hasPermission()->willReturn(true);
1450
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1451
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1452
1453
        $container->has('security.csrf.token_manager')->willReturn(true);
1454
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1455
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(false);
1456
1457
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1458
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1459
        $resource->getId()->willReturn(1);
1460
1461
        $configuration->isHtmlRequest()->willReturn(true);
1462
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1463
1464
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1465
        $event->isStopped()->shouldNotBeCalled();
1466
1467
        $repository->remove($resource)->shouldNotBeCalled();
1468
1469
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1470
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1471
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1472
1473
        $this
1474
            ->shouldThrow(new HttpException(403, 'Invalid csrf token.'))
1475
            ->during('deleteAction', [$request])
1476
        ;
1477
    }
1478
1479
    function it_throws_a_403_exception_if_user_is_unauthorized_to_apply_state_machine_transition_on_resource(
1480
        MetadataInterface $metadata,
1481
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1482
        RequestConfiguration $configuration,
1483
        Request $request,
1484
        AuthorizationCheckerInterface $authorizationChecker
1485
    ) {
1486
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1487
        $configuration->hasPermission()->willReturn(true);
1488
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1489
1490
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
1491
1492
        $this
1493
            ->shouldThrow(new AccessDeniedException())
1494
            ->during('applyStateMachineTransitionAction', [$request])
1495
        ;
1496
    }
1497
1498
    function it_throws_a_404_exception_if_resource_is_not_found_when_trying_to_apply_state_machine_transition(
1499
        MetadataInterface $metadata,
1500
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1501
        RequestConfiguration $configuration,
1502
        Request $request,
1503
        AuthorizationCheckerInterface $authorizationChecker,
1504
        RepositoryInterface $repository,
1505
        SingleResourceProviderInterface $singleResourceProvider
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1506
    ) {
1507
        $metadata->getHumanizedName()->willReturn('product');
1508
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1509
        $configuration->hasPermission()->willReturn(true);
1510
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1511
1512
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1513
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
1514
1515
        $this
1516
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
1517
            ->during('applyStateMachineTransitionAction', [$request])
1518
        ;
1519
    }
1520
1521
    function it_does_not_apply_state_machine_transition_on_resource_if_not_applicable_and_returns_400_bad_request(
1522
        MetadataInterface $metadata,
1523
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1524
        RequestConfiguration $configuration,
1525
        AuthorizationCheckerInterface $authorizationChecker,
1526
        RepositoryInterface $repository,
1527
        ObjectManager $objectManager,
1528
        StateMachineInterface $stateMachine,
1529
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1530
        ResourceInterface $resource,
1531
        FlashHelperInterface $flashHelper,
1532
        EventDispatcherInterface $eventDispatcher,
1533
        ResourceControllerEvent $event,
1534
        Request $request
1535
    ) {
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::UPDATE)->willReturn('sylius.product.update');
1542
1543
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1544
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1545
1546
        $configuration->isHtmlRequest()->willReturn(true);
1547
1548
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1549
        $event->isStopped()->willReturn(false);
1550
1551
        $stateMachine->can($configuration, $resource)->willReturn(false);
1552
1553
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
1554
        $objectManager->flush()->shouldNotBeCalled();
1555
1556
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1557
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1558
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1559
1560
        $this
1561
            ->shouldThrow(new BadRequestHttpException())
1562
            ->during('applyStateMachineTransitionAction', [$request])
1563
        ;
1564
    }
1565
1566
    function it_applies_state_machine_transition_to_resource_and_redirects_for_html_request(
1567
        MetadataInterface $metadata,
1568
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1569
        RepositoryInterface $repository,
1570
        ObjectManager $manager,
1571
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1572
        RedirectHandlerInterface $redirectHandler,
1573
        FlashHelperInterface $flashHelper,
1574
        AuthorizationCheckerInterface $authorizationChecker,
1575
        EventDispatcherInterface $eventDispatcher,
1576
        StateMachineInterface $stateMachine,
1577
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1578
        RequestConfiguration $configuration,
1579
        ResourceInterface $resource,
1580
        ResourceControllerEvent $event,
1581
        Request $request,
1582
        Response $redirectResponse
1583
    ) {
1584
        $metadata->getApplicationName()->willReturn('sylius');
1585
        $metadata->getName()->willReturn('product');
1586
1587
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1588
        $configuration->hasPermission()->willReturn(true);
1589
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1590
1591
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1592
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1593
1594
        $configuration->isHtmlRequest()->willReturn(true);
1595
1596
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1597
        $event->isStopped()->willReturn(false);
1598
1599
        $stateMachine->can($configuration, $resource)->willReturn(true);
1600
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1601
1602
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1603
1604
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1605
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1606
1607
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1608
    }
1609
1610
    function it_does_not_apply_state_machine_transition_on_resource_and_redirects_for_html_requests_stopped_via_event(
1611
        MetadataInterface $metadata,
1612
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1613
        RequestConfiguration $configuration,
1614
        AuthorizationCheckerInterface $authorizationChecker,
1615
        StateMachineInterface $stateMachine,
1616
        ObjectManager $manager,
1617
        RepositoryInterface $repository,
1618
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1619
        ResourceInterface $resource,
1620
        RedirectHandlerInterface $redirectHandler,
1621
        FlashHelperInterface $flashHelper,
1622
        EventDispatcherInterface $eventDispatcher,
1623
        ResourceControllerEvent $event,
1624
        Request $request,
1625
        Response $redirectResponse
1626
    ) {
1627
        $metadata->getApplicationName()->willReturn('sylius');
1628
        $metadata->getName()->willReturn('product');
1629
1630
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1631
        $configuration->hasPermission()->willReturn(true);
1632
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1633
1634
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1635
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1636
1637
        $configuration->isHtmlRequest()->willReturn(true);
1638
1639
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1640
        $event->isStopped()->willReturn(true);
1641
1642
        $manager->flush()->shouldNotBeCalled();
1643
        $stateMachine->apply($resource)->shouldNotBeCalled();
1644
1645
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled();
1646
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled();
1647
1648
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1649
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1650
1651
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1652
    }
1653
1654
    function it_applies_state_machine_transition_on_resource_and_returns_200_for_non_html_requests(
1655
        MetadataInterface $metadata,
1656
        ParameterBagInterface $parameterBag,
1657
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1658
        ViewHandlerInterface $viewHandler,
1659
        RepositoryInterface $repository,
1660
        ObjectManager $manager,
1661
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1662
        AuthorizationCheckerInterface $authorizationChecker,
1663
        EventDispatcherInterface $eventDispatcher,
1664
        StateMachineInterface $stateMachine,
1665
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1666
        RequestConfiguration $configuration,
1667
        ResourceInterface $resource,
1668
        ResourceControllerEvent $event,
1669
        Request $request,
1670
        Response $response
1671
    ) {
1672
        $metadata->getApplicationName()->willReturn('sylius');
1673
        $metadata->getName()->willReturn('product');
1674
1675
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1676
        $configuration->getParameters()->willReturn($parameterBag);
1677
        $configuration->hasPermission()->willReturn(true);
1678
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1679
1680
        $parameterBag->get('return_content', true)->willReturn(true);
1681
1682
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1683
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1684
1685
        $configuration->isHtmlRequest()->willReturn(false);
1686
1687
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1688
        $event->isStopped()->willReturn(false);
1689
1690
        $stateMachine->can($configuration, $resource)->willReturn(true);
1691
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1692
1693
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1694
1695
        $expectedView = View::create($resource, 200);
1696
1697
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1698
1699
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
1700
    }
1701
1702
    function it_applies_state_machine_transition_on_resource_and_returns_204_for_non_html_requests_if_additional_option_added(
1703
        MetadataInterface $metadata,
1704
        ParameterBagInterface $parameterBag,
1705
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1706
        ViewHandlerInterface $viewHandler,
1707
        RepositoryInterface $repository,
1708
        ObjectManager $manager,
1709
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1710
        AuthorizationCheckerInterface $authorizationChecker,
1711
        EventDispatcherInterface $eventDispatcher,
1712
        StateMachineInterface $stateMachine,
1713
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceUpdateHandler exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1714
        RequestConfiguration $configuration,
1715
        ResourceInterface $resource,
1716
        ResourceControllerEvent $event,
1717
        Request $request,
1718
        Response $response
1719
    ) {
1720
        $metadata->getApplicationName()->willReturn('sylius');
1721
        $metadata->getName()->willReturn('product');
1722
1723
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1724
        $configuration->getParameters()->willReturn($parameterBag);
1725
        $configuration->hasPermission()->willReturn(true);
1726
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1727
1728
        $parameterBag->get('return_content', true)->willReturn(false);
1729
1730
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1731
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1732
1733
        $configuration->isHtmlRequest()->willReturn(false);
1734
1735
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1736
        $event->isStopped()->willReturn(false);
1737
1738
        $stateMachine->can($configuration, $resource)->willReturn(true);
1739
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1740
1741
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1742
1743
        $expectedView = View::create(null, 204);
1744
1745
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1746
1747
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
1748
    }
1749
1750
    function it_does_not_apply_state_machine_transition_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
1751
        MetadataInterface $metadata,
1752
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $requestConfigurationFactory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1753
        RequestConfiguration $configuration,
1754
        AuthorizationCheckerInterface $authorizationChecker,
1755
        RepositoryInterface $repository,
1756
        ObjectManager $objectManager,
1757
        StateMachineInterface $stateMachine,
1758
        SingleResourceProviderInterface $singleResourceProvider,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $singleResourceProvider exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
1759
        ResourceInterface $resource,
1760
        FlashHelperInterface $flashHelper,
1761
        EventDispatcherInterface $eventDispatcher,
1762
        ResourceControllerEvent $event,
1763
        Request $request
1764
    ) {
1765
        $metadata->getApplicationName()->willReturn('sylius');
1766
        $metadata->getName()->willReturn('product');
1767
1768
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1769
        $configuration->hasPermission()->willReturn(true);
1770
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1771
1772
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1773
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1774
1775
        $configuration->isHtmlRequest()->willReturn(false);
1776
1777
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1778
        $event->isStopped()->willReturn(true);
1779
        $event->getMessage()->willReturn('Cannot approve this product.');
1780
        $event->getErrorCode()->willReturn(500);
1781
1782
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
1783
        $objectManager->flush()->shouldNotBeCalled();
1784
1785
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1786
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1787
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1788
1789
        $this
1790
            ->shouldThrow(new HttpException(500, 'Cannot approve this product.'))
1791
            ->during('applyStateMachineTransitionAction', [$request])
1792
        ;
1793
    }
1794
1795
    /**
1796
     * {@inheritdoc}
1797
     */
1798
    private function getViewComparingCallback(View $expectedView)
1799
    {
1800
        return function ($value) use ($expectedView) {
1801
            if (!$value instanceof View) {
1802
                return false;
1803
            }
1804
1805
            // Need to unwrap phpspec's Collaborators to ensure proper comparison.
1806
            $this->unwrapViewData($expectedView);
1807
            $this->nullifyDates($value);
1808
            $this->nullifyDates($expectedView);
1809
1810
            return
1811
                $expectedView->getStatusCode() === $value->getStatusCode() &&
1812
                $expectedView->getHeaders() === $value->getHeaders() &&
1813
                $expectedView->getEngine() === $value->getEngine() &&
1814
                $expectedView->getFormat() === $value->getFormat() &&
1815
                $expectedView->getData() === $value->getData() &&
1816
                $expectedView->getTemplate() === $value->getTemplate() &&
1817
                $expectedView->getTemplateVar() === $value->getTemplateVar()
1818
            ;
1819
        };
1820
    }
1821
1822
    /**
1823
     * @param View $view
1824
     */
1825
    private function unwrapViewData(View $view)
1826
    {
1827
        $view->setData($this->unwrapIfCollaborator($view->getData()));
1828
    }
1829
1830
    /**
1831
     * @param mixed $value
1832
     *
1833
     * @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...
1834
     */
1835
    private function unwrapIfCollaborator($value)
1836
    {
1837
        if (null === $value) {
1838
            return null;
1839
        }
1840
1841
        if ($value instanceof Collaborator) {
1842
            return $value->getWrappedObject();
1843
        }
1844
1845
        if (is_array($value)) {
1846
            foreach ($value as $key => $childValue) {
1847
                $value[$key] = $this->unwrapIfCollaborator($childValue);
1848
            }
1849
        }
1850
1851
        return $value;
1852
    }
1853
1854
    /**
1855
     * @param View $view
1856
     */
1857
    private function nullifyDates(View $view)
1858
    {
1859
        $headers = $view->getHeaders();
1860
        unset($headers['date']);
1861
        $view->setHeaders($headers);
1862
    }
1863
}
1864