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);
|
|
|
|
|
2018-09-16 19:36:02 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
|
2016-07-30 15:12:56 +03:00
|
|
|
|
2018-11-17 20:06:06 +03:00
|
|
|
use PHPUnit\Framework\Assert;
|
2022-10-22 14:27:48 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-12-09 11:45:15 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\CreateShortUrlCommand;
|
2019-10-02 21:22:42 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
2016-07-30 15:12:56 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
2019-10-02 21:22:42 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
2022-08-05 09:38:05 +03:00
|
|
|
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2021-02-02 00:55:52 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
|
2022-09-23 19:42:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\UrlShortener;
|
2021-04-08 14:42:56 +03:00
|
|
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
2016-07-30 15:12:56 +03:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
2021-12-09 11:45:15 +03:00
|
|
|
class CreateShortUrlCommandTest extends TestCase
|
2016-07-30 15:12:56 +03:00
|
|
|
{
|
2021-04-08 14:42:56 +03:00
|
|
|
use CliTestUtilsTrait;
|
2020-11-02 13:50:19 +03:00
|
|
|
|
2021-12-09 12:24:58 +03:00
|
|
|
private const DEFAULT_DOMAIN = 'default.com';
|
|
|
|
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
2022-10-22 14:27:48 +03:00
|
|
|
private MockObject $urlShortener;
|
|
|
|
private MockObject $stringifier;
|
2016-07-30 15:12:56 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2016-07-30 15:12:56 +03:00
|
|
|
{
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->urlShortener = $this->createMock(UrlShortener::class);
|
|
|
|
$this->stringifier = $this->createMock(ShortUrlStringifierInterface::class);
|
2021-02-02 00:55:52 +03:00
|
|
|
|
2021-12-09 12:24:58 +03:00
|
|
|
$command = new CreateShortUrlCommand(
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->urlShortener,
|
|
|
|
$this->stringifier,
|
2022-09-17 16:54:43 +03:00
|
|
|
new UrlShortenerOptions(domain: ['hostname' => self::DEFAULT_DOMAIN], defaultShortCodesLength: 5),
|
2021-12-09 12:24:58 +03:00
|
|
|
);
|
2021-04-08 14:42:56 +03:00
|
|
|
$this->commandTester = $this->testerForCommand($command);
|
2016-07-30 15:12:56 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-02 21:22:42 +03:00
|
|
|
public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void
|
2016-07-30 15:12:56 +03:00
|
|
|
{
|
2021-01-30 16:18:44 +03:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->urlShortener->expects($this->once())->method('shorten')->withAnyParameters()->willReturn($shortUrl);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->stringifier->expects($this->once())->method('stringify')->with($shortUrl)->willReturn(
|
2022-10-22 14:27:48 +03:00
|
|
|
'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',
|
2021-01-30 13:25:20 +03:00
|
|
|
'--max-visits' => '3',
|
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());
|
2021-02-02 00:55:52 +03:00
|
|
|
self::assertStringContainsString('stringified_short_url', $output);
|
2016-07-30 15:12:56 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-02 21:22:42 +03:00
|
|
|
public function exceptionWhileParsingLongUrlOutputsError(): void
|
2016-07-30 15:12:56 +03:00
|
|
|
{
|
2019-11-28 20:42:27 +03:00
|
|
|
$url = 'http://domain.com/invalid';
|
2022-10-22 14:27:48 +03:00
|
|
|
$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
|
|
|
|
2019-11-28 20:42:27 +03:00
|
|
|
$this->commandTester->execute(['longUrl' => $url]);
|
2016-07-30 15:12:56 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2019-10-02 21:22:42 +03:00
|
|
|
|
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);
|
2019-10-02 21:22:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function providingNonUniqueSlugOutputsError(): void
|
|
|
|
{
|
2022-10-22 14:27:48 +03:00
|
|
|
$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
|
|
|
);
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
|
2019-10-02 21:22:42 +03:00
|
|
|
|
2021-01-30 13:25:20 +03:00
|
|
|
$this->commandTester->execute(['longUrl' => 'http://domain.com/invalid', '--custom-slug' => 'my-slug']);
|
2019-10-02 21:22:42 +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 slug "my-slug" is already in use', $output);
|
2016-07-30 15:12:56 +03:00
|
|
|
}
|
2018-11-17 20:06:06 +03:00
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-02 21:22:42 +03:00
|
|
|
public function properlyProcessesProvidedTags(): void
|
2018-11-17 20:06:06 +03:00
|
|
|
{
|
2021-01-30 16:18:44 +03:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->urlShortener->expects($this->once())->method('shorten')->with(
|
|
|
|
$this->callback(function (ShortUrlCreation $meta) {
|
2021-01-30 21:17:12 +03:00
|
|
|
$tags = $meta->getTags();
|
2018-11-17 20:06:06 +03:00
|
|
|
Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $tags);
|
2021-01-30 21:17:12 +03:00
|
|
|
return true;
|
2018-11-17 20:06:06 +03:00
|
|
|
}),
|
2019-10-11 10:14:25 +03:00
|
|
|
)->willReturn($shortUrl);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->stringifier->expects($this->once())->method('stringify')->with($shortUrl)->willReturn(
|
2022-10-22 14:27:48 +03:00
|
|
|
'stringified_short_url',
|
|
|
|
);
|
2018-11-17 20:06:06 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'longUrl' => 'http://domain.com/foo/bar',
|
2019-02-03 14:09:56 +03:00
|
|
|
'--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());
|
2021-02-02 00:55:52 +03:00
|
|
|
self::assertStringContainsString('stringified_short_url', $output);
|
2018-11-17 20:06:06 +03:00
|
|
|
}
|
2020-09-24 22:54:03 +03:00
|
|
|
|
2021-12-09 12:24:58 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideDomains
|
|
|
|
*/
|
|
|
|
public function properlyProcessesProvidedDomain(array $input, ?string $expectedDomain): void
|
|
|
|
{
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->urlShortener->expects($this->once())->method('shorten')->with(
|
|
|
|
$this->callback(function (ShortUrlCreation $meta) use ($expectedDomain) {
|
2021-12-09 12:24:58 +03:00
|
|
|
Assert::assertEquals($expectedDomain, $meta->getDomain());
|
|
|
|
return true;
|
|
|
|
}),
|
|
|
|
)->willReturn(ShortUrl::createEmpty());
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
|
2021-12-09 12:24:58 +03:00
|
|
|
|
|
|
|
$input['longUrl'] = 'http://domain.com/foo/bar';
|
|
|
|
$this->commandTester->execute($input);
|
|
|
|
|
|
|
|
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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];
|
|
|
|
}
|
|
|
|
|
2020-09-24 22:54:03 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFlags
|
|
|
|
*/
|
2021-12-15 00:21:53 +03:00
|
|
|
public function urlValidationHasExpectedValueBasedOnProvidedFlags(array $options, ?bool $expectedValidateUrl): void
|
2020-09-24 22:54:03 +03:00
|
|
|
{
|
2021-01-30 16:18:44 +03:00
|
|
|
$shortUrl = ShortUrl::createEmpty();
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->urlShortener->expects($this->once())->method('shorten')->with(
|
|
|
|
$this->callback(function (ShortUrlCreation $meta) use ($expectedValidateUrl) {
|
2020-09-24 22:54:03 +03:00
|
|
|
Assert::assertEquals($expectedValidateUrl, $meta->doValidateUrl());
|
2022-10-22 14:27:48 +03:00
|
|
|
return true;
|
2020-09-24 22:54:03 +03:00
|
|
|
}),
|
|
|
|
)->willReturn($shortUrl);
|
2022-10-22 14:27:48 +03:00
|
|
|
$this->stringifier->method('stringify')->with($this->isInstanceOf(ShortUrl::class))->willReturn('');
|
2020-09-24 22:54:03 +03:00
|
|
|
|
|
|
|
$options['longUrl'] = 'http://domain.com/foo/bar';
|
|
|
|
$this->commandTester->execute($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideFlags(): iterable
|
|
|
|
{
|
|
|
|
yield 'no flags' => [[], null];
|
|
|
|
yield 'validate-url' => [['--validate-url' => true], true];
|
|
|
|
}
|
2016-07-30 15:12:56 +03:00
|
|
|
}
|