2016-07-30 07:12:56 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-16 11:36:02 -05:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
|
2016-07-30 07:12:56 -05:00
|
|
|
|
2018-11-17 11:06:06 -06:00
|
|
|
use PHPUnit\Framework\Assert;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 07:12:56 -05:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-09-16 11:36:02 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\GenerateShortUrlCommand;
|
2019-10-02 13:22:42 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
2018-09-12 13:32:58 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 07:12:56 -05:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
2019-10-02 13:22:42 -05:00
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
2020-09-24 14:54:03 -05:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2016-07-30 07:12:56 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
2018-11-17 11:06:06 -06:00
|
|
|
class GenerateShortUrlCommandTest extends TestCase
|
2016-07-30 07:12:56 -05:00
|
|
|
{
|
2019-10-11 02:14:25 -05:00
|
|
|
private const DOMAIN_CONFIG = [
|
|
|
|
'schema' => 'http',
|
|
|
|
'hostname' => 'foo.com',
|
|
|
|
];
|
|
|
|
|
2019-12-29 15:27:00 -06:00
|
|
|
private CommandTester $commandTester;
|
|
|
|
private ObjectProphecy $urlShortener;
|
2016-07-30 07:12:56 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-07-30 07:12:56 -05:00
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
2020-02-18 13:34:48 -06:00
|
|
|
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), self::DOMAIN_CONFIG, 5);
|
2016-07-30 07:12:56 -05:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-10-02 13:22:42 -05:00
|
|
|
public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void
|
2016-07-30 07:12:56 -05:00
|
|
|
{
|
2019-10-11 02:14:25 -05:00
|
|
|
$shortUrl = new ShortUrl('');
|
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn($shortUrl);
|
2016-07-30 07:12:56 -05:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
2017-10-12 03:13:20 -05:00
|
|
|
'longUrl' => 'http://domain.com/foo/bar',
|
2018-11-17 11:06:06 -06:00
|
|
|
'--maxVisits' => '3',
|
2016-07-30 07:12:56 -05:00
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 11:06:06 -06:00
|
|
|
|
2019-10-02 13:22:42 -05:00
|
|
|
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
|
2019-10-11 02:14:25 -05:00
|
|
|
$this->assertStringContainsString($shortUrl->toString(self::DOMAIN_CONFIG), $output);
|
2018-11-17 11:06:06 -06:00
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce();
|
2016-07-30 07:12:56 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-10-02 13:22:42 -05:00
|
|
|
public function exceptionWhileParsingLongUrlOutputsError(): void
|
2016-07-30 07:12:56 -05:00
|
|
|
{
|
2019-11-28 11:42:27 -06:00
|
|
|
$url = 'http://domain.com/invalid';
|
|
|
|
$this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(InvalidUrlException::fromUrl($url))
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 07:12:56 -05:00
|
|
|
|
2019-11-28 11:42:27 -06:00
|
|
|
$this->commandTester->execute(['longUrl' => $url]);
|
2016-07-30 07:12:56 -05:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2019-10-02 13:22:42 -05:00
|
|
|
|
|
|
|
$this->assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
|
2019-11-28 11:42:27 -06:00
|
|
|
$this->assertStringContainsString('Provided URL http://domain.com/invalid is invalid.', $output);
|
2019-10-02 13:22:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function providingNonUniqueSlugOutputsError(): void
|
|
|
|
{
|
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(
|
2020-01-01 13:48:31 -06:00
|
|
|
NonUniqueSlugException::fromSlug('my-slug'),
|
2016-07-30 07:12:56 -05:00
|
|
|
);
|
2019-10-02 13:22:42 -05:00
|
|
|
|
|
|
|
$this->commandTester->execute(['longUrl' => 'http://domain.com/invalid', '--customSlug' => 'my-slug']);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
|
|
|
|
$this->assertStringContainsString('Provided slug "my-slug" is already in use', $output);
|
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce();
|
2016-07-30 07:12:56 -05:00
|
|
|
}
|
2018-11-17 11:06:06 -06:00
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-10-02 13:22:42 -05:00
|
|
|
public function properlyProcessesProvidedTags(): void
|
2018-11-17 11:06:06 -06:00
|
|
|
{
|
2019-10-11 02:14:25 -05:00
|
|
|
$shortUrl = new ShortUrl('');
|
2018-11-17 11:06:06 -06:00
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(
|
2020-06-27 04:09:56 -05:00
|
|
|
Argument::type('string'),
|
2018-11-17 11:06:06 -06:00
|
|
|
Argument::that(function (array $tags) {
|
|
|
|
Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $tags);
|
|
|
|
return $tags;
|
|
|
|
}),
|
2020-01-01 13:48:31 -06:00
|
|
|
Argument::cetera(),
|
2019-10-11 02:14:25 -05:00
|
|
|
)->willReturn($shortUrl);
|
2018-11-17 11:06:06 -06:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'longUrl' => 'http://domain.com/foo/bar',
|
2019-02-03 05:09:56 -06:00
|
|
|
'--tags' => ['foo,bar', 'baz', 'boo,zar,baz'],
|
2018-11-17 11:06:06 -06:00
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2019-10-02 13:22:42 -05:00
|
|
|
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
|
2019-10-11 02:14:25 -05:00
|
|
|
$this->assertStringContainsString($shortUrl->toString(self::DOMAIN_CONFIG), $output);
|
2018-11-17 11:06:06 -06:00
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2020-09-24 14:54:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFlags
|
|
|
|
*/
|
|
|
|
public function urlValidationHasExpectedValueBasedOnProvidedTags(array $options, ?bool $expectedValidateUrl): void
|
|
|
|
{
|
|
|
|
$shortUrl = new ShortUrl('');
|
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(
|
|
|
|
Argument::type('string'),
|
|
|
|
Argument::type('array'),
|
|
|
|
Argument::that(function (ShortUrlMeta $meta) use ($expectedValidateUrl) {
|
|
|
|
Assert::assertEquals($expectedValidateUrl, $meta->doValidateUrl());
|
|
|
|
return $meta;
|
|
|
|
}),
|
|
|
|
)->willReturn($shortUrl);
|
|
|
|
|
|
|
|
$options['longUrl'] = 'http://domain.com/foo/bar';
|
|
|
|
$this->commandTester->execute($options);
|
|
|
|
|
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideFlags(): iterable
|
|
|
|
{
|
|
|
|
yield 'no flags' => [[], null];
|
|
|
|
yield 'no-validate-url only' => [['--no-validate-url' => true], false];
|
|
|
|
yield 'validate-url' => [['--validate-url' => true], true];
|
|
|
|
yield 'both flags' => [['--validate-url' => true, '--no-validate-url' => true], false];
|
|
|
|
}
|
2016-07-30 07:12:56 -05:00
|
|
|
}
|