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

108 lines
3.7 KiB
PHP
Raw Normal View History

2016-08-18 18:10:40 +03:00
<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
2016-08-18 18:10:40 +03:00
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-08-18 18:10:40 +03:00
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\ShortUrl\GeneratePreviewCommand;
2016-08-18 18:10:40 +03:00
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Service\ShortUrlService;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;
use function count;
use function substr_count;
2016-08-18 18:10:40 +03:00
class GeneratePreviewCommandTest extends TestCase
{
/**
* @var CommandTester
*/
protected $commandTester;
/**
* @var ObjectProphecy
*/
private $previewGenerator;
/**
* @var ObjectProphecy
*/
private $shortUrlService;
public function setUp()
{
$this->previewGenerator = $this->prophesize(PreviewGenerator::class);
$this->shortUrlService = $this->prophesize(ShortUrlService::class);
2018-11-18 18:02:52 +03:00
$command = new GeneratePreviewCommand($this->shortUrlService->reveal(), $this->previewGenerator->reveal());
2016-08-18 18:10:40 +03:00
$app = new Application();
$app->add($command);
$this->commandTester = new CommandTester($command);
}
/**
* @test
*/
public function previewsForEveryUrlAreGenerated()
{
$paginator = $this->createPaginator([
new ShortUrl('http://foo.com'),
new ShortUrl('https://bar.com'),
new ShortUrl('http://baz.com/something'),
2016-08-18 18:10:40 +03:00
]);
2018-11-11 15:18:21 +03:00
$this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledOnce();
2016-08-18 18:10:40 +03:00
2018-11-17 20:06:06 +03:00
$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('');
2016-08-18 18:10:40 +03:00
$this->commandTester->execute([
2017-10-12 11:13:20 +03:00
'command' => 'shortcode:process-previews',
2016-08-18 18:10:40 +03:00
]);
2018-11-17 20:06:06 +03:00
$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();
2016-08-18 18:10:40 +03:00
}
/**
* @test
*/
public function exceptionWillOutputError()
{
$items = [
new ShortUrl('http://foo.com'),
new ShortUrl('https://bar.com'),
new ShortUrl('http://baz.com/something'),
2016-08-18 18:10:40 +03:00
];
$paginator = $this->createPaginator($items);
2018-11-11 15:18:21 +03:00
$this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledOnce();
2016-08-18 18:10:40 +03:00
$this->previewGenerator->generatePreview(Argument::any())->willThrow(PreviewGenerationException::class)
->shouldBeCalledTimes(count($items));
$this->commandTester->execute([
2017-10-12 11:13:20 +03:00
'command' => 'shortcode:process-previews',
2016-08-18 18:10:40 +03:00
]);
$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;
}
}