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

CreateShortUrlActionTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 106
Duplicated Lines 24.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

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