Passed
Pull Request — master (#500)
by Alejandro
06:23
created

findsOrCreatesDomainWhenValueIsProvided()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 15
rs 9.9332
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Core\Domain\Resolver;
5
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Doctrine\ORM\EntityManagerInterface;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Shlinkio\Shlink\Core\Domain\Resolver\PersistenceDomainResolver;
11
use Shlinkio\Shlink\Core\Entity\Domain;
12
13
class PersistenceDomainResolverTest extends TestCase
14
{
15
    /** @var PersistenceDomainResolver */
16
    private $domainResolver;
17
    /** @var ObjectProphecy */
18
    private $em;
19
20
    public function setUp(): void
21
    {
22
        $this->em = $this->prophesize(EntityManagerInterface::class);
23
        $this->domainResolver = new PersistenceDomainResolver($this->em->reveal());
24
    }
25
26
    /** @test */
27
    public function returnsEmptyWhenNoDomainIsProvided(): void
28
    {
29
        $getRepository = $this->em->getRepository(Domain::class);
30
31
        $this->assertNull($this->domainResolver->resolveDomain(null));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->domainResolver->resolveDomain(null) targeting Shlinkio\Shlink\Core\Dom...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...
32
        $getRepository->shouldNotHaveBeenCalled();
33
    }
34
35
    /**
36
     * @test
37
     * @dataProvider provideFoundDomains
38
     */
39
    public function findsOrCreatesDomainWhenValueIsProvided(?Domain $foundDomain, string $authority): void
40
    {
41
        $repo = $this->prophesize(ObjectRepository::class);
42
        $findDomain = $repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
43
        $getRepository = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
44
45
        $result = $this->domainResolver->resolveDomain($authority);
46
47
        if ($foundDomain !== null) {
48
            $this->assertSame($result, $foundDomain);
49
        }
50
        $this->assertInstanceOf(Domain::class, $result);
51
        $this->assertEquals($authority, $result->getAuthority());
52
        $findDomain->shouldHaveBeenCalledOnce();
53
        $getRepository->shouldHaveBeenCalledOnce();
54
    }
55
56
    public function provideFoundDomains(): iterable
57
    {
58
        $authority = 'doma.in';
59
60
        yield 'without found domain' => [null, $authority];
61
        yield 'with found domain' => [new Domain($authority), $authority];
62
    }
63
}
64