|
1
|
|
|
<?php |
|
2
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Shortcode; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
|
5
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
6
|
|
|
use Shlinkio\Shlink\CLI\Command\Shortcode\ResolveUrlCommand; |
|
7
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener; |
|
9
|
|
|
use Symfony\Component\Console\Application; |
|
10
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
11
|
|
|
use Zend\I18n\Translator\Translator; |
|
12
|
|
|
|
|
13
|
|
|
class ResolveUrlCommandTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var CommandTester |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $commandTester; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ObjectProphecy |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $urlShortener; |
|
23
|
|
|
|
|
24
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class); |
|
27
|
|
|
$command = new ResolveUrlCommand($this->urlShortener->reveal(), Translator::factory([])); |
|
28
|
|
|
$app = new Application(); |
|
29
|
|
|
$app->add($command); |
|
30
|
|
|
|
|
31
|
|
|
$this->commandTester = new CommandTester($command); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @test |
|
36
|
|
|
*/ |
|
37
|
|
View Code Duplication |
public function correctShortCodeResolvesUrl() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$shortCode = 'abc123'; |
|
40
|
|
|
$expectedUrl = 'http://domain.com/foo/bar'; |
|
41
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($expectedUrl) |
|
42
|
|
|
->shouldBeCalledTimes(1); |
|
43
|
|
|
|
|
44
|
|
|
$this->commandTester->execute([ |
|
45
|
|
|
'command' => 'shortcode:parse', |
|
46
|
|
|
'shortCode' => $shortCode, |
|
47
|
|
|
]); |
|
48
|
|
|
$output = $this->commandTester->getDisplay(); |
|
49
|
|
|
$this->assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @test |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function incorrectShortCodeOutputsErrorMessage() |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$shortCode = 'abc123'; |
|
58
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null) |
|
59
|
|
|
->shouldBeCalledTimes(1); |
|
60
|
|
|
|
|
61
|
|
|
$this->commandTester->execute([ |
|
62
|
|
|
'command' => 'shortcode:parse', |
|
63
|
|
|
'shortCode' => $shortCode, |
|
64
|
|
|
]); |
|
65
|
|
|
$output = $this->commandTester->getDisplay(); |
|
66
|
|
|
$this->assertEquals('No URL found for short code "' . $shortCode . '"' . PHP_EOL, $output); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @test |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function wrongShortCodeFormatOutputsErrorMessage() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$shortCode = 'abc123'; |
|
75
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(new InvalidShortCodeException()) |
|
76
|
|
|
->shouldBeCalledTimes(1); |
|
77
|
|
|
|
|
78
|
|
|
$this->commandTester->execute([ |
|
79
|
|
|
'command' => 'shortcode:parse', |
|
80
|
|
|
'shortCode' => $shortCode, |
|
81
|
|
|
]); |
|
82
|
|
|
$output = $this->commandTester->getDisplay(); |
|
83
|
|
|
$this->assertEquals('Provided short code "' . $shortCode . '" has an invalid format.' . PHP_EOL, $output); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.