Passed
Pull Request — master (#17)
by Theo
01:41
created

CalendarControllerTest::getEventDispatcherMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CalendarBundle\Tests\Controller;
6
7
use CalendarBundle\CalendarEvents;
8
use CalendarBundle\Controller\CalendarController;
9
use CalendarBundle\Entity\Event;
10
use CalendarBundle\Event\CalendarEvent;
11
use CalendarBundle\Serializer\SerializerInterface;
12
use PHPUnit\Framework\TestCase;
13
use Prophecy\Argument;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
18
19
class CalendarControllerTest extends TestCase
20
{
21
    private $calendarEvent;
22
    private $event;
23
    private $eventDispatcher;
24
    private $request;
25
    private $serializer;
26
    private $controller;
27
28
    public function setUp(): void
29
    {
30
        $this->calendarEvent = $this->prophesize(CalendarEvent::class);
31
        $this->event = $this->prophesize(Event::class);
32
        $this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
33
        $this->request = $this->prophesize(Request::class);
34
        $this->serializer = $this->prophesize(SerializerInterface::class);
35
36
        $this->controller = new CalendarController(
37
            $this->eventDispatcher->reveal(),
38
            $this->serializer->reveal()
39
        );
40
    }
41
42
    public function testItProvidesAnEventsFeedForACalendar()
43
    {
44
        $this->request->get('start')->willReturn('2016-03-01');
45
        $this->request->get('end')->willReturn('2016-03-19 15:11:00');
46
        $this->request->get('filters', '{}')->willReturn('{}');
47
48
        $this->calendarEvent->getEvents()->willReturn([$this->event]);
49
50
        $dispatcher = $this->getEventDispatcherMock();
51
        if ($dispatcher instanceof ContractsEventDispatcherInterface) {
52
            $this->eventDispatcher
53
                ->dispatch(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA)
54
                ->willReturn($this->calendarEvent)
55
            ;
56
        } else {
57
            $this->eventDispatcher
58
                ->dispatch(CalendarEvents::SET_DATA, Argument::type(CalendarEvent::class))
59
                ->willReturn($this->calendarEvent)
60
            ;
61
        }
62
63
        $data = json_encode([
64
            [
65
                'title' => 'Birthday!',
66
                'start' => '2016-03-01',
67
                'allDay' => true,
68
            ],
69
            [
70
                'title' => 'Flight to somewhere sunny',
71
                'start' => '2016-03-12T08:55:00Z',
72
                'allDay' => false,
73
                'end' => '2016-03-12T11:50:00Z',
74
            ],
75
        ]);
76
77
        $this->serializer->serialize([$this->event])
78
            ->willReturn($data)
79
        ;
80
81
        $response = $this->controller->loadAction($this->request->reveal());
82
83
        $this->assertInstanceOf(Response::class, $response);
84
85
        $this->assertEquals('application/json', $response->headers->get('Content-Type'));
86
        $this->assertEquals($data, $response->getContent());
87
        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
88
    }
89
90
    public function testItNotFindAnyEvents()
91
    {
92
        $this->request->get('start')->willReturn('2016-03-01');
93
        $this->request->get('end')->willReturn('2016-03-19 15:11:00');
94
        $this->request->get('filters', '{}')->willReturn('{}');
95
96
        $this->calendarEvent->getEvents()->willReturn([$this->event]);
97
        $dispatcher = $this->getEventDispatcherMock();
98
        if ($dispatcher instanceof ContractsEventDispatcherInterface) {
99
            $this->eventDispatcher
100
                ->dispatch(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA)
101
                ->willReturn($this->calendarEvent)
102
            ;
103
        } else {
104
            $this->eventDispatcher
105
                ->dispatch(CalendarEvents::SET_DATA, Argument::type(CalendarEvent::class))
106
                ->willReturn($this->calendarEvent)
107
            ;
108
        }
109
110
        $data = '';
111
112
        $this->serializer->serialize([$this->event])
113
            ->willReturn($data)
114
        ;
115
116
        $response = $this->controller->loadAction($this->request->reveal());
117
118
        $this->assertInstanceOf(Response::class, $response);
119
120
        $this->assertEquals('application/json', $response->headers->get('Content-Type'));
121
        $this->assertEquals($data, $response->getContent());
122
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
123
    }
124
125
    private function getEventDispatcherMock()
126
    {
127
        return $this->getMockBuilder(EventDispatcherInterface::class)
128
            ->disableOriginalConstructor()
129
            ->getMock();
130
    }
131
}
132