|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request; |
|
4
|
|
|
|
|
5
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvents; |
|
6
|
|
|
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException; |
|
7
|
|
|
use PhpSpec\ObjectBehavior; |
|
8
|
|
|
use Prophecy\Argument; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
|
|
11
|
|
|
class FormValidRequestHandlerSpec extends ObjectBehavior |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher |
|
15
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
|
16
|
|
|
* @param \Symfony\Bundle\FrameworkBundle\Routing\Router $router |
|
17
|
|
|
*/ |
|
18
|
|
|
function let($eventDispatcher, $event, $router) |
|
19
|
|
|
{ |
|
20
|
|
|
$event->hasResponse()->willReturn(false); |
|
21
|
|
|
$this->beConstructedWith($eventDispatcher, $router); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
function it_is_context_request_handler() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->shouldHaveType('FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\ListEvent $listEvent |
|
31
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
32
|
|
|
*/ |
|
33
|
|
|
function it_throw_exception_for_non_form_event($listEvent, $request) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->shouldThrow( |
|
36
|
|
|
new RequestHandlerException( |
|
37
|
|
|
"FSi\\Bundle\\AdminBundle\\Admin\\CRUD\\Context\\Request\\FormValidRequestHandler require FormEvent" |
|
38
|
|
|
) |
|
39
|
|
|
)->during('handleRequest', array($listEvent, $request)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $formEvent |
|
44
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
45
|
|
|
*/ |
|
46
|
|
|
function it_throw_exception_for_non_redirectable_element($formEvent, $request) |
|
47
|
|
|
{ |
|
48
|
|
|
$formEvent->getElement()->willReturn(new \stdClass()); |
|
49
|
|
|
|
|
50
|
|
|
$this->shouldThrow( |
|
51
|
|
|
new RequestHandlerException( |
|
52
|
|
|
"FSi\\Bundle\\AdminBundle\\Admin\\CRUD\\Context\\Request\\FormValidRequestHandler require RedirectableElement" |
|
53
|
|
|
) |
|
54
|
|
|
)->during('handleRequest', array($formEvent, $request)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
|
59
|
|
|
* @param \FSi\Bundle\AdminBundle\Admin\CRUD\FormElement $element |
|
60
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
61
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher |
|
62
|
|
|
*/ |
|
63
|
|
|
function it_do_nothing_on_non_POST_request($event, $element, $request, $eventDispatcher) |
|
64
|
|
|
{ |
|
65
|
|
|
$request->isMethod('POST')->willReturn(false); |
|
66
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_RESPONSE_PRE_RENDER, $event) |
|
67
|
|
|
->shouldBeCalled(); |
|
68
|
|
|
$event->getElement()->willReturn($element); |
|
69
|
|
|
|
|
70
|
|
|
$this->handleRequest($event, $request)->shouldReturn(null); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
|
75
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
76
|
|
|
* @param \Symfony\Component\HttpFoundation\ParameterBag $queryParameterbag |
|
77
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher |
|
78
|
|
|
* @param \Symfony\Component\Form\Form $form |
|
79
|
|
|
* @param \FSi\Bundle\AdminBundle\Admin\CRUD\FormElement $element |
|
80
|
|
|
* @param \Symfony\Bundle\FrameworkBundle\Routing\Router $router |
|
81
|
|
|
*/ |
|
82
|
|
|
function it_handle_POST_request( |
|
83
|
|
|
$event, |
|
84
|
|
|
$request, |
|
85
|
|
|
$queryParameterbag, |
|
86
|
|
|
$eventDispatcher, |
|
87
|
|
|
$form, |
|
88
|
|
|
$element, |
|
89
|
|
|
$router |
|
90
|
|
|
) { |
|
91
|
|
|
$request->isMethod('POST')->willReturn(true); |
|
92
|
|
|
$request->query = $queryParameterbag; |
|
93
|
|
|
|
|
94
|
|
|
$event->getForm()->willReturn($form); |
|
95
|
|
|
$form->isValid()->willReturn(true); |
|
96
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_DATA_PRE_SAVE, $event) |
|
97
|
|
|
->shouldBeCalled(); |
|
98
|
|
|
|
|
99
|
|
|
$form->getData()->willReturn(new \stdClass()); |
|
100
|
|
|
$event->getElement()->willReturn($element); |
|
101
|
|
|
$element->save(Argument::type('stdClass'))->shouldBeCalled(); |
|
102
|
|
|
|
|
103
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_DATA_POST_SAVE, $event) |
|
104
|
|
|
->shouldBeCalled(); |
|
105
|
|
|
|
|
106
|
|
|
$element->getSuccessRoute()->willReturn('fsi_admin_list'); |
|
107
|
|
|
$element->getSuccessRouteParameters()->willReturn(array('element' => 'element_list_id')); |
|
108
|
|
|
$element->getId()->willReturn('element_form_id'); |
|
109
|
|
|
$queryParameterbag->has('redirect_uri')->willReturn(false); |
|
110
|
|
|
$router->generate('fsi_admin_list', array('element' => 'element_list_id'))->willReturn('/list/page'); |
|
111
|
|
|
|
|
112
|
|
|
$this->handleRequest($event, $request) |
|
113
|
|
|
->shouldReturnAnInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
|
118
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
119
|
|
|
* @param \Symfony\Component\HttpFoundation\ParameterBag $queryParameterbag |
|
120
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher |
|
121
|
|
|
* @param \Symfony\Component\Form\Form $form |
|
122
|
|
|
* @param \FSi\Bundle\AdminBundle\Admin\CRUD\FormElement $element |
|
123
|
|
|
*/ |
|
124
|
|
|
function it_return_redirect_response_with_redirect_uri_passed_by_request( |
|
125
|
|
|
$event, |
|
126
|
|
|
$request, |
|
127
|
|
|
$queryParameterbag, |
|
128
|
|
|
$eventDispatcher, |
|
129
|
|
|
$form, |
|
130
|
|
|
$element |
|
131
|
|
|
) { |
|
132
|
|
|
$request->isMethod('POST')->willReturn(true); |
|
133
|
|
|
$request->query = $queryParameterbag; |
|
134
|
|
|
|
|
135
|
|
|
$event->getForm()->willReturn($form); |
|
136
|
|
|
$form->isValid()->willReturn(true); |
|
137
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_DATA_PRE_SAVE, $event) |
|
138
|
|
|
->shouldBeCalled(); |
|
139
|
|
|
|
|
140
|
|
|
$form->getData()->willReturn(new \stdClass()); |
|
141
|
|
|
$event->getElement()->willReturn($element); |
|
142
|
|
|
$element->save(Argument::type('stdClass'))->shouldBeCalled(); |
|
143
|
|
|
|
|
144
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_DATA_POST_SAVE, $event) |
|
145
|
|
|
->shouldBeCalled(); |
|
146
|
|
|
|
|
147
|
|
|
$element->getSuccessRoute()->willReturn('fsi_admin_list'); |
|
148
|
|
|
$element->getSuccessRouteParameters()->willReturn(array('element' => 'element_list_id')); |
|
149
|
|
|
$element->getId()->willReturn('element_form_id'); |
|
150
|
|
|
$queryParameterbag->has('redirect_uri')->willReturn(true); |
|
151
|
|
|
$queryParameterbag->get('redirect_uri')->willReturn('some_redirect_uri'); |
|
152
|
|
|
|
|
153
|
|
|
$response = $this->handleRequest($event, $request); |
|
154
|
|
|
$response->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse'); |
|
155
|
|
|
$response->getTargetUrl()->shouldReturn('some_redirect_uri'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
|
160
|
|
|
* @param \FSi\Bundle\AdminBundle\Admin\CRUD\FormElement $element |
|
161
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
162
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher |
|
163
|
|
|
*/ |
|
164
|
|
|
function it_return_response_from_pre_render_event($event, $element, $request, $eventDispatcher) |
|
165
|
|
|
{ |
|
166
|
|
|
$request->isMethod('POST')->willReturn(false); |
|
167
|
|
|
|
|
168
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_RESPONSE_PRE_RENDER, $event) |
|
169
|
|
|
->will(function() use ($event) { |
|
170
|
|
|
$event->hasResponse()->willReturn(true); |
|
171
|
|
|
$event->getResponse()->willReturn(new Response()); |
|
172
|
|
|
}); |
|
173
|
|
|
$event->getElement()->willReturn($element); |
|
174
|
|
|
|
|
175
|
|
|
$this->handleRequest($event, $request) |
|
176
|
|
|
->shouldReturnAnInstanceOf('Symfony\Component\HttpFoundation\Response'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
|
181
|
|
|
* @param \FSi\Bundle\AdminBundle\Admin\CRUD\FormElement $element |
|
182
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
183
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher |
|
184
|
|
|
* @param \Symfony\Component\Form\Form $form |
|
185
|
|
|
*/ |
|
186
|
|
|
function it_return_response_from_pre_data_save_event($event, $element, $request, $eventDispatcher, $form) |
|
187
|
|
|
{ |
|
188
|
|
|
$request->isMethod('POST')->willReturn(true); |
|
189
|
|
|
|
|
190
|
|
|
$event->getForm()->willReturn($form); |
|
191
|
|
|
$form->isValid()->willReturn(true); |
|
192
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_DATA_PRE_SAVE, $event) |
|
193
|
|
|
->will(function() use ($event) { |
|
194
|
|
|
$event->hasResponse()->willReturn(true); |
|
195
|
|
|
$event->getResponse()->willReturn(new Response()); |
|
196
|
|
|
}); |
|
197
|
|
|
$event->getElement()->willReturn($element); |
|
198
|
|
|
|
|
199
|
|
|
$this->handleRequest($event, $request) |
|
200
|
|
|
->shouldReturnAnInstanceOf('Symfony\Component\HttpFoundation\Response'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
|
205
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
206
|
|
|
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher |
|
207
|
|
|
* @param \Symfony\Component\Form\Form $form |
|
208
|
|
|
* @param \FSi\Bundle\AdminBundle\Admin\CRUD\FormElement $element |
|
209
|
|
|
*/ |
|
210
|
|
|
function it_return_response_from_post_data_save_event($event, $request, $eventDispatcher, $form, $element) |
|
211
|
|
|
{ |
|
212
|
|
|
$request->isMethod('POST')->willReturn(true); |
|
213
|
|
|
|
|
214
|
|
|
$event->getForm()->willReturn($form); |
|
215
|
|
|
$form->isValid()->willReturn(true); |
|
216
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_DATA_PRE_SAVE, $event) |
|
217
|
|
|
->shouldBeCalled(); |
|
218
|
|
|
|
|
219
|
|
|
$form->getData()->willReturn(new \stdClass()); |
|
220
|
|
|
$event->getElement()->willReturn($element); |
|
221
|
|
|
$element->save(Argument::type('stdClass'))->shouldBeCalled(); |
|
222
|
|
|
|
|
223
|
|
|
$eventDispatcher->dispatch(FormEvents::FORM_DATA_POST_SAVE, $event) |
|
224
|
|
|
->will(function() use ($event) { |
|
225
|
|
|
$event->hasResponse()->willReturn(true); |
|
226
|
|
|
$event->getResponse()->willReturn(new Response()); |
|
227
|
|
|
}); |
|
228
|
|
|
|
|
229
|
|
|
$this->handleRequest($event, $request) |
|
230
|
|
|
->shouldReturnAnInstanceOf('Symfony\Component\HttpFoundation\Response'); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|