Completed
Push — master ( 065cdd...96faaf )
by Alejandro
09:28
created

requestsFromOtherMethodsJustFallbackToNextMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.4285
1
<?php
2
namespace ShlinkioTest\Shlink\Rest\Middleware;
3
4
use PHPUnit_Framework_TestCase as TestCase;
5
use Psr\Http\Message\ServerRequestInterface as Request;
6
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
7
use Zend\Diactoros\Response;
8
use Zend\Diactoros\ServerRequestFactory;
9
use Zend\Diactoros\Stream;
10
11
class BodyParserMiddlewareTest extends TestCase
12
{
13
    /**
14
     * @var BodyParserMiddleware
15
     */
16
    private $middleware;
17
18
    public function setUp()
19
    {
20
        $this->middleware = new BodyParserMiddleware();
21
    }
22
23
    /**
24
     * @test
25
     */
26
    public function requestsFromOtherMethodsJustFallbackToNextMiddleware()
27
    {
28
        $request = ServerRequestFactory::fromGlobals()->withMethod('GET');
29
        $test = $this;
30
        $this->middleware->__invoke($request, new Response(), function ($req, $resp) use ($test, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
            $test->assertSame($request, $req);
32
        });
33
34
        $request = $request->withMethod('POST');
35
        $test = $this;
36
        $this->middleware->__invoke($request, new Response(), function ($req, $resp) use ($test, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
            $test->assertSame($request, $req);
38
        });
39
    }
40
41
    /**
42
     * @test
43
     */
44 View Code Duplication
    public function jsonRequestsAreJsonDecoded()
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...
45
    {
46
        $body = new Stream('php://temp', 'wr');
47
        $body->write('{"foo": "bar", "bar": ["one", 5]}');
48
        $request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
49
                                                      ->withBody($body)
50
                                                      ->withHeader('content-type', 'application/json');
51
        $test = $this;
52
        $this->middleware->__invoke($request, new Response(), function (Request $req, $resp) use ($test, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
            $test->assertNotSame($request, $req);
54
            $test->assertEquals([
55
                'foo' => 'bar',
56
                'bar' => ['one', 5],
57
            ], $req->getParsedBody());
58
        });
59
    }
60
61
    /**
62
     * @test
63
     */
64 View Code Duplication
    public function regularRequestsAreUrlDecoded()
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...
65
    {
66
        $body = new Stream('php://temp', 'wr');
67
        $body->write('foo=bar&bar[]=one&bar[]=5');
68
        $request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
69
                                                      ->withBody($body);
70
        $test = $this;
71
        $this->middleware->__invoke($request, new Response(), function (Request $req, $resp) use ($test, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $resp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
            $test->assertNotSame($request, $req);
73
            $test->assertEquals([
74
                'foo' => 'bar',
75
                'bar' => ['one', 5],
76
            ], $req->getParsedBody());
77
        });
78
    }
79
}
80