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
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-22 13:44:10 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 20:34:18 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-09-16 18:36:02 +02:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
|
2019-11-24 23:11:25 +01:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
|
2022-09-23 19:03:32 +02:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 18:05:17 +02:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2022-09-23 18:42:38 +02:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
2023-06-18 10:51:59 +02:00
|
|
|
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
|
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
|
|
|
|
{
|
2019-12-29 22:27:00 +01:00
|
|
|
private CommandTester $commandTester;
|
2022-10-24 19:53:13 +02:00
|
|
|
private MockObject & ShortUrlResolverInterface $urlResolver;
|
2016-07-30 16:48:02 +02:00
|
|
|
|
2022-09-11 12:02:49 +02:00
|
|
|
protected function setUp(): void
|
2016-07-30 16:48:02 +02:00
|
|
|
{
|
2022-10-22 13:44:10 +02:00
|
|
|
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
|
2023-06-18 10:51:59 +02:00
|
|
|
$this->commandTester = CliTestUtils::testerForCommand(new ResolveUrlCommand($this->urlResolver));
|
2016-07-30 16:48:02 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +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);
|
2022-10-22 13:44:10 +02:00
|
|
|
$this->urlResolver->expects($this->once())->method('resolveShortUrl')->with(
|
2022-10-23 18:15:57 +02:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
2022-10-22 13:44:10 +02:00
|
|
|
)->willReturn($shortUrl);
|
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
|
|
|
}
|
|
|
|
|
2023-02-09 20:42:18 +01:00
|
|
|
#[Test]
|
2019-10-04 21:17:02 +02:00
|
|
|
public function incorrectShortCodeOutputsErrorMessage(): void
|
2016-07-30 16:48:02 +02:00
|
|
|
{
|
2022-04-23 14:00:47 +02:00
|
|
|
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain('abc123');
|
|
|
|
$shortCode = $identifier->shortCode;
|
2020-02-01 11:46:54 +01:00
|
|
|
|
2022-10-23 18:15:57 +02:00
|
|
|
$this->urlResolver->expects($this->once())->method('resolveShortUrl')->with($identifier)->willThrowException(
|
|
|
|
ShortUrlNotFoundException::fromNotFound($identifier),
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
}
|