2016-07-30 15:12:56 +03:00
|
|
|
<?php
|
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;
|
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;
|
2018-09-16 19:36:02 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\GenerateShortUrlCommand;
|
2018-09-12 21:32:58 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 15:12:56 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
|
|
|
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
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var CommandTester */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $commandTester;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 21:37:22 +03:00
|
|
|
private $urlShortener;
|
2016-07-30 15:12:56 +03:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
2018-11-18 18:02:52 +03:00
|
|
|
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), [
|
2016-07-30 15:12:56 +03:00
|
|
|
'schema' => 'http',
|
2017-10-12 11:13:20 +03:00
|
|
|
'hostname' => 'foo.com',
|
2016-07-30 15:12:56 +03:00
|
|
|
]);
|
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function properShortCodeIsCreatedIfLongUrlIsCorrect()
|
|
|
|
{
|
2018-11-17 20:06:06 +03:00
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn(
|
|
|
|
(new ShortUrl(''))->setShortCode('abc123')
|
|
|
|
);
|
2016-07-30 15:12:56 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:generate',
|
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->assertContains('http://foo.com/abc123', $output);
|
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce();
|
2016-07-30 15:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function exceptionWhileParsingLongUrlOutputsError()
|
|
|
|
{
|
2016-08-21 11:39:00 +03:00
|
|
|
$this->urlShortener->urlToShortCode(Argument::cetera())->willThrow(new InvalidUrlException())
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 15:12:56 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:generate',
|
2017-10-12 11:13:20 +03:00
|
|
|
'longUrl' => 'http://domain.com/invalid',
|
2016-07-30 15:12:56 +03:00
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2017-12-31 20:12:43 +03:00
|
|
|
$this->assertContains(
|
|
|
|
'Provided URL "http://domain.com/invalid" is invalid.',
|
|
|
|
$output
|
2016-07-30 15:12:56 +03:00
|
|
|
);
|
|
|
|
}
|
2018-11-17 20:06:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function properlyProcessesProvidedTags()
|
|
|
|
{
|
|
|
|
$urlToShortCode = $this->urlShortener->urlToShortCode(
|
|
|
|
Argument::type(UriInterface::class),
|
|
|
|
Argument::that(function (array $tags) {
|
|
|
|
Assert::assertEquals(['foo', 'bar', 'baz', 'boo', 'zar'], $tags);
|
|
|
|
return $tags;
|
|
|
|
}),
|
|
|
|
Argument::cetera()
|
|
|
|
)->willReturn((new ShortUrl(''))->setShortCode('abc123'));
|
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:generate',
|
|
|
|
'longUrl' => 'http://domain.com/foo/bar',
|
|
|
|
'--tags' => ['foo,bar', 'baz', 'boo,zar'],
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertContains('http://foo.com/abc123', $output);
|
|
|
|
$urlToShortCode->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2016-07-30 15:12:56 +03:00
|
|
|
}
|