1
|
|
|
<?php |
2
|
|
|
namespace ShlinkioTest\Shlink\Core\Action; |
3
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
5
|
|
|
use Prophecy\Argument; |
6
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
7
|
|
|
use Shlinkio\Shlink\Common\Response\QrCodeResponse; |
8
|
|
|
use Shlinkio\Shlink\Core\Action\QrCodeAction; |
9
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
10
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
11
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener; |
12
|
|
|
use Zend\Diactoros\Response; |
13
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
14
|
|
|
use Zend\Expressive\Router\RouterInterface; |
15
|
|
|
|
16
|
|
|
class QrCodeActionTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var QrCodeAction |
20
|
|
|
*/ |
21
|
|
|
protected $action; |
22
|
|
|
/** |
23
|
|
|
* @var ObjectProphecy |
24
|
|
|
*/ |
25
|
|
|
protected $urlShortener; |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
$router = $this->prophesize(RouterInterface::class); |
30
|
|
|
$router->generateUri(Argument::cetera())->willReturn('/foo/bar'); |
31
|
|
|
|
32
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class); |
33
|
|
|
|
34
|
|
|
$this->action = new QrCodeAction($router->reveal(), $this->urlShortener->reveal()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @test |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function aNonexistentShortCodeWillReturnNotFoundResponse() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$shortCode = 'abc123'; |
43
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)->shouldBeCalledTimes(1); |
44
|
|
|
|
45
|
|
|
$resp = $this->action->__invoke( |
46
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), |
47
|
|
|
new Response(), |
48
|
|
|
function ($req, $resp) { |
49
|
|
|
return $resp; |
50
|
|
|
} |
51
|
|
|
); |
52
|
|
|
$this->assertEquals(404, $resp->getStatusCode()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function anInvalidShortCodeWillReturnNotFoundResponse() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$shortCode = 'abc123'; |
61
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class) |
62
|
|
|
->shouldBeCalledTimes(1); |
63
|
|
|
|
64
|
|
|
$resp = $this->action->__invoke( |
65
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), |
66
|
|
|
new Response(), |
67
|
|
|
function ($req, $resp) { |
68
|
|
|
return $resp; |
69
|
|
|
} |
70
|
|
|
); |
71
|
|
|
$this->assertEquals(404, $resp->getStatusCode()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function aCorrectRequestReturnsTheQrCodeResponse() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$shortCode = 'abc123'; |
80
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(new ShortUrl())->shouldBeCalledTimes(1); |
81
|
|
|
|
82
|
|
|
$resp = $this->action->__invoke( |
83
|
|
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), |
84
|
|
|
new Response(), |
85
|
|
|
function ($req, $resp) { |
86
|
|
|
return $resp; |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->assertInstanceOf(QrCodeResponse::class, $resp); |
91
|
|
|
$this->assertEquals(200, $resp->getStatusCode()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.