Completed
Push — master ( ad3b69...02c092 )
by John
09:08 queued 05:48
created

willNotHandleIfNotMasterRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
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\SwaggerBundle\EventListener\Request\RequestMeta;
12
use KleijnWeb\SwaggerBundle\EventListener\Request\RequestProcessor;
13
use KleijnWeb\SwaggerBundle\EventListener\RequestListener;
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
17
18
/**
19
 * @author John Kleijn <[email protected]>
20
 */
21
class RequestListenerTest extends \PHPUnit_Framework_TestCase
22
{
23
    const DOCUMENT_PATH = '/hi.yaml';
24
    const SWAGGER_PATH = '/a/b/{hello}';
25
26
    /**
27
     * @var \PHPUnit_Framework_MockObject_MockObject
28
     */
29
    private $processorMock;
30
31
    /**
32
     * @var RequestListener
33
     */
34
    private $listener;
35
36
    /**
37
     * @var \PHPUnit_Framework_MockObject_MockObject
38
     */
39
    private $eventMock;
40
41
    /**
42
     * @var GetResponseForExceptionEvent
43
     */
44
    private $event;
45
46
    /**
47
     * Create mocks
48
     */
49
    protected function setUp()
50
    {
51
        $this->eventMock = $this->event = $this
52
            ->getMockBuilder(GetResponseForExceptionEvent::class)
53
            ->disableOriginalConstructor()
54
            ->getMock();
55
56
        /** @var RequestProcessor $processor */
57
        $this->processorMock = $processor = $this
58
            ->getMockBuilder(RequestProcessor::class)
59
            ->disableOriginalConstructor()
60
            ->getMock();
61
62
        $this->listener = new RequestListener($processor);
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function willNotHandleIfNotMasterRequest()
69
    {
70
        $this->eventMock
71
            ->expects($this->once())
72
            ->method('isMasterRequest')
73
            ->willReturn(false);
74
75
        $this->processorMock
76
            ->expects($this->never())
77
            ->method('process');
78
79
        $this->listener->onKernelRequest($this->event);
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function willNotHandleIfNoDocumentUriInAttributes()
86
    {
87
        $this->eventMock
88
            ->expects($this->once())
89
            ->method('isMasterRequest')
90
            ->willReturn(true);
91
92
        $this->eventMock
93
            ->expects($this->once())
94
            ->method('getRequest')
95
            ->willReturn(
96
                new Request()
97
            );
98
99
        $this->processorMock
100
            ->expects($this->never())
101
            ->method('process');
102
103
        $this->listener->onKernelRequest($this->event);
104
    }
105
106
    /**
107
     * @test
108
     */
109
    public function willInvokeProcessor()
110
    {
111
        $this->eventMock
112
            ->expects($this->once())
113
            ->method('isMasterRequest')
114
            ->willReturn(true);
115
116
        $request = new class extends Request
117
        {
118
            /**
119
             */
120
            function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
121
            {
122
                parent::__construct();
123
                $this->attributes = new ParameterBag([RequestMeta::ATTRIBUTE_URI => '/uri']);
124
            }
125
        };
126
127
        $this->eventMock
128
            ->expects($this->once())
129
            ->method('getRequest')
130
            ->willReturn($request);
131
132
        $this->processorMock
133
            ->expects($this->once())
134
            ->method('process');
135
136
        $this->listener->onKernelRequest($this->event);
137
    }
138
}
139