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

213 lines
9.1 KiB
PHP
Raw Normal View History

2016-07-30 15:12:56 +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 15:12:56 +03:00
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
2018-11-17 20:06:06 +03:00
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\DataProvider;
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\CreateShortUrlCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
2016-07-30 15:12:56 +03:00
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Model\UrlShorteningResult;
2022-10-24 20:53:13 +03:00
use Shlinkio\Shlink\Core\ShortUrl\UrlShortenerInterface;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Output\OutputInterface;
2016-07-30 15:12:56 +03:00
use Symfony\Component\Console\Tester\CommandTester;
class CreateShortUrlCommandTest extends TestCase
2016-07-30 15:12:56 +03:00
{
use CliTestUtilsTrait;
2020-11-02 13:50:19 +03:00
private const DEFAULT_DOMAIN = 'default.com';
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
2022-10-24 20:53:13 +03:00
private MockObject & UrlShortenerInterface $urlShortener;
private MockObject & ShortUrlStringifierInterface $stringifier;
2016-07-30 15:12:56 +03:00
protected function setUp(): void
2016-07-30 15:12:56 +03:00
{
2022-10-24 20:53:13 +03:00
$this->urlShortener = $this->createMock(UrlShortenerInterface::class);
$this->stringifier = $this->createMock(ShortUrlStringifierInterface::class);
$command = new CreateShortUrlCommand(
$this->urlShortener,
$this->stringifier,
2022-10-24 21:11:25 +03:00
new UrlShortenerOptions(
domain: ['hostname' => self::DEFAULT_DOMAIN, 'schema' => ''],
defaultShortCodesLength: 5,
),
);
$this->commandTester = $this->testerForCommand($command);
2016-07-30 15:12:56 +03:00
}
#[Test]
public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void
2016-07-30 15:12:56 +03:00
{
$shortUrl = ShortUrl::createFake();
$this->urlShortener->expects($this->once())->method('shorten')->withAnyParameters()->willReturn(
UrlShorteningResult::withoutErrorOnEventDispatching($shortUrl),
);
$this->stringifier->expects($this->once())->method('stringify')->with($shortUrl)->willReturn(
'stringified_short_url',
);
2016-07-30 15:12:56 +03:00
$this->commandTester->execute([
2017-10-12 11:13:20 +03:00
'longUrl' => 'http://domain.com/foo/bar',
'--max-visits' => '3',
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
2016-07-30 15:12:56 +03:00
$output = $this->commandTester->getDisplay();
2018-11-17 20:06:06 +03:00
2020-10-04 01:35:14 +03:00
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertStringContainsString('stringified_short_url', $output);
self::assertStringNotContainsString('but the real-time updates cannot', $output);
2016-07-30 15:12:56 +03:00
}
#[Test]
public function exceptionWhileParsingLongUrlOutputsError(): void
2016-07-30 15:12:56 +03:00
{
$url = 'http://domain.com/invalid';
$this->urlShortener->expects($this->once())->method('shorten')->withAnyParameters()->willThrowException(
InvalidUrlException::fromUrl($url),
);
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
2016-07-30 15:12:56 +03:00
$this->commandTester->execute(['longUrl' => $url]);
2016-07-30 15:12:56 +03:00
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
self::assertStringContainsString('Provided URL http://domain.com/invalid is invalid.', $output);
}
#[Test]
public function providingNonUniqueSlugOutputsError(): void
{
$this->urlShortener->expects($this->once())->method('shorten')->withAnyParameters()->willThrowException(
2020-01-01 22:48:31 +03:00
NonUniqueSlugException::fromSlug('my-slug'),
2016-07-30 15:12:56 +03:00
);
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
$this->commandTester->execute(['longUrl' => 'http://domain.com/invalid', '--custom-slug' => 'my-slug']);
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
self::assertStringContainsString('Provided slug "my-slug" is already in use', $output);
2016-07-30 15:12:56 +03:00
}
2018-11-17 20:06:06 +03:00
#[Test]
public function properlyProcessesProvidedTags(): void
2018-11-17 20:06:06 +03:00
{
$shortUrl = ShortUrl::createFake();
$this->urlShortener->expects($this->once())->method('shorten')->with(
$this->callback(function (ShortUrlCreation $creation) {
Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $creation->tags);
return true;
2018-11-17 20:06:06 +03:00
}),
)->willReturn(UrlShorteningResult::withoutErrorOnEventDispatching($shortUrl));
$this->stringifier->expects($this->once())->method('stringify')->with($shortUrl)->willReturn(
'stringified_short_url',
);
2018-11-17 20:06:06 +03:00
$this->commandTester->execute([
'longUrl' => 'http://domain.com/foo/bar',
'--tags' => ['foo,bar', 'baz', 'boo,zar,baz'],
2018-11-17 20:06:06 +03:00
]);
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
self::assertStringContainsString('stringified_short_url', $output);
2018-11-17 20:06:06 +03:00
}
#[Test, DataProvider('provideDomains')]
public function properlyProcessesProvidedDomain(array $input, ?string $expectedDomain): void
{
$this->urlShortener->expects($this->once())->method('shorten')->with(
$this->callback(function (ShortUrlCreation $meta) use ($expectedDomain) {
Assert::assertEquals($expectedDomain, $meta->domain);
return true;
}),
)->willReturn(UrlShorteningResult::withoutErrorOnEventDispatching(ShortUrl::createFake()));
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
$input['longUrl'] = 'http://domain.com/foo/bar';
$this->commandTester->execute($input);
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
}
2023-02-09 11:32:38 +03:00
public static function provideDomains(): iterable
{
yield 'no domain' => [[], null];
yield 'non-default domain foo' => [['--domain' => 'foo.com'], 'foo.com'];
yield 'non-default domain bar' => [['-d' => 'bar.com'], 'bar.com'];
yield 'default domain' => [['--domain' => self::DEFAULT_DOMAIN], null];
}
#[Test, DataProvider('provideFlags')]
2021-12-15 00:21:53 +03:00
public function urlValidationHasExpectedValueBasedOnProvidedFlags(array $options, ?bool $expectedValidateUrl): void
{
$shortUrl = ShortUrl::createFake();
$this->urlShortener->expects($this->once())->method('shorten')->with(
$this->callback(function (ShortUrlCreation $meta) use ($expectedValidateUrl) {
Assert::assertEquals($expectedValidateUrl, $meta->doValidateUrl());
return true;
}),
)->willReturn(UrlShorteningResult::withoutErrorOnEventDispatching($shortUrl));
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
$options['longUrl'] = 'http://domain.com/foo/bar';
$this->commandTester->execute($options);
}
2023-02-09 11:32:38 +03:00
public static function provideFlags(): iterable
{
yield 'no flags' => [[], null];
yield 'validate-url' => [['--validate-url' => true], true];
}
/**
* @param callable(string $output): void $assert
*/
#[Test, DataProvider('provideDispatchBehavior')]
public function warningIsPrintedInVerboseModeWhenDispatchErrors(int $verbosity, callable $assert): void
{
$shortUrl = ShortUrl::createFake();
$this->urlShortener->expects($this->once())->method('shorten')->withAnyParameters()->willReturn(
UrlShorteningResult::withErrorOnEventDispatching($shortUrl, new ServiceNotFoundException()),
);
$this->stringifier->method('stringify')->willReturn('stringified_short_url');
$this->commandTester->execute(['longUrl' => 'http://domain.com/foo/bar'], ['verbosity' => $verbosity]);
$output = $this->commandTester->getDisplay();
$assert($output);
}
public static function provideDispatchBehavior(): iterable
{
$containsAssertion = static fn (string $output) => self::assertStringContainsString(
'but the real-time updates cannot',
$output,
);
$doesNotContainAssertion = static fn (string $output) => self::assertStringNotContainsString(
'but the real-time updates cannot',
$output,
);
yield 'quiet' => [OutputInterface::VERBOSITY_QUIET, $doesNotContainAssertion];
yield 'normal' => [OutputInterface::VERBOSITY_NORMAL, $doesNotContainAssertion];
yield 'verbose' => [OutputInterface::VERBOSITY_VERBOSE, $containsAssertion];
yield 'very verbose' => [OutputInterface::VERBOSITY_VERY_VERBOSE, $containsAssertion];
yield 'debug' => [OutputInterface::VERBOSITY_DEBUG, $containsAssertion];
}
2016-07-30 15:12:56 +03:00
}