Passed
Pull Request — master (#2)
by Alex
08:31
created

testImplementsEntityManagerProviderInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasDoctrine\Service\EntityManager;
6
7
use Arp\LaminasDoctrine\Config\DoctrineConfig;
8
use Arp\LaminasDoctrine\Factory\Service\EntityManagerFactory;
9
use Arp\LaminasDoctrine\Service\EntityManager\ContainerInterface;
10
use Arp\LaminasDoctrine\Service\EntityManager\EntityManagerProvider;
11
use Arp\LaminasDoctrine\Service\EntityManager\EntityManagerProviderInterface;
12
use Arp\LaminasDoctrine\Service\EntityManager\Exception\EntityManagerProviderException;
13
use Doctrine\ORM\EntityManagerInterface;
14
use Laminas\Log\Filter\Mock;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * @covers  \Arp\LaminasDoctrine\Service\EntityManager\EntityManagerProvider
20
 *
21
 * @author  Alex Patterson <[email protected]>
22
 * @package ArpTest\LaminasDoctrine\Service\EntityManager
23
 */
24
final class EntityManagerProviderTest extends TestCase
25
{
26
    /**
27
     * @var DoctrineConfig|MockObject
28
     */
29
    private $config;
30
31
    /**
32
     * @var ContainerInterface|MockObject
33
     */
34
    private $container;
35
36
    /**
37
     * Prepare the test case dependencies
38
     */
39
    public function setUp(): void
40
    {
41
        $this->config = $this->createMock(DoctrineConfig::class);
42
43
        $this->container = $this->createMock(ContainerInterface::class);
44
    }
45
46
    /**
47
     * Assert the class is an instance of the EntityManagerProviderInterface
48
     */
49
    public function testImplementsEntityManagerProviderInterface(): void
50
    {
51
        $provider = new EntityManagerProvider($this->config, $this->container);
52
53
        $this->assertInstanceOf(EntityManagerProviderInterface::class, $provider);
54
    }
55
56
    /**
57
     * Assert that getCollection() will return a matching connection by name
58
     *
59
     * @throws EntityManagerProviderException
60
     */
61
    public function testGetEntityManagerWillReturnEntityManagerByName(): void
62
    {
63
        $provider = new EntityManagerProvider($this->config, $this->container);
64
65
        /** @var EntityManagerInterface|MockObject $entityManager */
66
        $entityManager = $this->createMock(EntityManagerInterface::class);
67
        $name = 'FooEntityManager';
68
69
        $this->container->expects($this->exactly(2))
70
            ->method('has')
71
            ->withConsecutive([$name], [$name])
72
            ->willReturn(true, true);
73
74
        $this->container->expects($this->once())
75
            ->method('get')
76
            ->with($name)
77
            ->willReturn($entityManager);
78
79
        $this->assertSame($entityManager, $provider->getEntityManager($name));
80
    }
81
82
    /**
83
     * Assert that getCollection() will return a connection by lazy loading the configuration
84
     *
85
     * @throws EntityManagerProviderException
86
     */
87
    public function testGetEntityManagerWillReturnLazyLoadedEntityManagerByName(): void
88
    {
89
        $provider = new EntityManagerProvider($this->config, $this->container);
90
91
        /** @var EntityManagerInterface|MockObject $entityManager */
92
        $entityManager = $this->createMock(EntityManagerInterface::class);
93
        $name = 'FooEntityManager';
94
        $config = [
95
            'foo' => 'bar',
96
        ];
97
98
        $this->container->expects($this->exactly(3))
99
            ->method('has')
100
            ->withConsecutive([$name], [$name], [$name])
101
            ->willReturn(false, false, true);
102
103
        $this->config->expects($this->once())
104
            ->method('hasEntityManagerConfig')
105
            ->with($name)
106
            ->willReturn(true);
107
108
        $this->config->expects($this->once())
109
            ->method('getEntityManagerConfig')
110
            ->with($name)
111
            ->willReturn($config);
112
113
        $this->container->expects($this->once())
114
            ->method('setFactory')
115
            ->with($name, EntityManagerFactory::class);
116
117
        $this->container->expects($this->once())
118
            ->method('build')
119
            ->with($name, $config)
120
            ->willReturn($entityManager);
121
122
        $this->container->expects($this->once())
123
            ->method('get')
124
            ->with($name)
125
            ->willReturn($entityManager);
126
127
        $this->assertSame($entityManager, $provider->getEntityManager($name));
128
    }
129
130
    /**
131
     * Assert an EntityManager instance can be refreshed by closing the existing connection and creating a new
132
     * instance based on the configuration
133
     *
134
     * @throws EntityManagerProviderException
135
     */
136
    public function testRefresh(): void
137
    {
138
        $provider = new EntityManagerProvider($this->config, $this->container);
139
140
        $name = 'FooEntityManager';
141
142
        /** @var EntityManagerInterface|MockObject $entityManager */
143
        $entityManager = $this->createMock(EntityManagerInterface::class);
144
145
        /** @var EntityManagerInterface|MockObject $newInstance */
146
        $newInstance = $this->createMock(EntityManagerInterface::class);
147
148
        $config = [
149
            'foo' => 123,
150
            'bar' => 'test',
151
        ];
152
153
        $this->container->expects($this->exactly(4))
154
            ->method('has')
155
            ->withConsecutive([$name], [$name], [$name], [$name])
156
            ->willReturnOnConsecutiveCalls(true, true, true, true);
157
158
        $this->container->expects($this->once())
159
            ->method('get')
160
            ->with($name)
161
            ->willReturn($entityManager);
162
163
        $entityManager->expects($this->once())
164
            ->method('isOpen')
165
            ->willReturn(true);
166
167
        $entityManager->expects($this->once())
168
            ->method('close');
169
170
        $this->config->expects($this->once())
171
            ->method('getEntityManagerConfig')
172
            ->with($name)
173
            ->willReturn($config);
174
175
        $this->container->expects($this->once())
176
            ->method('build')
177
            ->with($name, $config)
178
            ->willReturn($newInstance);
179
180
        $this->assertSame($newInstance, $provider->refresh($name));
181
    }
182
183
}
184