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
|
|
|
|