Completed
Pull Request — 3.1 (#347)
by Łukasz
16:49 queued 11:36
created

DataSourceBindParametersHandlerSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 5 1
A it_is_context_request_handler() 0 4 1
A it_throws_exception_for_non_list_event() 0 8 1
A it_binds_request_to_datasource_and_dispatch_events() 0 15 1
A it_returns_response_from_pre_datasource_bind_parameters_event() 0 15 1
A it_returns_response_from_post_datasource_bind_parameters_event() 0 21 1
1
<?php
2
3
namespace spec\FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request;
4
5
use FSi\Bundle\AdminBundle\Event\AdminEvent;
6
use FSi\Bundle\AdminBundle\Event\ListEvent;
7
use FSi\Bundle\AdminBundle\Event\ListEvents;
8
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
9
use FSi\Component\DataSource\DataSourceInterface;
10
use PhpSpec\ObjectBehavior;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\Request;
14
use FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface;
15
16
class DataSourceBindParametersHandlerSpec extends ObjectBehavior
17
{
18
    function let(EventDispatcherInterface $eventDispatcher, ListEvent $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_throws_exception_for_non_list_event(AdminEvent $event, Request $request)
30
    {
31
        $this->shouldThrow(
32
            new RequestHandlerException(
33
                "FSi\\Bundle\\AdminBundle\\Admin\\CRUD\\Context\\Request\\DataSourceBindParametersHandler requires ListEvent"
34
            )
35
        )->during('handleRequest', [$event, $request]);
36
    }
37
38
    function it_binds_request_to_datasource_and_dispatch_events(
39
        ListEvent $event,
40
        DataSourceInterface $dataSource,
41
        Request $request,
42
        EventDispatcherInterface $eventDispatcher
43
    ) {
44
        $eventDispatcher->dispatch(ListEvents::LIST_DATASOURCE_REQUEST_PRE_BIND, $event)->shouldBeCalled();
45
46
        $event->getDataSource()->willReturn($dataSource);
47
        $dataSource->bindParameters($request)->shouldBeCalled();
48
49
        $eventDispatcher->dispatch(ListEvents::LIST_DATASOURCE_REQUEST_POST_BIND, $event)->shouldBeCalled();
50
51
        $this->handleRequest($event, $request)->shouldReturn(null);
52
    }
53
54
    function it_returns_response_from_pre_datasource_bind_parameters_event(
55
        ListEvent $event,
56
        Request $request,
57
        EventDispatcherInterface $eventDispatcher,
58
        Response $response
59
    ) {
60
        $eventDispatcher->dispatch(ListEvents::LIST_DATASOURCE_REQUEST_PRE_BIND, $event)
61
            ->will(function() use ($event, $response) {
62
                $event->hasResponse()->willReturn(true);
63
                $event->getResponse()->willReturn($response);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component\HttpFoundation\Response>.

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...
64
            });
65
66
        $this->handleRequest($event, $request)
67
            ->shouldReturnAnInstanceOf(Response::class);
68
    }
69
70
    function it_returns_response_from_post_datasource_bind_parameters_event(
71
        ListEvent $event,
72
        Request $request,
73
        EventDispatcherInterface $eventDispatcher,
74
        DataSourceInterface $dataSource,
75
        Response $response
76
    ) {
77
        $eventDispatcher->dispatch(ListEvents::LIST_DATASOURCE_REQUEST_PRE_BIND, $event)->shouldBeCalled();
78
79
        $event->getDataSource()->willReturn($dataSource);
80
        $dataSource->bindParameters($request)->shouldBecalled();
81
82
        $eventDispatcher->dispatch(ListEvents::LIST_DATASOURCE_REQUEST_POST_BIND, $event)
83
            ->will(function() use ($event, $response) {
84
                $event->hasResponse()->willReturn(true);
85
                $event->getResponse()->willReturn($response);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Symfony\Component\HttpFoundation\Response>.

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...
86
            });
87
88
        $this->handleRequest($event, $request)
89
            ->shouldReturnAnInstanceOf(Response::class);
90
    }
91
}
92