Completed
Pull Request — master (#213)
by Alejandro
04:40
created

ResolveShortUrlActionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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