Completed
Push — master ( 076b0c...47e232 )
by Alejandro
10s
created

ResolveShortUrlActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 42.31 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 33
loc 78
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A incorrectShortCodeReturnsError() 11 11 1
A correctShortCodeReturnsSuccess() 0 12 1
A invalidShortCodeExceptionReturnsError() 11 11 1
A unexpectedExceptionWillReturnError() 11 11 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\Action\ShortUrl;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Prophecy\ObjectProphecy;
8
use Shlinkio\Shlink\Core\Entity\ShortUrl;
9
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
10
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
11
use Shlinkio\Shlink\Core\Service\UrlShortener;
12
use Shlinkio\Shlink\Rest\Action\ShortUrl\ResolveShortUrlAction;
13
use Shlinkio\Shlink\Rest\Util\RestUtils;
14
use Zend\Diactoros\ServerRequestFactory;
15
use Zend\I18n\Translator\Translator;
16
17
class ResolveShortUrlActionTest extends TestCase
18
{
19
    /**
20
     * @var ResolveShortUrlAction
21
     */
22
    protected $action;
23
    /**
24
     * @var ObjectProphecy
25
     */
26
    protected $urlShortener;
27
28
    public function setUp()
29
    {
30
        $this->urlShortener = $this->prophesize(UrlShortener::class);
31
        $this->action = new ResolveShortUrlAction($this->urlShortener->reveal(), Translator::factory([]), []);
32
    }
33
34
    /**
35
     * @test
36
     */
37 View Code Duplication
    public function incorrectShortCodeReturnsError()
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...
38
    {
39
        $shortCode = 'abc123';
40
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
41
                                                       ->shouldBeCalledTimes(1);
42
43
        $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
44
        $response = $this->action->handle($request);
45
        $this->assertEquals(404, $response->getStatusCode());
46
        $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_ARGUMENT_ERROR) > 0);
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function correctShortCodeReturnsSuccess()
53
    {
54
        $shortCode = 'abc123';
55
        $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(
56
            (new ShortUrl())->setLongUrl('http://domain.com/foo/bar')
57
        )->shouldBeCalledTimes(1);
58
59
        $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
60
        $response = $this->action->handle($request);
61
        $this->assertEquals(200, $response->getStatusCode());
62
        $this->assertTrue(strpos($response->getBody()->getContents(), 'http://domain.com/foo/bar') > 0);
63
    }
64
65
    /**
66
     * @test
67
     */
68 View Code Duplication
    public function invalidShortCodeExceptionReturnsError()
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...
69
    {
70
        $shortCode = 'abc123';
71
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
72
                                                       ->shouldBeCalledTimes(1);
73
74
        $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
75
        $response = $this->action->handle($request);
76
        $this->assertEquals(400, $response->getStatusCode());
77
        $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_SHORTCODE_ERROR) > 0);
78
    }
79
80
    /**
81
     * @test
82
     */
83 View Code Duplication
    public function unexpectedExceptionWillReturnError()
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...
84
    {
85
        $shortCode = 'abc123';
86
        $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class)
87
                                                       ->shouldBeCalledTimes(1);
88
89
        $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
90
        $response = $this->action->handle($request);
91
        $this->assertEquals(500, $response->getStatusCode());
92
        $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
93
    }
94
}
95