diff --git a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php index 860e3277..128ed9b7 100644 --- a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php +++ b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php @@ -126,7 +126,7 @@ class ProcessVisitsCommand extends Command $this->getApplication()->renderException($e, $this->output); } - throw new IpCannotBeLocatedException('An error occurred while locating IP', 0, $e); + throw new IpCannotBeLocatedException('An error occurred while locating IP', $e->getCode(), $e); } } } diff --git a/module/CLI/test/Command/ShortUrl/GeneratePreviewCommandTest.php b/module/CLI/test/Command/ShortUrl/GeneratePreviewCommandTest.php index 8469d24d..62b9496a 100644 --- a/module/CLI/test/Command/ShortUrl/GeneratePreviewCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GeneratePreviewCommandTest.php @@ -62,13 +62,22 @@ class GeneratePreviewCommandTest extends TestCase ]); $this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledOnce(); - $this->previewGenerator->generatePreview('http://foo.com')->shouldBeCalledOnce(); - $this->previewGenerator->generatePreview('https://bar.com')->shouldBeCalledOnce(); - $this->previewGenerator->generatePreview('http://baz.com/something')->shouldBeCalledOnce(); + $generatePreview1 = $this->previewGenerator->generatePreview('http://foo.com')->willReturn(''); + $generatePreview2 = $this->previewGenerator->generatePreview('https://bar.com')->willReturn(''); + $generatePreview3 = $this->previewGenerator->generatePreview('http://baz.com/something')->willReturn(''); $this->commandTester->execute([ 'command' => 'shortcode:process-previews', ]); + $output = $this->commandTester->getDisplay(); + + $this->assertContains('Processing URL http://foo.com', $output); + $this->assertContains('Processing URL https://bar.com', $output); + $this->assertContains('Processing URL http://baz.com/something', $output); + $this->assertContains('Finished processing all URLs', $output); + $generatePreview1->shouldHaveBeenCalledOnce(); + $generatePreview2->shouldHaveBeenCalledOnce(); + $generatePreview3->shouldHaveBeenCalledOnce(); } /** diff --git a/module/CLI/test/Command/ShortUrl/GenerateShortcodeCommandTest.php b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php similarity index 61% rename from module/CLI/test/Command/ShortUrl/GenerateShortcodeCommandTest.php rename to module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php index 34d7e34a..7f3a339e 100644 --- a/module/CLI/test/Command/ShortUrl/GenerateShortcodeCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php @@ -3,9 +3,11 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl; +use PHPUnit\Framework\Assert; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; +use Psr\Http\Message\UriInterface; use Shlinkio\Shlink\CLI\Command\ShortUrl\GenerateShortUrlCommand; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; @@ -13,9 +15,8 @@ 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; -class GenerateShortcodeCommandTest extends TestCase +class GenerateShortUrlCommandTest extends TestCase { /** * @var CommandTester @@ -43,18 +44,19 @@ class GenerateShortcodeCommandTest extends TestCase */ public function properShortCodeIsCreatedIfLongUrlIsCorrect() { - $this->urlShortener->urlToShortCode(Argument::cetera()) - ->willReturn( - (new ShortUrl(''))->setShortCode('abc123') - ) - ->shouldBeCalledOnce(); + $urlToShortCode = $this->urlShortener->urlToShortCode(Argument::cetera())->willReturn( + (new ShortUrl(''))->setShortCode('abc123') + ); $this->commandTester->execute([ 'command' => 'shortcode:generate', 'longUrl' => 'http://domain.com/foo/bar', + '--maxVisits' => '3', ]); $output = $this->commandTester->getDisplay(); - $this->assertTrue(strpos($output, 'http://foo.com/abc123') > 0); + + $this->assertContains('http://foo.com/abc123', $output); + $urlToShortCode->shouldHaveBeenCalledOnce(); } /** @@ -75,4 +77,29 @@ class GenerateShortcodeCommandTest extends TestCase $output ); } + + /** + * @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(); + } } diff --git a/module/CLI/test/Command/ShortUrl/ListShortcodesCommandTest.php b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php similarity index 82% rename from module/CLI/test/Command/ShortUrl/ListShortcodesCommandTest.php rename to module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php index 3480a289..bbde52fd 100644 --- a/module/CLI/test/Command/ShortUrl/ListShortcodesCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/ListShortUrlsCommandTest.php @@ -15,7 +15,7 @@ use Zend\I18n\Translator\Translator; use Zend\Paginator\Adapter\ArrayAdapter; use Zend\Paginator\Paginator; -class ListShortcodesCommandTest extends TestCase +class ListShortUrlsCommandTest extends TestCase { /** * @var CommandTester @@ -52,7 +52,7 @@ class ListShortcodesCommandTest extends TestCase */ public function loadingMorePagesCallsListMoreTimes() { - // The paginator will return more than one page for the first 3 times + // The paginator will return more than one page $data = []; for ($i = 0; $i < 50; $i++) { $data[] = new ShortUrl('url_' . $i); @@ -64,6 +64,11 @@ class ListShortcodesCommandTest extends TestCase $this->commandTester->setInputs(['y', 'y', 'n']); $this->commandTester->execute(['command' => 'shortcode:list']); + $output = $this->commandTester->getDisplay(); + + $this->assertContains('Continue with page 2?', $output); + $this->assertContains('Continue with page 3?', $output); + $this->assertContains('Continue with page 4?', $output); } /** @@ -82,6 +87,15 @@ class ListShortcodesCommandTest extends TestCase $this->commandTester->setInputs(['n']); $this->commandTester->execute(['command' => 'shortcode:list']); + $output = $this->commandTester->getDisplay(); + + $this->assertContains('url_1', $output); + $this->assertContains('url_9', $output); + $this->assertNotContains('url_10', $output); + $this->assertNotContains('url_20', $output); + $this->assertNotContains('url_30', $output); + $this->assertContains('Continue with page 2?', $output); + $this->assertNotContains('Continue with page 3?', $output); } /** diff --git a/module/Common/src/Service/PreviewGenerator.php b/module/Common/src/Service/PreviewGenerator.php index bc084171..26ff32fc 100644 --- a/module/Common/src/Service/PreviewGenerator.php +++ b/module/Common/src/Service/PreviewGenerator.php @@ -35,11 +35,9 @@ class PreviewGenerator implements PreviewGeneratorInterface /** * Generates and stores preview for provided website and returns the path to the image file * - * @param string $url - * @return string * @throws PreviewGenerationException */ - public function generatePreview($url) + public function generatePreview(string $url): string { /** @var Image $image */ $image = $this->imageBuilder->build(Image::class, ['url' => $url]); diff --git a/module/Common/src/Service/PreviewGeneratorInterface.php b/module/Common/src/Service/PreviewGeneratorInterface.php index f9e5dcae..3fff0158 100644 --- a/module/Common/src/Service/PreviewGeneratorInterface.php +++ b/module/Common/src/Service/PreviewGeneratorInterface.php @@ -10,9 +10,7 @@ interface PreviewGeneratorInterface /** * Generates and stores preview for provided website and returns the path to the image file * - * @param string $url - * @return string * @throws PreviewGenerationException */ - public function generatePreview($url); + public function generatePreview(string $url): string; }