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\ResponseListener; |
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 stdClass; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
19
|
|
|
|
20
|
|
|
class ResponseListenerTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testSkipsEventIfResponseIsControllerResult(): void |
23
|
|
|
{ |
24
|
|
|
$mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class)->reveal(); |
25
|
|
|
|
26
|
|
|
$listener = new ResponseListener( |
27
|
|
|
$mediaTypeNegotiator, |
28
|
|
|
MediaTypes::TYPE_APPLICATION_XML |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$event = $this->prophesize(GetResponseForControllerResultEvent::class); |
32
|
|
|
$event->getControllerResult()->willReturn($this->prophesize(Response::class)); |
33
|
|
|
$event->setResponse(Argument::any())->shouldNotBeCalled(); |
34
|
|
|
|
35
|
|
|
$listener->onKernelView($event->reveal()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testWillFallbackToDefaultContentType(): void |
39
|
|
|
{ |
40
|
|
|
$serializer = $this->getMockBuilder([Serializer::class, MediaTypeHandler::class])->getMock(); |
41
|
|
|
$mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class); |
42
|
|
|
$mediaTypeNegotiator->negotiate(MediaTypes::TYPE_APPLICATION_JSON)->willThrow(new NonNegotiableMediaTypeException('application/json')); |
43
|
|
|
$mediaTypeNegotiator->negotiate(MediaTypes::TYPE_APPLICATION_XML)->shouldBeCalledTimes(1)->willReturn($serializer); |
44
|
|
|
|
45
|
|
|
$listener = new ResponseListener( |
46
|
|
|
$mediaTypeNegotiator->reveal(), |
47
|
|
|
MediaTypes::TYPE_APPLICATION_XML |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$event = $this->prophesize(GetResponseForControllerResultEvent::class); |
51
|
|
|
$event->getRequest()->willReturn(Request::create('/', Request::METHOD_GET, [], [], [], [ |
52
|
|
|
'HTTP_ACCEPT' => 'application/json;q=0.8', |
53
|
|
|
])); |
54
|
|
|
$event->getControllerResult()->willReturn(new stdClass()); |
55
|
|
|
$event->setResponse(Argument::type(Response::class))->shouldBeCalled(); |
56
|
|
|
|
57
|
|
|
$listener->onKernelView($event->reveal()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testWillFallBackToDefaultContentTypeOnAcceptedTypeAny(): void |
61
|
|
|
{ |
62
|
|
|
$serializer = $this->getMockBuilder([Serializer::class, MediaTypeHandler::class])->getMock(); |
63
|
|
|
$mediaTypeNegotiator = $this->prophesize(MediaTypeNegotiator::class); |
64
|
|
|
$mediaTypeNegotiator->negotiate(MediaTypes::TYPE_APPLICATION_XML)->shouldBeCalledTimes(1)->willReturn($serializer); |
65
|
|
|
|
66
|
|
|
$listener = new ResponseListener( |
67
|
|
|
$mediaTypeNegotiator->reveal(), |
68
|
|
|
MediaTypes::TYPE_APPLICATION_XML |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$event = $this->prophesize(GetResponseForControllerResultEvent::class); |
72
|
|
|
$event->getRequest()->willReturn(Request::create('/', Request::METHOD_GET, [], [], [], [ |
73
|
|
|
'HTTP_ACCEPT' => '*/*;q=0.8', |
74
|
|
|
])); |
75
|
|
|
$event->getResponse()->willReturn(null); |
76
|
|
|
$event->getControllerResult()->willReturn(new stdClass()); |
77
|
|
|
$event->setResponse(Argument::type(Response::class))->shouldBeCalled(); |
78
|
|
|
|
79
|
|
|
$listener->onKernelView($event->reveal()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|