ShortCodeHelperTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 34
dl 0
loc 63
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A provideDomains() 0 4 1
A inUseSlugReturnsError() 0 13 1
A shortCodeIsRegeneratedIfAlreadyInUse() 0 20 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Core\Service\ShortUrl;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\PhpUnit\ProphecyTrait;
10
use Prophecy\Prophecy\ObjectProphecy;
11
use Shlinkio\Shlink\Core\Entity\Domain;
12
use Shlinkio\Shlink\Core\Entity\ShortUrl;
13
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
14
use Shlinkio\Shlink\Core\Service\ShortUrl\ShortCodeHelper;
15
16
class ShortCodeHelperTest extends TestCase
17
{
18
    use ProphecyTrait;
19
20
    private ShortCodeHelper $helper;
21
    private ObjectProphecy $em;
22
    private ObjectProphecy $shortUrl;
23
24
    protected function setUp(): void
25
    {
26
        $this->em = $this->prophesize(EntityManagerInterface::class);
27
        $this->helper = new ShortCodeHelper($this->em->reveal());
28
29
        $this->shortUrl = $this->prophesize(ShortUrl::class);
30
        $this->shortUrl->getShortCode()->willReturn('abc123');
31
    }
32
33
    /**
34
     * @test
35
     * @dataProvider provideDomains
36
     */
37
    public function shortCodeIsRegeneratedIfAlreadyInUse(?Domain $domain, ?string $expectedAuthority): void
38
    {
39
        $callIndex = 0;
40
        $expectedCalls = 3;
41
        $repo = $this->prophesize(ShortUrlRepository::class);
42
        $shortCodeIsInUse = $repo->shortCodeIsInUse('abc123', $expectedAuthority)->will(
43
            function () use (&$callIndex, $expectedCalls) {
44
                $callIndex++;
45
                return $callIndex < $expectedCalls;
46
            },
47
        );
48
        $getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
49
        $this->shortUrl->getDomain()->willReturn($domain);
50
51
        $result = $this->helper->ensureShortCodeUniqueness($this->shortUrl->reveal(), false);
52
53
        self::assertTrue($result);
54
        $this->shortUrl->regenerateShortCode()->shouldHaveBeenCalledTimes($expectedCalls - 1);
55
        $getRepo->shouldBeCalledTimes($expectedCalls);
56
        $shortCodeIsInUse->shouldBeCalledTimes($expectedCalls);
57
    }
58
59
    public function provideDomains(): iterable
60
    {
61
        yield 'no domain' => [null, null];
62
        yield 'domain' => [new Domain($authority = 'doma.in'), $authority];
63
    }
64
65
    /** @test */
66
    public function inUseSlugReturnsError(): void
67
    {
68
        $repo = $this->prophesize(ShortUrlRepository::class);
69
        $shortCodeIsInUse = $repo->shortCodeIsInUse('abc123', null)->willReturn(true);
70
        $getRepo = $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
71
        $this->shortUrl->getDomain()->willReturn(null);
72
73
        $result = $this->helper->ensureShortCodeUniqueness($this->shortUrl->reveal(), true);
74
75
        self::assertFalse($result);
76
        $this->shortUrl->regenerateShortCode()->shouldNotHaveBeenCalled();
77
        $getRepo->shouldBeCalledOnce();
78
        $shortCodeIsInUse->shouldBeCalledOnce();
79
    }
80
}
81