Completed
Push — master ( f7424d...b53e51 )
by Alejandro
07:43
created

QrCodeActionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 75
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 15
loc 75
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A aNotFoundShortCodeWillDelegateIntoNextMiddleware() 0 13 1
A anInvalidShortCodeWillReturnNotFoundResponse() 0 14 1
A aCorrectRequestReturnsTheQrCodeResponse() 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\Core\Action;
3
4
use Interop\Http\ServerMiddleware\DelegateInterface;
5
use PHPUnit\Framework\TestCase;
6
use Prophecy\Argument;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
9
use Shlinkio\Shlink\Core\Action\QrCodeAction;
10
use Shlinkio\Shlink\Core\Entity\ShortUrl;
11
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
12
use Shlinkio\Shlink\Core\Service\UrlShortener;
13
use Zend\Diactoros\ServerRequestFactory;
14
use Zend\Expressive\Router\RouterInterface;
15
16
class QrCodeActionTest extends TestCase
17
{
18
    /**
19
     * @var QrCodeAction
20
     */
21
    protected $action;
22
    /**
23
     * @var ObjectProphecy
24
     */
25
    protected $urlShortener;
26
27
    public function setUp()
28
    {
29
        $router = $this->prophesize(RouterInterface::class);
30
        $router->generateUri(Argument::cetera())->willReturn('/foo/bar');
31
32
        $this->urlShortener = $this->prophesize(UrlShortener::class);
33
34
        $this->action = new QrCodeAction($router->reveal(), $this->urlShortener->reveal());
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function aNotFoundShortCodeWillDelegateIntoNextMiddleware()
41
    {
42
        $shortCode = 'abc123';
43
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)->shouldBeCalledTimes(1);
44
        $delegate = $this->prophesize(DelegateInterface::class);
45
46
        $this->action->process(
47
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
48
            $delegate->reveal()
49
        );
50
51
        $delegate->process(Argument::any())->shouldHaveBeenCalledTimes(1);
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function anInvalidShortCodeWillReturnNotFoundResponse()
58
    {
59
        $shortCode = 'abc123';
60
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
61
                                                       ->shouldBeCalledTimes(1);
62
        $delegate = $this->prophesize(DelegateInterface::class);
63
64
        $this->action->process(
65
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
66
            $delegate->reveal()
67
        );
68
69
        $delegate->process(Argument::any())->shouldHaveBeenCalledTimes(1);
70
    }
71
72
    /**
73
     * @test
74
     */
75 View Code Duplication
    public function aCorrectRequestReturnsTheQrCodeResponse()
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...
76
    {
77
        $shortCode = 'abc123';
78
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(new ShortUrl())->shouldBeCalledTimes(1);
79
        $delegate = $this->prophesize(DelegateInterface::class);
80
81
        $resp = $this->action->process(
82
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
83
            $delegate->reveal()
84
        );
85
86
        $this->assertInstanceOf(QrCodeResponse::class, $resp);
87
        $this->assertEquals(200, $resp->getStatusCode());
88
        $delegate->process(Argument::any())->shouldHaveBeenCalledTimes(0);
89
    }
90
}
91