Completed
Push — master ( 386146...a2d301 )
by John
07:40
created

RequestListenerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 204
Duplicated Lines 29.9 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 6
dl 61
loc 204
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 30 1
B willTellTransformerToCoerceRequest() 30 30 1
A willNotTellTransformerToCoerceRequestWhenNotMasterRequest() 0 17 1
A willIgnoreRequestWithoutDefinition() 0 16 1
A willFailOnRequestWithDefinitionButWithoutSwaggerPath() 0 20 1
B canGetOperationDefinitionUsingSwaggerPath() 31 31 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\EventListener\RequestListener;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class RequestListenerTest extends \PHPUnit_Framework_TestCase
18
{
19
    const DOCUMENT_PATH = '/what/a/crock';
20
    const SWAGGER_PATH = '/a/b/{hello}';
21
22
    /**
23
     * @var \PHPUnit_Framework_MockObject_MockObject
24
     */
25
    private $repositoryMock;
26
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockObject
29
     */
30
    private $documentMock;
31
32
    /**
33
     * @var Request
34
     */
35
    private $request;
36
37
    /**
38
     * @var \PHPUnit_Framework_MockObject_MockObject
39
     */
40
    private $transformerMock;
41
42
    /**
43
     * @var RequestListener
44
     */
45
    private $listener;
46
47
    /**
48
     * @var \PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $eventMock;
51
52
    /**
53
     * Create mocks
54
     */
55
    protected function setUp()
56
    {
57
        $this->request = new Request(
58
            [],
59
            [],
60
            ['_definition' => self::DOCUMENT_PATH, '_swagger_path' => self::SWAGGER_PATH]
61
        );
62
63
        $this->documentMock = $this
64
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Document\SwaggerDocument')
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
68
        $this->eventMock = $this
69
            ->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent')
70
            ->disableOriginalConstructor()
71
            ->getMock();
72
73
        $this->repositoryMock = $this
74
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Document\DocumentRepository')
75
            ->disableOriginalConstructor()
76
            ->getMock();
77
78
        $this->transformerMock = $this
79
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Request\RequestProcessor')
80
            ->disableOriginalConstructor()
81
            ->getMock();
82
83
        $this->listener = new RequestListener($this->repositoryMock, $this->transformerMock);
84
    }
85
86
    /**
87
     * @test
88
     */
89 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...
90
    {
91
        $this->eventMock
92
            ->expects($this->once())
93
            ->method('isMasterRequest')
94
            ->willReturn(true);
95
96
        $this->documentMock
97
            ->expects($this->once())
98
            ->method('getOperationDefinition')
99
            ->willReturn([]);
100
101
        $this->repositoryMock
102
            ->expects($this->once())
103
            ->method('get')
104
            ->with(self::DOCUMENT_PATH)
105
            ->willReturn($this->documentMock);
106
107
        $this->eventMock
108
            ->expects($this->once())
109
            ->method('getRequest')
110
            ->willReturn($this->request);
111
112
        $this->transformerMock
113
            ->expects($this->once())
114
            ->method('process')
115
            ->with($this->request);
116
117
        $this->listener->onKernelRequest($this->eventMock);
118
    }
119
120
    /**
121
     * @test
122
     */
123
    public function willNotTellTransformerToCoerceRequestWhenNotMasterRequest()
124
    {
125
        $this->eventMock
126
            ->expects($this->once())
127
            ->method('isMasterRequest')
128
            ->willReturn(false);
129
130
        $this->documentMock
131
            ->expects($this->never())
132
            ->method('getOperationDefinition');
133
134
        $this->transformerMock
135
            ->expects($this->never())
136
            ->method('process');
137
138
        $this->listener->onKernelRequest($this->eventMock);
139
    }
140
141
    /**
142
     * @test
143
     */
144
    public function willIgnoreRequestWithoutDefinition()
145
    {
146
        $wrongRequest = new Request();
147
148
        $this->eventMock
149
            ->expects($this->once())
150
            ->method('isMasterRequest')
151
            ->willReturn(true);
152
153
        $this->eventMock
154
            ->expects($this->once())
155
            ->method('getRequest')
156
            ->willReturn($wrongRequest);
157
158
        $this->listener->onKernelRequest($this->eventMock);
159
    }
160
161
    /**
162
     * @test
163
     * @expectedException \LogicException
164
     */
165
    public function willFailOnRequestWithDefinitionButWithoutSwaggerPath()
166
    {
167
        $wrongRequest = new Request(
168
            [],
169
            [],
170
            ['_definition' => self::DOCUMENT_PATH]
171
        );
172
173
        $this->eventMock
174
            ->expects($this->once())
175
            ->method('isMasterRequest')
176
            ->willReturn(true);
177
178
        $this->eventMock
179
            ->expects($this->once())
180
            ->method('getRequest')
181
            ->willReturn($wrongRequest);
182
183
        $this->listener->onKernelRequest($this->eventMock);
184
    }
185
186
    /**
187
     * @test
188
     */
189 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...
190
    {
191
        $this->eventMock
192
            ->expects($this->once())
193
            ->method('isMasterRequest')
194
            ->willReturn(true);
195
196
        $this->documentMock
197
            ->expects($this->once())
198
            ->method('getOperationDefinition')
199
            ->with(self::SWAGGER_PATH)
200
            ->willReturn([]);
201
202
        $this->repositoryMock
203
            ->expects($this->once())
204
            ->method('get')
205
            ->with(self::DOCUMENT_PATH)
206
            ->willReturn($this->documentMock);
207
208
        $this->eventMock
209
            ->expects($this->once())
210
            ->method('getRequest')
211
            ->willReturn($this->request);
212
213
        $this->transformerMock
214
            ->expects($this->once())
215
            ->method('process')
216
            ->with($this->request);
217
218
        $this->listener->onKernelRequest($this->eventMock);
219
    }
220
}
221