2016-07-30 17:48:02 +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 17:48:02 +03:00
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 17:48:02 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-09-16 19:36:02 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
|
2018-08-11 11:40:44 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2017-10-12 12:28:45 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
2016-07-30 17:48:02 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:34:02 +03:00
|
|
|
use const PHP_EOL;
|
2016-07-30 17:48:02 +03:00
|
|
|
|
|
|
|
class ResolveUrlCommandTest extends TestCase
|
|
|
|
{
|
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 17:48:02 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-07-30 17:48:02 +03:00
|
|
|
{
|
|
|
|
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
2018-11-18 18:02:52 +03:00
|
|
|
$command = new ResolveUrlCommand($this->urlShortener->reveal());
|
2016-07-30 17:48:02 +03:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-04 22:17:02 +03:00
|
|
|
public function correctShortCodeResolvesUrl(): void
|
2016-07-30 17:48:02 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$expectedUrl = 'http://domain.com/foo/bar';
|
2018-10-28 18:00:54 +03:00
|
|
|
$shortUrl = new ShortUrl($expectedUrl);
|
2019-10-04 22:17:02 +03:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode, null)->willReturn($shortUrl)
|
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 17:48:02 +03:00
|
|
|
|
2019-04-14 23:20:58 +03:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 17:48:02 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
$this->assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output);
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-04 22:17:02 +03:00
|
|
|
public function incorrectShortCodeOutputsErrorMessage(): void
|
2016-07-30 17:48:02 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2019-10-04 22:17:02 +03:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(EntityDoesNotExistException::class)
|
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 17:48:02 +03:00
|
|
|
|
2019-04-14 23:20:58 +03:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 17:48:02 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString('Provided short code "' . $shortCode . '" could not be found.', $output);
|
2016-07-30 17:48:02 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-10-04 22:17:02 +03:00
|
|
|
public function wrongShortCodeFormatOutputsErrorMessage(): void
|
2016-07-30 17:48:02 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2019-10-04 22:17:02 +03:00
|
|
|
$this->urlShortener->shortCodeToUrl($shortCode, null)->willThrow(new InvalidShortCodeException())
|
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 17:48:02 +03:00
|
|
|
|
2019-04-14 23:20:58 +03:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 17:48:02 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2019-02-16 12:53:45 +03:00
|
|
|
$this->assertStringContainsString('Provided short code "' . $shortCode . '" has an invalid format.', $output);
|
2016-07-30 17:48:02 +03:00
|
|
|
}
|
|
|
|
}
|