Completed
Pull Request — master (#136)
by Alejandro
03:07
created

PathVersionMiddlewareTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 35.48 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 22
loc 62
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A whenVersionIsProvidedRequestRemainsUnchanged() 11 11 1
A whenPathDoesNotStartWithRestRemainsUnchanged() 11 11 1
A versionOneIsPrependedWhenNoVersionIsDefined() 0 16 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
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Rest\Middleware;
5
6
use Interop\Http\ServerMiddleware\DelegateInterface;
7
use PHPUnit\Framework\Assert;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Argument;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
12
use Zend\Diactoros\Response;
13
use Zend\Diactoros\ServerRequestFactory;
14
use Zend\Diactoros\Uri;
15
16
class PathVersionMiddlewareTest extends TestCase
17
{
18
    /**
19
     * @var PathVersionMiddleware
20
     */
21
    protected $middleware;
22
23
    public function setUp()
24
    {
25
        $this->middleware = new PathVersionMiddleware();
26
    }
27
28
    /**
29
     * @test
30
     */
31 View Code Duplication
    public function whenVersionIsProvidedRequestRemainsUnchanged()
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...
32
    {
33
        $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo'));
34
35
        $delegate = $this->prophesize(DelegateInterface::class);
36
        $process = $delegate->process($request)->willReturn(new Response());
37
38
        $this->middleware->process($request, $delegate->reveal());
39
40
        $process->shouldHaveBeenCalled();
41
    }
42
43
    /**
44
     * @test
45
     */
46 View Code Duplication
    public function whenPathDoesNotStartWithRestRemainsUnchanged()
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...
47
    {
48
        $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo'));
49
50
        $delegate = $this->prophesize(DelegateInterface::class);
51
        $process = $delegate->process($request)->willReturn(new Response());
52
53
        $this->middleware->process($request, $delegate->reveal());
54
55
        $process->shouldHaveBeenCalled();
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function versionOneIsPrependedWhenNoVersionIsDefined()
62
    {
63
        $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz'));
64
65
        $delegate = $this->prophesize(DelegateInterface::class);
66
        $delegate->process(Argument::type(Request::class))->will(function (array $args) use ($request) {
67
            $req = \array_shift($args);
68
69
            Assert::assertNotSame($request, $req);
70
            Assert::assertEquals('/rest/v1/bar/baz', $req->getUri()->getPath());
71
            return new Response();
72
        });
73
74
75
        $this->middleware->process($request, $delegate->reveal());
76
    }
77
}
78