Completed
Push — 1.0-phpstan ( ddfe6c )
by Kamil
23:36
created

ResourceControllerSpec::unwrapViewData()   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 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\ResourceBundle\Controller;
15
16
use Doctrine\Common\Persistence\ObjectManager;
17
use FOS\RestBundle\View\View;
18
use PhpSpec\ObjectBehavior;
19
use PhpSpec\Wrapper\Collaborator;
20
use Prophecy\Argument;
21
use Sylius\Bundle\ResourceBundle\Controller\AuthorizationCheckerInterface;
22
use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
23
use Sylius\Bundle\ResourceBundle\Controller\FlashHelperInterface;
24
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
25
use Sylius\Bundle\ResourceBundle\Controller\RedirectHandlerInterface;
26
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
27
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
28
use Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandlerInterface;
29
use Sylius\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface;
30
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
31
use Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandlerInterface;
32
use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface;
33
use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
34
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
35
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
36
use Sylius\Component\Resource\Exception\DeleteHandlingException;
37
use Sylius\Component\Resource\Factory\FactoryInterface;
38
use Sylius\Component\Resource\Metadata\MetadataInterface;
39
use Sylius\Component\Resource\Model\ResourceInterface;
40
use Sylius\Component\Resource\Repository\RepositoryInterface;
41
use Sylius\Component\Resource\ResourceActions;
42
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
43
use Symfony\Component\DependencyInjection\ContainerInterface;
44
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
45
use Symfony\Component\Form\Form;
46
use Symfony\Component\Form\FormView;
47
use Symfony\Component\HttpFoundation\ParameterBag;
48
use Symfony\Component\HttpFoundation\Request;
49
use Symfony\Component\HttpFoundation\Response;
50
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
51
use Symfony\Component\HttpKernel\Exception\HttpException;
52
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
53
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
54
use Symfony\Component\Security\Csrf\CsrfToken;
55
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
56
57
final class ResourceControllerSpec extends ObjectBehavior
58
{
59
    function let(
60
        MetadataInterface $metadata,
61
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
62
        ViewHandlerInterface $viewHandler,
63
        RepositoryInterface $repository,
64
        FactoryInterface $factory,
65
        NewResourceFactoryInterface $newResourceFactory,
66
        ObjectManager $manager,
67
        SingleResourceProviderInterface $singleResourceProvider,
68
        ResourcesCollectionProviderInterface $resourcesCollectionProvider,
69
        ResourceFormFactoryInterface $resourceFormFactory,
70
        RedirectHandlerInterface $redirectHandler,
71
        FlashHelperInterface $flashHelper,
72
        AuthorizationCheckerInterface $authorizationChecker,
73
        EventDispatcherInterface $eventDispatcher,
74
        StateMachineInterface $stateMachine,
75
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
76
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
77
        ContainerInterface $container
78
    ): void {
79
        $this->beConstructedWith(
80
            $metadata,
81
            $requestConfigurationFactory,
82
            $viewHandler,
83
            $repository,
84
            $factory,
85
            $newResourceFactory,
86
            $manager,
87
            $singleResourceProvider,
88
            $resourcesCollectionProvider,
89
            $resourceFormFactory,
90
            $redirectHandler,
91
            $flashHelper,
92
            $authorizationChecker,
93
            $eventDispatcher,
94
            $stateMachine,
95
            $resourceUpdateHandler,
96
            $resourceDeleteHandler
97
        );
98
99
        $this->setContainer($container);
100
    }
101
102
    function it_extends_base_Symfony_controller(): void
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...
103
    {
104
        $this->shouldHaveType(Controller::class);
105
    }
106
107
    function it_throws_a_403_exception_if_user_is_unauthorized_to_view_a_single_resource(
108
        MetadataInterface $metadata,
109
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
110
        RequestConfiguration $configuration,
111
        Request $request,
112
        AuthorizationCheckerInterface $authorizationChecker
113
    ): void {
114
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
        $configuration->hasPermission()->willReturn(true);
116
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
117
118
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(false);
119
120
        $this
121
            ->shouldThrow(new AccessDeniedException())
122
            ->during('showAction', [$request])
123
        ;
124
    }
125
126
    function it_throws_a_404_exception_if_resource_is_not_found_based_on_configuration(
127
        MetadataInterface $metadata,
128
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
129
        RequestConfiguration $configuration,
130
        Request $request,
131
        AuthorizationCheckerInterface $authorizationChecker,
132
        RepositoryInterface $repository,
133
        SingleResourceProviderInterface $singleResourceProvider
134
    ): void {
135
        $metadata->getHumanizedName()->willReturn('product');
136
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        $configuration->hasPermission()->willReturn(true);
138
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
139
140
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
141
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
142
143
        $this
144
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
145
            ->during('showAction', [$request])
146
        ;
147
    }
148
149
    function it_returns_a_response_for_html_view_of_a_single_resource(
150
        MetadataInterface $metadata,
151
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
152
        RequestConfiguration $configuration,
153
        AuthorizationCheckerInterface $authorizationChecker,
154
        RepositoryInterface $repository,
155
        SingleResourceProviderInterface $singleResourceProvider,
156
        ResourceInterface $resource,
157
        ViewHandlerInterface $viewHandler,
158
        EventDispatcherInterface $eventDispatcher,
159
        Request $request,
160
        Response $response
161
    ): void {
162
        $metadata->getApplicationName()->willReturn('sylius');
163
        $metadata->getName()->willReturn('product');
164
165
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
        $configuration->hasPermission()->willReturn(true);
167
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
168
169
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
170
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
172
        $configuration->isHtmlRequest()->willReturn(true);
173
        $configuration->getTemplate(ResourceActions::SHOW . '.html')->willReturn('SyliusShopBundle:Product:show.html.twig');
174
175
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
176
177
        $expectedView = View::create()
178
            ->setData([
179
                'configuration' => $configuration,
180
                'metadata' => $metadata,
181
                'resource' => $resource,
182
                'product' => $resource,
183
            ])
184
            ->setTemplateVar('product')
185
            ->setTemplate('SyliusShopBundle:Product:show.html.twig')
186
        ;
187
188
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
189
190
        $this->showAction($request)->shouldReturn($response);
191
    }
192
193
    function it_returns_a_response_for_non_html_view_of_single_resource(
194
        MetadataInterface $metadata,
195
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
196
        RequestConfiguration $configuration,
197
        AuthorizationCheckerInterface $authorizationChecker,
198
        RepositoryInterface $repository,
199
        SingleResourceProviderInterface $singleResourceProvider,
200
        ResourceInterface $resource,
201
        ViewHandlerInterface $viewHandler,
202
        EventDispatcherInterface $eventDispatcher,
203
        Request $request,
204
        Response $response
205
    ): void {
206
        $metadata->getApplicationName()->willReturn('sylius');
207
        $metadata->getName()->willReturn('product');
208
209
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
210
        $configuration->hasPermission()->willReturn(true);
211
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
212
213
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
214
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
216
        $configuration->isHtmlRequest()->willReturn(false);
217
218
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
219
220
        $expectedView = View::create($resource);
221
222
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
223
224
        $this->showAction($request)->shouldReturn($response);
225
    }
226
227
    function it_throws_a_403_exception_if_user_is_unauthorized_to_view_an_index_of_resources(
228
        MetadataInterface $metadata,
229
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
230
        RequestConfiguration $configuration,
231
        Request $request,
232
        AuthorizationCheckerInterface $authorizationChecker
233
    ): void {
234
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
235
        $configuration->hasPermission()->willReturn(true);
236
        $configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index');
237
238
        $authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(false);
239
240
        $this
241
            ->shouldThrow(new AccessDeniedException())
242
            ->during('indexAction', [$request])
243
        ;
244
    }
245
246
    function it_returns_a_response_for_html_view_of_paginated_resources(
247
        MetadataInterface $metadata,
248
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
249
        RequestConfiguration $configuration,
250
        AuthorizationCheckerInterface $authorizationChecker,
251
        RepositoryInterface $repository,
252
        ResourcesCollectionProviderInterface $resourcesCollectionProvider,
253
        EventDispatcherInterface $eventDispatcher,
254
        ResourceInterface $resource1,
255
        ResourceInterface $resource2,
256
        ViewHandlerInterface $viewHandler,
257
        Request $request,
258
        Response $response
259
    ): void {
260
        $metadata->getApplicationName()->willReturn('sylius');
261
        $metadata->getName()->willReturn('product');
262
        $metadata->getPluralName()->willReturn('products');
263
264
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
265
        $configuration->hasPermission()->willReturn(true);
266
        $configuration->getPermission(ResourceActions::INDEX)->willReturn('sylius.product.index');
267
268
        $authorizationChecker->isGranted($configuration, 'sylius.product.index')->willReturn(true);
269
270
        $configuration->isHtmlRequest()->willReturn(true);
271
        $configuration->getTemplate(ResourceActions::INDEX . '.html')->willReturn('SyliusShopBundle:Product:index.html.twig');
272
        $resourcesCollectionProvider->get($configuration, $repository)->willReturn([$resource1, $resource2]);
273
274
        $eventDispatcher->dispatchMultiple(ResourceActions::INDEX, $configuration, [$resource1, $resource2])->shouldBeCalled();
275
276
        $expectedView = View::create()
277
            ->setData([
278
                'configuration' => $configuration,
279
                'metadata' => $metadata,
280
                'resources' => [$resource1, $resource2],
281
                'products' => [$resource1, $resource2],
282
            ])
283
            ->setTemplateVar('products')
284
            ->setTemplate('SyliusShopBundle:Product:index.html.twig')
285
        ;
286
287
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
288
289
        $this->indexAction($request)->shouldReturn($response);
290
    }
291
292
    function it_throws_a_403_exception_if_user_is_unauthorized_to_create_a_new_resource(
293
        MetadataInterface $metadata,
294
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
295
        RequestConfiguration $configuration,
296
        Request $request,
297
        AuthorizationCheckerInterface $authorizationChecker
298
    ): void {
299
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
300
        $configuration->hasPermission()->willReturn(true);
301
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
302
303
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(false);
304
305
        $this
306
            ->shouldThrow(new AccessDeniedException())
307
            ->during('createAction', [$request])
308
        ;
309
    }
310
311
    function it_returns_a_html_response_for_creating_new_resource_form(
312
        MetadataInterface $metadata,
313
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
314
        RequestConfiguration $configuration,
315
        AuthorizationCheckerInterface $authorizationChecker,
316
        ViewHandlerInterface $viewHandler,
317
        FactoryInterface $factory,
318
        NewResourceFactoryInterface $newResourceFactory,
319
        ResourceInterface $newResource,
320
        ResourceFormFactoryInterface $resourceFormFactory,
321
        EventDispatcherInterface $eventDispatcher,
322
        ResourceControllerEvent $event,
323
        Form $form,
324
        FormView $formView,
325
        Request $request,
326
        Response $response
327
    ): void {
328
        $metadata->getApplicationName()->willReturn('sylius');
329
        $metadata->getName()->willReturn('product');
330
331
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
332
        $configuration->hasPermission()->willReturn(true);
333
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
334
335
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
336
337
        $configuration->isHtmlRequest()->willReturn(true);
338
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
339
340
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
341
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
342
343
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
344
345
        $request->isMethod('POST')->willReturn(false);
346
        $form->createView()->willReturn($formView);
347
348
        $expectedView = View::create()
349
            ->setData([
350
                'configuration' => $configuration,
351
                'metadata' => $metadata,
352
                'resource' => $newResource,
353
                'product' => $newResource,
354
                'form' => $formView,
355
            ])
356
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
357
        ;
358
359
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
360
361
        $this->createAction($request)->shouldReturn($response);
362
    }
363
364
    function it_returns_a_html_response_for_invalid_form_during_resource_creation(
365
        MetadataInterface $metadata,
366
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
367
        RequestConfiguration $configuration,
368
        AuthorizationCheckerInterface $authorizationChecker,
369
        ViewHandlerInterface $viewHandler,
370
        FactoryInterface $factory,
371
        NewResourceFactoryInterface $newResourceFactory,
372
        ResourceInterface $newResource,
373
        ResourceFormFactoryInterface $resourceFormFactory,
374
        EventDispatcherInterface $eventDispatcher,
375
        ResourceControllerEvent $event,
376
        Form $form,
377
        FormView $formView,
378
        Request $request,
379
        Response $response
380
    ): void {
381
        $metadata->getApplicationName()->willReturn('sylius');
382
        $metadata->getName()->willReturn('product');
383
384
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
385
        $configuration->hasPermission()->willReturn(true);
386
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
387
388
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
389
390
        $configuration->isHtmlRequest()->willReturn(true);
391
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
392
393
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
394
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
395
396
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
397
398
        $request->isMethod('POST')->willReturn(true);
399
        $form->handleRequest($request)->willReturn($form);
400
        $form->isValid()->willReturn(false);
401
        $form->createView()->willReturn($formView);
402
403
        $expectedView = View::create()
404
            ->setData([
405
                'configuration' => $configuration,
406
                'metadata' => $metadata,
407
                'resource' => $newResource,
408
                'product' => $newResource,
409
                'form' => $formView,
410
            ])
411
            ->setTemplate('SyliusShopBundle:Product:create.html.twig')
412
        ;
413
414
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
415
416
        $this->createAction($request)->shouldReturn($response);
417
    }
418
419
    function it_returns_a_non_html_response_for_invalid_form_during_resource_creation(
420
        MetadataInterface $metadata,
421
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
422
        RequestConfiguration $configuration,
423
        AuthorizationCheckerInterface $authorizationChecker,
424
        ViewHandlerInterface $viewHandler,
425
        FactoryInterface $factory,
426
        NewResourceFactoryInterface $newResourceFactory,
427
        ResourceInterface $newResource,
428
        ResourceFormFactoryInterface $resourceFormFactory,
429
        Form $form,
430
        Request $request,
431
        Response $response
432
    ): void {
433
        $metadata->getApplicationName()->willReturn('sylius');
434
        $metadata->getName()->willReturn('product');
435
436
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
437
        $configuration->hasPermission()->willReturn(true);
438
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
439
440
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
441
442
        $configuration->isHtmlRequest()->willReturn(false);
443
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
444
445
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
446
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
447
448
        $request->isMethod('POST')->willReturn(true);
449
        $form->handleRequest($request)->willReturn($form);
450
        $form->isValid()->willReturn(false);
451
452
        $expectedView = View::create($form, 400);
453
454
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
455
456
        $this->createAction($request)->shouldReturn($response);
457
    }
458
459
    function it_does_not_create_the_resource_and_redirects_to_index_for_html_requests_stopped_via_events(
460
        MetadataInterface $metadata,
461
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
462
        RequestConfiguration $configuration,
463
        AuthorizationCheckerInterface $authorizationChecker,
464
        ViewHandlerInterface $viewHandler,
465
        FactoryInterface $factory,
466
        NewResourceFactoryInterface $newResourceFactory,
467
        RepositoryInterface $repository,
468
        ResourceInterface $newResource,
469
        ResourceFormFactoryInterface $resourceFormFactory,
470
        Form $form,
471
        RedirectHandlerInterface $redirectHandler,
472
        FlashHelperInterface $flashHelper,
473
        EventDispatcherInterface $eventDispatcher,
474
        ResourceControllerEvent $event,
475
        Request $request,
476
        Response $redirectResponse
477
    ): void {
478
        $metadata->getApplicationName()->willReturn('sylius');
479
        $metadata->getName()->willReturn('product');
480
481
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
482
        $configuration->hasPermission()->willReturn(true);
483
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
484
485
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
486
487
        $configuration->isHtmlRequest()->willReturn(true);
488
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
489
490
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
491
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
492
493
        $request->isMethod('POST')->willReturn(true);
494
        $form->handleRequest($request)->willReturn($form);
495
        $form->isValid()->willReturn(true);
496
        $form->getData()->willReturn($newResource);
497
498
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
499
        $event->isStopped()->willReturn(true);
500
501
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
502
503
        $event->hasResponse()->willReturn(false);
504
505
        $repository->add($newResource)->shouldNotBeCalled();
506
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
507
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
508
509
        $redirectHandler->redirectToIndex($configuration, $newResource)->willReturn($redirectResponse);
510
511
        $this->createAction($request)->shouldReturn($redirectResponse);
512
    }
513
514
    function it_does_not_create_the_resource_and_return_response_for_html_requests_stopped_via_events(
515
        MetadataInterface $metadata,
516
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
517
        RequestConfiguration $configuration,
518
        AuthorizationCheckerInterface $authorizationChecker,
519
        FactoryInterface $factory,
520
        NewResourceFactoryInterface $newResourceFactory,
521
        RepositoryInterface $repository,
522
        ResourceInterface $newResource,
523
        ResourceFormFactoryInterface $resourceFormFactory,
524
        Form $form,
525
        FlashHelperInterface $flashHelper,
526
        EventDispatcherInterface $eventDispatcher,
527
        ResourceControllerEvent $event,
528
        Request $request,
529
        Response $response
530
    ): void {
531
        $metadata->getApplicationName()->willReturn('sylius');
532
        $metadata->getName()->willReturn('product');
533
534
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
535
        $configuration->hasPermission()->willReturn(true);
536
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
537
538
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
539
540
        $configuration->isHtmlRequest()->willReturn(true);
541
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
542
543
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
544
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
545
546
        $request->isMethod('POST')->willReturn(true);
547
        $form->handleRequest($request)->willReturn($form);
548
        $form->isValid()->willReturn(true);
549
        $form->getData()->willReturn($newResource);
550
551
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
552
        $event->isStopped()->willReturn(true);
553
554
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
555
556
        $event->hasResponse()->willReturn(true);
557
        $event->getResponse()->willReturn($response);
558
559
        $repository->add($newResource)->shouldNotBeCalled();
560
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
561
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
562
563
        $this->createAction($request)->shouldReturn($response);
564
    }
565
566
    function it_redirects_to_newly_created_resource(
567
        MetadataInterface $metadata,
568
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
569
        RequestConfiguration $configuration,
570
        AuthorizationCheckerInterface $authorizationChecker,
571
        ViewHandlerInterface $viewHandler,
572
        FactoryInterface $factory,
573
        NewResourceFactoryInterface $newResourceFactory,
574
        RepositoryInterface $repository,
575
        ResourceInterface $newResource,
576
        ResourceFormFactoryInterface $resourceFormFactory,
577
        StateMachineInterface $stateMachine,
578
        Form $form,
579
        RedirectHandlerInterface $redirectHandler,
580
        FlashHelperInterface $flashHelper,
581
        EventDispatcherInterface $eventDispatcher,
582
        ResourceControllerEvent $event,
583
        ResourceControllerEvent $postEvent,
584
        Request $request,
585
        Response $redirectResponse
586
    ): void {
587
        $metadata->getApplicationName()->willReturn('sylius');
588
        $metadata->getName()->willReturn('product');
589
590
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
591
        $configuration->hasPermission()->willReturn(true);
592
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
593
        $configuration->hasStateMachine()->willReturn(true);
594
595
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
596
597
        $configuration->isHtmlRequest()->willReturn(true);
598
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
599
600
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
601
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
602
603
        $request->isMethod('POST')->willReturn(true);
604
        $form->handleRequest($request)->willReturn($form);
605
        $form->isValid()->willReturn(true);
606
        $form->getData()->willReturn($newResource);
607
608
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
609
        $event->isStopped()->willReturn(false);
610
611
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
612
613
        $repository->add($newResource)->shouldBeCalled();
614
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($postEvent);
615
616
        $postEvent->hasResponse()->willReturn(false);
617
618
        $flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled();
619
        $redirectHandler->redirectToResource($configuration, $newResource)->willReturn($redirectResponse);
620
621
        $this->createAction($request)->shouldReturn($redirectResponse);
622
    }
623
624
    function it_uses_response_from_post_create_event_if_defined(
625
        MetadataInterface $metadata,
626
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
627
        RequestConfiguration $configuration,
628
        AuthorizationCheckerInterface $authorizationChecker,
629
        FactoryInterface $factory,
630
        NewResourceFactoryInterface $newResourceFactory,
631
        RepositoryInterface $repository,
632
        ResourceInterface $newResource,
633
        ResourceFormFactoryInterface $resourceFormFactory,
634
        StateMachineInterface $stateMachine,
635
        Form $form,
636
        FlashHelperInterface $flashHelper,
637
        EventDispatcherInterface $eventDispatcher,
638
        ResourceControllerEvent $event,
639
        ResourceControllerEvent $postEvent,
640
        Request $request,
641
        Response $redirectResponse
642
    ): void {
643
        $metadata->getApplicationName()->willReturn('sylius');
644
        $metadata->getName()->willReturn('product');
645
646
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
647
        $configuration->hasPermission()->willReturn(true);
648
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
649
        $configuration->hasStateMachine()->willReturn(true);
650
651
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
652
653
        $configuration->isHtmlRequest()->willReturn(true);
654
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
655
656
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
657
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
658
659
        $request->isMethod('POST')->willReturn(true);
660
        $form->handleRequest($request)->willReturn($form);
661
        $form->isValid()->willReturn(true);
662
        $form->getData()->willReturn($newResource);
663
664
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
665
        $event->isStopped()->willReturn(false);
666
667
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
668
669
        $repository->add($newResource)->shouldBeCalled();
670
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($postEvent);
671
        $flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource)->shouldBeCalled();
672
673
        $postEvent->hasResponse()->willReturn(true);
674
        $postEvent->getResponse()->willReturn($redirectResponse);
675
676
        $this->createAction($request)->shouldReturn($redirectResponse);
677
    }
678
679
    function it_returns_a_non_html_response_for_correctly_created_resources(
680
        MetadataInterface $metadata,
681
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
682
        RequestConfiguration $configuration,
683
        AuthorizationCheckerInterface $authorizationChecker,
684
        ViewHandlerInterface $viewHandler,
685
        FactoryInterface $factory,
686
        NewResourceFactoryInterface $newResourceFactory,
687
        RepositoryInterface $repository,
688
        ResourceInterface $newResource,
689
        ResourceFormFactoryInterface $resourceFormFactory,
690
        FlashHelperInterface $flashHelper,
691
        EventDispatcherInterface $eventDispatcher,
692
        ResourceControllerEvent $event,
693
        StateMachineInterface $stateMachine,
694
        Form $form,
695
        Request $request,
696
        Response $response
697
    ): void {
698
        $metadata->getApplicationName()->willReturn('sylius');
699
        $metadata->getName()->willReturn('product');
700
701
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
702
        $configuration->hasPermission()->willReturn(true);
703
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
704
        $configuration->hasStateMachine()->willReturn(true);
705
706
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
707
708
        $configuration->isHtmlRequest()->willReturn(false);
709
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
710
711
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
712
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
713
714
        $request->isMethod('POST')->willReturn(true);
715
        $form->handleRequest($request)->willReturn($form);
716
        $form->isValid()->willReturn(true);
717
        $form->getData()->willReturn($newResource);
718
719
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
720
        $event->isStopped()->willReturn(false);
721
722
        $stateMachine->apply($configuration, $newResource)->shouldBeCalled();
723
724
        $repository->add($newResource)->shouldBeCalled();
725
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldBeCalled();
726
727
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
728
729
        $expectedView = View::create($newResource, 201);
730
731
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
732
733
        $this->createAction($request)->shouldReturn($response);
734
    }
735
736
    function it_does_not_create_the_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
737
        MetadataInterface $metadata,
738
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
739
        RequestConfiguration $configuration,
740
        AuthorizationCheckerInterface $authorizationChecker,
741
        FactoryInterface $factory,
742
        NewResourceFactoryInterface $newResourceFactory,
743
        RepositoryInterface $repository,
744
        ResourceInterface $newResource,
745
        ResourceFormFactoryInterface $resourceFormFactory,
746
        FlashHelperInterface $flashHelper,
747
        EventDispatcherInterface $eventDispatcher,
748
        Form $form,
749
        Request $request,
750
        ResourceControllerEvent $event
751
    ): void {
752
        $metadata->getApplicationName()->willReturn('sylius');
753
        $metadata->getName()->willReturn('product');
754
755
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
756
        $configuration->hasPermission()->willReturn(true);
757
        $configuration->getPermission(ResourceActions::CREATE)->willReturn('sylius.product.create');
758
759
        $authorizationChecker->isGranted($configuration, 'sylius.product.create')->willReturn(true);
760
761
        $configuration->isHtmlRequest()->willReturn(false);
762
        $configuration->getTemplate(ResourceActions::CREATE . '.html')->willReturn('SyliusShopBundle:Product:create.html.twig');
763
764
        $newResourceFactory->create($configuration, $factory)->willReturn($newResource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
765
        $resourceFormFactory->create($configuration, $newResource)->willReturn($form);
766
767
        $request->isMethod('POST')->willReturn(true);
768
        $form->handleRequest($request)->willReturn($form);
769
        $form->isValid()->willReturn(true);
770
        $form->getData()->willReturn($newResource);
771
772
        $eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource)->willReturn($event);
773
        $event->isStopped()->willReturn(true);
774
        $event->getMessage()->willReturn('You cannot add a new product right now.');
775
        $event->getErrorCode()->willReturn(500);
776
777
        $repository->add($newResource)->shouldNotBeCalled();
778
        $eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource)->shouldNotBeCalled();
779
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
780
781
        $this
782
            ->shouldThrow(new HttpException(500, 'You cannot add a new product right now.'))
783
            ->during('createAction', [$request])
784
        ;
785
    }
786
787
    function it_throws_a_403_exception_if_user_is_unauthorized_to_edit_a_single_resource(
788
        MetadataInterface $metadata,
789
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
790
        RequestConfiguration $configuration,
791
        Request $request,
792
        AuthorizationCheckerInterface $authorizationChecker
793
    ): void {
794
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
795
        $configuration->hasPermission()->willReturn(true);
796
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
797
798
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
799
800
        $this
801
            ->shouldThrow(new AccessDeniedException())
802
            ->during('updateAction', [$request])
803
        ;
804
    }
805
806
    function it_throws_a_404_exception_if_resource_to_update_is_not_found_based_on_configuration(
807
        MetadataInterface $metadata,
808
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
809
        RequestConfiguration $configuration,
810
        Request $request,
811
        AuthorizationCheckerInterface $authorizationChecker,
812
        RepositoryInterface $repository,
813
        SingleResourceProviderInterface $singleResourceProvider
814
    ): void {
815
        $metadata->getHumanizedName()->willReturn('product');
816
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
817
        $configuration->hasPermission()->willReturn(true);
818
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
819
820
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
821
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
822
823
        $this
824
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
825
            ->during('updateAction', [$request])
826
        ;
827
    }
828
829
    function it_returns_a_html_response_for_updating_resource_form(
830
        MetadataInterface $metadata,
831
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
832
        RequestConfiguration $configuration,
833
        AuthorizationCheckerInterface $authorizationChecker,
834
        ViewHandlerInterface $viewHandler,
835
        RepositoryInterface $repository,
836
        SingleResourceProviderInterface $singleResourceProvider,
837
        ResourceInterface $resource,
838
        ResourceFormFactoryInterface $resourceFormFactory,
839
        EventDispatcherInterface $eventDispatcher,
840
        ResourceControllerEvent $event,
841
        Form $form,
842
        FormView $formView,
843
        Request $request,
844
        Response $response
845
    ): void {
846
        $metadata->getApplicationName()->willReturn('sylius');
847
        $metadata->getName()->willReturn('product');
848
849
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
850
        $configuration->hasPermission()->willReturn(true);
851
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
852
        $configuration->hasStateMachine()->willReturn(false);
853
854
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
855
856
        $configuration->isHtmlRequest()->willReturn(true);
857
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
858
859
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
860
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
861
862
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
863
864
        $request->isMethod('PATCH')->willReturn(false);
865
        $request->getMethod()->willReturn('GET');
866
867
        $form->handleRequest($request)->willReturn($form);
868
        $form->createView()->willReturn($formView);
869
870
        $expectedView = View::create()
871
            ->setData([
872
                'configuration' => $configuration,
873
                'metadata' => $metadata,
874
                'resource' => $resource,
875
                'product' => $resource,
876
                'form' => $formView,
877
            ])
878
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
879
        ;
880
881
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
882
883
        $this->updateAction($request)->shouldReturn($response);
884
    }
885
886
    function it_returns_a_html_response_for_invalid_form_during_resource_update(
887
        MetadataInterface $metadata,
888
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
889
        RequestConfiguration $configuration,
890
        AuthorizationCheckerInterface $authorizationChecker,
891
        ViewHandlerInterface $viewHandler,
892
        RepositoryInterface $repository,
893
        SingleResourceProviderInterface $singleResourceProvider,
894
        ResourceInterface $resource,
895
        ResourceFormFactoryInterface $resourceFormFactory,
896
        EventDispatcherInterface $eventDispatcher,
897
        ResourceControllerEvent $event,
898
        Form $form,
899
        FormView $formView,
900
        Request $request,
901
        Response $response
902
    ): void {
903
        $metadata->getApplicationName()->willReturn('sylius');
904
        $metadata->getName()->willReturn('product');
905
906
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
907
        $configuration->hasPermission()->willReturn(true);
908
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
909
910
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
911
912
        $configuration->isHtmlRequest()->willReturn(true);
913
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
914
915
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
916
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
917
918
        $eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
919
920
        $request->isMethod('PATCH')->willReturn(false);
921
        $request->getMethod()->willReturn('PUT');
922
923
        $form->handleRequest($request)->willReturn($form);
924
925
        $form->isValid()->willReturn(false);
926
        $form->createView()->willReturn($formView);
927
928
        $expectedView = View::create()
929
            ->setData([
930
                'configuration' => $configuration,
931
                'metadata' => $metadata,
932
                'resource' => $resource,
933
                'product' => $resource,
934
                'form' => $formView,
935
            ])
936
            ->setTemplate('SyliusShopBundle:Product:update.html.twig')
937
        ;
938
939
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
940
941
        $this->updateAction($request)->shouldReturn($response);
942
    }
943
944
    function it_returns_a_non_html_response_for_invalid_form_during_resource_update(
945
        MetadataInterface $metadata,
946
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
947
        RequestConfiguration $configuration,
948
        AuthorizationCheckerInterface $authorizationChecker,
949
        ViewHandlerInterface $viewHandler,
950
        RepositoryInterface $repository,
951
        SingleResourceProviderInterface $singleResourceProvider,
952
        ResourceInterface $resource,
953
        ResourceFormFactoryInterface $resourceFormFactory,
954
        Form $form,
955
        Request $request,
956
        Response $response
957
    ): void {
958
        $metadata->getApplicationName()->willReturn('sylius');
959
        $metadata->getName()->willReturn('product');
960
961
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
962
        $configuration->hasPermission()->willReturn(true);
963
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
964
        $configuration->isHtmlRequest()->willReturn(false);
965
966
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
967
968
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
969
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
970
971
        $request->isMethod('PATCH')->willReturn(true);
972
        $request->getMethod()->willReturn('PATCH');
973
974
        $form->handleRequest($request)->willReturn($form);
975
        $form->isValid()->willReturn(false);
976
977
        $expectedView = View::create($form, 400);
978
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
979
980
        $this->updateAction($request)->shouldReturn($response);
981
    }
982
983
    function it_does_not_update_the_resource_and_redirects_to_resource_for_html_request_if_stopped_via_event(
984
        MetadataInterface $metadata,
985
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
986
        RequestConfiguration $configuration,
987
        AuthorizationCheckerInterface $authorizationChecker,
988
        ObjectManager $manager,
989
        RepositoryInterface $repository,
990
        SingleResourceProviderInterface $singleResourceProvider,
991
        ResourceInterface $resource,
992
        ResourceFormFactoryInterface $resourceFormFactory,
993
        Form $form,
994
        EventDispatcherInterface $eventDispatcher,
995
        RedirectHandlerInterface $redirectHandler,
996
        FlashHelperInterface $flashHelper,
997
        ResourceControllerEvent $event,
998
        Request $request,
999
        Response $redirectResponse
1000
    ): void {
1001
        $metadata->getApplicationName()->willReturn('sylius');
1002
        $metadata->getName()->willReturn('product');
1003
1004
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1005
        $configuration->hasPermission()->willReturn(true);
1006
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1007
1008
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1009
1010
        $configuration->isHtmlRequest()->willReturn(true);
1011
1012
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1013
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1014
1015
        $request->isMethod('PATCH')->willReturn(false);
1016
        $request->getMethod()->willReturn('PUT');
1017
1018
        $form->handleRequest($request)->willReturn($form);
1019
1020
        $form->isSubmitted()->willReturn(true);
1021
        $form->isValid()->willReturn(true);
1022
        $form->getData()->willReturn($resource);
1023
1024
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1025
        $event->isStopped()->willReturn(true);
1026
        $event->hasResponse()->willReturn(false);
1027
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1028
1029
        $manager->flush()->shouldNotBeCalled();
1030
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to dispatchPostEvent() misses some required arguments starting with $requestConfiguration.
Loading history...
1031
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
1032
1033
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1034
1035
        $this->updateAction($request)->shouldReturn($redirectResponse);
1036
    }
1037
1038
    function it_redirects_to_updated_resource(
1039
        MetadataInterface $metadata,
1040
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1041
        RepositoryInterface $repository,
1042
        ObjectManager $manager,
1043
        SingleResourceProviderInterface $singleResourceProvider,
1044
        ResourceFormFactoryInterface $resourceFormFactory,
1045
        RedirectHandlerInterface $redirectHandler,
1046
        FlashHelperInterface $flashHelper,
1047
        AuthorizationCheckerInterface $authorizationChecker,
1048
        EventDispatcherInterface $eventDispatcher,
1049
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1050
        RequestConfiguration $configuration,
1051
        ResourceInterface $resource,
1052
        Form $form,
1053
        ResourceControllerEvent $preEvent,
1054
        ResourceControllerEvent $postEvent,
1055
        Request $request,
1056
        Response $redirectResponse
1057
    ): void {
1058
        $metadata->getApplicationName()->willReturn('sylius');
1059
        $metadata->getName()->willReturn('product');
1060
1061
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1062
        $configuration->hasPermission()->willReturn(true);
1063
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1064
        $configuration->hasStateMachine()->willReturn(false);
1065
1066
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1067
1068
        $configuration->isHtmlRequest()->willReturn(true);
1069
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
1070
1071
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1072
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1073
1074
        $request->isMethod('PATCH')->willReturn(false);
1075
        $request->getMethod()->willReturn('PUT');
1076
1077
        $form->handleRequest($request)->willReturn($form);
1078
1079
        $form->isSubmitted()->willReturn(true);
1080
        $form->isValid()->willReturn(true);
1081
        $form->getData()->willReturn($resource);
1082
1083
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent);
1084
        $preEvent->isStopped()->willReturn(false);
1085
1086
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1087
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1088
1089
        $postEvent->hasResponse()->willReturn(false);
1090
1091
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1092
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1093
1094
        $this->updateAction($request)->shouldReturn($redirectResponse);
1095
    }
1096
1097
    function it_uses_response_from_post_update_event_if_defined(
1098
        MetadataInterface $metadata,
1099
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1100
        RepositoryInterface $repository,
1101
        ObjectManager $manager,
1102
        SingleResourceProviderInterface $singleResourceProvider,
1103
        ResourceFormFactoryInterface $resourceFormFactory,
1104
        RedirectHandlerInterface $redirectHandler,
1105
        FlashHelperInterface $flashHelper,
1106
        AuthorizationCheckerInterface $authorizationChecker,
1107
        EventDispatcherInterface $eventDispatcher,
1108
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1109
        RequestConfiguration $configuration,
1110
        ResourceInterface $resource,
1111
        Form $form,
1112
        ResourceControllerEvent $preEvent,
1113
        ResourceControllerEvent $postEvent,
1114
        Request $request,
1115
        Response $redirectResponse
1116
    ): void {
1117
        $metadata->getApplicationName()->willReturn('sylius');
1118
        $metadata->getName()->willReturn('product');
1119
1120
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1121
        $configuration->hasPermission()->willReturn(true);
1122
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1123
        $configuration->hasStateMachine()->willReturn(false);
1124
1125
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1126
1127
        $configuration->isHtmlRequest()->willReturn(true);
1128
        $configuration->getTemplate(ResourceActions::UPDATE . '.html')->willReturn('SyliusShopBundle:Product:update.html.twig');
1129
1130
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1131
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1132
1133
        $request->isMethod('PATCH')->willReturn(false);
1134
        $request->getMethod()->willReturn('PUT');
1135
1136
        $form->handleRequest($request)->willReturn($form);
1137
1138
        $form->isSubmitted()->willReturn(true);
1139
        $form->isValid()->willReturn(true);
1140
        $form->getData()->willReturn($resource);
1141
1142
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent);
1143
        $preEvent->isStopped()->willReturn(false);
1144
1145
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1146
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1147
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1148
1149
        $postEvent->hasResponse()->willReturn(true);
1150
        $postEvent->getResponse()->willReturn($redirectResponse);
1151
1152
        $redirectHandler->redirectToResource($configuration, $resource)->shouldNotBeCalled();
1153
1154
        $this->updateAction($request)->shouldReturn($redirectResponse);
1155
    }
1156
1157
    function it_returns_a_non_html_response_for_correctly_updated_resource(
1158
        MetadataInterface $metadata,
1159
        ParameterBagInterface $parameterBag,
1160
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1161
        ViewHandlerInterface $viewHandler,
1162
        RepositoryInterface $repository,
1163
        ObjectManager $manager,
1164
        SingleResourceProviderInterface $singleResourceProvider,
1165
        ResourceFormFactoryInterface $resourceFormFactory,
1166
        AuthorizationCheckerInterface $authorizationChecker,
1167
        EventDispatcherInterface $eventDispatcher,
1168
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1169
        RequestConfiguration $configuration,
1170
        ResourceInterface $resource,
1171
        ResourceControllerEvent $event,
1172
        Form $form,
1173
        Request $request,
1174
        Response $response
1175
    ): void {
1176
        $metadata->getApplicationName()->willReturn('sylius');
1177
        $metadata->getName()->willReturn('product');
1178
1179
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1180
        $configuration->hasPermission()->willReturn(true);
1181
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1182
        $configuration->isHtmlRequest()->willReturn(false);
1183
        $configuration->hasStateMachine()->willReturn(false);
1184
1185
        $configuration->getParameters()->willReturn($parameterBag);
1186
        $parameterBag->get('return_content', false)->willReturn(false);
1187
1188
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1189
1190
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1191
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1192
1193
        $request->isMethod('PATCH')->willReturn(false);
1194
        $request->getMethod()->willReturn('PUT');
1195
1196
        $form->handleRequest($request)->willReturn($form);
1197
        $form->isValid()->willReturn(true);
1198
        $form->getData()->willReturn($resource);
1199
1200
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1201
        $event->isStopped()->willReturn(false);
1202
1203
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1204
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
1205
1206
        $expectedView = View::create(null, 204);
1207
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1208
1209
        $this->updateAction($request)->shouldReturn($response);
1210
    }
1211
1212
    function it_does_not_update_the_resource_throws_a_http_exception_for_non_html_requests_stopped_via_event(
1213
        MetadataInterface $metadata,
1214
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1215
        RequestConfiguration $configuration,
1216
        AuthorizationCheckerInterface $authorizationChecker,
1217
        ObjectManager $manager,
1218
        RepositoryInterface $repository,
1219
        SingleResourceProviderInterface $singleResourceProvider,
1220
        ResourceInterface $resource,
1221
        ResourceFormFactoryInterface $resourceFormFactory,
1222
        EventDispatcherInterface $eventDispatcher,
1223
        ResourceControllerEvent $event,
1224
        Form $form,
1225
        Request $request
1226
    ): void {
1227
        $metadata->getApplicationName()->willReturn('sylius');
1228
        $metadata->getName()->willReturn('product');
1229
1230
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1231
        $configuration->hasPermission()->willReturn(true);
1232
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1233
        $configuration->isHtmlRequest()->willReturn(false);
1234
1235
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1236
1237
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1238
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1239
1240
        $request->isMethod('PATCH')->willReturn(false);
1241
        $request->getMethod()->willReturn('PUT');
1242
1243
        $form->handleRequest($request)->willReturn($form);
1244
        $form->isValid()->willReturn(true);
1245
        $form->getData()->willReturn($resource);
1246
1247
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1248
        $event->isStopped()->willReturn(true);
1249
        $event->getMessage()->willReturn('Cannot update this channel.');
1250
        $event->getErrorCode()->willReturn(500);
1251
1252
        $manager->flush()->shouldNotBeCalled();
1253
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to dispatchPostEvent() misses some required arguments starting with $requestConfiguration.
Loading history...
1254
1255
        $this
1256
            ->shouldThrow(new HttpException(500, 'Cannot update this channel.'))
1257
            ->during('updateAction', [$request])
1258
        ;
1259
    }
1260
1261
    function it_applies_state_machine_transition_to_updated_resource_if_configured(
1262
        MetadataInterface $metadata,
1263
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1264
        RepositoryInterface $repository,
1265
        ObjectManager $manager,
1266
        SingleResourceProviderInterface $singleResourceProvider,
1267
        ResourceFormFactoryInterface $resourceFormFactory,
1268
        RedirectHandlerInterface $redirectHandler,
1269
        FlashHelperInterface $flashHelper,
1270
        AuthorizationCheckerInterface $authorizationChecker,
1271
        EventDispatcherInterface $eventDispatcher,
1272
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1273
        RequestConfiguration $configuration,
1274
        ResourceInterface $resource,
1275
        Form $form,
1276
        ResourceControllerEvent $preEvent,
1277
        ResourceControllerEvent $postEvent,
1278
        Request $request,
1279
        Response $redirectResponse
1280
    ): void {
1281
        $metadata->getApplicationName()->willReturn('sylius');
1282
        $metadata->getName()->willReturn('product');
1283
1284
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1285
        $configuration->hasPermission()->willReturn(true);
1286
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1287
        $configuration->hasStateMachine()->willReturn(true);
1288
1289
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1290
1291
        $configuration->isHtmlRequest()->willReturn(true);
1292
        $configuration->getTemplate(ResourceActions::UPDATE)->willReturn('SyliusShopBundle:Product:update.html.twig');
1293
1294
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1295
        $resourceFormFactory->create($configuration, $resource)->willReturn($form);
1296
1297
        $request->isMethod('PATCH')->willReturn(false);
1298
        $request->getMethod()->willReturn('PUT');
1299
1300
        $form->handleRequest($request)->willReturn($form);
1301
1302
        $form->isSubmitted()->willReturn(true);
1303
        $form->isValid()->willReturn(true);
1304
        $form->getData()->willReturn($resource);
1305
1306
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($preEvent);
1307
        $preEvent->isStopped()->willReturn(false);
1308
1309
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1310
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1311
1312
        $postEvent->hasResponse()->willReturn(false);
1313
1314
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1315
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1316
1317
        $this->updateAction($request)->shouldReturn($redirectResponse);
1318
    }
1319
1320
    function it_throws_a_403_exception_if_user_is_unauthorized_to_delete_a_single_resource(
1321
        MetadataInterface $metadata,
1322
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1323
        RequestConfiguration $configuration,
1324
        Request $request,
1325
        AuthorizationCheckerInterface $authorizationChecker
1326
    ): void {
1327
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1328
        $configuration->hasPermission()->willReturn(true);
1329
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1330
1331
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(false);
1332
1333
        $this
1334
            ->shouldThrow(new AccessDeniedException())
1335
            ->during('deleteAction', [$request])
1336
        ;
1337
    }
1338
1339
    function it_throws_a_404_exception_if_resource_for_deletion_is_not_found_based_on_configuration(
1340
        MetadataInterface $metadata,
1341
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1342
        RequestConfiguration $configuration,
1343
        Request $request,
1344
        AuthorizationCheckerInterface $authorizationChecker,
1345
        RepositoryInterface $repository,
1346
        SingleResourceProviderInterface $singleResourceProvider
1347
    ): void {
1348
        $metadata->getHumanizedName()->willReturn('product');
1349
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1350
        $configuration->hasPermission()->willReturn(true);
1351
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1352
1353
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1354
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1355
1356
        $this
1357
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
1358
            ->during('deleteAction', [$request])
1359
        ;
1360
    }
1361
1362
    function it_deletes_a_resource_and_redirects_to_index_by_for_html_request(
1363
        MetadataInterface $metadata,
1364
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1365
        RequestConfiguration $configuration,
1366
        AuthorizationCheckerInterface $authorizationChecker,
1367
        RepositoryInterface $repository,
1368
        SingleResourceProviderInterface $singleResourceProvider,
1369
        ResourceInterface $resource,
1370
        RedirectHandlerInterface $redirectHandler,
1371
        FlashHelperInterface $flashHelper,
1372
        EventDispatcherInterface $eventDispatcher,
1373
        CsrfTokenManagerInterface $csrfTokenManager,
1374
        ContainerInterface $container,
1375
        ResourceControllerEvent $event,
1376
        ResourceControllerEvent $postEvent,
1377
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1378
        Request $request,
1379
        Response $redirectResponse
1380
    ): void {
1381
        $metadata->getApplicationName()->willReturn('sylius');
1382
        $metadata->getName()->willReturn('product');
1383
1384
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1385
        $configuration->hasPermission()->willReturn(true);
1386
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1387
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1388
1389
        $container->has('security.csrf.token_manager')->willReturn(true);
1390
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1391
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1392
1393
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1394
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1395
        $resource->getId()->willReturn(1);
1396
1397
        $configuration->isHtmlRequest()->willReturn(true);
1398
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1399
1400
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1401
        $event->isStopped()->willReturn(false);
1402
1403
        $resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled();
1404
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($postEvent);
1405
1406
        $postEvent->hasResponse()->willReturn(false);
1407
1408
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled();
1409
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1410
1411
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1412
    }
1413
1414
    function it_uses_response_from_post_delete_event_if_defined(
1415
        MetadataInterface $metadata,
1416
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1417
        RequestConfiguration $configuration,
1418
        AuthorizationCheckerInterface $authorizationChecker,
1419
        RepositoryInterface $repository,
1420
        SingleResourceProviderInterface $singleResourceProvider,
1421
        ResourceInterface $resource,
1422
        FlashHelperInterface $flashHelper,
1423
        EventDispatcherInterface $eventDispatcher,
1424
        CsrfTokenManagerInterface $csrfTokenManager,
1425
        ContainerInterface $container,
1426
        ResourceControllerEvent $event,
1427
        ResourceControllerEvent $postEvent,
1428
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1429
        Request $request,
1430
        Response $redirectResponse
1431
    ): void {
1432
        $metadata->getApplicationName()->willReturn('sylius');
1433
        $metadata->getName()->willReturn('product');
1434
1435
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1436
        $configuration->hasPermission()->willReturn(true);
1437
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1438
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1439
1440
        $container->has('security.csrf.token_manager')->willReturn(true);
1441
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1442
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1443
1444
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1445
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1446
        $resource->getId()->willReturn(1);
1447
1448
        $configuration->isHtmlRequest()->willReturn(true);
1449
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1450
1451
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1452
        $event->isStopped()->willReturn(false);
1453
1454
        $resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled();
1455
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($postEvent);
1456
1457
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldBeCalled();
1458
1459
        $postEvent->hasResponse()->willReturn(true);
1460
        $postEvent->getResponse()->willReturn($redirectResponse);
1461
1462
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1463
    }
1464
1465
    function it_does_not_delete_a_resource_and_redirects_to_index_for_html_requests_stopped_via_event(
1466
        MetadataInterface $metadata,
1467
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1468
        RequestConfiguration $configuration,
1469
        AuthorizationCheckerInterface $authorizationChecker,
1470
        RepositoryInterface $repository,
1471
        SingleResourceProviderInterface $singleResourceProvider,
1472
        ResourceInterface $resource,
1473
        RedirectHandlerInterface $redirectHandler,
1474
        FlashHelperInterface $flashHelper,
1475
        EventDispatcherInterface $eventDispatcher,
1476
        CsrfTokenManagerInterface $csrfTokenManager,
1477
        ContainerInterface $container,
1478
        ResourceControllerEvent $event,
1479
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1480
        Request $request,
1481
        Response $redirectResponse
1482
    ): void {
1483
        $metadata->getApplicationName()->willReturn('sylius');
1484
        $metadata->getName()->willReturn('product');
1485
1486
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1487
        $configuration->hasPermission()->willReturn(true);
1488
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1489
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1490
1491
        $container->has('security.csrf.token_manager')->willReturn(true);
1492
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1493
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1494
1495
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1496
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1497
        $resource->getId()->willReturn(1);
1498
1499
        $configuration->isHtmlRequest()->willReturn(true);
1500
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1501
1502
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1503
        $event->isStopped()->willReturn(true);
1504
        $event->hasResponse()->willReturn(false);
1505
1506
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1507
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1508
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled();
1509
1510
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1511
        $redirectHandler->redirectToIndex($configuration, $resource)->willReturn($redirectResponse);
1512
1513
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1514
    }
1515
1516
    function it_does_not_delete_a_resource_and_uses_response_from_event_if_defined(
1517
        MetadataInterface $metadata,
1518
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1519
        RequestConfiguration $configuration,
1520
        AuthorizationCheckerInterface $authorizationChecker,
1521
        RepositoryInterface $repository,
1522
        SingleResourceProviderInterface $singleResourceProvider,
1523
        ResourceInterface $resource,
1524
        RedirectHandlerInterface $redirectHandler,
1525
        FlashHelperInterface $flashHelper,
1526
        EventDispatcherInterface $eventDispatcher,
1527
        CsrfTokenManagerInterface $csrfTokenManager,
1528
        ContainerInterface $container,
1529
        ResourceControllerEvent $event,
1530
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1531
        Request $request,
1532
        Response $redirectResponse
1533
    ): void {
1534
        $metadata->getApplicationName()->willReturn('sylius');
1535
        $metadata->getName()->willReturn('product');
1536
1537
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1538
        $configuration->hasPermission()->willReturn(true);
1539
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1540
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1541
1542
        $container->has('security.csrf.token_manager')->willReturn(true);
1543
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1544
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1545
1546
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1547
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1548
        $resource->getId()->willReturn(1);
1549
1550
        $configuration->isHtmlRequest()->willReturn(true);
1551
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1552
1553
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1554
        $event->isStopped()->willReturn(true);
1555
1556
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1557
1558
        $event->hasResponse()->willReturn(true);
1559
        $event->getResponse()->willReturn($redirectResponse);
1560
1561
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1562
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1563
        $flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource)->shouldNotBeCalled();
1564
1565
        $redirectHandler->redirectToIndex($configuration, $resource)->shouldNotBeCalled();
1566
1567
        $this->deleteAction($request)->shouldReturn($redirectResponse);
1568
    }
1569
1570
    function it_does_not_correctly_delete_a_resource_and_returns_500_for_not_html_response(
1571
        MetadataInterface $metadata,
1572
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1573
        RequestConfiguration $configuration,
1574
        AuthorizationCheckerInterface $authorizationChecker,
1575
        ViewHandlerInterface $viewHandler,
1576
        RepositoryInterface $repository,
1577
        SingleResourceProviderInterface $singleResourceProvider,
1578
        ResourceInterface $resource,
1579
        EventDispatcherInterface $eventDispatcher,
1580
        CsrfTokenManagerInterface $csrfTokenManager,
1581
        ContainerInterface $container,
1582
        ResourceControllerEvent $event,
1583
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1584
        Request $request,
1585
        Response $response
1586
    ): void {
1587
        $metadata->getApplicationName()->willReturn('sylius');
1588
        $metadata->getName()->willReturn('product');
1589
1590
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1591
        $configuration->hasPermission()->willReturn(true);
1592
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1593
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1594
1595
        $container->has('security.csrf.token_manager')->willReturn(true);
1596
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1597
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1598
1599
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1600
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1601
        $resource->getId()->willReturn(1);
1602
1603
        $configuration->isHtmlRequest()->willReturn(false);
1604
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1605
1606
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1607
        $event->isStopped()->willReturn(false);
1608
1609
        $resourceDeleteHandler->handle($resource, $repository)->willThrow(new DeleteHandlingException());
1610
1611
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldNotBeCalled();
1612
1613
        $expectedView = View::create(null, 500);
1614
1615
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1616
1617
        $this->deleteAction($request)->shouldReturn($response);
1618
    }
1619
1620
    function it_deletes_a_resource_and_returns_204_for_non_html_requests(
1621
        MetadataInterface $metadata,
1622
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1623
        RequestConfiguration $configuration,
1624
        AuthorizationCheckerInterface $authorizationChecker,
1625
        ViewHandlerInterface $viewHandler,
1626
        RepositoryInterface $repository,
1627
        SingleResourceProviderInterface $singleResourceProvider,
1628
        ResourceInterface $resource,
1629
        EventDispatcherInterface $eventDispatcher,
1630
        CsrfTokenManagerInterface $csrfTokenManager,
1631
        ContainerInterface $container,
1632
        ResourceControllerEvent $event,
1633
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1634
        Request $request,
1635
        Response $response
1636
    ): void {
1637
        $metadata->getApplicationName()->willReturn('sylius');
1638
        $metadata->getName()->willReturn('product');
1639
1640
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1641
        $configuration->hasPermission()->willReturn(true);
1642
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1643
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1644
1645
        $container->has('security.csrf.token_manager')->willReturn(true);
1646
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1647
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1648
1649
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1650
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1651
        $resource->getId()->willReturn(1);
1652
1653
        $configuration->isHtmlRequest()->willReturn(false);
1654
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1655
1656
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1657
        $event->isStopped()->willReturn(false);
1658
1659
        $resourceDeleteHandler->handle($resource, $repository)->shouldBeCalled();
1660
        $eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource)->shouldBeCalled();
1661
1662
        $expectedView = View::create(null, 204);
1663
1664
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
1665
1666
        $this->deleteAction($request)->shouldReturn($response);
1667
    }
1668
1669
    function it_does_not_delete_a_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
1670
        MetadataInterface $metadata,
1671
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1672
        RequestConfiguration $configuration,
1673
        AuthorizationCheckerInterface $authorizationChecker,
1674
        RepositoryInterface $repository,
1675
        SingleResourceProviderInterface $singleResourceProvider,
1676
        ResourceInterface $resource,
1677
        FlashHelperInterface $flashHelper,
1678
        EventDispatcherInterface $eventDispatcher,
1679
        CsrfTokenManagerInterface $csrfTokenManager,
1680
        ContainerInterface $container,
1681
        ResourceControllerEvent $event,
1682
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1683
        Request $request
1684
    ): void {
1685
        $metadata->getApplicationName()->willReturn('sylius');
1686
        $metadata->getName()->willReturn('product');
1687
1688
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1689
        $configuration->hasPermission()->willReturn(true);
1690
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1691
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1692
1693
        $container->has('security.csrf.token_manager')->willReturn(true);
1694
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1695
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(true);
1696
1697
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1698
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1699
        $resource->getId()->willReturn(1);
1700
1701
        $configuration->isHtmlRequest()->willReturn(false);
1702
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1703
1704
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1705
        $event->isStopped()->willReturn(true);
1706
        $event->getMessage()->willReturn('Cannot delete this product.');
1707
        $event->getErrorCode()->willReturn(500);
1708
1709
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1710
1711
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to dispatchPostEvent() misses some required arguments starting with $requestConfiguration.
Loading history...
1712
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
1713
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addFlashFromEvent() misses a required argument $event.

This check looks for function calls that miss required arguments.

Loading history...
1714
1715
        $this
1716
            ->shouldThrow(new HttpException(500, 'Cannot delete this product.'))
1717
            ->during('deleteAction', [$request])
1718
        ;
1719
    }
1720
1721
    function it_throws_a_403_exception_if_csrf_token_is_invalid_during_delete_action(
1722
        MetadataInterface $metadata,
1723
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1724
        RequestConfiguration $configuration,
1725
        AuthorizationCheckerInterface $authorizationChecker,
1726
        RepositoryInterface $repository,
1727
        SingleResourceProviderInterface $singleResourceProvider,
1728
        ResourceInterface $resource,
1729
        FlashHelperInterface $flashHelper,
1730
        EventDispatcherInterface $eventDispatcher,
1731
        CsrfTokenManagerInterface $csrfTokenManager,
1732
        ContainerInterface $container,
1733
        ResourceControllerEvent $event,
1734
        ResourceDeleteHandlerInterface $resourceDeleteHandler,
1735
        Request $request
1736
    ): void {
1737
        $metadata->getApplicationName()->willReturn('sylius');
1738
        $metadata->getName()->willReturn('product');
1739
1740
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1741
        $configuration->hasPermission()->willReturn(true);
1742
        $configuration->getPermission(ResourceActions::DELETE)->willReturn('sylius.product.delete');
1743
        $request->request = new ParameterBag(['_csrf_token' => 'xyz']);
1744
1745
        $container->has('security.csrf.token_manager')->willReturn(true);
1746
        $container->get('security.csrf.token_manager')->willReturn($csrfTokenManager);
1747
        $csrfTokenManager->isTokenValid(new CsrfToken(1, 'xyz'))->willReturn(false);
1748
1749
        $authorizationChecker->isGranted($configuration, 'sylius.product.delete')->willReturn(true);
1750
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1751
        $resource->getId()->willReturn(1);
1752
1753
        $configuration->isHtmlRequest()->willReturn(true);
1754
        $configuration->isCsrfProtectionEnabled()->willReturn(true);
1755
1756
        $eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource)->willReturn($event);
1757
        $event->isStopped()->shouldNotBeCalled();
1758
1759
        $resourceDeleteHandler->handle($resource, $repository)->shouldNotBeCalled();
1760
1761
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to dispatchPostEvent() misses some required arguments starting with $requestConfiguration.
Loading history...
1762
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
1763
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addFlashFromEvent() misses a required argument $event.

This check looks for function calls that miss required arguments.

Loading history...
1764
1765
        $this
1766
            ->shouldThrow(new HttpException(403, 'Invalid csrf token.'))
1767
            ->during('deleteAction', [$request])
1768
        ;
1769
    }
1770
1771
    function it_throws_a_403_exception_if_user_is_unauthorized_to_apply_state_machine_transition_on_resource(
1772
        MetadataInterface $metadata,
1773
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1774
        RequestConfiguration $configuration,
1775
        Request $request,
1776
        AuthorizationCheckerInterface $authorizationChecker
1777
    ): void {
1778
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1779
        $configuration->hasPermission()->willReturn(true);
1780
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1781
1782
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(false);
1783
1784
        $this
1785
            ->shouldThrow(new AccessDeniedException())
1786
            ->during('applyStateMachineTransitionAction', [$request])
1787
        ;
1788
    }
1789
1790
    function it_throws_a_404_exception_if_resource_is_not_found_when_trying_to_apply_state_machine_transition(
1791
        MetadataInterface $metadata,
1792
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1793
        RequestConfiguration $configuration,
1794
        Request $request,
1795
        AuthorizationCheckerInterface $authorizationChecker,
1796
        RepositoryInterface $repository,
1797
        SingleResourceProviderInterface $singleResourceProvider
1798
    ): void {
1799
        $metadata->getHumanizedName()->willReturn('product');
1800
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1801
        $configuration->hasPermission()->willReturn(true);
1802
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1803
1804
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1805
        $singleResourceProvider->get($configuration, $repository)->willReturn(null);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1806
1807
        $this
1808
            ->shouldThrow(new NotFoundHttpException('The "product" has not been found'))
1809
            ->during('applyStateMachineTransitionAction', [$request])
1810
        ;
1811
    }
1812
1813
    function it_does_not_apply_state_machine_transition_on_resource_if_not_applicable_and_returns_400_bad_request(
1814
        MetadataInterface $metadata,
1815
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1816
        RequestConfiguration $configuration,
1817
        AuthorizationCheckerInterface $authorizationChecker,
1818
        RepositoryInterface $repository,
1819
        ObjectManager $objectManager,
1820
        StateMachineInterface $stateMachine,
1821
        SingleResourceProviderInterface $singleResourceProvider,
1822
        ResourceInterface $resource,
1823
        FlashHelperInterface $flashHelper,
1824
        EventDispatcherInterface $eventDispatcher,
1825
        ResourceControllerEvent $event,
1826
        Request $request
1827
    ): void {
1828
        $metadata->getApplicationName()->willReturn('sylius');
1829
        $metadata->getName()->willReturn('product');
1830
1831
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1832
        $configuration->hasPermission()->willReturn(true);
1833
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1834
1835
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1836
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1837
1838
        $configuration->isHtmlRequest()->willReturn(true);
1839
1840
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1841
        $event->isStopped()->willReturn(false);
1842
1843
        $stateMachine->can($configuration, $resource)->willReturn(false);
1844
1845
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
1846
        $objectManager->flush()->shouldNotBeCalled();
1847
1848
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to dispatchPostEvent() misses some required arguments starting with $requestConfiguration.
Loading history...
1849
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
1850
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addFlashFromEvent() misses a required argument $event.

This check looks for function calls that miss required arguments.

Loading history...
1851
1852
        $this
1853
            ->shouldThrow(new BadRequestHttpException())
1854
            ->during('applyStateMachineTransitionAction', [$request])
1855
        ;
1856
    }
1857
1858
    function it_applies_state_machine_transition_to_resource_and_redirects_for_html_request(
1859
        MetadataInterface $metadata,
1860
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1861
        RepositoryInterface $repository,
1862
        ObjectManager $manager,
1863
        SingleResourceProviderInterface $singleResourceProvider,
1864
        RedirectHandlerInterface $redirectHandler,
1865
        FlashHelperInterface $flashHelper,
1866
        AuthorizationCheckerInterface $authorizationChecker,
1867
        EventDispatcherInterface $eventDispatcher,
1868
        StateMachineInterface $stateMachine,
1869
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1870
        RequestConfiguration $configuration,
1871
        ResourceInterface $resource,
1872
        ResourceControllerEvent $event,
1873
        ResourceControllerEvent $postEvent,
1874
        Request $request,
1875
        Response $redirectResponse
1876
    ): void {
1877
        $metadata->getApplicationName()->willReturn('sylius');
1878
        $metadata->getName()->willReturn('product');
1879
1880
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1881
        $configuration->hasPermission()->willReturn(true);
1882
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1883
1884
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1885
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1886
1887
        $configuration->isHtmlRequest()->willReturn(true);
1888
1889
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1890
        $event->isStopped()->willReturn(false);
1891
1892
        $stateMachine->can($configuration, $resource)->willReturn(true);
1893
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1894
1895
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1896
1897
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1898
1899
        $postEvent->hasResponse()->willReturn(false);
1900
1901
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1902
1903
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1904
    }
1905
1906
    function it_uses_response_from_post_apply_state_machine_transition_event_if_defined(
1907
        MetadataInterface $metadata,
1908
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1909
        RepositoryInterface $repository,
1910
        ObjectManager $manager,
1911
        SingleResourceProviderInterface $singleResourceProvider,
1912
        RedirectHandlerInterface $redirectHandler,
1913
        FlashHelperInterface $flashHelper,
1914
        AuthorizationCheckerInterface $authorizationChecker,
1915
        EventDispatcherInterface $eventDispatcher,
1916
        StateMachineInterface $stateMachine,
1917
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
1918
        RequestConfiguration $configuration,
1919
        ResourceInterface $resource,
1920
        ResourceControllerEvent $event,
1921
        ResourceControllerEvent $postEvent,
1922
        Request $request,
1923
        Response $redirectResponse
1924
    ): void {
1925
        $metadata->getApplicationName()->willReturn('sylius');
1926
        $metadata->getName()->willReturn('product');
1927
1928
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1929
        $configuration->hasPermission()->willReturn(true);
1930
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1931
1932
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1933
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1934
1935
        $configuration->isHtmlRequest()->willReturn(true);
1936
1937
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1938
        $event->isStopped()->willReturn(false);
1939
1940
        $stateMachine->can($configuration, $resource)->willReturn(true);
1941
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
1942
1943
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldBeCalled();
1944
1945
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($postEvent);
1946
1947
        $postEvent->hasResponse()->willReturn(true);
1948
        $postEvent->getResponse()->willReturn($redirectResponse);
1949
1950
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1951
    }
1952
1953
    function it_does_not_apply_state_machine_transition_on_resource_and_redirects_for_html_requests_stopped_via_event(
1954
        MetadataInterface $metadata,
1955
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
1956
        RequestConfiguration $configuration,
1957
        AuthorizationCheckerInterface $authorizationChecker,
1958
        StateMachineInterface $stateMachine,
1959
        ObjectManager $manager,
1960
        RepositoryInterface $repository,
1961
        SingleResourceProviderInterface $singleResourceProvider,
1962
        ResourceInterface $resource,
1963
        RedirectHandlerInterface $redirectHandler,
1964
        FlashHelperInterface $flashHelper,
1965
        EventDispatcherInterface $eventDispatcher,
1966
        ResourceControllerEvent $event,
1967
        Request $request,
1968
        Response $redirectResponse
1969
    ): void {
1970
        $metadata->getApplicationName()->willReturn('sylius');
1971
        $metadata->getName()->willReturn('product');
1972
1973
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1974
        $configuration->hasPermission()->willReturn(true);
1975
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
1976
1977
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
1978
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1979
1980
        $configuration->isHtmlRequest()->willReturn(true);
1981
1982
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
1983
        $event->isStopped()->willReturn(true);
1984
1985
        $manager->flush()->shouldNotBeCalled();
1986
        $stateMachine->apply($resource)->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to apply() misses a required argument $resource.

This check looks for function calls that miss required arguments.

Loading history...
1987
1988
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled();
1989
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled();
1990
1991
        $event->hasResponse()->willReturn(false);
1992
1993
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
1994
        $redirectHandler->redirectToResource($configuration, $resource)->willReturn($redirectResponse);
1995
1996
        $this->applyStateMachineTransitionAction($request)->shouldReturn($redirectResponse);
1997
    }
1998
1999
    function it_does_not_apply_state_machine_transition_on_resource_and_return_event_response_for_html_requests_stopped_via_event(
2000
        MetadataInterface $metadata,
2001
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
2002
        RequestConfiguration $configuration,
2003
        AuthorizationCheckerInterface $authorizationChecker,
2004
        StateMachineInterface $stateMachine,
2005
        ObjectManager $manager,
2006
        RepositoryInterface $repository,
2007
        SingleResourceProviderInterface $singleResourceProvider,
2008
        ResourceInterface $resource,
2009
        FlashHelperInterface $flashHelper,
2010
        EventDispatcherInterface $eventDispatcher,
2011
        ResourceControllerEvent $event,
2012
        Request $request,
2013
        Response $response
2014
    ): void {
2015
        $metadata->getApplicationName()->willReturn('sylius');
2016
        $metadata->getName()->willReturn('product');
2017
2018
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2019
        $configuration->hasPermission()->willReturn(true);
2020
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
2021
2022
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
2023
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2024
2025
        $configuration->isHtmlRequest()->willReturn(true);
2026
2027
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
2028
        $event->isStopped()->willReturn(true);
2029
2030
        $manager->flush()->shouldNotBeCalled();
2031
        $stateMachine->apply($resource)->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to apply() misses a required argument $resource.

This check looks for function calls that miss required arguments.

Loading history...
2032
2033
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldNotBeCalled();
2034
        $flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource)->shouldNotBeCalled();
2035
2036
        $flashHelper->addFlashFromEvent($configuration, $event)->shouldBeCalled();
2037
2038
        $event->hasResponse()->willReturn(true);
2039
        $event->getResponse()->willReturn($response);
2040
2041
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
2042
    }
2043
2044
    function it_applies_state_machine_transition_on_resource_and_returns_200_for_non_html_requests(
2045
        MetadataInterface $metadata,
2046
        ParameterBagInterface $parameterBag,
2047
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
2048
        ViewHandlerInterface $viewHandler,
2049
        RepositoryInterface $repository,
2050
        ObjectManager $manager,
2051
        SingleResourceProviderInterface $singleResourceProvider,
2052
        AuthorizationCheckerInterface $authorizationChecker,
2053
        EventDispatcherInterface $eventDispatcher,
2054
        StateMachineInterface $stateMachine,
2055
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
2056
        RequestConfiguration $configuration,
2057
        ResourceInterface $resource,
2058
        ResourceControllerEvent $event,
2059
        Request $request,
2060
        Response $response
2061
    ): void {
2062
        $metadata->getApplicationName()->willReturn('sylius');
2063
        $metadata->getName()->willReturn('product');
2064
2065
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2066
        $configuration->getParameters()->willReturn($parameterBag);
2067
        $configuration->hasPermission()->willReturn(true);
2068
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
2069
2070
        $parameterBag->get('return_content', true)->willReturn(true);
2071
2072
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
2073
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2074
2075
        $configuration->isHtmlRequest()->willReturn(false);
2076
2077
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
2078
        $event->isStopped()->willReturn(false);
2079
2080
        $stateMachine->can($configuration, $resource)->willReturn(true);
2081
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
2082
2083
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
2084
2085
        $expectedView = View::create($resource, 200);
2086
2087
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
2088
2089
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
2090
    }
2091
2092
    function it_applies_state_machine_transition_on_resource_and_returns_204_for_non_html_requests_if_additional_option_added(
2093
        MetadataInterface $metadata,
2094
        ParameterBagInterface $parameterBag,
2095
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
2096
        ViewHandlerInterface $viewHandler,
2097
        RepositoryInterface $repository,
2098
        ObjectManager $manager,
2099
        SingleResourceProviderInterface $singleResourceProvider,
2100
        AuthorizationCheckerInterface $authorizationChecker,
2101
        EventDispatcherInterface $eventDispatcher,
2102
        StateMachineInterface $stateMachine,
2103
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
2104
        RequestConfiguration $configuration,
2105
        ResourceInterface $resource,
2106
        ResourceControllerEvent $event,
2107
        Request $request,
2108
        Response $response
2109
    ): void {
2110
        $metadata->getApplicationName()->willReturn('sylius');
2111
        $metadata->getName()->willReturn('product');
2112
2113
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2114
        $configuration->getParameters()->willReturn($parameterBag);
2115
        $configuration->hasPermission()->willReturn(true);
2116
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
2117
2118
        $parameterBag->get('return_content', true)->willReturn(false);
2119
2120
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
2121
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2122
2123
        $configuration->isHtmlRequest()->willReturn(false);
2124
2125
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
2126
        $event->isStopped()->willReturn(false);
2127
2128
        $stateMachine->can($configuration, $resource)->willReturn(true);
2129
        $resourceUpdateHandler->handle($resource, $configuration, $manager)->shouldBeCalled();
2130
2131
        $eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource)->shouldBeCalled();
2132
2133
        $expectedView = View::create(null, 204);
2134
2135
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
2136
2137
        $this->applyStateMachineTransitionAction($request)->shouldReturn($response);
2138
    }
2139
2140
    function it_does_not_apply_state_machine_transition_resource_and_throws_http_exception_for_non_html_requests_stopped_via_event(
2141
        MetadataInterface $metadata,
2142
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
2143
        RequestConfiguration $configuration,
2144
        AuthorizationCheckerInterface $authorizationChecker,
2145
        RepositoryInterface $repository,
2146
        ObjectManager $objectManager,
2147
        StateMachineInterface $stateMachine,
2148
        SingleResourceProviderInterface $singleResourceProvider,
2149
        ResourceInterface $resource,
2150
        FlashHelperInterface $flashHelper,
2151
        EventDispatcherInterface $eventDispatcher,
2152
        ResourceControllerEvent $event,
2153
        Request $request
2154
    ): void {
2155
        $metadata->getApplicationName()->willReturn('sylius');
2156
        $metadata->getName()->willReturn('product');
2157
2158
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Bundle\Res...r\RequestConfiguration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2159
        $configuration->hasPermission()->willReturn(true);
2160
        $configuration->getPermission(ResourceActions::UPDATE)->willReturn('sylius.product.update');
2161
2162
        $authorizationChecker->isGranted($configuration, 'sylius.product.update')->willReturn(true);
2163
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...odel\ResourceInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
2164
2165
        $configuration->isHtmlRequest()->willReturn(false);
2166
2167
        $eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource)->willReturn($event);
2168
        $event->isStopped()->willReturn(true);
2169
        $event->getMessage()->willReturn('Cannot approve this product.');
2170
        $event->getErrorCode()->willReturn(500);
2171
2172
        $stateMachine->apply($configuration, $resource)->shouldNotBeCalled();
2173
        $objectManager->flush()->shouldNotBeCalled();
2174
2175
        $eventDispatcher->dispatchPostEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to dispatchPostEvent() misses some required arguments starting with $requestConfiguration.
Loading history...
2176
        $flashHelper->addSuccessFlash(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addSuccessFlash() misses a required argument $actionName.

This check looks for function calls that miss required arguments.

Loading history...
2177
        $flashHelper->addFlashFromEvent(Argument::any())->shouldNotBeCalled();
0 ignored issues
show
Bug introduced by
The call to addFlashFromEvent() misses a required argument $event.

This check looks for function calls that miss required arguments.

Loading history...
2178
2179
        $this
2180
            ->shouldThrow(new HttpException(500, 'Cannot approve this product.'))
2181
            ->during('applyStateMachineTransitionAction', [$request])
2182
        ;
2183
    }
2184
2185
    /**
2186
     * {@inheritdoc}
2187
     */
2188
    private function getViewComparingCallback(View $expectedView)
2189
    {
2190
        return function ($value) use ($expectedView) {
2191
            if (!$value instanceof View) {
0 ignored issues
show
Bug introduced by
The class FOS\RestBundle\View\View does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
2192
                return false;
2193
            }
2194
2195
            // Need to unwrap phpspec's Collaborators to ensure proper comparison.
2196
            $this->unwrapViewData($expectedView);
2197
            $this->nullifyDates($value);
2198
            $this->nullifyDates($expectedView);
2199
2200
            return
2201
                $expectedView->getStatusCode() === $value->getStatusCode() &&
2202
                $expectedView->getHeaders() === $value->getHeaders() &&
2203
                $expectedView->getEngine() === $value->getEngine() &&
2204
                $expectedView->getFormat() === $value->getFormat() &&
2205
                $expectedView->getData() === $value->getData() &&
2206
                $expectedView->getTemplate() === $value->getTemplate() &&
2207
                $expectedView->getTemplateVar() === $value->getTemplateVar()
2208
            ;
2209
        };
2210
    }
2211
2212
    /**
2213
     * @param View $view
2214
     */
2215
    private function unwrapViewData(View $view)
2216
    {
2217
        $view->setData($this->unwrapIfCollaborator($view->getData()));
2218
    }
2219
2220
    /**
2221
     * @param mixed $value
2222
     *
2223
     * @return mixed
2224
     */
2225
    private function unwrapIfCollaborator($value)
2226
    {
2227
        if (null === $value) {
2228
            return null;
2229
        }
2230
2231
        if ($value instanceof Collaborator) {
0 ignored issues
show
Bug introduced by
The class PhpSpec\Wrapper\Collaborator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
2232
            return $value->getWrappedObject();
2233
        }
2234
2235
        if (is_array($value)) {
2236
            foreach ($value as $key => $childValue) {
2237
                $value[$key] = $this->unwrapIfCollaborator($childValue);
2238
            }
2239
        }
2240
2241
        return $value;
2242
    }
2243
2244
    /**
2245
     * @param View $view
2246
     */
2247
    private function nullifyDates(View $view)
2248
    {
2249
        $headers = $view->getHeaders();
2250
        unset($headers['date']);
2251
        $view->setHeaders($headers);
2252
    }
2253
}
2254