Completed
Pull Request — 3.1 (#348)
by Piotr
01:23
created

BatchFormSubmitHandlerSpec   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 90
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 5 1
A it_is_context_request_handler() 0 4 1
A it_throw_exception_for_non_form_event() 0 8 1
A it_does_nothing_on_non_POST_request() 0 6 1
A it_submit_form_on_POST_request() 0 19 1
A it_return_response_from_request_pre_submit_event() 0 16 1
A it_return_response_from_request_post_submit_event() 0 23 1
1
<?php
2
3
namespace spec\FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request;
4
5
use FSi\Bundle\AdminBundle\Event\BatchEvents;
6
use FSi\Bundle\AdminBundle\Event\FormEvent;
7
use FSi\Bundle\AdminBundle\Event\ListEvent;
8
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
9
use PhpSpec\ObjectBehavior;
10
use Symfony\Component\EventDispatcher\EventDispatcher;
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 BatchFormSubmitHandlerSpec extends ObjectBehavior
17
{
18
    function let(EventDispatcher $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\\BatchFormSubmitHandler requires FormEvent"
34
            )
35
        )->during('handleRequest', [$listEvent, $request]);
36
    }
37
38
    function it_does_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
        EventDispatcher $eventDispatcher,
49
        FormInterface $form
50
    ) {
51
        $request->isMethod(Request::METHOD_POST)->willReturn(true);
52
53
        $eventDispatcher->dispatch(BatchEvents::BATCH_REQUEST_PRE_SUBMIT, $event)
54
            ->shouldBeCalled();
55
56
        $event->getForm()->willReturn($form);
57
        $form->handleRequest($request)->shouldBeCalled();
58
59
        $eventDispatcher->dispatch(BatchEvents::BATCH_REQUEST_POST_SUBMIT, $event)
60
            ->shouldBeCalled();
61
62
        $this->handleRequest($event, $request)->shouldReturn(null);
63
    }
64
65
    function it_return_response_from_request_pre_submit_event(
66
        FormEvent $event,
67
        Request $request,
68
        EventDispatcher $eventDispatcher
69
    ) {
70
        $request->isMethod(Request::METHOD_POST)->willReturn(true);
71
72
        $eventDispatcher->dispatch(BatchEvents::BATCH_REQUEST_PRE_SUBMIT, $event)
73
            ->will(function() use ($event) {
74
                $event->hasResponse()->willReturn(true);
75
                $event->getResponse()->willReturn(new Response());
76
            });
77
78
        $this->handleRequest($event, $request)
79
            ->shouldReturnAnInstanceOf(Response::class);
80
    }
81
82
    function it_return_response_from_request_post_submit_event(
83
        FormEvent $event,
84
        Request $request,
85
        EventDispatcher $eventDispatcher,
86
        FormInterface $form
87
    ) {
88
        $request->isMethod(Request::METHOD_POST)->willReturn(true);
89
90
        $eventDispatcher->dispatch(BatchEvents::BATCH_REQUEST_PRE_SUBMIT, $event)
91
            ->shouldBeCalled();
92
93
        $event->getForm()->willReturn($form);
94
        $form->handleRequest($request)->shouldBeCalled();
95
96
        $eventDispatcher->dispatch(BatchEvents::BATCH_REQUEST_POST_SUBMIT, $event)
97
            ->will(function() use ($event) {
98
                $event->hasResponse()->willReturn(true);
99
                $event->getResponse()->willReturn(new Response());
100
            });
101
102
        $this->handleRequest($event, $request)
103
            ->shouldReturnAnInstanceOf(Response::class);
104
    }
105
}
106