|
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\Collections\Collection; |
|
17
|
|
|
use PhpSpec\ObjectBehavior; |
|
18
|
|
|
use Prophecy\Argument; |
|
19
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface as ControllerEventDispatcherInterface; |
|
20
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration; |
|
21
|
|
|
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; |
|
22
|
|
|
use Sylius\Component\Resource\Metadata\MetadataInterface; |
|
23
|
|
|
use Sylius\Component\Resource\Model\ResourceInterface; |
|
24
|
|
|
use Sylius\Component\Resource\ResourceActions; |
|
25
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
final class EventDispatcherSpec extends ObjectBehavior |
|
31
|
|
|
{ |
|
32
|
|
|
function let(EventDispatcherInterface $eventDispatcher): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->beConstructedWith($eventDispatcher); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function it_implements_event_dispatcher_interface(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->shouldImplement(ControllerEventDispatcherInterface::class); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function it_dispatches_appropriate_event_for_a_resource( |
|
43
|
|
|
RequestConfiguration $requestConfiguration, |
|
44
|
|
|
MetadataInterface $metadata, |
|
45
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
46
|
|
|
ResourceInterface $resource |
|
47
|
|
|
): void { |
|
48
|
|
|
$requestConfiguration->getEvent()->willReturn(null); |
|
49
|
|
|
$requestConfiguration->getMetadata()->willReturn($metadata); |
|
50
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
|
51
|
|
|
$metadata->getName()->willReturn('product'); |
|
52
|
|
|
|
|
53
|
|
|
$eventDispatcher->dispatch('sylius.product.show', Argument::type(ResourceControllerEvent::class))->shouldBeCalled(); |
|
54
|
|
|
|
|
55
|
|
|
$this->dispatch(ResourceActions::SHOW, $requestConfiguration, $resource)->shouldHaveType(ResourceControllerEvent::class); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
function it_dispatches_appropriate_custom_event_for_a_resource( |
|
59
|
|
|
RequestConfiguration $requestConfiguration, |
|
60
|
|
|
MetadataInterface $metadata, |
|
61
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
62
|
|
|
ResourceInterface $resource |
|
63
|
|
|
): void { |
|
64
|
|
|
$requestConfiguration->getEvent()->willReturn('register'); |
|
65
|
|
|
$requestConfiguration->getMetadata()->willReturn($metadata); |
|
66
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
|
67
|
|
|
$metadata->getName()->willReturn('product'); |
|
68
|
|
|
|
|
69
|
|
|
$eventDispatcher->dispatch('sylius.product.register', Argument::type(ResourceControllerEvent::class))->shouldBeCalled(); |
|
70
|
|
|
|
|
71
|
|
|
$this->dispatch(ResourceActions::CREATE, $requestConfiguration, $resource)->shouldHaveType(ResourceControllerEvent::class); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function it_dispatches_event_for_a_collection_of_resources( |
|
75
|
|
|
RequestConfiguration $requestConfiguration, |
|
76
|
|
|
MetadataInterface $metadata, |
|
77
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
78
|
|
|
Collection $resources |
|
79
|
|
|
): void { |
|
80
|
|
|
$requestConfiguration->getEvent()->willReturn('register'); |
|
81
|
|
|
$requestConfiguration->getMetadata()->willReturn($metadata); |
|
82
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
|
83
|
|
|
$metadata->getName()->willReturn('product'); |
|
84
|
|
|
|
|
85
|
|
|
$eventDispatcher->dispatch('sylius.product.register', Argument::type(ResourceControllerEvent::class))->shouldBeCalled(); |
|
86
|
|
|
|
|
87
|
|
|
$this->dispatchMultiple(ResourceActions::CREATE, $requestConfiguration, $resources)->shouldHaveType(ResourceControllerEvent::class); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
function it_dispatches_appropriate_pre_event_for_a_resource( |
|
91
|
|
|
RequestConfiguration $requestConfiguration, |
|
92
|
|
|
MetadataInterface $metadata, |
|
93
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
94
|
|
|
ResourceInterface $resource |
|
95
|
|
|
): void { |
|
96
|
|
|
$requestConfiguration->getEvent()->willReturn(null); |
|
97
|
|
|
$requestConfiguration->getMetadata()->willReturn($metadata); |
|
98
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
|
99
|
|
|
$metadata->getName()->willReturn('product'); |
|
100
|
|
|
|
|
101
|
|
|
$eventDispatcher->dispatch('sylius.product.pre_create', Argument::type(ResourceControllerEvent::class))->shouldBeCalled(); |
|
102
|
|
|
|
|
103
|
|
|
$this->dispatchPreEvent(ResourceActions::CREATE, $requestConfiguration, $resource); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
function it_dispatches_appropriate_custom_pre_event_for_a_resource( |
|
107
|
|
|
RequestConfiguration $requestConfiguration, |
|
108
|
|
|
MetadataInterface $metadata, |
|
109
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
110
|
|
|
ResourceInterface $resource |
|
111
|
|
|
): void { |
|
112
|
|
|
$requestConfiguration->getEvent()->willReturn('register'); |
|
113
|
|
|
$requestConfiguration->getMetadata()->willReturn($metadata); |
|
114
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
|
115
|
|
|
$metadata->getName()->willReturn('product'); |
|
116
|
|
|
|
|
117
|
|
|
$eventDispatcher->dispatch('sylius.product.pre_register', Argument::type(ResourceControllerEvent::class))->shouldBeCalled(); |
|
118
|
|
|
|
|
119
|
|
|
$this->dispatchPreEvent(ResourceActions::CREATE, $requestConfiguration, $resource); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
function it_dispatches_appropriate_post_event_for_a_resource( |
|
123
|
|
|
RequestConfiguration $requestConfiguration, |
|
124
|
|
|
MetadataInterface $metadata, |
|
125
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
126
|
|
|
ResourceInterface $resource |
|
127
|
|
|
): void { |
|
128
|
|
|
$requestConfiguration->getEvent()->willReturn(null); |
|
129
|
|
|
$requestConfiguration->getMetadata()->willReturn($metadata); |
|
130
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
|
131
|
|
|
$metadata->getName()->willReturn('product'); |
|
132
|
|
|
|
|
133
|
|
|
$eventDispatcher->dispatch('sylius.product.post_create', Argument::type(ResourceControllerEvent::class))->shouldBeCalled(); |
|
134
|
|
|
|
|
135
|
|
|
$this->dispatchPostEvent(ResourceActions::CREATE, $requestConfiguration, $resource); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
function it_dispatches_appropriate_custom_post_event_for_a_resource( |
|
139
|
|
|
RequestConfiguration $requestConfiguration, |
|
140
|
|
|
MetadataInterface $metadata, |
|
141
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
142
|
|
|
ResourceInterface $resource |
|
143
|
|
|
): void { |
|
144
|
|
|
$requestConfiguration->getEvent()->willReturn('register'); |
|
145
|
|
|
$requestConfiguration->getMetadata()->willReturn($metadata); |
|
146
|
|
|
$metadata->getApplicationName()->willReturn('sylius'); |
|
147
|
|
|
$metadata->getName()->willReturn('product'); |
|
148
|
|
|
|
|
149
|
|
|
$eventDispatcher->dispatch('sylius.product.post_register', Argument::type(ResourceControllerEvent::class))->shouldBeCalled(); |
|
150
|
|
|
|
|
151
|
|
|
$this->dispatchPostEvent(ResourceActions::CREATE, $requestConfiguration, $resource)->shouldHaveType(ResourceControllerEvent::class); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|