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\EntityRepositoryManager; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Alex Patterson <[email protected]> |
17
|
|
|
* @package ArpTest\LaminasEntity\Service |
18
|
|
|
*/ |
19
|
|
|
final class EntityRepositoryManagerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ContainerInterface|MockObject |
23
|
|
|
*/ |
24
|
|
|
private $container; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prepare the test case dependencies. |
28
|
|
|
*/ |
29
|
|
|
public function setUp(): void |
30
|
|
|
{ |
31
|
|
|
$this->container = $this->getMockForAbstractClass(ContainerInterface::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Assert that the EntityRepositoryManager is an instance of ContainerInterface. |
36
|
|
|
* |
37
|
|
|
* @covers \Arp\LaminasEntity\Service\EntityRepositoryManager |
38
|
|
|
*/ |
39
|
|
|
public function testImplementContainerInterface(): void |
40
|
|
|
{ |
41
|
|
|
$manager = new EntityRepositoryManager($this->container); |
42
|
|
|
|
43
|
|
|
$this->assertInstanceOf(ContainerInterface::class, $manager); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Assert that the EntityRepositoryManager is an instance of EntityRepositoryProviderInterface. |
48
|
|
|
* |
49
|
|
|
* @covers \Arp\LaminasEntity\Service\EntityRepositoryManager |
50
|
|
|
*/ |
51
|
|
|
public function testImplementsEntityRepositoryProviderInterface(): void |
52
|
|
|
{ |
53
|
|
|
$manager = new EntityRepositoryManager($this->container); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf(EntityRepositoryProviderInterface::class, $manager); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Assert that the call to hasRepository() with a non-existing entity name will result in boolean false |
60
|
|
|
* being returned. |
61
|
|
|
* |
62
|
|
|
* @covers \Arp\LaminasEntity\Service\EntityRepositoryManager::hasRepository |
63
|
|
|
*/ |
64
|
|
|
public function testHasRepositoryWillReturnFalseForNotExistingRepository(): void |
65
|
|
|
{ |
66
|
|
|
$entityName = EntityInterface::class; |
67
|
|
|
$config = [ |
68
|
|
|
'services' => [] |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
$manager = new EntityRepositoryManager($this->container, $config); |
72
|
|
|
|
73
|
|
|
$this->assertFalse($manager->hasRepository($entityName)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Assert that the call to hasRepository() with a non-existing entity name will result in boolean false |
78
|
|
|
* being returned. |
79
|
|
|
* |
80
|
|
|
* @covers \Arp\LaminasEntity\Service\EntityRepositoryManager::hasRepository |
81
|
|
|
*/ |
82
|
|
|
public function testHasRepositoryWillReturnTrueFromExistingRepository(): void |
83
|
|
|
{ |
84
|
|
|
/** @var EntityRepositoryInterface|MockObject $repository */ |
85
|
|
|
$repository = $this->getMockForAbstractClass(EntityRepositoryInterface::class); |
86
|
|
|
$entityName = EntityInterface::class; |
87
|
|
|
|
88
|
|
|
$config = [ |
89
|
|
|
'services' => [ |
90
|
|
|
EntityInterface::class => $repository, |
91
|
|
|
], |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$manager = new EntityRepositoryManager($this->container, $config); |
95
|
|
|
|
96
|
|
|
$this->assertTrue($manager->hasRepository($entityName)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Assert that calling for a service that is not registered a \Throwable exception error will be raised. |
101
|
|
|
* |
102
|
|
|
* @covers \Arp\LaminasEntity\Service\EntityRepositoryManager::getRepository |
103
|
|
|
* @throws \Throwable |
104
|
|
|
*/ |
105
|
|
|
public function testGetRepositoryWillThrowServiceNotFoundExceptionIfNotFound(): void |
106
|
|
|
{ |
107
|
|
|
$entityName = EntityInterface::class; |
108
|
|
|
$config = [ |
109
|
|
|
'services' => [], |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$manager = new EntityRepositoryManager($this->container, $config); |
113
|
|
|
|
114
|
|
|
$this->expectException(\Throwable::class); |
115
|
|
|
|
116
|
|
|
$manager->getRepository($entityName); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @covers \Arp\LaminasEntity\Service\EntityRepositoryManager::getRepository |
121
|
|
|
* @throws \Throwable |
122
|
|
|
*/ |
123
|
|
|
public function testGetRepositoryWillReturnRepository(): void |
124
|
|
|
{ |
125
|
|
|
/** @var EntityRepositoryInterface|MockObject $repository */ |
126
|
|
|
$repository = $this->getMockForAbstractClass(EntityRepositoryInterface::class); |
127
|
|
|
$entityName = EntityInterface::class; |
128
|
|
|
$config = [ |
129
|
|
|
'services' => [ |
130
|
|
|
$entityName => $repository, |
131
|
|
|
], |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
$manager = new EntityRepositoryManager($this->container, $config); |
135
|
|
|
|
136
|
|
|
$this->assertSame($repository, $manager->getRepository($entityName)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|