Passed
Pull Request — master (#2)
by Alex
02:56
created

ConnectionFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 67
c 1
b 0
f 0
dl 0
loc 180
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
A testCreateWillThrowAConnectionFactoryExceptionWithInvalidConfiguration() 0 26 1
A testImplementsConnectionFactoryInterface() 0 5 1
A getCreateWillReturnConfigurationData() 0 65 1
A testCreateWillReturnConfiguration() 0 40 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasDoctrine\Service\Connection;
6
7
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManagerInterface;
8
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationManagerException;
9
use Arp\LaminasDoctrine\Service\Connection\ConnectionFactory;
10
use Arp\LaminasDoctrine\Service\Connection\ConnectionFactoryInterface;
11
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionFactoryException;
12
use Doctrine\Common\EventManager;
13
use Doctrine\DBAL\Connection;
14
use Doctrine\ORM\Configuration;
15
use PHPUnit\Framework\Assert;
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * @covers  \Arp\LaminasDoctrine\Service\Connection\ConnectionFactory
21
 *
22
 * @author  Alex Patterson <[email protected]>
23
 * @package ArpTest\LaminasDoctrine\Service\Connection
24
 */
25
final class ConnectionFactoryTest extends TestCase
26
{
27
    /**
28
     * @var ConfigurationManagerInterface|MockObject
29
     */
30
    private $configurationManager;
31
32
    /**
33
     * Prepare the test case dependencies
34
     */
35
    public function setUp(): void
36
    {
37
        $this->configurationManager = $this->createMock(ConfigurationManagerInterface::class);
38
    }
39
40
    /**
41
     * Assert the class implements ConnectionFactoryInterface
42
     */
43
    public function testImplementsConnectionFactoryInterface(): void
44
    {
45
        $factory = new ConnectionFactory($this->configurationManager);
46
47
        $this->assertInstanceOf(ConnectionFactoryInterface::class, $factory);
48
    }
49
50
    /**
51
     * Assert that a ConnectionFactoryException is thrown if the provided $configuration is invalid
52
     *
53
     * @throws ConnectionFactoryException
54
     */
55
    public function testCreateWillThrowAConnectionFactoryExceptionWithInvalidConfiguration(): void
56
    {
57
        $factory = new ConnectionFactory($this->configurationManager);
58
59
        /** @var EventManager|MockObject $eventManager */
60
        $eventManager = $this->createMock(EventManager::class);
61
        $config = [
62
            'foo' => 'bar',
63
        ];
64
65
        $configuration = 'ConfigurationServiceName'; // Passing string requires the manager to load it
66
67
        $exceptionMessage = 'This is a test exception message for ' . __FUNCTION__;
68
        $exceptionCode = 8910;
69
        $exception = new ConfigurationManagerException($exceptionMessage, $exceptionCode);
70
71
        $this->configurationManager->expects($this->once())
72
            ->method('getConfiguration')
73
            ->with($configuration)
74
            ->willThrowException($exception);
75
76
        $this->expectException(ConnectionFactoryException::class);
77
        $this->expectExceptionCode($exceptionCode);
78
        $this->expectExceptionMessage(sprintf('Failed to create new connection: %s', $exceptionMessage));
79
80
        $factory->create($config, $configuration, $eventManager);
81
    }
82
83
    /**
84
     * Assert that the create() method will return a configuration instance
85
     *
86
     * @param array                                $defaultConfig
87
     * @param array                                $config
88
     * @param Configuration|MockObject|string|null $configuration
89
     * @param EventManager|MockObject|null         $eventManager
90
     *
91
     * @throws ConnectionFactoryException
92
     * @dataProvider getCreateWillReturnConfigurationData
93
     */
94
    public function testCreateWillReturnConfiguration(
95
        array $defaultConfig = [],
96
        array $config = [],
97
        $configuration = null,
98
        ?EventManager $eventManager = null
99
    ): void {
100
        $config = array_replace_recursive($defaultConfig, $config);
101
102
        if (is_string($configuration)) {
103
            $configurationString = $configuration;
104
105
            /** @var Configuration|MockObject $configuration */
106
            $configuration = $this->createMock(Configuration::class);
107
            $this->configurationManager->expects($this->once())
108
                ->method('getConfiguration')
109
                ->with($configurationString)
110
                ->willReturn($configuration);
111
        }
112
113
        /** @var Connection|MockObject $connection */
114
        $connection = $this->createMock(Connection::class);
115
        $doCreate = static function (
116
            array $params,
117
            ?Configuration $configurationArg,
118
            ?EventManager $eventManagerArg
119
        ) use ($connection, $defaultConfig, $config, $configuration, $eventManager): Connection {
0 ignored issues
show
Coding Style introduced by
Multi-line use declarations must define one parameter per line
Loading history...
120
            Assert::assertSame($configurationArg, $configuration);
121
            Assert::assertSame($eventManagerArg, $eventManager);
122
            Assert::assertSame(
123
                $params,
124
                array_replace_recursive($defaultConfig['params'] ?? [], $config['params'] ?? [])
125
            );
126
            return $connection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $connection returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Doctrine\DBAL\Connection.
Loading history...
127
        };
128
129
        $factory = new ConnectionFactory($this->configurationManager, $doCreate);
130
131
        $this->assertSame(
132
            $connection,
133
            $factory->create($config, $configurationString ?? $configuration, $eventManager)
134
        );
135
    }
136
137
    /**
138
     * @return array
139
     */
140
    public function getCreateWillReturnConfigurationData(): array
141
    {
142
        /** @var EventManager|MockObject $eventManager */
143
        $eventManager = $this->createMock(EventManager::class);
144
145
        /** @var Configuration|MockObject $configuration */
146
        $configuration = $this->createMock(Configuration::class);
147
148
        return [
149
            // Empty config test
150
            [
151
152
            ],
153
154
            // Config & Configuration
155
            [
156
                [],
157
                [
158
                    'foo'    => 'bar',
159
                    'params' => [
160
                        'test' => 123,
161
                    ],
162
                ],
163
                $configuration
164
            ],
165
166
            // Sting configuration
167
            [
168
                [],
169
                [],
170
                'FooConfigService',
171
            ],
172
173
            // Config & Configuration
174
            [
175
                [
176
                    'params' => [
177
                        'hello' => 123,
178
                    ],
179
                ],
180
                [
181
                    'foo'    => 'bar',
182
                    'params' => [
183
                        'test' => 123,
184
                    ],
185
                ],
186
                $configuration
187
            ],
188
189
            // Config, Configuration, EventManager
190
            [
191
                [
192
                    'params' => [
193
                        'test' => 999,
194
                    ],
195
                ],
196
                [
197
                    'bar'    => 'foo',
198
                    'params' => [
199
                        'database' => 'hello',
200
                        'user' => 'fred',
201
                    ],
202
                ],
203
                $configuration,
204
                $eventManager
205
            ],
206
207
        ];
208
    }
209
}
210