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

110 lines
4.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
2018-11-17 20:06:06 +03:00
use PHPUnit\Framework\Assert;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-07-30 15:12:56 +03:00
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
2018-11-17 20:06:06 +03:00
use Psr\Http\Message\UriInterface;
use Shlinkio\Shlink\CLI\Command\ShortUrl\GenerateShortUrlCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
2016-07-30 15:12:56 +03:00
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
2016-07-30 15:12:56 +03:00
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
2018-11-17 20:06:06 +03:00
class GenerateShortUrlCommandTest extends TestCase
2016-07-30 15:12:56 +03:00
{
private const DOMAIN_CONFIG = [
'schema' => 'http',
'hostname' => 'foo.com',
];
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
private ObjectProphecy $urlShortener;
2016-07-30 15:12:56 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2016-07-30 15:12:56 +03:00
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), self::DOMAIN_CONFIG);
2016-07-30 15:12:56 +03:00
$app = new Application();
$app->add($command);
$this->commandTester = new CommandTester($command);
}
2019-02-17 22:28:34 +03:00
/** @test */
public function properShortCodeIsCreatedIfLongUrlIsCorrect(): void
2016-07-30 15:12:56 +03:00
{
$shortUrl = new ShortUrl('');
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn($shortUrl);
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',
2018-11-17 20:06:06 +03:00
'--maxVisits' => '3',
2016-07-30 15:12:56 +03:00
]);
$output = $this->commandTester->getDisplay();
2018-11-17 20:06:06 +03:00
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
$this->assertStringContainsString($shortUrl->toString(self::DOMAIN_CONFIG), $output);
2018-11-17 20:06:06 +03:00
$urlToShortCode->shouldHaveBeenCalledOnce();
2016-07-30 15:12:56 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function exceptionWhileParsingLongUrlOutputsError(): void
2016-07-30 15:12:56 +03:00
{
$url = 'http://domain.com/invalid';
$this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(InvalidUrlException::fromUrl($url))
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
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();
$this->assertEquals(ExitCodes::EXIT_FAILURE, $this->commandTester->getStatusCode());
$this->assertStringContainsString('Provided URL http://domain.com/invalid is invalid.', $output);
}
/** @test */
public function providingNonUniqueSlugOutputsError(): void
{
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(
2020-01-01 22:48:31 +03:00
NonUniqueSlugException::fromSlug('my-slug'),
2016-07-30 15:12:56 +03: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 15:12:56 +03:00
}
2018-11-17 20:06:06 +03:00
2019-02-17 22:28:34 +03:00
/** @test */
public function properlyProcessesProvidedTags(): void
2018-11-17 20:06:06 +03:00
{
$shortUrl = new ShortUrl('');
2018-11-17 20:06:06 +03:00
$urlToShortCode = $this->urlShortener->urlToShortCode(
Argument::type(UriInterface::class),
Argument::that(function (array $tags) {
Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $tags);
return $tags;
}),
2020-01-01 22:48:31 +03:00
Argument::cetera(),
)->willReturn($shortUrl);
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();
$this->assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
$this->assertStringContainsString($shortUrl->toString(self::DOMAIN_CONFIG), $output);
2018-11-17 20:06:06 +03:00
$urlToShortCode->shouldHaveBeenCalledOnce();
}
2016-07-30 15:12:56 +03:00
}