Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

QrCodeActionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 57.69 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 45
loc 78
rs 10
wmc 4
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A aNonexistentShortCodeWillReturnNotFoundResponse() 14 14 1
A anInvalidShortCodeWillReturnNotFoundResponse() 15 15 1
A aCorrectRequestReturnsTheQrCodeResponse() 16 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
namespace ShlinkioTest\Shlink\Core\Action;
3
4
use PHPUnit_Framework_TestCase as TestCase;
5
use Prophecy\Argument;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
8
use Shlinkio\Shlink\Core\Action\QrCodeAction;
9
use Shlinkio\Shlink\Core\Entity\ShortUrl;
10
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
11
use Shlinkio\Shlink\Core\Service\UrlShortener;
12
use Zend\Diactoros\Response;
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 View Code Duplication
    public function aNonexistentShortCodeWillReturnNotFoundResponse()
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...
41
    {
42
        $shortCode = 'abc123';
43
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)->shouldBeCalledTimes(1);
44
45
        $resp = $this->action->__invoke(
46
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
47
            new Response(),
48
            function ($req, $resp) {
49
                return $resp;
50
            }
51
        );
52
        $this->assertEquals(404, $resp->getStatusCode());
53
    }
54
55
    /**
56
     * @test
57
     */
58 View Code Duplication
    public function anInvalidShortCodeWillReturnNotFoundResponse()
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...
59
    {
60
        $shortCode = 'abc123';
61
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
62
                                                       ->shouldBeCalledTimes(1);
63
64
        $resp = $this->action->__invoke(
65
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
66
            new Response(),
67
            function ($req, $resp) {
68
                return $resp;
69
            }
70
        );
71
        $this->assertEquals(404, $resp->getStatusCode());
72
    }
73
74
    /**
75
     * @test
76
     */
77 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...
78
    {
79
        $shortCode = 'abc123';
80
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(new ShortUrl())->shouldBeCalledTimes(1);
81
82
        $resp = $this->action->__invoke(
83
            ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
84
            new Response(),
85
            function ($req, $resp) {
86
                return $resp;
87
            }
88
        );
89
90
        $this->assertInstanceOf(QrCodeResponse::class, $resp);
91
        $this->assertEquals(200, $resp->getStatusCode());
92
    }
93
}
94