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

73 lines
2.6 KiB
PHP
Raw Normal View History

2016-07-30 16:48:02 +02:00
<?php
2017-10-12 10:13:20 +02:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
2016-07-30 16:48:02 +02:00
2017-03-24 20:34:18 +01:00
use PHPUnit\Framework\TestCase;
2016-07-30 16:48:02 +02:00
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\ShortUrl\ResolveUrlCommand;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
2016-07-30 16:48:02 +02:00
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
use Shlinkio\Shlink\Core\Service\UrlShortener;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use const PHP_EOL;
2016-07-30 16:48:02 +02:00
class ResolveUrlCommandTest extends TestCase
{
/** @var CommandTester */
private $commandTester;
/** @var ObjectProphecy */
private $urlShortener;
2016-07-30 16:48:02 +02:00
2019-02-16 10:53:45 +01:00
public function setUp(): void
2016-07-30 16:48:02 +02:00
{
$this->urlShortener = $this->prophesize(UrlShortener::class);
2018-11-18 16:02:52 +01:00
$command = new ResolveUrlCommand($this->urlShortener->reveal());
2016-07-30 16:48:02 +02:00
$app = new Application();
$app->add($command);
$this->commandTester = new CommandTester($command);
}
2019-02-17 20:28:34 +01:00
/** @test */
2016-07-30 16:48:02 +02:00
public function correctShortCodeResolvesUrl()
{
$shortCode = 'abc123';
$expectedUrl = 'http://domain.com/foo/bar';
$shortUrl = new ShortUrl($expectedUrl);
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($shortUrl)
2018-11-11 13:18:21 +01:00
->shouldBeCalledOnce();
2016-07-30 16:48:02 +02:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 16:48:02 +02:00
$output = $this->commandTester->getDisplay();
$this->assertEquals('Long URL: ' . $expectedUrl . PHP_EOL, $output);
}
2019-02-17 20:28:34 +01:00
/** @test */
2016-07-30 16:48:02 +02:00
public function incorrectShortCodeOutputsErrorMessage()
{
$shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(EntityDoesNotExistException::class)
2018-11-11 13:18:21 +01:00
->shouldBeCalledOnce();
2016-07-30 16:48:02 +02:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 16:48:02 +02:00
$output = $this->commandTester->getDisplay();
2019-02-16 10:53:45 +01:00
$this->assertStringContainsString('Provided short code "' . $shortCode . '" could not be found.', $output);
2016-07-30 16:48:02 +02:00
}
2019-02-17 20:28:34 +01:00
/** @test */
2016-07-30 16:48:02 +02:00
public function wrongShortCodeFormatOutputsErrorMessage()
{
$shortCode = 'abc123';
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(new InvalidShortCodeException())
2018-11-11 13:18:21 +01:00
->shouldBeCalledOnce();
2016-07-30 16:48:02 +02:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 16:48:02 +02:00
$output = $this->commandTester->getDisplay();
2019-02-16 10:53:45 +01:00
$this->assertStringContainsString('Provided short code "' . $shortCode . '" has an invalid format.', $output);
2016-07-30 16:48:02 +02:00
}
}