Completed
Pull Request — master (#9)
by Haralan
07:35
created

testWillHandleQueryAndBodySeparately()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
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 27
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace Graze\Silex\Tests\ControllerProvider;
4
5
use Graze\Silex\ControllerProvider\TrailingSlashControllerProvider;
6
use Mockery;
7
use Pimple\ServiceProviderInterface;
8
use Silex\Application;
9
use Silex\Api\ControllerProviderInterface;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Routing\Matcher\UrlMatcher;
12
13
/**
14
 * TrailingSlashControllerProvider test cases.
15
 */
16
class TrailingSlashControllerProviderTest extends \PHPUnit_Framework_TestCase
17
{
18
    public function testShouldInitalize()
19
    {
20
        $provider = new TrailingSlashControllerProvider();
21
22
        $this->assertInstanceOf(ControllerProviderInterface::class, $provider);
23
        $this->assertInstanceOf(ServiceProviderInterface::class, $provider);
24
    }
25
26
    public function testShouldRegisterUrlMatcher()
27
    {
28
        $app = new Application();
29
30
        $app->register(new TrailingSlashControllerProvider());
31
32
        $this->assertInstanceOf(UrlMatcher::class, $app['url_matcher']);
33
    }
34
35
    public function testShouldMount()
36
    {
37
        $app = new Application();
38
39
        // `mount` should return the application.
40
        $this->assertSame(
41
            $app,
42
            $app->mount('/', new TrailingSlashControllerProvider())
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
        );
44
    }
45
46
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$method" missing
Loading history...
47
     * @dataProvider requestMethodProvider
48
     */
49 View Code Duplication
    public function testShouldRespondOkWithoutTrailingSlash($method)
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...
50
    {
51
        $app = new Application();
52
53
        $app->match('/foo/', function () {
54
            return 'hunter42';
55
        })->method($method);
56
57
        $app->match('/foo/bar/', function () {
58
            return 'What\'s the question?';
59
        })->method($method);
60
61
        $app->match('/foo/bar/baz/', function () {
62
            return 'Fizz Buzz';
63
        })->method($method);
64
65
        $app->register(new TrailingSlashControllerProvider());
66
        $app->mount('/', new TrailingSlashControllerProvider());
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
68
        $request = Request::create('/foo', $method);
69
        $response = $app->handle($request);
70
71
        $this->assertEquals(200, $response->getStatusCode());
72
73
        $request = Request::create('/foo/bar', $method);
74
        $response = $app->handle($request);
75
76
        $this->assertEquals(200, $response->getStatusCode());
77
78
        $request = Request::create('/foo/bar/baz', $method);
79
        $response = $app->handle($request);
80
81
        $this->assertEquals(200, $response->getStatusCode());
82
    }
83
84
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$method" missing
Loading history...
85
     * This just shows that the mount order for the controller provider doesn't
86
     * matter when all routes are defined with a trailing slash.
87
     *
88
     * @dataProvider requestMethodProvider
89
     */
90 View Code Duplication
    public function testShouldRespondOkWithoutTrailingSlashWhenMountedFirst($method)
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
        $app = new Application();
93
94
        $app->register(new TrailingSlashControllerProvider());
95
        $app->mount('/', new TrailingSlashControllerProvider());
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
97
        $app->match('/foo/', function () {
98
            return 'hunter42';
99
        })->method($method);
100
101
        $app->match('/foo/bar/', function () {
102
            return 'What\'s the question?';
103
        })->method($method);
104
105
        $app->match('/foo/bar/baz/', function () {
106
            return 'Fizz Buzz';
107
        })->method($method);
108
109
        $request = Request::create('/foo', $method);
110
        $response = $app->handle($request);
111
112
        $this->assertEquals(200, $response->getStatusCode());
113
114
        $request = Request::create('/foo/bar', $method);
115
        $response = $app->handle($request);
116
117
        $this->assertEquals(200, $response->getStatusCode());
118
119
        $request = Request::create('/foo/bar/baz', $method);
120
        $response = $app->handle($request);
121
122
        $this->assertEquals(200, $response->getStatusCode());
123
    }
124
125
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$method" missing
Loading history...
126
     * This just shows that the controller provider is compatiable with other
127
     * controller providers.
128
     *
129
     * @dataProvider requestMethodProvider
130
     */
131
    public function testShouldRespondOkWithoutTrailingSlashWithMountedControllers($method)
132
    {
133
        $app = new Application();
134
135
        $app->register(new TrailingSlashControllerProvider());
136
        $app->mount('/', new TrailingSlashControllerProvider());
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
138
        $controller = $app['controllers_factory'];
139
140
        $controller->match('/foo/', function () {
141
            return 'hunter42';
142
        })->method($method);
143
144
        $controller->match('/foo/bar/', function () {
145
            return 'hunter42';
146
        })->method($method);
147
148
        $controller->match('/foo/bar/baz/', function () {
149
            return 'hunter42';
150
        })->method($method);
151
152
        $provider = Mockery::mock(ControllerProviderInterface::class);
153
        $provider->shouldReceive('connect')->andReturn($controller);
154
155
        $app->mount('/', $provider);
156
157
        $request = Request::create('/foo', $method);
158
        $response = $app->handle($request);
159
160
        $this->assertEquals(200, $response->getStatusCode());
161
162
        $request = Request::create('/foo/bar', $method);
163
        $response = $app->handle($request);
164
165
        $this->assertEquals(200, $response->getStatusCode());
166
167
        $request = Request::create('/foo/bar/baz', $method);
168
        $response = $app->handle($request);
169
170
        $this->assertEquals(200, $response->getStatusCode());
171
    }
172
173
    public function requestMethodProvider()
0 ignored issues
show
introduced by
Function has return keyword but no doc block
Loading history...
174
    {
175
        return [
176
            ['GET'],
177
            ['POST'],
178
            ['PUT'],
179
            ['PATCH'],
180
            ['DELETE'],
181
            ['PURGE'],
182
            ['OPTIONS'],
183
            ['TRACE'],
184
            ['CONNECT'],
185
        ];
186
    }
187
188
    public function testShouldRespondOkToHeadWithoutTrailingSlash()
189
    {
190
        $app = new Application();
191
192
        $app->get('/foo/', function () {
193
            return 'hunter42';
194
        });
195
196
        $app->get('/foo/bar/', function () {
197
            return 'What\'s the question?';
198
        });
199
200
        $app->get('/foo/bar/baz/', function () {
201
            return 'Fizz Buzz';
202
        });
203
204
        $app->register(new TrailingSlashControllerProvider());
205
        $app->mount('/', new TrailingSlashControllerProvider());
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
206
207
        $request = Request::create('/foo', 'HEAD');
208
        $response = $app->handle($request);
209
210
        $this->assertEquals(200, $response->getStatusCode());
211
212
        $request = Request::create('/foo/bar', 'HEAD');
213
        $response = $app->handle($request);
214
215
        $this->assertEquals(200, $response->getStatusCode());
216
217
        $request = Request::create('/foo/bar/baz', 'HEAD');
218
        $response = $app->handle($request);
219
220
        $this->assertEquals(200, $response->getStatusCode());
221
    }
222
223
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$method" missing
Loading history...
224
     * This is just to show when defining routes that the trailing slash is
225
     * required when the controller provider is mounted before any other routes.
226
     *
227
     * @dataProvider requestMethodProvider
228
     */
229 View Code Duplication
    public function testWillRespondWithNotFoundForRouteWithNoTrailingSlashWhenMountedFirst($method)
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...
230
    {
231
        $app = new Application();
232
233
        $app->register(new TrailingSlashControllerProvider());
234
        $app->mount('/', new TrailingSlashControllerProvider());
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
235
236
        $app->match('/foo', function () {
237
            return 'hunter42';
238
        })->method($method);
239
240
        $app->match('/foo/bar', function () {
241
            return 'hunter42';
242
        })->method($method);
243
244
        $request = Request::create('/foo', $method);
245
        $response = $app->handle($request);
246
247
        $this->assertEquals(404, $response->getStatusCode());
248
249
        $request = Request::create('/foo/bar', $method);
250
        $response = $app->handle($request);
251
252
        $this->assertEquals(404, $response->getStatusCode());
253
    }
254
255
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$method" missing
Loading history...
256
     * This is just to show when defining routes with no trailing slash before
257
     * mounting the controller provider they should respond as expected.
258
     *
259
     * @dataProvider requestMethodProvider
260
     */
261 View Code Duplication
    public function testWillRespondWithOkForRouteWithNoTrailingSlashWhenMountedLast($method)
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...
262
    {
263
        $app = new Application();
264
265
        $app->match('/foo', function () {
266
            return 'hunter42';
267
        })->method($method);
268
269
        $app->match('/foo/bar', function () {
270
            return 'hunter42';
271
        })->method($method);
272
273
        $app->register(new TrailingSlashControllerProvider());
274
        $app->mount('/', new TrailingSlashControllerProvider());
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
275
276
        $request = Request::create('/foo', $method);
277
        $response = $app->handle($request);
278
279
        $this->assertEquals(200, $response->getStatusCode());
280
281
        $request = Request::create('/foo/bar', $method);
282
        $response = $app->handle($request);
283
284
        $this->assertEquals(200, $response->getStatusCode());
285
    }
286
287
    /**
288
     * Test the case in which a request should have both query
289
     * string params and body params
290
     */
291
    public function testWillHandleQueryAndBodySeparately()
292
    {
293
        $app = new Application();
294
295
        $app->match('/foo/', function (Request $request) {
296
            $response = [
297
                'query' => $request->query->all(),
298
                'request' => $request->request->all()
299
            ];
300
            return json_encode($response, true);
301
        })->method('POST');
302
303
        $app->register(new TrailingSlashControllerProvider());
304
        $app->mount('/', new TrailingSlashControllerProvider());
0 ignored issues
show
Documentation introduced by
new \Graze\Silex\Control...ashControllerProvider() is of type object<Graze\Silex\Contr...lashControllerProvider>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
305
306
        $request = Request::create('/foo?q=1', 'POST', ['r' => 2]);
307
        $response = $app->handle($request);
308
        $this->assertEquals(200, $response->getStatusCode());
309
        $body = json_decode($response->getContent(), true);
310
        $this->assertRequestQuery($body);
311
312
        $request = Request::create('/foo/?q=1', 'POST', ['r' => 2]);
313
        $response = $app->handle($request);
314
        $this->assertEquals(200, $response->getStatusCode());
315
        $body = json_decode($response->getContent(), true);
316
        $this->assertRequestQuery($body);
317
    }
318
319
    private function assertRequestQuery($body) {
0 ignored issues
show
introduced by
Function has parameters but no doc block
Loading history...
320
        $this->assertArrayHasKey('query', $body);
321
        $this->assertArrayHasKey('request', $body);
322
323
        $this->assertArrayHasKey('q', $body['query']);
324
        $this->assertEquals(1, $body['query']['q']);
325
326
        $this->assertArrayHasKey('r', $body['request']);
327
        $this->assertEquals(2, $body['request']['r']);
328
    }
329
}
330