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

79 lines
2.3 KiB
PHP
Raw Normal View History

2016-07-30 15:12:56 +03:00
<?php
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
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;
use Shlinkio\Shlink\CLI\Command\ShortUrl\GenerateShortUrlCommand;
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;
use Zend\I18n\Translator\Translator;
use function strpos;
2016-07-30 15:12:56 +03:00
class GenerateShortcodeCommandTest extends TestCase
{
/**
* @var CommandTester
*/
protected $commandTester;
/**
* @var ObjectProphecy
*/
protected $urlShortener;
public function setUp()
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
$command = new GenerateShortUrlCommand($this->urlShortener->reveal(), Translator::factory([]), [
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()
{
$this->urlShortener->urlToShortCode(Argument::cetera())
->willReturn(
(new ShortUrl(''))->setShortCode('abc123')
)
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/foo/bar',
2016-07-30 15:12:56 +03:00
]);
$output = $this->commandTester->getDisplay();
$this->assertTrue(strpos($output, 'http://foo.com/abc123') > 0);
}
/**
* @test
*/
public function exceptionWhileParsingLongUrlOutputsError()
{
$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();
$this->assertContains(
'Provided URL "http://domain.com/invalid" is invalid.',
$output
2016-07-30 15:12:56 +03:00
);
}
}