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

FormSubmitHandlerSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace spec\FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request;
4
5
use FSi\Bundle\AdminBundle\Event\FormEvent;
6
use FSi\Bundle\AdminBundle\Event\FormEvents;
7
use FSi\Bundle\AdminBundle\Event\ListEvent;
8
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
9
use PhpSpec\ObjectBehavior;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\Form\FormInterface;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\Request;
14
use FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface;
15
16
class FormSubmitHandlerSpec extends ObjectBehavior
17
{
18
    function let(EventDispatcherInterface $eventDispatcher, FormEvent $event)
19
    {
20
        $event->hasResponse()->willReturn(false);
21
        $this->beConstructedWith($eventDispatcher);
22
    }
23
24
    function it_is_context_request_handler()
25
    {
26
        $this->shouldHaveType(HandlerInterface::class);
27
    }
28
29
    function it_throw_exception_for_non_form_event(ListEvent $listEvent, Request $request)
30
    {
31
        $this->shouldThrow(
32
            new RequestHandlerException(
33
                "FSi\\Bundle\\AdminBundle\\Admin\\CRUD\\Context\\Request\\FormSubmitHandler requires FormEvent"
34
            )
35
        )->during('handleRequest', [$listEvent, $request]);
36
    }
37
38
    function it_do_nothing_on_non_POST_request(FormEvent $event, Request $request)
39
    {
40
        $request->isMethod(Request::METHOD_POST)->willReturn(false);
41
42
        $this->handleRequest($event, $request)->shouldReturn(null);
43
    }
44
45
    function it_submit_form_on_POST_request(
46
        FormEvent $event,
47
        Request $request,
48
        EventDispatcherInterface $eventDispatcher,
49
        FormInterface $form
50
    ) {
51
        $request->isMethod(Request::METHOD_POST)->willReturn(true);
52
        $eventDispatcher->dispatch(FormEvents::FORM_REQUEST_PRE_SUBMIT, $event)
53
            ->shouldBeCalled();
54
55
        $event->getForm()->willReturn($form);
56
        $form->handleRequest($request)->shouldBeCalled();
57
58
        $eventDispatcher->dispatch(FormEvents::FORM_REQUEST_POST_SUBMIT, $event)
59
            ->shouldBeCalled();
60
61
        $this->handleRequest($event, $request)->shouldReturn(null);
62
    }
63
64
    function it_return_response_from_request_pre_submit_event(
65
        FormEvent $event,
66
        Request $request,
67
        EventDispatcherInterface $eventDispatcher,
68
        Response $response
69
    ) {
70
        $request->isMethod(Request::METHOD_POST)->willReturn(true);
71
        $eventDispatcher->dispatch(FormEvents::FORM_REQUEST_PRE_SUBMIT, $event)
72
            ->will(function() use ($event, $response) {
73
                $event->hasResponse()->willReturn(true);
74
                $event->getResponse()->willReturn($response);
75
            });
76
77
        $this->handleRequest($event, $request)
78
            ->shouldReturnAnInstanceOf(Response::class);
79
    }
80
81
    function it_return_response_from_request_post_submit_event(
82
        FormEvent $event,
83
        Request $request,
84
        EventDispatcherInterface $eventDispatcher,
85
        FormInterface $form,
86
        Response $response
87
    ) {
88
        $request->isMethod(Request::METHOD_POST)->willReturn(true);
89
        $eventDispatcher->dispatch(FormEvents::FORM_REQUEST_PRE_SUBMIT, $event)
90
            ->shouldBeCalled();
91
92
        $event->getForm()->willReturn($form);
93
        $form->handleRequest($request)->shouldBeCalled();
94
95
        $eventDispatcher->dispatch(FormEvents::FORM_REQUEST_POST_SUBMIT, $event)
96
            ->will(function() use ($event, $response) {
97
                $event->hasResponse()->willReturn(true);
98
                $event->getResponse()->willReturn($response);
99
            });
100
101
        $this->handleRequest($event, $request)
102
            ->shouldReturnAnInstanceOf(Response::class);
103
    }
104
}
105