Completed
Push — develop ( 0686ac...7ecc3a )
by Alejandro
18s queued 11s
created

ShortCodeHelperTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

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