Completed
Push — symfony3-directorystructure ( c2fd26 )
by Kamil
85:38 queued 68:27
created

ResourceControllerSpec::it_is_container_aware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
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\SingleResourceProviderInterface;
30
use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
31
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
32
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
33
use Sylius\Component\Resource\Factory\FactoryInterface;
34
use Sylius\Component\Resource\Metadata\MetadataInterface;
35
use Sylius\Component\Resource\Model\ResourceInterface;
36
use Sylius\Component\Resource\Repository\RepositoryInterface;
37
use Sylius\Component\Resource\ResourceActions;
38
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
39
use Symfony\Component\DependencyInjection\ContainerAware;
40
use Symfony\Component\Form\Form;
41
use Symfony\Component\Form\FormView;
42
use Symfony\Component\HttpFoundation\Request;
43
use Symfony\Component\HttpFoundation\Response;
44
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
45
use Symfony\Component\HttpKernel\Exception\HttpException;
46
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
47
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
48
49
/**
50
 * @author Paweł Jędrzejewski <[email protected]>
51
 */
52
final class ResourceControllerSpec extends ObjectBehavior
53
{
54
    function let(
55
        MetadataInterface $metadata,
56
        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...
57
        ViewHandlerInterface $viewHandler,
58
        RepositoryInterface $repository,
59
        FactoryInterface $factory,
60
        NewResourceFactoryInterface $newResourceFactory,
61
        ObjectManager $manager,
62
        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...
63
        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...
64
        ResourceFormFactoryInterface $resourceFormFactory,
65
        RedirectHandlerInterface $redirectHandler,
66
        FlashHelperInterface $flashHelper,
67
        AuthorizationCheckerInterface $authorizationChecker,
68
        EventDispatcherInterface $eventDispatcher,
69
        StateMachineInterface $stateMachine
70
    ) {
71
        $this->beConstructedWith(
72
            $metadata,
73
            $requestConfigurationFactory,
74
            $viewHandler,
75
            $repository,
76
            $factory,
77
            $newResourceFactory,
78
            $manager,
79
            $singleResourceProvider,
80
            $resourcesCollectionProvider,
81
            $resourceFormFactory,
82
            $redirectHandler,
83
            $flashHelper,
84
            $authorizationChecker,
85
            $eventDispatcher,
86
            $stateMachine
87
        );
88
    }
89
90
    function it_is_initializable()
91
    {
92
        $this->shouldHaveType(ResourceController::class);
93
    }
94
95
    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...
96
    {
97
        $this->shouldHaveType(Controller::class);
98
    }
99
100
    function it_throws_a_403_exception_if_user_is_unauthorized_to_view_a_single_resource(
101
        MetadataInterface $metadata,
102
        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...
103
        RequestConfiguration $configuration,
104
        Request $request,
105
        AuthorizationCheckerInterface $authorizationChecker
106
    ) {
107
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
108
        $configuration->hasPermission()->willReturn(true);
109
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
110
111
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(false);
112
113
        $this
114
            ->shouldThrow(new AccessDeniedException())
115
            ->during('showAction', [$request])
116
        ;
117
    }
118
119
    function it_throws_a_404_exception_if_resource_is_not_found_based_on_configuration(
120
        MetadataInterface $metadata,
121
        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...
122
        RequestConfiguration $configuration,
123
        Request $request,
124
        AuthorizationCheckerInterface $authorizationChecker,
125
        RepositoryInterface $repository,
126
        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...
127
    ) {
128
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
129
        $configuration->hasPermission()->willReturn(true);
130
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
131
132
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
133
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
134
135
        $this
136
            ->shouldThrow(new NotFoundHttpException())
137
            ->during('showAction', [$request])
138
        ;
139
    }
140
141
    function it_returns_a_response_for_html_view_of_a_single_resource(
142
        MetadataInterface $metadata,
143
        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...
144
        RequestConfiguration $configuration,
145
        AuthorizationCheckerInterface $authorizationChecker,
146
        RepositoryInterface $repository,
147
        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...
148
        ResourceInterface $resource,
149
        ViewHandlerInterface $viewHandler,
150
        EventDispatcherInterface $eventDispatcher,
151
        Request $request,
152
        Response $response
153
    ) {
154
        $metadata->getApplicationName()->willReturn('sylius');
155
        $metadata->getName()->willReturn('product');
156
157
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
158
        $configuration->hasPermission()->willReturn(true);
159
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
160
161
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
162
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
163
164
        $configuration->isHtmlRequest()->willReturn(true);
165
        $configuration->getTemplate(ResourceActions::SHOW . '.html')->willReturn('SyliusShopBundle:Product:show.html.twig');
166
167
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
168
169
        $expectedView = View::create()
170
            ->setData([
171
                'configuration' => $configuration,
172
                'metadata' => $metadata,
173
                'resource' => $resource,
174
                'product' => $resource,
175
            ])
176
            ->setTemplateVar('product')
177
            ->setTemplate('SyliusShopBundle:Product:show.html.twig')
178
        ;
179
180
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
181
182
        $this->showAction($request)->shouldReturn($response);
183
    }
184
185
    function it_returns_a_response_for_non_html_view_of_single_resource(
186
        MetadataInterface $metadata,
187
        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...
188
        RequestConfiguration $configuration,
189
        AuthorizationCheckerInterface $authorizationChecker,
190
        RepositoryInterface $repository,
191
        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...
192
        ResourceInterface $resource,
193
        ViewHandlerInterface $viewHandler,
194
        EventDispatcherInterface $eventDispatcher,
195
        Request $request,
196
        Response $response
197
    ) {
198
        $metadata->getApplicationName()->willReturn('sylius');
199
        $metadata->getName()->willReturn('product');
200
201
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
202
        $configuration->hasPermission()->willReturn(true);
203
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
204
205
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
206
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
207
208
        $configuration->isHtmlRequest()->willReturn(false);
209
210
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
211
212
        $expectedView = View::create($resource);
213
214
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
215
216
        $this->showAction($request)->shouldReturn($response);
217
    }
218
219
    function it_throws_a_403_exception_if_user_is_unauthorized_to_view_an_index_of_resources(
220
        MetadataInterface $metadata,
221
        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...
222
        RequestConfiguration $configuration,
223
        Request $request,
224
        AuthorizationCheckerInterface $authorizationChecker
225
    ) {
226
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
227
        $configuration->hasPermission()->willReturn(true);
228
        $configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index');
229
230
        $authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(false);
231
232
        $this
233
            ->shouldThrow(new AccessDeniedException())
234
            ->during('indexAction', [$request])
235
        ;
236
    }
237
238
    function it_returns_a_response_for_html_view_of_paginated_resources(
239
        MetadataInterface $metadata,
240
        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...
241
        RequestConfiguration $configuration,
242
        AuthorizationCheckerInterface $authorizationChecker,
243
        RepositoryInterface $repository,
244
        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...
245
        ResourceInterface $resource1,
246
        ResourceInterface $resource2,
247
        ViewHandlerInterface $viewHandler,
248
        Request $request,
249
        Response $response
250
    ) {
251
        $metadata->getApplicationName()->willReturn('sylius');
252
        $metadata->getName()->willReturn('product');
253
        $metadata->getPluralName()->willReturn('products');
254
255
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
256
        $configuration->hasPermission()->willReturn(true);
257
        $configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index');
258
259
        $authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(true);
260
261
        $configuration->isHtmlRequest()->willReturn(true);
262
        $configuration->getTemplate(ResourceActions::INDEX . '.html')->willReturn('SyliusShopBundle:Product:index.html.twig');
263
        $resourcesCollectionProvider->get($configuration, $repository)->willReturn([$resource1, $resource2]);
264
265
        $expectedView = View::create()
266
            ->setData([
267
                'configuration' => $configuration,
268
                'metadata' => $metadata,
269
                'resources' => [$resource1, $resource2],
270
                'products' => [$resource1, $resource2],
271
            ])
272
            ->setTemplateVar('products')
273
            ->setTemplate('SyliusShopBundle:Product:index.html.twig')
274
        ;
275
276
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
277
278
        $this->indexAction($request)->shouldReturn($response);
279
    }
280
281
    function it_throws_a_403_exception_if_user_is_unauthorized_to_create_a_new_resource(
282
        MetadataInterface $metadata,
283
        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...
284
        RequestConfiguration $configuration,
285
        Request $request,
286
        AuthorizationCheckerInterface $authorizationChecker
287
    ) {
288
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
289
        $configuration->hasPermission()->willReturn(true);
290
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
291
292
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(false);
293
294
        $this
295
            ->shouldThrow(new AccessDeniedException())
296
            ->during('createAction', [$request])
297
        ;
298
    }
299
300
    function it_returns_a_html_response_for_creating_new_resource_form(
301
        MetadataInterface $metadata,
302
        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...
303
        RequestConfiguration $configuration,
304
        AuthorizationCheckerInterface $authorizationChecker,
305
        ViewHandlerInterface $viewHandler,
306
        FactoryInterface $factory,
307
        NewResourceFactoryInterface $newResourceFactory,
308
        ResourceInterface $newResource,
309
        ResourceFormFactoryInterface $resourceFormFactory,
310
        Form $form,
311
        FormView $formView,
312
        Request $request,
313
        Response $response
314
    ) {
315
        $metadata->getApplicationName()->willReturn('sylius');
316
        $metadata->getName()->willReturn('product');
317
318
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
319
        $configuration->hasPermission()->willReturn(true);
320
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
321
322
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
323
324
        $configuration->isHtmlRequest()->willReturn(true);
325
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
326
327
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
328
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
329
330
        $request->isMethod('POST')->willReturn(false);
331
        $form->createView()->willReturn($formView);
332
333
        $expectedView = View::create()
334
            ->setData([
335
                'configuration' => $configuration,
336
                'metadata' => $metadata,
337
                'resource' => $newResource,
338
                'product' => $newResource,
339
                'form' => $formView,
340
            ])
341
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
342
        ;
343
344
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
345
346
        $this->createAction($request)->shouldReturn($response);
347
    }
348
349
    function it_returns_a_html_response_for_invalid_form_during_resource_creation(
350
        MetadataInterface $metadata,
351
        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...
352
        RequestConfiguration $configuration,
353
        AuthorizationCheckerInterface $authorizationChecker,
354
        ViewHandlerInterface $viewHandler,
355
        FactoryInterface $factory,
356
        NewResourceFactoryInterface $newResourceFactory,
357
        ResourceInterface $newResource,
358
        ResourceFormFactoryInterface $resourceFormFactory,
359
        Form $form,
360
        FormView $formView,
361
        Request $request,
362
        Response $response
363
    ) {
364
        $metadata->getApplicationName()->willReturn('sylius');
365
        $metadata->getName()->willReturn('product');
366
367
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
368
        $configuration->hasPermission()->willReturn(true);
369
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
370
371
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
372
373
        $configuration->isHtmlRequest()->willReturn(true);
374
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
375
376
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
377
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
378
379
        $request->isMethod('POST')->willReturn(true);
380
        $form->submit($request)->willReturn($form);
381
        $form->isValid()->willReturn(false);
382
        $form->createView()->willReturn($formView);
383
384
        $expectedView = View::create()
385
            ->setData([
386
                'configuration' => $configuration,
387
                'metadata' => $metadata,
388
                'resource' => $newResource,
389
                'product' => $newResource,
390
                'form' => $formView,
391
            ])
392
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
393
        ;
394
395
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
396
397
        $this->createAction($request)->shouldReturn($response);
398
    }
399
400
    function it_returns_a_non_html_response_for_invalid_form_during_resource_creation(
401
        MetadataInterface $metadata,
402
        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...
403
        RequestConfiguration $configuration,
404
        AuthorizationCheckerInterface $authorizationChecker,
405
        ViewHandlerInterface $viewHandler,
406
        FactoryInterface $factory,
407
        NewResourceFactoryInterface $newResourceFactory,
408
        ResourceInterface $newResource,
409
        ResourceFormFactoryInterface $resourceFormFactory,
410
        Form $form,
411
        Request $request,
412
        Response $response
413
    ) {
414
        $metadata->getApplicationName()->willReturn('sylius');
415
        $metadata->getName()->willReturn('product');
416
417
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
418
        $configuration->hasPermission()->willReturn(true);
419
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
420
421
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
422
423
        $configuration->isHtmlRequest()->willReturn(false);
424
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
425
426
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
427
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
428
429
        $request->isMethod('POST')->willReturn(true);
430
        $form->submit($request)->willReturn($form);
431
        $form->isValid()->willReturn(false);
432
433
        $expectedView = View::create($form, 400);
434
435
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
436
437
        $this->createAction($request)->shouldReturn($response);
438
    }
439
440
    function it_does_not_create_the_resource_and_redirects_to_index_for_html_requests_stopped_via_events(
441
        MetadataInterface $metadata,
442
        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...
443
        RequestConfiguration $configuration,
444
        AuthorizationCheckerInterface $authorizationChecker,
445
        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...
446
        FactoryInterface $factory,
447
        NewResourceFactoryInterface $newResourceFactory,
448
        RepositoryInterface $repository,
449
        ResourceInterface $newResource,
450
        ResourceFormFactoryInterface $resourceFormFactory,
451
        Form $form,
452
        RedirectHandlerInterface $redirectHandler,
453
        FlashHelperInterface $flashHelper,
454
        EventDispatcherInterface $eventDispatcher,
455
        ResourceControllerEvent $event,
456
        Request $request,
457
        Response $redirectResponse
458
    ) {
459
        $metadata->getApplicationName()->willReturn('sylius');
460
        $metadata->getName()->willReturn('product');
461
462
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
463
        $configuration->hasPermission()->willReturn(true);
464
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
465
466
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
467
468
        $configuration->isHtmlRequest()->willReturn(true);
469
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
470
471
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
472
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
473
474
        $request->isMethod('POST')->willReturn(true);
475
        $form->submit($request)->willReturn($form);
476
        $form->isValid()->willReturn(true);
477
        $form->getData()->willReturn($newResource);
478
479
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
480
        $event->isStopped()->willReturn(true);
481
482
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
483
484
        $repository->add($newResource)->shouldNotBeCalled();
485
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
486
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
487
488
        $redirectHandler->redirectToIndex($configuration, $newResource)->willReturn($redirectResponse);
489
490
        $this->createAction($request)->shouldReturn($redirectResponse);
491
    }
492
493
    function it_redirects_to_newly_created_resource(
494
        MetadataInterface $metadata,
495
        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...
496
        RequestConfiguration $configuration,
497
        AuthorizationCheckerInterface $authorizationChecker,
498
        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...
499
        FactoryInterface $factory,
500
        NewResourceFactoryInterface $newResourceFactory,
501
        RepositoryInterface $repository,
502
        ResourceInterface $newResource,
503
        ResourceFormFactoryInterface $resourceFormFactory,
504
        StateMachineInterface $stateMachine,
505
        Form $form,
506
        RedirectHandlerInterface $redirectHandler,
507
        FlashHelperInterface $flashHelper,
508
        EventDispatcherInterface $eventDispatcher,
509
        ResourceControllerEvent $event,
510
        Request $request,
511
        Response $redirectResponse
512
    ) {
513
        $metadata->getApplicationName()->willReturn('sylius');
514
        $metadata->getName()->willReturn('product');
515
516
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
517
        $configuration->hasPermission()->willReturn(true);
518
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
519
        $configuration->hasStateMachine()->willReturn(true);
520
521
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
522
523
        $configuration->isHtmlRequest()->willReturn(true);
524
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
525
526
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
527
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
528
529
        $request->isMethod('POST')->willReturn(true);
530
        $form->submit($request)->willReturn($form);
531
        $form->isValid()->willReturn(true);
532
        $form->getData()->willReturn($newResource);
533
534
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
535
        $event->isStopped()->willReturn(false);
536
537
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
538
539
        $repository->add($newResource)->shouldBeCalled();
540
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldBeCalled();
541
542
        $flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled();
543
        $redirectHandler->redirectToResource($configuration, $newResource)->willReturn($redirectResponse);
544
545
        $this->createAction($request)->shouldReturn($redirectResponse);
546
    }
547
548
    function it_returns_a_non_html_response_for_correctly_created_resources(
549
        MetadataInterface $metadata,
550
        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...
551
        RequestConfiguration $configuration,
552
        AuthorizationCheckerInterface $authorizationChecker,
553
        ViewHandlerInterface $viewHandler,
554
        FactoryInterface $factory,
555
        NewResourceFactoryInterface $newResourceFactory,
556
        RepositoryInterface $repository,
557
        ResourceInterface $newResource,
558
        ResourceFormFactoryInterface $resourceFormFactory,
559
        FlashHelperInterface $flashHelper,
560
        EventDispatcherInterface $eventDispatcher,
561
        ResourceControllerEvent $event,
562
        StateMachineInterface $stateMachine,
563
        Form $form,
564
        Request $request,
565
        Response $response
566
    ) {
567
        $metadata->getApplicationName()->willReturn('sylius');
568
        $metadata->getName()->willReturn('product');
569
570
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
571
        $configuration->hasPermission()->willReturn(true);
572
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
573
        $configuration->hasStateMachine()->willReturn(true);
574
575
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
576
577
        $configuration->isHtmlRequest()->willReturn(false);
578
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
579
580
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
581
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
582
583
        $request->isMethod('POST')->willReturn(true);
584
        $form->submit($request)->willReturn($form);
585
        $form->isValid()->willReturn(true);
586
        $form->getData()->willReturn($newResource);
587
588
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
589
        $event->isStopped()->willReturn(false);
590
591
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
592
593
        $repository->add($newResource)->shouldBeCalled();
594
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldBeCalled();
595
596
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
597
598
        $expectedView = View::create($newResource, 201);
599
600
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
601
602
        $this->createAction($request)->shouldReturn($response);
603
    }
604
605
    function it_does_not_create_the_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
606
        MetadataInterface $metadata,
607
        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...
608
        RequestConfiguration $configuration,
609
        AuthorizationCheckerInterface $authorizationChecker,
610
        FactoryInterface $factory,
611
        NewResourceFactoryInterface $newResourceFactory,
612
        RepositoryInterface $repository,
613
        ResourceInterface $newResource,
614
        ResourceFormFactoryInterface $resourceFormFactory,
615
        FlashHelperInterface $flashHelper,
616
        EventDispatcherInterface $eventDispatcher,
617
        Form $form,
618
        Request $request,
619
        ResourceControllerEvent $event
620
    ) {
621
        $metadata->getApplicationName()->willReturn('sylius');
622
        $metadata->getName()->willReturn('product');
623
624
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
625
        $configuration->hasPermission()->willReturn(true);
626
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
627
628
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
629
630
        $configuration->isHtmlRequest()->willReturn(false);
631
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
632
633
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
634
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
635
636
        $request->isMethod('POST')->willReturn(true);
637
        $form->submit($request)->willReturn($form);
638
        $form->isValid()->willReturn(true);
639
        $form->getData()->willReturn($newResource);
640
641
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
642
        $event->isStopped()->willReturn(true);
643
        $event->getMessage()->willReturn('You cannot add a new product right now.');
644
        $event->getErrorCode()->willReturn(500);
645
646
        $repository->add($newResource)->shouldNotBeCalled();
647
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
648
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
649
650
        $this
651
            ->shouldThrow(new HttpException(500, 'You cannot add a new product right now.'))
652
            ->during('createAction', [$request])
653
        ;
654
    }
655
656
    function it_throws_a_403_exception_if_user_is_unauthorized_to_edit_a_single_resource(
657
        MetadataInterface $metadata,
658
        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...
659
        RequestConfiguration $configuration,
660
        Request $request,
661
        AuthorizationCheckerInterface $authorizationChecker
662
    ) {
663
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
664
        $configuration->hasPermission()->willReturn(true);
665
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
666
667
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
668
669
        $this
670
            ->shouldThrow(new AccessDeniedException())
671
            ->during('updateAction', [$request])
672
        ;
673
    }
674
675
    function it_throws_a_404_exception_if_resource_to_update_is_not_found_based_on_configuration(
676
        MetadataInterface $metadata,
677
        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...
678
        RequestConfiguration $configuration,
679
        Request $request,
680
        AuthorizationCheckerInterface $authorizationChecker,
681
        RepositoryInterface $repository,
682
        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...
683
    ) {
684
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
685
        $configuration->hasPermission()->willReturn(true);
686
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
687
688
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
689
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
690
691
        $this
692
            ->shouldThrow(new NotFoundHttpException())
693
            ->during('updateAction', [$request])
694
        ;
695
    }
696
697
    function it_returns_a_html_response_for_updating_resource_form(
698
        MetadataInterface $metadata,
699
        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...
700
        RequestConfiguration $configuration,
701
        AuthorizationCheckerInterface $authorizationChecker,
702
        ViewHandlerInterface $viewHandler,
703
        RepositoryInterface $repository,
704
        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...
705
        ResourceInterface $resource,
706
        ResourceFormFactoryInterface $resourceFormFactory,
707
        Form $form,
708
        FormView $formView,
709
        Request $request,
710
        Response $response
711
    ) {
712
        $metadata->getApplicationName()->willReturn('sylius');
713
        $metadata->getName()->willReturn('product');
714
715
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
716
        $configuration->hasPermission()->willReturn(true);
717
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
718
        $configuration->hasStateMachine()->willReturn(false);
719
720
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
721
722
        $configuration->isHtmlRequest()->willReturn(true);
723
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
724
725
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
726
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
727
728
        $request->isMethod('PATCH')->willReturn(false);
729
        $request->getMethod()->willReturn('GET');
730
731
        $form->submit($request, true)->willReturn($form);
732
        $form->createView()->willReturn($formView);
733
734
        $expectedView = View::create()
735
            ->setData([
736
                'configuration' => $configuration,
737
                'metadata' => $metadata,
738
                'resource' => $resource,
739
                'product' => $resource,
740
                'form' => $formView,
741
            ])
742
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
743
        ;
744
745
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
746
747
        $this->updateAction($request)->shouldReturn($response);
748
    }
749
750
    function it_returns_a_html_response_for_invalid_form_during_resource_update(
751
        MetadataInterface $metadata,
752
        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...
753
        RequestConfiguration $configuration,
754
        AuthorizationCheckerInterface $authorizationChecker,
755
        ViewHandlerInterface $viewHandler,
756
        RepositoryInterface $repository,
757
        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...
758
        ResourceInterface $resource,
759
        ResourceFormFactoryInterface $resourceFormFactory,
760
        Form $form,
761
        FormView $formView,
762
        Request $request,
763
        Response $response
764
    ) {
765
        $metadata->getApplicationName()->willReturn('sylius');
766
        $metadata->getName()->willReturn('product');
767
768
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
769
        $configuration->hasPermission()->willReturn(true);
770
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
771
772
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
773
774
        $configuration->isHtmlRequest()->willReturn(true);
775
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
776
777
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
778
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
779
780
        $request->isMethod('PATCH')->willReturn(false);
781
        $request->getMethod()->willReturn('PUT');
782
783
        $form->submit($request, true)->willReturn($form);
784
785
        $form->isValid()->willReturn(false);
786
        $form->createView()->willReturn($formView);
787
788
        $expectedView = View::create()
789
            ->setData([
790
                'configuration' => $configuration,
791
                'metadata' => $metadata,
792
                'resource' => $resource,
793
                'product' => $resource,
794
                'form' => $formView,
795
            ])
796
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
797
        ;
798
799
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
800
801
        $this->updateAction($request)->shouldReturn($response);
802
    }
803
804
    function it_returns_a_non_html_response_for_invalid_form_during_resource_update(
805
        MetadataInterface $metadata,
806
        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...
807
        RequestConfiguration $configuration,
808
        AuthorizationCheckerInterface $authorizationChecker,
809
        ViewHandlerInterface $viewHandler,
810
        RepositoryInterface $repository,
811
        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...
812
        ResourceInterface $resource,
813
        ResourceFormFactoryInterface $resourceFormFactory,
814
        Form $form,
815
        Request $request,
816
        Response $response
817
    ) {
818
        $metadata->getApplicationName()->willReturn('sylius');
819
        $metadata->getName()->willReturn('product');
820
821
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
822
        $configuration->hasPermission()->willReturn(true);
823
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
824
        $configuration->isHtmlRequest()->willReturn(false);
825
826
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
827
828
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
829
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
830
831
        $request->isMethod('PATCH')->willReturn(true);
832
        $request->getMethod()->willReturn('PATCH');
833
834
        $form->submit($request, false)->willReturn($form);
835
        $form->isValid()->willReturn(false);
836
837
        $expectedView = View::create($form, 400);
838
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
839
840
        $this->updateAction($request)->shouldReturn($response);
841
    }
842
843
    function it_does_not_update_the_resource_and_redirects_to_resource_for_html_request_if_stopped_via_event(
844
        MetadataInterface $metadata,
845
        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...
846
        RequestConfiguration $configuration,
847
        AuthorizationCheckerInterface $authorizationChecker,
848
        ObjectManager $manager,
849
        RepositoryInterface $repository,
850
        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...
851
        ResourceInterface $resource,
852
        ResourceFormFactoryInterface $resourceFormFactory,
853
        Form $form,
854
        EventDispatcherInterface $eventDispatcher,
855
        RedirectHandlerInterface $redirectHandler,
856
        FlashHelperInterface $flashHelper,
857
        ResourceControllerEvent $event,
858
        Request $request,
859
        Response $redirectResponse
860
    ) {
861
        $metadata->getApplicationName()->willReturn('sylius');
862
        $metadata->getName()->willReturn('product');
863
864
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
865
        $configuration->hasPermission()->willReturn(true);
866
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
867
868
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
869
870
        $configuration->isHtmlRequest()->willReturn(true);
871
872
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
873
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
874
875
        $request->isMethod('PATCH')->willReturn(false);
876
        $request->getMethod()->willReturn('PUT');
877
878
        $form->submit($request, true)->willReturn($form);
879
880
        $form->isSubmitted()->willReturn(true);
881
        $form->isValid()->willReturn(true);
882
        $form->getData()->willReturn($resource);
883
884
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
885
        $event->isStopped()->willReturn(true);
886
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
887
888
        $manager->flush()->shouldNotBeCalled();
889
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
890
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
891
892
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
893
894
        $this->updateAction($request)->shouldReturn($redirectResponse);
895
    }
896
897
    function it_redirects_to_updated_resource(
898
        MetadataInterface $metadata,
899
        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...
900
        RequestConfiguration $configuration,
901
        AuthorizationCheckerInterface $authorizationChecker,
902
        ObjectManager $manager,
903
        RepositoryInterface $repository,
904
        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...
905
        ResourceInterface $resource,
906
        ResourceFormFactoryInterface $resourceFormFactory,
907
        Form $form,
908
        EventDispatcherInterface $eventDispatcher,
909
        RedirectHandlerInterface $redirectHandler,
910
        FlashHelperInterface $flashHelper,
911
        ResourceControllerEvent $event,
912
        Request $request,
913
        Response $redirectResponse
914
    ) {
915
        $metadata->getApplicationName()->willReturn('sylius');
916
        $metadata->getName()->willReturn('product');
917
918
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
919
        $configuration->hasPermission()->willReturn(true);
920
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
921
        $configuration->hasStateMachine()->willReturn(false);
922
923
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
924
925
        $configuration->isHtmlRequest()->willReturn(true);
926
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
927
928
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
929
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
930
931
        $request->isMethod('PATCH')->willReturn(false);
932
        $request->getMethod()->willReturn('PUT');
933
934
        $form->submit($request, true)->willReturn($form);
935
936
        $form->isSubmitted()->willReturn(true);
937
        $form->isValid()->willReturn(true);
938
        $form->getData()->willReturn($resource);
939
940
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
941
        $event->isStopped()->willReturn(false);
942
943
        $manager->flush()->shouldBeCalled();
944
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
945
946
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
947
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
948
949
        $this->updateAction($request)->shouldReturn($redirectResponse);
950
    }
951
952
    function it_returns_a_non_html_response_for_correctly_updated_resource(
953
        MetadataInterface $metadata,
954
        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...
955
        RequestConfiguration $configuration,
956
        AuthorizationCheckerInterface $authorizationChecker,
957
        ViewHandlerInterface $viewHandler,
958
        ObjectManager $manager,
959
        RepositoryInterface $repository,
960
        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...
961
        ResourceInterface $resource,
962
        ResourceFormFactoryInterface $resourceFormFactory,
963
        EventDispatcherInterface $eventDispatcher,
964
        ResourceControllerEvent $event,
965
        Form $form,
966
        Request $request,
967
        Response $response
968
    ) {
969
        $metadata->getApplicationName()->willReturn('sylius');
970
        $metadata->getName()->willReturn('product');
971
972
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
973
        $configuration->hasPermission()->willReturn(true);
974
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
975
        $configuration->isHtmlRequest()->willReturn(false);
976
        $configuration->hasStateMachine()->willReturn(false);
977
978
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
979
980
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
981
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
982
983
        $request->isMethod('PATCH')->willReturn(false);
984
        $request->getMethod()->willReturn('PUT');
985
986
        $form->submit($request, true)->willReturn($form);
987
        $form->isValid()->willReturn(true);
988
        $form->getData()->willReturn($resource);
989
990
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
991
        $event->isStopped()->willReturn(false);
992
993
        $manager->flush()->shouldBeCalled();
994
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
995
996
        $expectedView = View::create(null, 204);
997
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
998
999
        $this->updateAction($request)->shouldReturn($response);
1000
    }
1001
1002
    function it_does_not_update_the_resource_throws_a_http_exception_for_non_html_requests_stopped_via_event(
1003
        MetadataInterface $metadata,
1004
        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...
1005
        RequestConfiguration $configuration,
1006
        AuthorizationCheckerInterface $authorizationChecker,
1007
        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...
1008
        ObjectManager $manager,
1009
        RepositoryInterface $repository,
1010
        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...
1011
        ResourceInterface $resource,
1012
        ResourceFormFactoryInterface $resourceFormFactory,
1013
        EventDispatcherInterface $eventDispatcher,
1014
        ResourceControllerEvent $event,
1015
        Form $form,
1016
        Request $request
1017
    ) {
1018
        $metadata->getApplicationName()->willReturn('sylius');
1019
        $metadata->getName()->willReturn('product');
1020
1021
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1022
        $configuration->hasPermission()->willReturn(true);
1023
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1024
        $configuration->isHtmlRequest()->willReturn(false);
1025
1026
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1027
1028
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1029
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1030
1031
        $request->isMethod('PATCH')->willReturn(false);
1032
        $request->getMethod()->willReturn('PUT');
1033
1034
        $form->submit($request, true)->willReturn($form);
1035
        $form->isValid()->willReturn(true);
1036
        $form->getData()->willReturn($resource);
1037
1038
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1039
        $event->isStopped()->willReturn(true);
1040
        $event->getMessage()->willReturn('Cannot update this channel.');
1041
        $event->getErrorCode()->willReturn(500);
1042
1043
        $manager->flush()->shouldNotBeCalled();
1044
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1045
1046
        $this
1047
            ->shouldThrow(new HttpException(500, 'Cannot update this channel.'))
1048
            ->during('updateAction', [$request])
1049
        ;
1050
    }
1051
1052
    function it_applies_state_machine_transition_to_updated_resource_if_configured(
1053
        MetadataInterface $metadata,
1054
        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...
1055
        RequestConfiguration $configuration,
1056
        AuthorizationCheckerInterface $authorizationChecker,
1057
        ObjectManager $manager,
1058
        RepositoryInterface $repository,
1059
        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...
1060
        StateMachineInterface $stateMachine,
1061
        ResourceInterface $resource,
1062
        ResourceFormFactoryInterface $resourceFormFactory,
1063
        Form $form,
1064
        EventDispatcherInterface $eventDispatcher,
1065
        RedirectHandlerInterface $redirectHandler,
1066
        FlashHelperInterface $flashHelper,
1067
        ResourceControllerEvent $event,
1068
        Request $request,
1069
        Response $redirectResponse
1070
    ) {
1071
        $metadata->getApplicationName()->willReturn('sylius');
1072
        $metadata->getName()->willReturn('product');
1073
1074
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1075
        $configuration->hasPermission()->willReturn(true);
1076
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1077
        $configuration->hasStateMachine()->willReturn(true);
1078
1079
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1080
1081
        $configuration->isHtmlRequest()->willReturn(true);
1082
        $configuration->getTemplate(ResourceActions::UPDATE)->willReturn('SyliusShopBundle:Product:update.html.twig');
1083
1084
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1085
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1086
1087
        $request->isMethod('PATCH')->willReturn(false);
1088
        $request->getMethod()->willReturn('PUT');
1089
1090
        $form->submit($request, true)->willReturn($form);
1091
1092
        $form->isSubmitted()->willReturn(true);
1093
        $form->isValid()->willReturn(true);
1094
        $form->getData()->willReturn($resource);
1095
1096
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1097
        $event->isStopped()->willReturn(false);
1098
1099
        $manager->flush()->shouldBeCalled();
1100
        $stateMachine->apply($configuration, $resource)->shouldBeCalled();
1101
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1102
1103
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1104
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1105
1106
        $this->updateAction($request)->shouldReturn($redirectResponse);
1107
    }
1108
1109
    function it_throws_a_403_exception_if_user_is_unauthorized_to_delete_a_single_resource(
1110
        MetadataInterface $metadata,
1111
        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...
1112
        RequestConfiguration $configuration,
1113
        Request $request,
1114
        AuthorizationCheckerInterface $authorizationChecker
1115
    ) {
1116
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1117
        $configuration->hasPermission()->willReturn(true);
1118
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1119
1120
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(false);
1121
1122
        $this
1123
            ->shouldThrow(new AccessDeniedException())
1124
            ->during('deleteAction', [$request])
1125
        ;
1126
    }
1127
1128
    function it_throws_a_404_exception_if_resource_for_deletion_is_not_found_based_on_configuration(
1129
        MetadataInterface $metadata,
1130
        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...
1131
        RequestConfiguration $configuration,
1132
        Request $request,
1133
        AuthorizationCheckerInterface $authorizationChecker,
1134
        RepositoryInterface $repository,
1135
        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...
1136
    ) {
1137
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1138
        $configuration->hasPermission()->willReturn(true);
1139
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1140
1141
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1142
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
1143
1144
        $this
1145
            ->shouldThrow(new NotFoundHttpException())
1146
            ->during('deleteAction', [$request])
1147
        ;
1148
    }
1149
1150
    function it_deletes_a_resource_and_redirects_to_index_by_for_html_request(
1151
        MetadataInterface $metadata,
1152
        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...
1153
        RequestConfiguration $configuration,
1154
        AuthorizationCheckerInterface $authorizationChecker,
1155
        RepositoryInterface $repository,
1156
        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...
1157
        ResourceInterface $resource,
1158
        RedirectHandlerInterface $redirectHandler,
1159
        FlashHelperInterface $flashHelper,
1160
        EventDispatcherInterface $eventDispatcher,
1161
        ResourceControllerEvent $event,
1162
        Request $request,
1163
        Response $redirectResponse
1164
    ) {
1165
        $metadata->getApplicationName()->willReturn('sylius');
1166
        $metadata->getName()->willReturn('product');
1167
1168
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1169
        $configuration->hasPermission()->willReturn(true);
1170
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1171
1172
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1173
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1174
1175
        $configuration->isHtmlRequest()->willReturn(true);
1176
1177
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1178
        $event->isStopped()->willReturn(false);
1179
1180
        $repository->remove($resource)->shouldBeCalled();
1181
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldBeCalled();
1182
1183
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled();
1184
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1185
1186
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1187
    }
1188
1189
    function it_does_not_delete_a_resource_and_redirects_to_index_for_html_requests_stopped_via_event(
1190
        MetadataInterface $metadata,
1191
        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...
1192
        RequestConfiguration $configuration,
1193
        AuthorizationCheckerInterface $authorizationChecker,
1194
        RepositoryInterface $repository,
1195
        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...
1196
        ResourceInterface $resource,
1197
        RedirectHandlerInterface $redirectHandler,
1198
        FlashHelperInterface $flashHelper,
1199
        EventDispatcherInterface $eventDispatcher,
1200
        ResourceControllerEvent $event,
1201
        Request $request,
1202
        Response $redirectResponse
1203
    ) {
1204
        $metadata->getApplicationName()->willReturn('sylius');
1205
        $metadata->getName()->willReturn('product');
1206
1207
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1208
        $configuration->hasPermission()->willReturn(true);
1209
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1210
1211
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1212
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1213
1214
        $configuration->isHtmlRequest()->willReturn(true);
1215
1216
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1217
        $event->isStopped()->willReturn(true);
1218
1219
        $repository->remove($resource)->shouldNotBeCalled();
1220
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1221
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled();
1222
1223
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1224
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1225
1226
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1227
    }
1228
1229
    function it_deletes_a_resource_and_returns_204_for_non_html_requests(
1230
        MetadataInterface $metadata,
1231
        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...
1232
        RequestConfiguration $configuration,
1233
        AuthorizationCheckerInterface $authorizationChecker,
1234
        ViewHandlerInterface $viewHandler,
1235
        RepositoryInterface $repository,
1236
        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...
1237
        ResourceInterface $resource,
1238
        EventDispatcherInterface $eventDispatcher,
1239
        ResourceControllerEvent $event,
1240
        Request $request,
1241
        Response $response
1242
    ) {
1243
        $metadata->getApplicationName()->willReturn('sylius');
1244
        $metadata->getName()->willReturn('product');
1245
1246
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1247
        $configuration->hasPermission()->willReturn(true);
1248
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1249
1250
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1251
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1252
1253
        $configuration->isHtmlRequest()->willReturn(false);
1254
1255
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1256
        $event->isStopped()->willReturn(false);
1257
1258
        $repository->remove($resource)->shouldBeCalled();
1259
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldBeCalled();
1260
1261
        $expectedView = View::create(null, 204);
1262
1263
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1264
1265
        $this->deleteAction($request)->shouldReturn($response);
1266
    }
1267
1268
    function it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
1269
        MetadataInterface $metadata,
1270
        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...
1271
        RequestConfiguration $configuration,
1272
        AuthorizationCheckerInterface $authorizationChecker,
1273
        RepositoryInterface $repository,
1274
        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...
1275
        ResourceInterface $resource,
1276
        FlashHelperInterface $flashHelper,
1277
        EventDispatcherInterface $eventDispatcher,
1278
        ResourceControllerEvent $event,
1279
        Request $request
1280
    ) {
1281
        $metadata->getApplicationName()->willReturn('sylius');
1282
        $metadata->getName()->willReturn('product');
1283
1284
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1285
        $configuration->hasPermission()->willReturn(true);
1286
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1287
1288
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1289
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1290
1291
        $configuration->isHtmlRequest()->willReturn(false);
1292
1293
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1294
        $event->isStopped()->willReturn(true);
1295
        $event->getMessage()->willReturn('Cannot delete this product.');
1296
        $event->getErrorCode()->willReturn(500);
1297
1298
        $repository->remove($resource)->shouldNotBeCalled();
1299
1300
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1301
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1302
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1303
1304
        $this
1305
            ->shouldThrow(new HttpException(500, 'Cannot delete this product.'))
1306
            ->during('deleteAction', [$request])
1307
        ;
1308
    }
1309
1310
    function it_throws_a_403_exception_if_user_is_unauthorized_to_apply_state_machine_transition_on_resource(
1311
        MetadataInterface $metadata,
1312
        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...
1313
        RequestConfiguration $configuration,
1314
        Request $request,
1315
        AuthorizationCheckerInterface $authorizationChecker
1316
    ) {
1317
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1318
        $configuration->hasPermission()->willReturn(true);
1319
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1320
1321
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
1322
1323
        $this
1324
            ->shouldThrow(new AccessDeniedException())
1325
            ->during('applyStateMachineTransitionAction', [$request])
1326
        ;
1327
    }
1328
1329
    function it_throws_a_404_exception_if_resource_is_not_found_when_trying_to_apply_state_machine_transition(
1330
        MetadataInterface $metadata,
1331
        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...
1332
        RequestConfiguration $configuration,
1333
        Request $request,
1334
        AuthorizationCheckerInterface $authorizationChecker,
1335
        RepositoryInterface $repository,
1336
        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...
1337
    ) {
1338
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1339
        $configuration->hasPermission()->willReturn(true);
1340
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1341
1342
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1343
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
1344
1345
        $this
1346
            ->shouldThrow(new NotFoundHttpException())
1347
            ->during('applyStateMachineTransitionAction', [$request])
1348
        ;
1349
    }
1350
1351
    function it_does_not_apply_state_machine_transition_on_resource_if_not_applicable_and_returns_400_bad_request(
1352
        MetadataInterface $metadata,
1353
        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...
1354
        RequestConfiguration $configuration,
1355
        AuthorizationCheckerInterface $authorizationChecker,
1356
        RepositoryInterface $repository,
1357
        ObjectManager $objectManager,
1358
        StateMachineInterface $stateMachine,
1359
        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...
1360
        ResourceInterface $resource,
1361
        FlashHelperInterface $flashHelper,
1362
        EventDispatcherInterface $eventDispatcher,
1363
        ResourceControllerEvent $event,
1364
        Request $request
1365
    ) {
1366
        $metadata->getApplicationName()->willReturn('sylius');
1367
        $metadata->getName()->willReturn('product');
1368
1369
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1370
        $configuration->hasPermission()->willReturn(true);
1371
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1372
1373
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1374
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1375
1376
        $configuration->isHtmlRequest()->willReturn(true);
1377
1378
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1379
        $event->isStopped()->willReturn(false);
1380
1381
        $stateMachine->can($configuration, $resource)->willReturn(false);
1382
1383
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
1384
        $objectManager->flush()->shouldNotBeCalled();
1385
1386
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1387
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1388
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1389
1390
        $this
1391
            ->shouldThrow(new BadRequestHttpException())
1392
            ->during('applyStateMachineTransitionAction', [$request])
1393
        ;
1394
    }
1395
1396
    function it_applies_state_machine_transition_to_resource_and_redirects_for_html_request(
1397
        MetadataInterface $metadata,
1398
        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...
1399
        RequestConfiguration $configuration,
1400
        AuthorizationCheckerInterface $authorizationChecker,
1401
        StateMachineInterface $stateMachine,
1402
        RepositoryInterface $repository,
1403
        ObjectManager $manager,
1404
        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...
1405
        ResourceInterface $resource,
1406
        RedirectHandlerInterface $redirectHandler,
1407
        FlashHelperInterface $flashHelper,
1408
        EventDispatcherInterface $eventDispatcher,
1409
        ResourceControllerEvent $event,
1410
        Request $request,
1411
        Response $redirectResponse
1412
    ) {
1413
        $metadata->getApplicationName()->willReturn('sylius');
1414
        $metadata->getName()->willReturn('product');
1415
1416
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1417
        $configuration->hasPermission()->willReturn(true);
1418
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1419
1420
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1421
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1422
1423
        $configuration->isHtmlRequest()->willReturn(true);
1424
1425
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1426
        $event->isStopped()->willReturn(false);
1427
1428
        $stateMachine->can($configuration, $resource)->willReturn(true);
1429
        $stateMachine->apply($configuration, $resource)->shouldBeCalled();
1430
        $manager->flush()->shouldBeCalled();
1431
1432
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1433
1434
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1435
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1436
1437
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1438
    }
1439
1440
    function it_does_not_apply_state_machine_transition_on_resource_and_redirects_for_html_requests_stopped_via_event(
1441
        MetadataInterface $metadata,
1442
        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...
1443
        RequestConfiguration $configuration,
1444
        AuthorizationCheckerInterface $authorizationChecker,
1445
        StateMachineInterface $stateMachine,
1446
        ObjectManager $manager,
1447
        RepositoryInterface $repository,
1448
        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...
1449
        ResourceInterface $resource,
1450
        RedirectHandlerInterface $redirectHandler,
1451
        FlashHelperInterface $flashHelper,
1452
        EventDispatcherInterface $eventDispatcher,
1453
        ResourceControllerEvent $event,
1454
        Request $request,
1455
        Response $redirectResponse
1456
    ) {
1457
        $metadata->getApplicationName()->willReturn('sylius');
1458
        $metadata->getName()->willReturn('product');
1459
1460
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1461
        $configuration->hasPermission()->willReturn(true);
1462
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1463
1464
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1465
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1466
1467
        $configuration->isHtmlRequest()->willReturn(true);
1468
1469
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1470
        $event->isStopped()->willReturn(true);
1471
1472
        $manager->flush()->shouldNotBeCalled();
1473
        $stateMachine->apply($resource)->shouldNotBeCalled();
1474
1475
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled();
1476
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled();
1477
1478
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1479
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1480
1481
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1482
    }
1483
1484
    function it_applies_state_machine_transition_on_resource_and_returns_200_for_non_html_requests(
1485
        MetadataInterface $metadata,
1486
        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...
1487
        RequestConfiguration $configuration,
1488
        AuthorizationCheckerInterface $authorizationChecker,
1489
        ViewHandlerInterface $viewHandler,
1490
        StateMachineInterface $stateMachine,
1491
        ObjectManager $manager,
1492
        RepositoryInterface $repository,
1493
        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...
1494
        ResourceInterface $resource,
1495
        EventDispatcherInterface $eventDispatcher,
1496
        ResourceControllerEvent $event,
1497
        Request $request,
1498
        Response $response
1499
    ) {
1500
        $metadata->getApplicationName()->willReturn('sylius');
1501
        $metadata->getName()->willReturn('product');
1502
1503
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1504
        $configuration->hasPermission()->willReturn(true);
1505
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1506
1507
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1508
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1509
1510
        $configuration->isHtmlRequest()->willReturn(false);
1511
1512
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1513
        $event->isStopped()->willReturn(false);
1514
1515
        $stateMachine->can($configuration, $resource)->willReturn(true);
1516
        $stateMachine->apply($configuration, $resource)->shouldBeCalled();
1517
        $manager->flush()->shouldBeCalled();
1518
1519
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1520
1521
        $expectedView = View::create($resource, 200);
1522
1523
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1524
1525
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
1526
    }
1527
1528
    function it_does_not_apply_state_machine_transition_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
1529
        MetadataInterface $metadata,
1530
        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...
1531
        RequestConfiguration $configuration,
1532
        AuthorizationCheckerInterface $authorizationChecker,
1533
        RepositoryInterface $repository,
1534
        ObjectManager $objectManager,
1535
        StateMachineInterface $stateMachine,
1536
        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...
1537
        ResourceInterface $resource,
1538
        FlashHelperInterface $flashHelper,
1539
        EventDispatcherInterface $eventDispatcher,
1540
        ResourceControllerEvent $event,
1541
        Request $request
1542
    ) {
1543
        $metadata->getApplicationName()->willReturn('sylius');
1544
        $metadata->getName()->willReturn('product');
1545
1546
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
1547
        $configuration->hasPermission()->willReturn(true);
1548
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1549
1550
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1551
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
1552
1553
        $configuration->isHtmlRequest()->willReturn(false);
1554
1555
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1556
        $event->isStopped()->willReturn(true);
1557
        $event->getMessage()->willReturn('Cannot approve this product.');
1558
        $event->getErrorCode()->willReturn(500);
1559
1560
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
1561
        $objectManager->flush()->shouldNotBeCalled();
1562
1563
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
1564
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
1565
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
1566
1567
        $this
1568
            ->shouldThrow(new HttpException(500, 'Cannot approve this product.'))
1569
            ->during('applyStateMachineTransitionAction', [$request])
1570
        ;
1571
    }
1572
1573
    /**
1574
     * {@inheritdoc}
1575
     */
1576
    private function getViewComparingCallback(View $expectedView)
1577
    {
1578
        return function ($value) use ($expectedView) {
1579
            if (!$value instanceof View) {
1580
                return false;
1581
            }
1582
1583
            // Need to unwrap phpspec's Collaborators to ensure proper comparison.
1584
            $this->unwrapViewData($expectedView);
1585
            $this->nullifyDates($value);
1586
            $this->nullifyDates($expectedView);
1587
1588
            return
1589
                $expectedView->getStatusCode() === $value->getStatusCode() &&
1590
                $expectedView->getHeaders() === $value->getHeaders() &&
1591
                $expectedView->getEngine() === $value->getEngine() &&
1592
                $expectedView->getFormat() === $value->getFormat() &&
1593
                $expectedView->getData() === $value->getData() &&
1594
                $expectedView->getTemplate() === $value->getTemplate() &&
1595
                $expectedView->getTemplateVar() === $value->getTemplateVar()
1596
            ;
1597
        };
1598
    }
1599
1600
    /**
1601
     * @param View $view
1602
     */
1603
    private function unwrapViewData(View $view)
1604
    {
1605
        $view->setData($this->unwrapIfCollaborator($view->getData()));
1606
    }
1607
1608
    /**
1609
     * @param mixed $value
1610
     *
1611
     * @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...
1612
     */
1613
    private function unwrapIfCollaborator($value)
1614
    {
1615
        if (null === $value) {
1616
            return null;
1617
        }
1618
1619
        if ($value instanceof Collaborator) {
1620
            return $value->getWrappedObject();
1621
        }
1622
1623
        if (is_array($value)) {
1624
            foreach ($value as $key => $childValue) {
1625
                $value[$key] = $this->unwrapIfCollaborator($childValue);
1626
            }
1627
        }
1628
1629
        return $value;
1630
    }
1631
1632
    /**
1633
     * @param View $view
1634
     */
1635
    private function nullifyDates(View $view)
1636
    {
1637
        $headers = $view->getHeaders();
1638
        unset($headers['date']);
1639
        $view->setHeaders($headers);
1640
    }
1641
}
1642