|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Prophecy\Argument; |
|
8
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
9
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\GeneratePreviewCommand; |
|
10
|
|
|
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException; |
|
11
|
|
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlService; |
|
14
|
|
|
use Symfony\Component\Console\Application; |
|
15
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
16
|
|
|
use Zend\I18n\Translator\Translator; |
|
17
|
|
|
use Zend\Paginator\Adapter\ArrayAdapter; |
|
18
|
|
|
use Zend\Paginator\Paginator; |
|
19
|
|
|
|
|
20
|
|
|
class GeneratePreviewCommandTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var CommandTester |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $commandTester; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ObjectProphecy |
|
28
|
|
|
*/ |
|
29
|
|
|
private $previewGenerator; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ObjectProphecy |
|
32
|
|
|
*/ |
|
33
|
|
|
private $shortUrlService; |
|
34
|
|
|
|
|
35
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$this->previewGenerator = $this->prophesize(PreviewGenerator::class); |
|
38
|
|
|
$this->shortUrlService = $this->prophesize(ShortUrlService::class); |
|
39
|
|
|
|
|
40
|
|
|
$command = new GeneratePreviewCommand( |
|
41
|
|
|
$this->shortUrlService->reveal(), |
|
42
|
|
|
$this->previewGenerator->reveal(), |
|
43
|
|
|
Translator::factory([]) |
|
44
|
|
|
); |
|
45
|
|
|
$app = new Application(); |
|
46
|
|
|
$app->add($command); |
|
47
|
|
|
|
|
48
|
|
|
$this->commandTester = new CommandTester($command); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @test |
|
53
|
|
|
*/ |
|
54
|
|
|
public function previewsForEveryUrlAreGenerated() |
|
55
|
|
|
{ |
|
56
|
|
|
$paginator = $this->createPaginator([ |
|
57
|
|
|
(new ShortUrl())->setOriginalUrl('http://foo.com'), |
|
|
|
|
|
|
58
|
|
|
(new ShortUrl())->setOriginalUrl('https://bar.com'), |
|
|
|
|
|
|
59
|
|
|
(new ShortUrl())->setOriginalUrl('http://baz.com/something'), |
|
|
|
|
|
|
60
|
|
|
]); |
|
61
|
|
|
$this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledTimes(1); |
|
62
|
|
|
|
|
63
|
|
|
$this->previewGenerator->generatePreview('http://foo.com')->shouldBeCalledTimes(1); |
|
64
|
|
|
$this->previewGenerator->generatePreview('https://bar.com')->shouldBeCalledTimes(1); |
|
65
|
|
|
$this->previewGenerator->generatePreview('http://baz.com/something')->shouldBeCalledTimes(1); |
|
66
|
|
|
|
|
67
|
|
|
$this->commandTester->execute([ |
|
68
|
|
|
'command' => 'shortcode:process-previews', |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @test |
|
74
|
|
|
*/ |
|
75
|
|
|
public function exceptionWillOutputError() |
|
76
|
|
|
{ |
|
77
|
|
|
$items = [ |
|
78
|
|
|
(new ShortUrl())->setOriginalUrl('http://foo.com'), |
|
|
|
|
|
|
79
|
|
|
(new ShortUrl())->setOriginalUrl('https://bar.com'), |
|
|
|
|
|
|
80
|
|
|
(new ShortUrl())->setOriginalUrl('http://baz.com/something'), |
|
|
|
|
|
|
81
|
|
|
]; |
|
82
|
|
|
$paginator = $this->createPaginator($items); |
|
83
|
|
|
$this->shortUrlService->listShortUrls(1)->willReturn($paginator)->shouldBeCalledTimes(1); |
|
84
|
|
|
$this->previewGenerator->generatePreview(Argument::any())->willThrow(PreviewGenerationException::class) |
|
85
|
|
|
->shouldBeCalledTimes(count($items)); |
|
86
|
|
|
|
|
87
|
|
|
$this->commandTester->execute([ |
|
88
|
|
|
'command' => 'shortcode:process-previews', |
|
89
|
|
|
]); |
|
90
|
|
|
$output = $this->commandTester->getDisplay(); |
|
91
|
|
|
$this->assertEquals(count($items), substr_count($output, 'Error')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function createPaginator(array $items) |
|
95
|
|
|
{ |
|
96
|
|
|
$paginator = new Paginator(new ArrayAdapter($items)); |
|
97
|
|
|
$paginator->setItemCountPerPage(count($items)); |
|
98
|
|
|
|
|
99
|
|
|
return $paginator; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.