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

SwaggerRouteLoaderTest::canUseDiKeyAsOperationId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
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\Routing;
10
11
use KleijnWeb\SwaggerBundle\Routing\SwaggerRouteLoader;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class SwaggerRouteLoaderTest extends \PHPUnit_Framework_TestCase
17
{
18
    const DOCUMENT_PATH = '/totally/non-existent/path';
19
20
    /**
21
     * @var \PHPUnit_Framework_MockObject_MockObject
22
     */
23
    private $repositoryMock;
24
25
    /**
26
     * @var \PHPUnit_Framework_MockObject_MockObject
27
     */
28
    private $documentMock;
29
30
    /**
31
     * @var SwaggerRouteLoader
32
     */
33
    private $loader;
34
35
    /**
36
     * Create mocks
37
     */
38
    protected function setUp()
39
    {
40
        $this->documentMock = $this
41
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Document\SwaggerDocument')
42
            ->disableOriginalConstructor()
43
            ->getMock();
44
45
        $this->repositoryMock = $this
46
            ->getMockBuilder('KleijnWeb\SwaggerBundle\Document\DocumentRepository')
47
            ->disableOriginalConstructor()
48
            ->getMock();
49
50
        $this->repositoryMock
51
            ->expects($this->any())
52
            ->method('get')
53
            ->willReturn($this->documentMock);
54
55
        $this->loader = new SwaggerRouteLoader($this->repositoryMock);
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function supportSwaggerAsRouteTypeOnly()
62
    {
63
        $this->assertFalse($this->loader->supports('/a/b/c'));
64
        $this->assertTrue($this->loader->supports('/a/b/c', 'swagger'));
65
    }
66
67
    /**
68
     * @test
69
     */
70 View Code Duplication
    public function canLoadMultipleDocuments()
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...
71
    {
72
        $this->documentMock
73
            ->expects($this->any())
74
            ->method('getPathDefinitions')
75
            ->willReturn([]);
76
77
        $this->loader->load(self::DOCUMENT_PATH);
78
        $this->loader->load(self::DOCUMENT_PATH . '2');
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function loadingMultipleDocumentWillPreventRouteKeyCollisions()
85
    {
86
        $pathDefinitions = (object)[
87
            '/a'     => (object)['get' => (object)[]],
88
            '/a/b'   => (object)['get' => (object)[], 'post' => (object)[]],
89
            '/a/b/c' => (object)['put' => (object)[]],
90
        ];
91
92
        $this->documentMock
93
            ->expects($this->exactly(2))
94
            ->method('getPathDefinitions')
95
            ->willReturn($pathDefinitions);
96
97
        $routes1 = $this->loader->load(self::DOCUMENT_PATH);
98
        $routes2 = $this->loader->load(self::DOCUMENT_PATH . '2');
99
        $this->assertSame(count($routes1), count(array_diff_key($routes1->all(), $routes2->all())));
100
    }
101
102
    /**
103
     * @test
104
     * @expectedException \RuntimeException
105
     */
106 View Code Duplication
    public function cannotTryToLoadSameDocumentMoreThanOnce()
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...
107
    {
108
        $this->documentMock
109
            ->expects($this->any())
110
            ->method('getPathDefinitions')
111
            ->willReturn([]);
112
113
        $this->loader->load(self::DOCUMENT_PATH);
114
        $this->loader->load(self::DOCUMENT_PATH);
115
    }
116
117
    /**
118
     * @test
119
     */
120 View Code Duplication
    public function willReturnRouteCollection()
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...
121
    {
122
        $this->documentMock
123
            ->expects($this->once())
124
            ->method('getPathDefinitions')
125
            ->willReturn([]);
126
127
        $routes = $this->loader->load(self::DOCUMENT_PATH);
128
        $this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $routes);
129
    }
130
131
    /**
132
     * @test
133
     */
134
    public function routeCollectionWillContainOneRouteForEveryPathAndMethod()
135
    {
136
        $pathDefinitions = (object)[
137
            '/a' => (object)['get' => (object)[], 'post' => (object)[]],
138
            '/b' => (object)['get' => (object)[]],
139
        ];
140
141
        $this->documentMock
142
            ->expects($this->once())
143
            ->method('getPathDefinitions')
144
            ->willReturn($pathDefinitions);
145
146
        $routes = $this->loader->load(self::DOCUMENT_PATH);
147
148
        $this->assertCount(3, $routes);
149
    }
150
151
    /**
152
     * @test
153
     */
154 View Code Duplication
    public function routeCollectionWillIncludeSeparateRoutesForSubPaths()
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...
155
    {
156
        $pathDefinitions = (object)[
157
            '/a'     => (object)['get' => (object)[]],
158
            '/a/b'   => (object)['get' => (object)[]],
159
            '/a/b/c' => (object)['get' => (object)[]],
160
        ];
161
162
        $this->documentMock
163
            ->expects($this->once())
164
            ->method('getPathDefinitions')
165
            ->willReturn($pathDefinitions);
166
167
        $routes = $this->loader->load(self::DOCUMENT_PATH);
168
169
        $this->assertCount(3, $routes);
170
    }
171
172
    /**
173
     * @test
174
     */
175
    public function canUseDiKeyAsOperationId()
176
    {
177
        $expected = 'my.controller.key:methodName';
178
        $pathDefinitions = (object)[
179
            '/a' => [
180
                'get'  => (object)[],
181
                'post' => (object)['operationId' => $expected]
182
            ],
183
            '/b' => (object)['get' => (object)[]],
184
        ];
185
186
        $this->documentMock
187
            ->expects($this->any())
188
            ->method('getPathDefinitions')
189
            ->willReturn($pathDefinitions);
190
191
        $routes = $this->loader->load(self::DOCUMENT_PATH);
192
193
        $actual = $routes->get('swagger.path.a.my.controller.key:methodName');
194
        $this->assertNotNull($actual);
195
        $this->assertSame($expected, $actual->getDefault('_controller'));
196
    }
197
198
    /**
199
     * @test
200
     */
201 View Code Duplication
    public function routeCollectionWillIncludeSeparateRoutesForSubPathMethodCombinations()
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...
202
    {
203
        $pathDefinitions = (object)[
204
            '/a'     => (object)['get' => (object)[]],
205
            '/a/b'   => (object)['get' => (object)[], 'post' => (object)[]],
206
            '/a/b/c' => (object)['put' => (object)[]],
207
        ];
208
209
        $this->documentMock
210
            ->expects($this->once())
211
            ->method('getPathDefinitions')
212
            ->willReturn($pathDefinitions);
213
214
        $routes = $this->loader->load(self::DOCUMENT_PATH);
215
216
        $this->assertCount(4, $routes);
217
    }
218
219
    /**
220
     * @test
221
     */
222
    public function routeCollectionWillContainPathFromSwaggerDoc()
223
    {
224
        $pathDefinitions = (object)[
225
            '/a'                => (object)['get' => (object)[]],
226
            '/a/b'              => (object)['get' => (object)[]],
227
            '/a/b/c'            => (object)['get' => (object)[]],
228
            '/d/f/g'            => (object)['get' => (object)[]],
229
            '/1/2/3'            => (object)['get' => (object)[]],
230
            '/foo/{bar}/{blah}' => (object)['get' => (object)[]],
231
            '/z'                => (object)['get' => (object)[]],
232
        ];
233
234
        $this->documentMock
235
            ->expects($this->once())
236
            ->method('getPathDefinitions')
237
            ->willReturn($pathDefinitions);
238
239
        $routes = $this->loader->load(self::DOCUMENT_PATH);
240
241
        $definitionPaths = array_keys((array)$pathDefinitions);
242
        sort($definitionPaths);
243
        $routePaths = array_map(function ($route) {
244
            return $route->getPath();
245
        }, $routes->getIterator()->getArrayCopy());
246
        sort($routePaths);
247
        $this->assertSame($definitionPaths, $routePaths);
248
    }
249
250
    /**
251
     * @test
252
     */
253
    public function willAddRequirementsForIntegerPathParams()
254
    {
255
        $pathDefinitions = (object)[
256
            '/a' => (object)[
257
                'get' => (object)[
258
                    'parameters' => (object)[
259
                        (object)['name' => 'foo', 'in' => 'path', 'type' => 'integer']
260
                    ]
261
                ]
262
            ],
263
        ];
264
265
        $this->documentMock
266
            ->expects($this->once())
267
            ->method('getPathDefinitions')
268
            ->willReturn($pathDefinitions);
269
270
        $this->documentMock
271
            ->expects($this->once())
272
            ->method('getOperationDefinition')
273
            ->with('/a', 'get')
274
            ->willReturn($pathDefinitions->{'/a'}->get);
275
276
        $routes = $this->loader->load(self::DOCUMENT_PATH);
277
        $actual = $routes->get('swagger.path.a.get');
278
        $this->assertNotNull($actual);
279
        $requirements = $actual->getRequirements();
280
        $this->assertNotNull($requirements);
281
282
        $this->assertSame($requirements['foo'], '\d+');
283
    }
284
285
    /**
286
     * @test
287
     */
288
    public function willAddRequirementsForStringPatternParams()
289
    {
290
        $expected = '\d{2}hello';
291
        $pathDefinitions = (object)[
292
            '/a' => (object)[
293
                'get' => (object)[
294
                    'parameters' => (object)[
295
                        (object)['name' => 'aString', 'in' => 'path', 'type' => 'string', 'pattern' => $expected]
296
                    ]
297
                ]
298
            ],
299
        ];
300
301
        $this->documentMock
302
            ->expects($this->once())
303
            ->method('getPathDefinitions')
304
            ->willReturn($pathDefinitions);
305
306
        $this->documentMock
307
            ->expects($this->once())
308
            ->method('getOperationDefinition')
309
            ->with('/a', 'get')
310
            ->willReturn($pathDefinitions->{'/a'}->get);
311
312
        $routes = $this->loader->load(self::DOCUMENT_PATH);
313
        $actual = $routes->get('swagger.path.a.get');
314
        $this->assertNotNull($actual);
315
        $requirements = $actual->getRequirements();
316
        $this->assertNotNull($requirements);
317
318
        $this->assertSame($expected, $requirements['aString']);
319
    }
320
321
    /**
322
     * @test
323
     */
324
    public function willAddRequirementsForStringEnumParams()
325
    {
326
        $enum = ['a', 'b', 'c'];
327
        $expected = '(a|b|c)';
328
        $pathDefinitions = (object)[
329
            '/a' => (object)[
330
                'get' => (object)[
331
                    'parameters' => (object)[
332
                        (object)['name' => 'aString', 'in' => 'path', 'type' => 'string', 'enum' => $enum]
333
                    ]
334
                ]
335
            ],
336
        ];
337
338
        $this->documentMock
339
            ->expects($this->once())
340
            ->method('getPathDefinitions')
341
            ->willReturn($pathDefinitions);
342
343
        $this->documentMock
344
            ->expects($this->once())
345
            ->method('getOperationDefinition')
346
            ->with('/a', 'get')
347
            ->willReturn($pathDefinitions->{'/a'}->get);
348
349
        $routes = $this->loader->load(self::DOCUMENT_PATH);
350
        $actual = $routes->get('swagger.path.a.get');
351
        $this->assertNotNull($actual);
352
        $requirements = $actual->getRequirements();
353
        $this->assertNotNull($requirements);
354
355
        $this->assertSame($expected, $requirements['aString']);
356
    }
357
}
358