|
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\DeleteShortUrlCommand; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Exception; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface; |
|
12
|
|
|
use Symfony\Component\Console\Application; |
|
13
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
14
|
|
|
use Zend\I18n\Translator\Translator; |
|
15
|
|
|
|
|
16
|
|
|
class DeleteShortCodeCommandTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var CommandTester |
|
20
|
|
|
*/ |
|
21
|
|
|
private $commandTester; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var ObjectProphecy |
|
24
|
|
|
*/ |
|
25
|
|
|
private $service; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->service = $this->prophesize(DeleteShortUrlServiceInterface::class); |
|
30
|
|
|
|
|
31
|
|
|
$command = new DeleteShortUrlCommand($this->service->reveal(), Translator::factory([])); |
|
32
|
|
|
$app = new Application(); |
|
33
|
|
|
$app->add($command); |
|
34
|
|
|
|
|
35
|
|
|
$this->commandTester = new CommandTester($command); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function successMessageIsPrintedIfUrlIsProperlyDeleted() |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$shortCode = 'abc123'; |
|
44
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->will(function () { |
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]); |
|
48
|
|
|
$output = $this->commandTester->getDisplay(); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertContains(\sprintf('Short URL with short code "%s" successfully deleted.', $shortCode), $output); |
|
51
|
|
|
$deleteByShortCode->shouldHaveBeenCalledTimes(1); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @test |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function invalidShortCodePrintsMessage() |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$shortCode = 'abc123'; |
|
60
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow( |
|
61
|
|
|
Exception\InvalidShortCodeException::class |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]); |
|
65
|
|
|
$output = $this->commandTester->getDisplay(); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertContains(\sprintf('Provided short code "%s" could not be found.', $shortCode), $output); |
|
68
|
|
|
$deleteByShortCode->shouldHaveBeenCalledTimes(1); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @test |
|
73
|
|
|
*/ |
|
74
|
|
|
public function deleteIsRetriedWhenThresholdIsReachedAndQuestionIsAccepted() |
|
75
|
|
|
{ |
|
76
|
|
|
$shortCode = 'abc123'; |
|
77
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, Argument::type('bool'))->will( |
|
78
|
|
|
function (array $args) { |
|
79
|
|
|
$ignoreThreshold = \array_pop($args); |
|
80
|
|
|
|
|
81
|
|
|
if (!$ignoreThreshold) { |
|
82
|
|
|
throw new Exception\DeleteShortUrlException(10); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
); |
|
86
|
|
|
$this->commandTester->setInputs(['yes']); |
|
87
|
|
|
|
|
88
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]); |
|
89
|
|
|
$output = $this->commandTester->getDisplay(); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertContains(\sprintf( |
|
92
|
|
|
'It was not possible to delete the short URL with short code "%s" because it has more than 10 visits.', |
|
93
|
|
|
$shortCode |
|
94
|
|
|
), $output); |
|
95
|
|
|
$this->assertContains(\sprintf('Short URL with short code "%s" successfully deleted.', $shortCode), $output); |
|
96
|
|
|
$deleteByShortCode->shouldHaveBeenCalledTimes(2); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @test |
|
101
|
|
|
*/ |
|
102
|
|
|
public function deleteIsNotRetriedWhenThresholdIsReachedAndQuestionIsDeclined() |
|
103
|
|
|
{ |
|
104
|
|
|
$shortCode = 'abc123'; |
|
105
|
|
|
$deleteByShortCode = $this->service->deleteByShortCode($shortCode, false)->willThrow( |
|
106
|
|
|
new Exception\DeleteShortUrlException(10) |
|
107
|
|
|
); |
|
108
|
|
|
$this->commandTester->setInputs(['no']); |
|
109
|
|
|
|
|
110
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]); |
|
111
|
|
|
$output = $this->commandTester->getDisplay(); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertContains(\sprintf( |
|
114
|
|
|
'It was not possible to delete the short URL with short code "%s" because it has more than 10 visits.', |
|
115
|
|
|
$shortCode |
|
116
|
|
|
), $output); |
|
117
|
|
|
$this->assertContains('Short URL was not deleted.', $output); |
|
118
|
|
|
$deleteByShortCode->shouldHaveBeenCalledTimes(1); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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.