CalendarControllerTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 5
eloc 55
c 5
b 2
f 0
dl 0
loc 95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testItProvidesAnEventsFeedForACalendar() 0 35 1
A testItNotFindAnyEvents() 0 22 1
A eventDispatcherWithBC() 0 11 2
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\Component\HttpKernel\Kernel;
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
        $this->eventDispatcherWithBC(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA);
51
52
        $data = json_encode([
53
            [
54
                'title' => 'Birthday!',
55
                'start' => '2016-03-01',
56
                'allDay' => true,
57
            ],
58
            [
59
                'title' => 'Flight to somewhere sunny',
60
                'start' => '2016-03-12T08:55:00Z',
61
                'allDay' => false,
62
                'end' => '2016-03-12T11:50:00Z',
63
            ],
64
        ]);
65
66
        $this->serializer->serialize([$this->event])
67
            ->willReturn($data)
68
        ;
69
70
        $response = $this->controller->loadAction($this->request->reveal());
71
72
        $this->assertInstanceOf(Response::class, $response);
73
74
        $this->assertEquals('application/json', $response->headers->get('Content-Type'));
75
        $this->assertEquals($data, $response->getContent());
76
        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
77
    }
78
79
    public function testItNotFindAnyEvents()
80
    {
81
        $this->request->get('start')->willReturn('2016-03-01');
82
        $this->request->get('end')->willReturn('2016-03-19 15:11:00');
83
        $this->request->get('filters', '{}')->willReturn('{}');
84
85
        $this->calendarEvent->getEvents()->willReturn([$this->event]);
86
        $this->eventDispatcherWithBC(Argument::type(CalendarEvent::class), CalendarEvents::SET_DATA);
87
88
        $data = '';
89
90
        $this->serializer->serialize([$this->event])
91
            ->willReturn($data)
92
        ;
93
94
        $response = $this->controller->loadAction($this->request->reveal());
95
96
        $this->assertInstanceOf(Response::class, $response);
97
98
        $this->assertEquals('application/json', $response->headers->get('Content-Type'));
99
        $this->assertEquals($data, $response->getContent());
100
        $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode());
101
    }
102
103
    private function eventDispatcherWithBC($event, ?string $eventName = null)
104
    {
105
        if (Kernel::VERSION_ID >= 40300) {
106
            $this->eventDispatcher
107
                ->dispatch($event, $eventName)
108
                ->willReturn($this->calendarEvent)
109
            ;
110
        }
111
        $this->eventDispatcher
112
            ->dispatch($eventName, $event)
113
            ->willReturn($this->calendarEvent)
114
        ;
115
    }
116
}
117