Completed
Push — master ( 5b9784...ff8441 )
by Alejandro
12s
created

DeleteShortUrlServiceTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 37.5 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 36
loc 96
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A deleteByShortCodeThrowsExceptionWhenThresholdIsReached() 0 11 1
A deleteByShortCodeDeletesUrlWhenThresholdIsReachedButExplicitlyIgnored() 12 12 1
A deleteByShortCodeDeletesUrlWhenThresholdIsReachedButCheckIsDisabled() 12 12 1
A deleteByShortCodeDeletesUrlWhenThresholdIsNotReached() 12 12 1
A createService() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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