shlink/module/CLI/test/Command/ShortUrl/ResolveUrlCommandTest.php

65 lines
2.2 KiB
PHP
Raw Normal View History

2016-07-30 17:48:02 +03:00
<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
2016-07-30 17:48:02 +03:00
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
2016-07-30 17:48:02 +03:00
use Symfony\Component\Console\Tester\CommandTester;
use function sprintf;
use const PHP_EOL;
2016-07-30 17:48:02 +03:00
class ResolveUrlCommandTest extends TestCase
{
use CliTestUtilsTrait;
2020-11-02 13:50:19 +03:00
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
2022-10-24 20:53:13 +03:00
private MockObject & ShortUrlResolverInterface $urlResolver;
2016-07-30 17:48:02 +03:00
protected function setUp(): void
2016-07-30 17:48:02 +03:00
{
$this->urlResolver = $this->createMock(ShortUrlResolverInterface::class);
$this->commandTester = $this->testerForCommand(new ResolveUrlCommand($this->urlResolver));
2016-07-30 17:48:02 +03:00
}
#[Test]
public function correctShortCodeResolvesUrl(): void
2016-07-30 17:48:02 +03:00
{
$shortCode = 'abc123';
$expectedUrl = 'http://domain.com/foo/bar';
$shortUrl = ShortUrl::withLongUrl($expectedUrl);
$this->urlResolver->expects($this->once())->method('resolveShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
)->willReturn($shortUrl);
2016-07-30 17:48:02 +03:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 17:48:02 +03:00
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output);
2016-07-30 17:48:02 +03:00
}
#[Test]
public function incorrectShortCodeOutputsErrorMessage(): void
2016-07-30 17:48:02 +03:00
{
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain('abc123');
$shortCode = $identifier->shortCode;
$this->urlResolver->expects($this->once())->method('resolveShortUrl')->with($identifier)->willThrowException(
ShortUrlNotFoundException::fromNotFound($identifier),
);
2016-07-30 17:48:02 +03:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 17:48:02 +03:00
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString(sprintf('No URL found with short code "%s"', $shortCode), $output);
2016-07-30 17:48:02 +03:00
}
}