2016-08-18 10:10:40 -05:00
|
|
|
<?php
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-16 11:36:02 -05:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
|
2016-08-18 10:10:40 -05:00
|
|
|
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-18 10:10:40 -05:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-09-16 11:36:02 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\GeneratePreviewCommand;
|
2016-08-18 10:10:40 -05: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;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2018-10-28 02:34:02 -05:00
|
|
|
use function count;
|
|
|
|
use function substr_count;
|
2016-08-18 10:10:40 -05:00
|
|
|
|
|
|
|
class GeneratePreviewCommandTest extends TestCase
|
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var CommandTester */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $commandTester;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2016-08-18 10:10:40 -05:00
|
|
|
private $previewGenerator;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2016-08-18 10:10:40 -05:00
|
|
|
private $shortUrlService;
|
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-08-18 10:10:40 -05:00
|
|
|
{
|
|
|
|
$this->previewGenerator = $this->prophesize(PreviewGenerator::class);
|
|
|
|
$this->shortUrlService = $this->prophesize(ShortUrlService::class);
|
|
|
|
|
2018-11-18 09:02:52 -06:00
|
|
|
$command = new GeneratePreviewCommand($this->shortUrlService->reveal(), $this->previewGenerator->reveal());
|
2016-08-18 10:10:40 -05:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2016-08-18 10:10:40 -05:00
|
|
|
public function previewsForEveryUrlAreGenerated()
|
|
|
|
{
|
|
|
|
$paginator = $this->createPaginator([
|
2018-10-28 10:00:54 -05:00
|
|
|
new ShortUrl('http://foo.com'),
|
|
|
|
new ShortUrl('https://bar.com'),
|
|
|
|
new ShortUrl('http://baz.com/something'),
|
2016-08-18 10:10:40 -05:00
|
|
|
]);
|
2018-11-11 06:18:21 -06:00
|
|
|
$this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledOnce();
|
2016-08-18 10:10:40 -05:00
|
|
|
|
2018-11-17 11:06:06 -06: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 10:10:40 -05:00
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute([]);
|
2018-11-17 11:06:06 -06:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
$this->assertStringContainsString('Processing URL http://foo.com', $output);
|
|
|
|
$this->assertStringContainsString('Processing URL https://bar.com', $output);
|
|
|
|
$this->assertStringContainsString('Processing URL http://baz.com/something', $output);
|
|
|
|
$this->assertStringContainsString('Finished processing all URLs', $output);
|
2018-11-17 11:06:06 -06:00
|
|
|
$generatePreview1->shouldHaveBeenCalledOnce();
|
|
|
|
$generatePreview2->shouldHaveBeenCalledOnce();
|
|
|
|
$generatePreview3->shouldHaveBeenCalledOnce();
|
2016-08-18 10:10:40 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2016-08-18 10:10:40 -05:00
|
|
|
public function exceptionWillOutputError()
|
|
|
|
{
|
|
|
|
$items = [
|
2018-10-28 10:00:54 -05:00
|
|
|
new ShortUrl('http://foo.com'),
|
|
|
|
new ShortUrl('https://bar.com'),
|
|
|
|
new ShortUrl('http://baz.com/something'),
|
2016-08-18 10:10:40 -05:00
|
|
|
];
|
|
|
|
$paginator = $this->createPaginator($items);
|
2018-11-11 06:18:21 -06:00
|
|
|
$this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledOnce();
|
2016-08-18 10:10:40 -05:00
|
|
|
$this->previewGenerator->generatePreview(Argument::any())->willThrow(PreviewGenerationException::class)
|
|
|
|
->shouldBeCalledTimes(count($items));
|
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute([]);
|
2016-08-18 10:10:40 -05: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;
|
|
|
|
}
|
|
|
|
}
|