diff --git a/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php b/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php index 358eb034..b224ad61 100644 --- a/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php +++ b/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php @@ -40,9 +40,9 @@ class GeneratePreviewCommand extends Command PreviewGeneratorInterface $previewGenerator, TranslatorInterface $translator ) { + $this->shortUrlService = $shortUrlService; $this->previewGenerator = $previewGenerator; $this->translator = $translator; - $this->shortUrlService = $shortUrlService; parent::__construct(null); } diff --git a/module/CLI/test/Command/Shortcode/GeneratePreviewCommandTest.php b/module/CLI/test/Command/Shortcode/GeneratePreviewCommandTest.php new file mode 100644 index 00000000..216c004a --- /dev/null +++ b/module/CLI/test/Command/Shortcode/GeneratePreviewCommandTest.php @@ -0,0 +1,99 @@ +previewGenerator = $this->prophesize(PreviewGenerator::class); + $this->shortUrlService = $this->prophesize(ShortUrlService::class); + + $command = new GeneratePreviewCommand( + $this->shortUrlService->reveal(), + $this->previewGenerator->reveal(), + Translator::factory([]) + ); + $app = new Application(); + $app->add($command); + + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function previewsForEveryUrlAreGenerated() + { + $paginator = $this->createPaginator([ + (new ShortUrl())->setOriginalUrl('http://foo.com'), + (new ShortUrl())->setOriginalUrl('https://bar.com'), + (new ShortUrl())->setOriginalUrl('http://baz.com/something'), + ]); + $this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledTimes(1); + + $this->previewGenerator->generatePreview('http://foo.com')->shouldBeCalledTimes(1); + $this->previewGenerator->generatePreview('https://bar.com')->shouldBeCalledTimes(1); + $this->previewGenerator->generatePreview('http://baz.com/something')->shouldBeCalledTimes(1); + + $this->commandTester->execute([ + 'command' => 'shortcode:process-previews' + ]); + } + + /** + * @test + */ + public function exceptionWillOutputError() + { + $items = [ + (new ShortUrl())->setOriginalUrl('http://foo.com'), + (new ShortUrl())->setOriginalUrl('https://bar.com'), + (new ShortUrl())->setOriginalUrl('http://baz.com/something'), + ]; + $paginator = $this->createPaginator($items); + $this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledTimes(1); + $this->previewGenerator->generatePreview(Argument::any())->willThrow(PreviewGenerationException::class) + ->shouldBeCalledTimes(count($items)); + + $this->commandTester->execute([ + 'command' => 'shortcode:process-previews' + ]); + $output = $this->commandTester->getDisplay(); + $this->assertEquals(count($items), substr_count($output, 'Error')); + } + + protected function createPaginator(array $items) + { + $paginator = new Paginator(new ArrayAdapter($items)); + $paginator->setItemCountPerPage(count($items)); + + return $paginator; + } +}