DeleteShortUrlServiceTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 38
dl 0
loc 83
rs 10
c 0
b 0
f 0

6 Methods

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