GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1)
by Pascal
07:37
created

testSkipsEventIfResponseIsPresent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saikootau\ApiBundle\Tests\Event\Listener;
6
7
use PHPUnit\Framework\TestCase;
8
use Prophecy\Argument;
9
use Saikootau\ApiBundle\Event\Listener\ExceptionResponseListener;
10
use Saikootau\ApiBundle\MediaType\Exception\NonNegotiableMediaTypeException;
11
use Saikootau\ApiBundle\MediaType\MediaTypeHandler;
12
use Saikootau\ApiBundle\MediaType\MediaTypeNegotiator;
13
use Saikootau\ApiBundle\MediaType\MediaTypes;
14
use Saikootau\ApiBundle\Serializer\Serializer;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
18
use Exception;
19
20
class ExceptionResponseListenerTest extends TestCase
21
{
22
    public function testSkipsEventIfNoRequestIsGiven(): void
23
    {
24
        $serializer = $this->getMockBuilder([Serializer::class, MediaTypeHandler::class])->getMock();
25
        $mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class);
26
        $mediaTypeNegotiator->negotiate(Argument::any())->willReturn($serializer);
27
28
        $listener = new ExceptionResponseListener(
29
            $mediaTypeNegotiator->reveal(),
30
            MediaTypes::TYPE_APPLICATION_XML
31
        );
32
33
        $event = $this->prophesize(GetResponseForExceptionEvent::class);
34
        $event->getRequest()->willReturn(null);
35
        $event->getResponse()->willReturn(null);
36
        $event->setResponse(Argument::any())->shouldNotBeCalled();
37
38
        $listener->onKernelException($event->reveal());
39
    }
40
41
    public function testSkipsEventIfResponseIsPresent(): void
42
    {
43
        $serializer = $this->getMockBuilder([Serializer::class, MediaTypeHandler::class])->getMock();
44
        $mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class);
45
        $mediaTypeNegotiator->negotiate(Argument::any())->willReturn($serializer);
46
47
        $listener = new ExceptionResponseListener(
48
            $mediaTypeNegotiator->reveal(),
49
            MediaTypes::TYPE_APPLICATION_XML
50
        );
51
52
        $event = $this->prophesize(GetResponseForExceptionEvent::class);
53
        $event->getRequest()->willReturn($this->prophesize(Request::class));
54
        $event->getResponse()->willReturn($this->prophesize(Response::class));
55
        $event->setResponse(Argument::any())->shouldNotBeCalled();
56
57
        $listener->onKernelException($event->reveal());
58
    }
59
60
    public function testConvertsExceptionToResponseIfNoResponseIsGiven(): void
61
    {
62
        $serializer = $this->getMockBuilder([Serializer::class, MediaTypeHandler::class])->getMock();
63
        $mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class);
64
        $mediaTypeNegotiator->negotiate(Argument::any())->willReturn($serializer);
65
66
        $listener = new ExceptionResponseListener(
67
            $mediaTypeNegotiator->reveal(),
68
            MediaTypes::TYPE_APPLICATION_XML
69
        );
70
71
        $event = $this->prophesize(GetResponseForExceptionEvent::class);
72
        $event->getRequest()->willReturn(Request::create('/', Request::METHOD_GET, [], [], [], [
73
            'HTTP_ACCEPT' => 'application/xml;q=0.8',
74
        ]));
75
        $event->getResponse()->willReturn(null);
76
        $event->getException()->willReturn($this->prophesize(Exception::class));
77
        $event->setResponse(Argument::type(Response::class))->shouldBeCalled();
78
79
        $listener->onKernelException($event->reveal());
80
    }
81
82
    public function testWillFallbackToDefaultContentType(): void
83
    {
84
        $serializer = $this->getMockBuilder([Serializer::class, MediaTypeHandler::class])->getMock();
85
        $mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class);
86
        $mediaTypeNegotiator->negotiate(MediaTypes::TYPE_APPLICATION_JSON)->willThrow(new NonNegotiableMediaTypeException('application/json'));
87
        $mediaTypeNegotiator->negotiate(MediaTypes::TYPE_APPLICATION_XML)->shouldBeCalledTimes(1)->willReturn($serializer);
88
89
        $listener = new ExceptionResponseListener(
90
            $mediaTypeNegotiator->reveal(),
91
            MediaTypes::TYPE_APPLICATION_XML
92
        );
93
94
        $event = $this->prophesize(GetResponseForExceptionEvent::class);
95
        $event->getRequest()->willReturn(Request::create('/', Request::METHOD_GET, [], [], [], [
96
            'HTTP_ACCEPT' => 'application/json;q=0.8',
97
        ]));
98
        $event->getResponse()->willReturn(null);
99
        $event->getException()->willReturn($this->prophesize(Exception::class));
100
        $event->setResponse(Argument::type(Response::class))->shouldBeCalled();
101
102
        $listener->onKernelException($event->reveal());
103
    }
104
105
    public function testWillFallBackToDefaultContentTypeOnAcceptedTypeAny(): void
106
    {
107
        $serializer = $this->getMockBuilder([Serializer::class, MediaTypeHandler::class])->getMock();
108
        $mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class);
109
        $mediaTypeNegotiator->negotiate(MediaTypes::TYPE_APPLICATION_XML)->shouldBeCalledTimes(1)->willReturn($serializer);
110
111
        $listener = new ExceptionResponseListener(
112
            $mediaTypeNegotiator->reveal(),
113
            MediaTypes::TYPE_APPLICATION_XML
114
        );
115
116
        $event = $this->prophesize(GetResponseForExceptionEvent::class);
117
        $event->getRequest()->willReturn(Request::create('/', Request::METHOD_GET, [], [], [], [
118
            'HTTP_ACCEPT' => '*/*;q=0.8',
119
        ]));
120
        $event->getResponse()->willReturn(null);
121
        $event->getException()->willReturn($this->prophesize(Exception::class));
122
        $event->setResponse(Argument::type(Response::class))->shouldBeCalled();
123
124
        $listener->onKernelException($event->reveal());
125
    }
126
}
127