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

BodyParserMiddlewareTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 44.93 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 31
loc 69
rs 10
wmc 4
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A requestsFromOtherMethodsJustFallbackToNextMiddleware() 0 14 1
A jsonRequestsAreJsonDecoded() 16 16 1
A regularRequestsAreUrlDecoded() 15 15 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
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