Completed
Pull Request — master (#52)
by John
02:48
created

RequestListenerTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
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\Document\OperationObject;
12
use KleijnWeb\SwaggerBundle\EventListener\RequestListener;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class RequestListenerTest extends \PHPUnit_Framework_TestCase
19
{
20
    const DOCUMENT_PATH = '/what/a/crock';
21
    const SWAGGER_PATH = '/a/b/{hello}';
22
23
    /**
24
     * @var \PHPUnit_Framework_MockObject_MockObject
25
     */
26
    private $repositoryMock;
27
28
    /**
29
     * @var \PHPUnit_Framework_MockObject_MockObject
30
     */
31
    private $documentMock;
32
33
    /**
34
     * @var Request
35
     */
36
    private $request;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject
40
     */
41
    private $transformerMock;
42
43
    /**
44
     * @var RequestListener
45
     */
46
    private $listener;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject
50
     */
51
    private $eventMock;
52
53
    /**
54
     * Create mocks
55
     */
56
    protected function setUp()
57
    {
58
        $this->request = new Request(
59
            [],
60
            [],
61
            ['_definition' => self::DOCUMENT_PATH, '_swagger_path' => self::SWAGGER_PATH]
62
        );
63
64
        $this->documentMock = $this
65
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Document\SwaggerDocument')
66
            ->disableOriginalConstructor()
67
            ->getMock();
68
69
        $this->eventMock = $this
70
            ->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent')
71
            ->disableOriginalConstructor()
72
            ->getMock();
73
74
        $this->repositoryMock = $this
75
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Document\DocumentRepository')
76
            ->disableOriginalConstructor()
77
            ->getMock();
78
79
        $this->transformerMock = $this
80
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Request\RequestProcessor')
81
            ->disableOriginalConstructor()
82
            ->getMock();
83
84
        $this->listener = new RequestListener($this->repositoryMock, $this->transformerMock);
85
    }
86
87
    /**
88
     * @test
89
     */
90 View Code Duplication
    public function willTellTransformerToCoerceRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $this->eventMock
93
            ->expects($this->once())
94
            ->method('isMasterRequest')
95
            ->willReturn(true);
96
97
        $this->documentMock
98
            ->expects($this->once())
99
            ->method('getOperationObject')
100
            ->willReturn(OperationObject::createFromOperationDefinition((object)[]));
101
102
        $this->repositoryMock
103
            ->expects($this->once())
104
            ->method('get')
105
            ->with(self::DOCUMENT_PATH)
106
            ->willReturn($this->documentMock);
107
108
        $this->eventMock
109
            ->expects($this->once())
110
            ->method('getRequest')
111
            ->willReturn($this->request);
112
113
        $this->transformerMock
114
            ->expects($this->once())
115
            ->method('process')
116
            ->with($this->request);
117
118
        $this->listener->onKernelRequest($this->eventMock);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function willNotTellTransformerToCoerceRequestWhenNotMasterRequest()
125
    {
126
        $this->eventMock
127
            ->expects($this->once())
128
            ->method('isMasterRequest')
129
            ->willReturn(false);
130
131
        $this->documentMock
132
            ->expects($this->never())
133
            ->method('getOperationObject');
134
135
        $this->transformerMock
136
            ->expects($this->never())
137
            ->method('process');
138
139
        $this->listener->onKernelRequest($this->eventMock);
140
    }
141
142
    /**
143
     * @test
144
     */
145
    public function willIgnoreRequestWithoutDefinition()
146
    {
147
        $wrongRequest = new Request();
148
149
        $this->eventMock
150
            ->expects($this->once())
151
            ->method('isMasterRequest')
152
            ->willReturn(true);
153
154
        $this->eventMock
155
            ->expects($this->once())
156
            ->method('getRequest')
157
            ->willReturn($wrongRequest);
158
159
        $this->listener->onKernelRequest($this->eventMock);
160
    }
161
162
    /**
163
     * @test
164
     * @expectedException \LogicException
165
     */
166
    public function willFailOnRequestWithDefinitionButWithoutSwaggerPath()
167
    {
168
        $wrongRequest = new Request(
169
            [],
170
            [],
171
            ['_definition' => self::DOCUMENT_PATH]
172
        );
173
174
        $this->eventMock
175
            ->expects($this->once())
176
            ->method('isMasterRequest')
177
            ->willReturn(true);
178
179
        $this->eventMock
180
            ->expects($this->once())
181
            ->method('getRequest')
182
            ->willReturn($wrongRequest);
183
184
        $this->listener->onKernelRequest($this->eventMock);
185
    }
186
187
    /**
188
     * @test
189
     */
190 View Code Duplication
    public function canGetOperationDefinitionUsingSwaggerPath()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $this->eventMock
193
            ->expects($this->once())
194
            ->method('isMasterRequest')
195
            ->willReturn(true);
196
197
        $this->documentMock
198
            ->expects($this->once())
199
            ->method('getOperationObject')
200
            ->with(self::SWAGGER_PATH)
201
            ->willReturn(OperationObject::createFromOperationDefinition((object)[]));
202
203
        $this->repositoryMock
204
            ->expects($this->once())
205
            ->method('get')
206
            ->with(self::DOCUMENT_PATH)
207
            ->willReturn($this->documentMock);
208
209
        $this->eventMock
210
            ->expects($this->once())
211
            ->method('getRequest')
212
            ->willReturn($this->request);
213
214
        $this->transformerMock
215
            ->expects($this->once())
216
            ->method('process')
217
            ->with($this->request);
218
219
        $this->listener->onKernelRequest($this->eventMock);
220
    }
221
}
222