2016-07-30 16:48:02 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-16 18:36:02 +02:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
|
2016-07-30 16:48:02 +02:00
|
|
|
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 16:48:02 +02:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-09-16 18:36:02 +02:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
|
2018-08-11 10:40:44 +02:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2019-11-24 23:11:25 +01:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2020-02-01 11:46:54 +01:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2020-01-26 19:21:51 +01:00
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface;
|
2021-04-08 13:42:56 +02:00
|
|
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
2016-07-30 16:48:02 +02:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2019-02-26 22:56:43 +01:00
|
|
|
|
2019-11-25 18:54:25 +01:00
|
|
|
use function sprintf;
|
|
|
|
|
2018-10-28 08:34:02 +01:00
|
|
|
use const PHP_EOL;
|
2016-07-30 16:48:02 +02:00
|
|
|
|
|
|
|
class ResolveUrlCommandTest extends TestCase
|
|
|
|
{
|
2021-04-08 13:42:56 +02:00
|
|
|
use CliTestUtilsTrait;
|
2020-11-02 11:50:19 +01:00
|
|
|
|
2019-12-29 22:27:00 +01:00
|
|
|
private CommandTester $commandTester;
|
2020-01-26 19:21:51 +01:00
|
|
|
private ObjectProphecy $urlResolver;
|
2016-07-30 16:48:02 +02:00
|
|
|
|
2019-02-16 10:53:45 +01:00
|
|
|
public function setUp(): void
|
2016-07-30 16:48:02 +02:00
|
|
|
{
|
2020-01-26 19:21:51 +01:00
|
|
|
$this->urlResolver = $this->prophesize(ShortUrlResolverInterface::class);
|
2021-04-08 13:42:56 +02:00
|
|
|
$this->commandTester = $this->testerForCommand(new ResolveUrlCommand($this->urlResolver->reveal()));
|
2016-07-30 16:48:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2019-10-04 21:17:02 +02:00
|
|
|
public function correctShortCodeResolvesUrl(): void
|
2016-07-30 16:48:02 +02:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$expectedUrl = 'http://domain.com/foo/bar';
|
2021-01-30 14:18:44 +01:00
|
|
|
$shortUrl = ShortUrl::withLongUrl($expectedUrl);
|
2020-02-01 11:46:54 +01:00
|
|
|
$this->urlResolver->resolveShortUrl(new ShortUrlIdentifier($shortCode))->willReturn($shortUrl)
|
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 16:48:02 +02:00
|
|
|
|
2019-04-14 22:20:58 +02:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 16:48:02 +02:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output);
|
2016-07-30 16:48:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 20:28:34 +01:00
|
|
|
/** @test */
|
2019-10-04 21:17:02 +02:00
|
|
|
public function incorrectShortCodeOutputsErrorMessage(): void
|
2016-07-30 16:48:02 +02:00
|
|
|
{
|
2020-02-01 11:46:54 +01:00
|
|
|
$identifier = new ShortUrlIdentifier('abc123');
|
|
|
|
$shortCode = $identifier->shortCode();
|
|
|
|
|
|
|
|
$this->urlResolver->resolveShortUrl($identifier)
|
|
|
|
->willThrow(ShortUrlNotFoundException::fromNotFound($identifier))
|
2019-11-28 18:42:27 +01:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 16:48:02 +02:00
|
|
|
|
2019-04-14 22:20:58 +02:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 16:48:02 +02:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2020-10-04 00:35:14 +02:00
|
|
|
self::assertStringContainsString(sprintf('No URL found with short code "%s"', $shortCode), $output);
|
2016-07-30 16:48:02 +02:00
|
|
|
}
|
|
|
|
}
|