Completed
Push — 3.1 ( 7e9d2a...4fdd2b )
by Piotr
13:37 queued 13:35
created

it_handles_POST_request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 9.264
c 0
b 0
f 0
cc 1
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace spec\FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request;
4
5
use FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface;
6
use FSi\Bundle\AdminBundle\Admin\CRUD\BatchElement;
7
use FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request\BatchFormValidRequestHandler;
8
use FSi\Bundle\AdminBundle\Admin\CRUD\DeleteElement;
9
use FSi\Bundle\AdminBundle\Admin\Element;
10
use FSi\Bundle\AdminBundle\Event\BatchEvent;
11
use FSi\Bundle\AdminBundle\Event\BatchEvents;
12
use FSi\Bundle\AdminBundle\Event\BatchPreApplyEvent;
13
use FSi\Bundle\AdminBundle\Event\FormEvent;
14
use FSi\Bundle\AdminBundle\Event\ListEvent;
15
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
16
use FSi\Bundle\AdminBundle\Message\FlashMessages;
17
use FSi\Component\DataIndexer\DataIndexerInterface;
18
use PhpSpec\ObjectBehavior;
19
use Prophecy\Argument;
20
use stdClass;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\HttpFoundation\ParameterBag;
24
use Symfony\Component\HttpFoundation\RedirectResponse;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
use Symfony\Component\Routing\RouterInterface;
28
29
class BatchFormValidRequestHandlerSpec extends ObjectBehavior
30
{
31
    function let(
32
        EventDispatcherInterface $eventDispatcher,
33
        RouterInterface $router,
34
        FlashMessages $flashMessage,
35
        Request $request,
36
        ParameterBag $requestParameterbag,
37
        ParameterBag $queryParameterbag,
38
        FormInterface $form,
39
        BatchElement $element,
40
        FormEvent $event
41
    ) {
42
        $requestParameterbag->get('indexes', [])->willReturn(['index']);
43
        $request->request = $requestParameterbag;
44
        $request->query = $queryParameterbag;
45
        $request->isMethod(Request::METHOD_POST)->willReturn(true);
46
        $event->getForm()->willReturn($form);
47
        $event->hasResponse()->willReturn(false);
48
        $event->getElement()->willReturn($element);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<FSi\Bundle\AdminBundle\Admin\Element>.

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...
49
        $form->isValid()->willReturn(true);
50
51
        $this->beConstructedWith($eventDispatcher, $router, $flashMessage);
52
    }
53
54
    function it_is_context_request_handler(
55
    ) {
56
        $this->shouldHaveType(HandlerInterface::class);
57
    }
58
59
    function it_throw_exception_for_non_form_event(ListEvent $listEvent, Request $request)
60
    {
61
        $this->shouldThrow(new RequestHandlerException(
62
            sprintf("%s requires FormEvent", BatchFormValidRequestHandler::class)
63
        ))->during('handleRequest', [$listEvent, $request]);
64
    }
65
66
    function it_throw_exception_for_non_redirectable_element(
67
        FormEvent $formEvent,
68
        Request $request,
69
        Element $genericElement
70
    ) {
71
        $formEvent->getElement()->willReturn($genericElement);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<FSi\Bundle\AdminBundle\Admin\Element>.

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...
72
73
        $this->shouldThrow(new RequestHandlerException(
74
            sprintf("%s requires RedirectableElement", BatchFormValidRequestHandler::class)
75
        ))->during('handleRequest', [$formEvent, $request]);
76
    }
77
78
    function it_handles_POST_request(
79
        FormEvent $event,
80
        Request $request,
81
        ParameterBag $queryParameterbag,
82
        EventDispatcherInterface $eventDispatcher,
83
        FormInterface $form,
84
        BatchElement $element,
85
        DataIndexerInterface $dataIndexer,
86
        RouterInterface $router,
87
        stdClass $object
88
    ) {
89
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_PRE_APPLY, $event)
90
            ->shouldBeCalled();
91
        $eventDispatcher->dispatch(
92
            BatchEvents::BATCH_OBJECT_PRE_APPLY,
93
            Argument::type(BatchPreApplyEvent::class)
94
        )->shouldBeCalled();
95
96
        $form->getData()->willReturn($object);
97
        $element->apply($object)->shouldBeCalled();
98
99
        $eventDispatcher->dispatch(
100
            BatchEvents::BATCH_OBJECT_POST_APPLY,
101
            Argument::type(BatchEvent::class)
102
        )->shouldBeCalled();
103
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_POST_APPLY, $event)
104
            ->shouldBeCalled();
105
106
        $element->getSuccessRoute()->willReturn('fsi_admin_list');
107
        $element->getSuccessRouteParameters()->willReturn(['element' => 'element_list_id']);
108
        $element->getId()->willReturn('element_form_id');
109
        $element->getDataIndexer()->willReturn($dataIndexer);
110
111
        $dataIndexer->getData('index')->willReturn($object);
112
113
        $queryParameterbag->has('redirect_uri')->willReturn(false);
114
        $router->generate('fsi_admin_list', ['element' => 'element_list_id'])->willReturn('/list/page');
115
116
        $this->handleRequest($event, $request)
117
            ->shouldReturnAnInstanceOf(RedirectResponse::class);
118
    }
119
120
    function it_returns_redirect_response_with_redirect_uri_passed_by_request(
121
        FormEvent $event,
122
        Request $request,
123
        ParameterBag $queryParameterbag,
124
        EventDispatcherInterface $eventDispatcher,
125
        FormInterface $form,
126
        BatchElement $element,
127
        DataIndexerInterface $dataIndexer,
128
        stdClass $object
129
    ) {
130
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_PRE_APPLY, $event)
131
            ->shouldBeCalled();
132
        $eventDispatcher->dispatch(
133
            BatchEvents::BATCH_OBJECT_PRE_APPLY,
134
            Argument::type(BatchPreApplyEvent::class)
135
        )->shouldBeCalled();
136
137
        $form->getData()->willReturn($object);
138
        $element->apply($object)->shouldBeCalled();
139
140
        $eventDispatcher->dispatch(
141
            BatchEvents::BATCH_OBJECT_POST_APPLY,
142
            Argument::type(BatchEvent::class)
143
        )->shouldBeCalled();
144
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_POST_APPLY, $event)
145
            ->shouldBeCalled();
146
147
        $element->getSuccessRoute()->willReturn('fsi_admin_list');
148
        $element->getSuccessRouteParameters()->willReturn(['element' => 'element_list_id']);
149
        $element->getId()->willReturn('element_form_id');
150
        $element->getDataIndexer()->willReturn($dataIndexer);
151
152
        $dataIndexer->getData('index')->willReturn($object);
153
154
        $queryParameterbag->has('redirect_uri')->willReturn(true);
155
        $queryParameterbag->get('redirect_uri')->willReturn('some_redirect_uri');
156
157
        $response = $this->handleRequest($event, $request);
158
        $response->shouldBeAnInstanceOf(RedirectResponse::class);
159
        $response->getTargetUrl()->shouldReturn('some_redirect_uri');
160
    }
161
162
    function it_return_response_from_pre_apply_event(
163
        FormEvent $event,
164
        Request $request,
165
        EventDispatcherInterface $eventDispatcher
166
    ) {
167
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_PRE_APPLY, $event)
168
            ->will(function() use ($event) {
169
                $event->hasResponse()->willReturn(true);
170
                $event->getResponse()->willReturn(new Response());
171
            });
172
173
        $this->handleRequest($event, $request)
174
            ->shouldReturnAnInstanceOf(Response::class);
175
    }
176
177
    function it_return_response_from_post_apply_event(
178
        FormEvent $event,
179
        Request $request,
180
        EventDispatcherInterface $eventDispatcher,
181
        FormInterface $form,
182
        BatchElement $element,
183
        DataIndexerInterface $dataIndexer,
184
        stdClass $object
185
    ) {
186
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_PRE_APPLY, $event)
187
            ->shouldBeCalled();
188
        $eventDispatcher->dispatch(
189
            BatchEvents::BATCH_OBJECT_PRE_APPLY,
190
            Argument::type(BatchPreApplyEvent::class)
191
        )->shouldBeCalled();
192
193
        $form->getData()->willReturn($object);
194
        $element->getDataIndexer()->willReturn($dataIndexer);
195
        $dataIndexer->getData('index')->willReturn($object);
196
197
        $element->apply($object)->shouldBeCalled();
198
199
        $eventDispatcher->dispatch(
200
            BatchEvents::BATCH_OBJECT_POST_APPLY,
201
            Argument::type(BatchEvent::class)
202
        )->shouldBeCalled();
203
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_POST_APPLY, $event)
204
            ->will(function() use ($event) {
205
                $event->hasResponse()->willReturn(true);
206
                $event->getResponse()->willReturn(new Response());
207
            });
208
209
        $this->handleRequest($event, $request)
210
            ->shouldReturnAnInstanceOf(Response::class);
211
    }
212
213
    public function it_displays_warning_when_no_elements_sent(
214
        FormEvent $event,
215
        Request $request,
216
        ParameterBag $requestParameterbag,
217
        DeleteElement $deleteEelement,
218
        EventDispatcherInterface $eventDispatcher,
219
        FlashMessages $flashMessage,
220
        stdClass $object
221
    ) {
222
        $requestParameterbag->get('indexes', [])->willReturn([]);
223
        $event->getElement()->willReturn($deleteEelement);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<FSi\Bundle\AdminBundle\Admin\Element>.

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...
224
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_PRE_APPLY, $event)
225
            ->shouldBeCalled();
226
227
        $deleteEelement->getOption('allow_delete')->willReturn(true);
228
        $deleteEelement->hasOption('allow_delete')->willReturn(true);
229
        $deleteEelement->apply($object)->shouldNotBeCalled();
230
        $flashMessage->warning(Argument::type('string'))->shouldBeCalled();
231
232
        $eventDispatcher->dispatch(BatchEvents::BATCH_OBJECTS_POST_APPLY, $event)
233
            ->will(function() use ($event) {
234
                $event->hasResponse()->willReturn(true);
235
                $event->getResponse()->willReturn(new Response());
236
            });
237
238
        $this->handleRequest($event, $request);
239
    }
240
}
241