|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Core\Domain\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepository; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Entity\Domain; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta; |
|
11
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey; |
|
13
|
|
|
use Shlinkio\Shlink\TestUtils\DbTest\DatabaseTestCase; |
|
14
|
|
|
|
|
15
|
|
|
class DomainRepositoryTest extends DatabaseTestCase |
|
16
|
|
|
{ |
|
17
|
|
|
protected const ENTITIES_TO_EMPTY = [ShortUrl::class, Domain::class]; |
|
18
|
|
|
|
|
19
|
|
|
private DomainRepository $repo; |
|
20
|
|
|
|
|
21
|
|
|
protected function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->repo = $this->getEntityManager()->getRepository(Domain::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** @test */ |
|
27
|
|
|
public function findDomainsReturnsExpectedResult(): void |
|
28
|
|
|
{ |
|
29
|
|
|
$fooDomain = new Domain('foo.com'); |
|
30
|
|
|
$this->getEntityManager()->persist($fooDomain); |
|
31
|
|
|
$fooShortUrl = $this->createShortUrl($fooDomain); |
|
32
|
|
|
$this->getEntityManager()->persist($fooShortUrl); |
|
33
|
|
|
|
|
34
|
|
|
$barDomain = new Domain('bar.com'); |
|
35
|
|
|
$this->getEntityManager()->persist($barDomain); |
|
36
|
|
|
$barShortUrl = $this->createShortUrl($barDomain); |
|
37
|
|
|
$this->getEntityManager()->persist($barShortUrl); |
|
38
|
|
|
|
|
39
|
|
|
$bazDomain = new Domain('baz.com'); |
|
40
|
|
|
$this->getEntityManager()->persist($bazDomain); |
|
41
|
|
|
$bazShortUrl = $this->createShortUrl($bazDomain); |
|
42
|
|
|
$this->getEntityManager()->persist($bazShortUrl); |
|
43
|
|
|
|
|
44
|
|
|
$detachedDomain = new Domain('detached.com'); |
|
45
|
|
|
$this->getEntityManager()->persist($detachedDomain); |
|
46
|
|
|
|
|
47
|
|
|
$this->getEntityManager()->flush(); |
|
48
|
|
|
|
|
49
|
|
|
self::assertEquals([$barDomain, $bazDomain, $fooDomain], $this->repo->findDomainsWithout()); |
|
50
|
|
|
self::assertEquals([$barDomain, $bazDomain], $this->repo->findDomainsWithout('foo.com')); |
|
51
|
|
|
self::assertEquals([$bazDomain, $fooDomain], $this->repo->findDomainsWithout('bar.com')); |
|
52
|
|
|
self::assertEquals([$barDomain, $fooDomain], $this->repo->findDomainsWithout('baz.com')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function createShortUrl(Domain $domain): ShortUrl |
|
56
|
|
|
{ |
|
57
|
|
|
return new ShortUrl( |
|
58
|
|
|
'foo', |
|
59
|
|
|
ShortUrlMeta::fromRawData(['domain' => $domain->getAuthority()]), |
|
60
|
|
|
new class ($domain) implements ShortUrlRelationResolverInterface { |
|
61
|
|
|
private Domain $domain; |
|
62
|
|
|
|
|
63
|
|
|
public function __construct(Domain $domain) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->domain = $domain; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function resolveDomain(?string $domain): ?Domain |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->domain; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function resolveApiKey(?string $key): ?ApiKey |
|
74
|
|
|
{ |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
}, |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|