Completed
Push — master ( 54ca24...92958f )
by John
12s
created

willNotHandleIfNoDocumentUriInAttributes()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 33
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\EventListener;
10
11
use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta;
12
use KleijnWeb\SwaggerBundle\EventListener\Response\ResponseFactory;
13
use KleijnWeb\SwaggerBundle\EventListener\ViewListener;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
17
18
/**
19
 * @author John Kleijn <[email protected]>
20
 */
21
class ViewListenerTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @test
25
     */
26
    public function willNotHandleIfNoDocumentUriInAttributes()
27
    {
28
        $eventMock = $this
29
            ->getMockBuilder(GetResponseForControllerResultEvent::class)
30
            ->disableOriginalConstructor()
31
            ->getMock();
32
33
        $eventMock
34
            ->expects($this->any())
35
            ->method('getRequest')
36
            ->willReturn(new Request());
37
38
        $eventMock
39
            ->expects($this->never())
40
            ->method('getControllerResult');
41
42
        $eventMock
43
            ->expects($this->never())
44
            ->method('setResponse');
45
46
        $factoryMock = $this
47
            ->getMockBuilder(ResponseFactory::class)
48
            ->disableOriginalConstructor()
49
            ->getMock();
50
        $factoryMock
51
            ->expects($this->never())
52
            ->method('createResponse');
53
54
        /** @var ResponseFactory $factoryMock */
55
        $listener = new ViewListener($factoryMock);
56
        /** @var GetResponseForControllerResultEvent $eventMock */
57
        $listener->onKernelView($eventMock);
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function willSetResponseFromFactoryOnEvent()
64
    {
65
        $attributes = [RequestMeta::ATTRIBUTE_URI => '/foo/bar'];
66
        $request = new Request($query = [], $request = [], $attributes);
67
        $response = new Response();
68
        $result = [uniqid()];
69
70
        $eventMock = $this
71
            ->getMockBuilder(GetResponseForControllerResultEvent::class)
72
            ->disableOriginalConstructor()
73
            ->getMock();
74
        $eventMock
75
            ->expects($this->once())
76
            ->method('getRequest')
77
            ->willReturn($request);
78
        $eventMock
79
            ->expects($this->once())
80
            ->method('getControllerResult')
81
            ->willReturn($result);
82
        $eventMock
83
            ->expects($this->once())
84
            ->method('setResponse')
85
            ->willReturn($response);
86
87
        $factoryMock = $this
88
            ->getMockBuilder(ResponseFactory::class)
89
            ->disableOriginalConstructor()
90
            ->getMock();
91
        $factoryMock
92
            ->expects($this->once())
93
            ->method('createResponse')
94
            ->with($request, $result)
95
            ->willReturn($response);
96
97
        /** @var ResponseFactory $factoryMock */
98
        $listener = new ViewListener($factoryMock);
99
        /** @var GetResponseForControllerResultEvent $eventMock */
100
        $listener->onKernelView($eventMock);
101
    }
102
}
103