testGetRepositoryWillUseRepositoryProviderIfRepositoryIsFound()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasEntity\Service;
6
7
use Arp\DoctrineEntityRepository\EntityRepositoryInterface;
8
use Arp\DoctrineEntityRepository\EntityRepositoryProviderInterface;
9
use Arp\Entity\EntityInterface;
10
use Arp\LaminasEntity\Service\EntityRepositoryFactory;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Doctrine\ORM\Repository\RepositoryFactory;
13
use Doctrine\Persistence\ObjectRepository;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * @author  Alex Patterson <[email protected]>
19
 * @package ArpTest\LaminasEntity
20
 */
21
final class EntityRepositoryFactoryTest extends TestCase
22
{
23
    /**
24
     * @var EntityRepositoryProviderInterface|MockObject
25
     */
26
    private $repositoryProvider;
27
28
    /**
29
     * @var RepositoryFactory|MockObject
30
     */
31
    private $repositoryFactory;
32
33
    /**
34
     * Prepare the test case dependencies.
35
     */
36
    public function setUp(): void
37
    {
38
        $this->repositoryProvider = $this->getMockForAbstractClass(EntityRepositoryProviderInterface::class);
39
40
        $this->repositoryFactory = $this->getMockForAbstractClass(RepositoryFactory::class);
41
    }
42
43
    /**
44
     * Assert that the class implements RepositoryFactory.
45
     *
46
     * @covers \Arp\LaminasEntity\Service\EntityRepositoryFactory::__construct
47
     */
48
    public function testImplementsRepositoryFactoryInterface(): void
49
    {
50
        $repositoryProvider = new EntityRepositoryFactory(
51
            $this->repositoryProvider,
52
            $this->repositoryFactory
53
        );
54
55
        $this->assertInstanceOf(RepositoryFactory::class, $repositoryProvider);
56
    }
57
58
    /**
59
     * Assert that if a repository is not found in the repository provider then the default repository factory
60
     * will be used.
61
     *
62
     * @covers \Arp\LaminasEntity\Service\EntityRepositoryFactory::getRepository
63
     */
64
    public function testGetRepositoryWillFallbackToDefaultRepositoryFactoryIfNotFoundInRepositoryProvider(): void
65
    {
66
        $repositoryProvider = new EntityRepositoryFactory(
67
            $this->repositoryProvider,
68
            $this->repositoryFactory
69
        );
70
71
        $entityName = EntityInterface::class;
72
73
        $this->repositoryProvider->expects($this->once())
74
            ->method('hasRepository')
75
            ->with($entityName)
76
            ->willReturn(false);
77
78
        /** @var EntityManagerInterface|MockObject $entityManager */
79
        $entityManager = $this->getMockForAbstractClass(EntityManagerInterface::class);
80
81
        /** @var ObjectRepository|MockObject $repository */
82
        $repository = $this->getMockForAbstractClass(ObjectRepository::class);
83
84
        $this->repositoryFactory->expects($this->once())
85
            ->method('getRepository')
86
            ->with($entityManager, $entityName)
87
            ->willReturn($repository);
88
89
        $this->assertSame($repository, $repositoryProvider->getRepository($entityManager, $entityName));
90
    }
91
92
    /**
93
     * Assert that if a repository is not found in the repository provider then the default repository factory
94
     * will be used.
95
     *
96
     * @covers \Arp\LaminasEntity\Service\EntityRepositoryFactory::getRepository
97
     *
98
     * @throws \Throwable
99
     */
100
    public function testGetRepositoryWillUseRepositoryProviderIfRepositoryIsFound(): void
101
    {
102
        $repositoryProvider = new EntityRepositoryFactory($this->repositoryProvider, $this->repositoryFactory);
103
104
        $entityName = EntityInterface::class;
105
106
        /** @var EntityManagerInterface|MockObject $entityManager */
107
        $entityManager = $this->getMockForAbstractClass(EntityManagerInterface::class);
108
109
        $this->repositoryProvider->expects($this->once())
110
            ->method('hasRepository')
111
            ->with($entityName)
112
            ->willReturn(true);
113
114
        $options = [
115
            'entity_name'    => $entityName,
116
            'entity_manager' => $entityManager,
117
        ];
118
119
        /** @var EntityRepositoryInterface|MockObject $repository */
120
        $repository = $this->getMockForAbstractClass(EntityRepositoryInterface::class);
121
122
        $this->repositoryProvider->expects($this->once())
123
            ->method('getRepository')
124
            ->with($entityName, $options)
125
            ->willReturn($repository);
126
127
        $this->assertSame($repository, $repositoryProvider->getRepository($entityManager, $entityName));
128
    }
129
}
130