Test Failed
Pull Request — master (#2)
by Alex
02:32
created

testGetConnectionWillReturnANamedConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasDoctrine\Service\Connection;
6
7
use Arp\LaminasDoctrine\Config\DoctrineConfig;
8
use Arp\LaminasDoctrine\Service\Connection\ConnectionFactoryInterface;
9
use Arp\LaminasDoctrine\Service\Connection\ConnectionManager;
10
use Arp\LaminasDoctrine\Service\Connection\ConnectionManagerInterface;
11
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionFactoryException;
12
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionManagerException;
13
use Doctrine\DBAL\Connection;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
17
/**
18
 * @covers  \Arp\LaminasDoctrine\Service\Connection\ConnectionManager
19
 *
20
 * @author  Alex Patterson <[email protected]>
21
 * @package ArpTest\LaminasDoctrine\Service\Connection
22
 */
23
final class ConnectionManagerTest extends TestCase
24
{
25
    /**
26
     * @var DoctrineConfig|MockObject
27
     */
28
    private DoctrineConfig $config;
29
30
    /**
31
     * @var ConnectionFactoryInterface|MockObject
32
     */
33
    private ConnectionFactoryInterface $connectionFactory;
34
35
    /**
36
     * @var Connection[]|MockObject[]|array
37
     */
38
    private array $connections = [];
39
40
    /**
41
     * Prepare the test case dependencies
42
     */
43
    public function setUp(): void
44
    {
45
        $this->config = $this->createMock(DoctrineConfig::class);
46
47
        $this->connectionFactory = $this->createMock(ConnectionFactoryInterface::class);
48
    }
49
50
    /**
51
     * Assert that the class implement ConnectionManagerInterface
52
     */
53
    public function testImplementsConnectionManagerInterface(): void
54
    {
55
        $manager = new ConnectionManager($this->config, $this->connectionFactory, $this->connections);
56
57
        $this->assertInstanceOf(ConnectionManagerInterface::class, $manager);
58
    }
59
60
    /**
61
     * Assert that a hasConnection() will return a boolean value for existing/missing connection values
62
     */
63
    public function testHasConnectionWillReturnBool(): void
64
    {
65
        $manager = new ConnectionManager($this->config, $this->connectionFactory, $this->connections);
66
67
        $connections = [
68
            'baz'  => $this->createMock(Connection::class),
69
            'bar'  => $this->createMock(Connection::class),
70
            'test' => [
71
                'hello' => 123,
72
                'fred'  => true,
73
                'test'  => 'Hello',
74
            ],
75
        ];
76
77
        $this->config->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Arp\LaminasDoctrine\Config\DoctrineConfig. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        $this->config->/** @scrutinizer ignore-call */ 
78
                       expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            ->method('setConnectionConfig')
79
            ->with('test', $connections['test']);
80
81
        $this->config->expects($this->exactly(2))
82
            ->method('hasConnectionConfig')
83
            ->withConsecutive(['test'], ['fred']
84
            )->willReturnOnConsecutiveCalls(true, false);
85
86
        $manager->setConnections($connections);
87
88
        $this->assertTrue($manager->hasConnection('baz'));
89
        $this->assertTrue($manager->hasConnection('bar'));
90
        $this->assertTrue($manager->hasConnection('test'));
91
        $this->assertFalse($manager->hasConnection('fred'));
92
    }
93
94
    /**
95
     * Assert that a connection can be fetched from the collection by $name
96
     *
97
     * @throws ConnectionManagerException
98
     */
99
    public function testGetConnectionWillReturnANamedConnection(): void
100
    {
101
        $manager = new ConnectionManager($this->config, $this->connectionFactory, $this->connections);
102
103
        $name = 'FooConnection';
104
        /** @var Connection|MockObject $expected */
105
        $expected = $this->createMock(Connection::class);
106
107
        $connections = [
108
            'bar' => $this->createMock(Connection::class),
109
            $name => $expected,
110
        ];
111
112
        $manager->setConnections($connections);
113
114
        $this->assertSame($expected, $manager->getConnection($name));
115
    }
116
117
    /**
118
     * Asset A ConnectionManagerException is thrown should a call to getConnection() be unable to return a
119
     * connection for a known connection $name
120
     *
121
     * @throws ConnectionManagerException
122
     */
123
    public function testGetConnectionWillThrowConnectionManagerExceptionIfUnableToCreateNamedConnection(): void
124
    {
125
        $manager = new ConnectionManager($this->config, $this->connectionFactory, $this->connections);
126
127
        $name = 'FooConnection';
128
        $connections = [
129
            $name => [
130
                'foo'  => 123,
131
                'test' => 'abc',
132
            ],
133
        ];
134
135
        $this->config->expects($this->once())
136
            ->method('setConnectionConfig')
137
            ->with($name, $connections[$name]);
138
139
        $this->config->expects($this->once())
140
            ->method('hasConnectionConfig')
141
            ->with($name)
142
            ->willReturn(true);
143
144
        $this->config->expects($this->once())
145
            ->method('getConnectionConfig')
146
            ->with($name)
147
            ->willReturn($connections[$name]);
148
149
        $exceptionMessage = 'This is a test exception message for ' . __FUNCTION__;
150
        $exceptionCode = 876;
151
        $exception = new ConnectionFactoryException($exceptionMessage, $exceptionCode);
152
153
        $this->connectionFactory->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Arp\LaminasDoctrine\Serv...nectionFactoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $this->connectionFactory->/** @scrutinizer ignore-call */ 
154
                                  expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
            ->method('create')
155
            ->with($connections[$name])
156
            ->willThrowException($exception);
157
158
        $this->expectException(ConnectionManagerException::class);
159
        $this->expectExceptionCode($exceptionCode);
160
        $this->expectExceptionMessage(sprintf('Failed to establish connection \'%s\': %s', $name, $exceptionMessage));
161
162
        $manager->setConnections($connections);
163
164
        $manager->getConnection($name);
165
    }
166
167
    /**
168
     * Assert that a collection can be lazy loaded from the collection by its $name
169
     *
170
     * @throws ConnectionManagerException
171
     */
172
    public function testGetConnectionWillLazyLoadAndReturnANamedConnection(): void
173
    {
174
        $manager = new ConnectionManager($this->config, $this->connectionFactory, $this->connections);
175
176
        $name = 'FooConnection';
177
        $connections = [
178
            $name => [
179
                'foo'  => 123,
180
                'test' => 'abc',
181
            ],
182
            'foo' => $this->createMock(Connection::class),
183
        ];
184
185
        $this->config->expects($this->once())
186
            ->method('setConnectionConfig')
187
            ->with($name, $connections[$name]);
188
189
        $this->config->expects($this->once())
190
            ->method('hasConnectionConfig')
191
            ->with($name)
192
            ->willReturn(true);
193
194
        $this->config->expects($this->once())
195
            ->method('getConnectionConfig')
196
            ->with($name)
197
            ->willReturn($connections[$name]);
198
199
        /** @var Connection|MockObject $expected */
200
        $expected = $this->createMock(Connection::class);
201
202
        $this->connectionFactory->expects($this->once())
203
            ->method('create')
204
            ->with($connections[$name])
205
            ->willReturn($expected);
206
207
        $manager->setConnections($connections);
208
209
        $this->assertSame($expected, $manager->getConnection($name));
210
    }
211
212
    /**
213
     * Assert that calling getConnection() with a non-existing connection $name a ConnectionManagerException is thrown
214
     *
215
     * @throws ConnectionManagerException
216
     */
217
    public function testGetConnectionWillThrowConnectionManagerExceptionIfNotFound(): void
218
    {
219
        $manager = new ConnectionManager($this->config, $this->connectionFactory, $this->connections);
220
221
        $name = 'NonExistingName';
222
223
        $this->config->expects($this->once())
224
            ->method('hasConnectionConfig')
225
            ->with($name)
226
            ->willReturn(false);
227
228
        $this->expectException(ConnectionManagerException::class);
229
        $this->expectExceptionMessage(
230
            sprintf('Failed to establish connection \'%s\': Failed to find a the required configuration', $name)
231
        );
232
233
        $manager->getConnection($name);
234
    }
235
}
236