Completed
Pull Request — master (#148)
by Alejandro
09:03 queued 02:46
created

CreateShortCodeActionTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 25.24 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 26
loc 103
rs 10
wmc 6
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A missingLongUrlParamReturnsError() 0 5 1
A properShortcodeConversionReturnsData() 0 13 1
A anInvalidUrlReturnsError() 13 13 1
A nonUniqueSlugReturnsError() 0 19 1
A aGenericExceptionWillReturnError() 13 13 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;
5
6
use PHPUnit\Framework\TestCase;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
10
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
11
use Shlinkio\Shlink\Core\Service\UrlShortener;
12
use Shlinkio\Shlink\Rest\Action\CreateShortCodeAction;
13
use Shlinkio\Shlink\Rest\Util\RestUtils;
14
use Zend\Diactoros\ServerRequestFactory;
15
use Zend\Diactoros\Uri;
16
use Zend\I18n\Translator\Translator;
17
18
class CreateShortCodeActionTest extends TestCase
19
{
20
    /**
21
     * @var CreateShortCodeAction
22
     */
23
    protected $action;
24
    /**
25
     * @var ObjectProphecy
26
     */
27
    protected $urlShortener;
28
29
    public function setUp()
30
    {
31
        $this->urlShortener = $this->prophesize(UrlShortener::class);
32
        $this->action = new CreateShortCodeAction($this->urlShortener->reveal(), Translator::factory([]), [
33
            'schema' => 'http',
34
            'hostname' => 'foo.com',
35
        ]);
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function missingLongUrlParamReturnsError()
42
    {
43
        $response = $this->action->handle(ServerRequestFactory::fromGlobals());
44
        $this->assertEquals(400, $response->getStatusCode());
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function properShortcodeConversionReturnsData()
51
    {
52
        $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
53
            ->willReturn('abc123')
54
            ->shouldBeCalledTimes(1);
55
56
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
57
            'longUrl' => 'http://www.domain.com/foo/bar',
58
        ]);
59
        $response = $this->action->handle($request);
60
        $this->assertEquals(200, $response->getStatusCode());
61
        $this->assertTrue(strpos($response->getBody()->getContents(), 'http://foo.com/abc123') > 0);
62
    }
63
64
    /**
65
     * @test
66
     */
67 View Code Duplication
    public function anInvalidUrlReturnsError()
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...
68
    {
69
        $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
70
            ->willThrow(InvalidUrlException::class)
71
            ->shouldBeCalledTimes(1);
72
73
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
74
            'longUrl' => 'http://www.domain.com/foo/bar',
75
        ]);
76
        $response = $this->action->handle($request);
77
        $this->assertEquals(400, $response->getStatusCode());
78
        $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::INVALID_URL_ERROR) > 0);
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function nonUniqueSlugReturnsError()
85
    {
86
        $this->urlShortener->urlToShortCode(
87
            Argument::type(Uri::class),
88
            Argument::type('array'),
89
            null,
90
            null,
91
            'foo',
92
            Argument::cetera()
93
        )->willThrow(NonUniqueSlugException::class)->shouldBeCalledTimes(1);
94
95
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
96
            'longUrl' => 'http://www.domain.com/foo/bar',
97
            'customSlug' => 'foo',
98
        ]);
99
        $response = $this->action->handle($request);
100
        $this->assertEquals(400, $response->getStatusCode());
101
        $this->assertContains(RestUtils::INVALID_SLUG_ERROR, (string) $response->getBody());
102
    }
103
104
    /**
105
     * @test
106
     */
107 View Code Duplication
    public function aGenericExceptionWillReturnError()
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...
108
    {
109
        $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'), Argument::cetera())
110
            ->willThrow(\Exception::class)
111
            ->shouldBeCalledTimes(1);
112
113
        $request = ServerRequestFactory::fromGlobals()->withParsedBody([
114
            'longUrl' => 'http://www.domain.com/foo/bar',
115
        ]);
116
        $response = $this->action->handle($request);
117
        $this->assertEquals(500, $response->getStatusCode());
118
        $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
119
    }
120
}
121