|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Prophecy\PhpUnit\ProphecyTrait; |
|
9
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
10
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface; |
|
15
|
|
|
use Symfony\Component\Console\Application; |
|
16
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
17
|
|
|
|
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
use const PHP_EOL; |
|
21
|
|
|
|
|
22
|
|
|
class ResolveUrlCommandTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
use ProphecyTrait; |
|
25
|
|
|
|
|
26
|
|
|
private CommandTester $commandTester; |
|
27
|
|
|
private ObjectProphecy $urlResolver; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class); |
|
32
|
|
|
$command = new ResolveUrlCommand($this->urlResolver->reveal()); |
|
33
|
|
|
$app = new Application(); |
|
34
|
|
|
$app->add($command); |
|
35
|
|
|
|
|
36
|
|
|
$this->commandTester = new CommandTester($command); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** @test */ |
|
40
|
|
|
public function correctShortCodeResolvesUrl(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$shortCode = 'abc123'; |
|
43
|
|
|
$expectedUrl = 'http://domain.com/foo/bar'; |
|
44
|
|
|
$shortUrl = new ShortUrl($expectedUrl); |
|
45
|
|
|
$this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode))->willReturn($shortUrl) |
|
46
|
|
|
->shouldBeCalledOnce(); |
|
47
|
|
|
|
|
48
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]); |
|
49
|
|
|
$output = $this->commandTester->getDisplay(); |
|
50
|
|
|
self::assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @test */ |
|
54
|
|
|
public function incorrectShortCodeOutputsErrorMessage(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$identifier = new ShortUrlIdentifier('abc123'); |
|
57
|
|
|
$shortCode = $identifier->shortCode(); |
|
58
|
|
|
|
|
59
|
|
|
$this->urlResolver->resolveShortUrl($identifier) |
|
60
|
|
|
->willThrow(ShortUrlNotFoundException::fromNotFound($identifier)) |
|
61
|
|
|
->shouldBeCalledOnce(); |
|
62
|
|
|
|
|
63
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]); |
|
64
|
|
|
$output = $this->commandTester->getDisplay(); |
|
65
|
|
|
self::assertStringContainsString(sprintf('No URL found with short code "%s"', $shortCode), $output); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|