Completed
Pull Request — master (#148)
by Alejandro
04:22
created

ResolveUrlActionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 42.86 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

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