|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Core\Service\ShortUrl; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Prophecy\Argument; |
|
10
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Exception\DeleteShortUrlException; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Options\DeleteShortUrlsOptions; |
|
15
|
|
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface; |
|
16
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlService; |
|
17
|
|
|
|
|
18
|
|
|
class DeleteShortUrlServiceTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var DeleteShortUrlService |
|
22
|
|
|
*/ |
|
23
|
|
|
private $service; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var ObjectProphecy |
|
26
|
|
|
*/ |
|
27
|
|
|
private $em; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$shortUrl = (new ShortUrl())->setShortCode('abc123') |
|
32
|
|
|
->setVisits(new ArrayCollection(\array_map(function () { |
|
33
|
|
|
return new Visit(); |
|
34
|
|
|
}, \range(0, 10)))); |
|
35
|
|
|
|
|
36
|
|
|
$this->em = $this->prophesize(EntityManagerInterface::class); |
|
37
|
|
|
|
|
38
|
|
|
$repo = $this->prophesize(ShortUrlRepositoryInterface::class); |
|
39
|
|
|
$repo->findOneBy(Argument::type('array'))->willReturn($shortUrl); |
|
40
|
|
|
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function deleteByShortCodeThrowsExceptionWhenThresholdIsReached() |
|
47
|
|
|
{ |
|
48
|
|
|
$service = $this->createService(); |
|
49
|
|
|
|
|
50
|
|
|
$this->expectException(DeleteShortUrlException::class); |
|
51
|
|
|
$this->expectExceptionMessage( |
|
52
|
|
|
'Impossible to delete short URL with short code "abc123" since it has more than "5" visits.' |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$service->deleteByShortCode('abc123'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @test |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function deleteByShortCodeDeletesUrlWhenThresholdIsReachedButExplicitlyIgnored() |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
$service = $this->createService(); |
|
64
|
|
|
|
|
65
|
|
|
$remove = $this->em->remove(Argument::type(ShortUrl::class))->willReturn(null); |
|
66
|
|
|
$flush = $this->em->flush()->willReturn(null); |
|
67
|
|
|
|
|
68
|
|
|
$service->deleteByShortCode('abc123', true); |
|
69
|
|
|
|
|
70
|
|
|
$remove->shouldHaveBeenCalledTimes(1); |
|
71
|
|
|
$flush->shouldHaveBeenCalledTimes(1); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @test |
|
76
|
|
|
*/ |
|
77
|
|
View Code Duplication |
public function deleteByShortCodeDeletesUrlWhenThresholdIsReachedButCheckIsDisabled() |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$service = $this->createService(false); |
|
80
|
|
|
|
|
81
|
|
|
$remove = $this->em->remove(Argument::type(ShortUrl::class))->willReturn(null); |
|
82
|
|
|
$flush = $this->em->flush()->willReturn(null); |
|
83
|
|
|
|
|
84
|
|
|
$service->deleteByShortCode('abc123'); |
|
85
|
|
|
|
|
86
|
|
|
$remove->shouldHaveBeenCalledTimes(1); |
|
87
|
|
|
$flush->shouldHaveBeenCalledTimes(1); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @test |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function deleteByShortCodeDeletesUrlWhenThresholdIsNotReached() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$service = $this->createService(true, 100); |
|
96
|
|
|
|
|
97
|
|
|
$remove = $this->em->remove(Argument::type(ShortUrl::class))->willReturn(null); |
|
98
|
|
|
$flush = $this->em->flush()->willReturn(null); |
|
99
|
|
|
|
|
100
|
|
|
$service->deleteByShortCode('abc123'); |
|
101
|
|
|
|
|
102
|
|
|
$remove->shouldHaveBeenCalledTimes(1); |
|
103
|
|
|
$flush->shouldHaveBeenCalledTimes(1); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private function createService(bool $checkVisitsThreshold = true, int $visitsThreshold = 5): DeleteShortUrlService |
|
107
|
|
|
{ |
|
108
|
|
|
return new DeleteShortUrlService($this->em->reveal(), new DeleteShortUrlsOptions([ |
|
109
|
|
|
'visitsThreshold' => $visitsThreshold, |
|
110
|
|
|
'checkVisitsThreshold' => $checkVisitsThreshold, |
|
111
|
|
|
])); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
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.