Passed
Push — develop ( 0d7fb1...8577d6 )
by Alejandro
09:01 queued 01:01
created

triesToFindApiKeyWhenValueIsProvided()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Core\ShortUrl\Resolver;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\Persistence\ObjectRepository;
9
use PHPUnit\Framework\TestCase;
10
use Prophecy\PhpUnit\ProphecyTrait;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Shlinkio\Shlink\Core\Entity\Domain;
13
use Shlinkio\Shlink\Core\ShortUrl\Resolver\PersistenceShortUrlRelationResolver;
14
use Shlinkio\Shlink\Rest\Entity\ApiKey;
15
16
class PersistenceShortUrlRelationResolverTest extends TestCase
17
{
18
    use ProphecyTrait;
19
20
    private PersistenceShortUrlRelationResolver $resolver;
21
    private ObjectProphecy $em;
22
23
    public function setUp(): void
24
    {
25
        $this->em = $this->prophesize(EntityManagerInterface::class);
26
        $this->resolver = new PersistenceShortUrlRelationResolver($this->em->reveal());
27
    }
28
29
    /** @test */
30
    public function returnsEmptyWhenNoDomainIsProvided(): void
31
    {
32
        $getRepository = $this->em->getRepository(Domain::class);
33
34
        self::assertNull($this->resolver->resolveDomain(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->resolver->resolveDomain(null) targeting Shlinkio\Shlink\Core\Sho...solver::resolveDomain() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35
        $getRepository->shouldNotHaveBeenCalled();
36
    }
37
38
    /**
39
     * @test
40
     * @dataProvider provideFoundDomains
41
     */
42
    public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void
43
    {
44
        $repo = $this->prophesize(ObjectRepository::class);
45
        $findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
46
        $getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
47
48
        $result = $this->resolver->resolveDomain($authority);
49
50
        if ($foundDomain !== null) {
51
            self::assertSame($result, $foundDomain);
52
        }
53
        self::assertInstanceOf(Domain::class, $result);
54
        self::assertEquals($authority, $result->getAuthority());
55
        $findDomain->shouldHaveBeenCalledOnce();
56
        $getRepository->shouldHaveBeenCalledOnce();
57
    }
58
59
    public function provideFoundDomains(): iterable
60
    {
61
        $authority = 'doma.in';
62
63
        yield 'not found domain' => [null, $authority];
64
        yield 'found domain' => [new Domain($authority), $authority];
65
    }
66
67
    /** @test */
68
    public function returnsEmptyWhenNoApiKeyIsProvided(): void
69
    {
70
        $getRepository = $this->em->getRepository(ApiKey::class);
71
72
        self::assertNull($this->resolver->resolveApiKey(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->resolver->resolveApiKey(null) targeting Shlinkio\Shlink\Core\Sho...solver::resolveApiKey() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
        $getRepository->shouldNotHaveBeenCalled();
74
    }
75
76
    /**
77
     * @test
78
     * @dataProvider provideFoundApiKeys
79
     */
80
    public function triesToFindApiKeyWhenValueIsProvided(?ApiKey $foundApiKey, string $key): void
81
    {
82
        $repo = $this->prophesize(ObjectRepository::class);
83
        $find = $repo->findOneBy(['key' => $key])->willReturn($foundApiKey);
84
        $getRepository = $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
85
86
        $result = $this->resolver->resolveApiKey($key);
87
88
        self::assertSame($result, $foundApiKey);
89
        $find->shouldHaveBeenCalledOnce();
90
        $getRepository->shouldHaveBeenCalledOnce();
91
    }
92
93
    public function provideFoundApiKeys(): iterable
94
    {
95
        $key = 'abc123';
96
97
        yield 'not found api key' => [null, $key];
98
        yield 'found api key' => [new ApiKey(), $key];
99
    }
100
}
101