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

getCreateWillReturnConfigurationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 63
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 25
c 2
b 1
f 0
dl 0
loc 63
rs 9.52
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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<mixed>  $defaultConfig
87
     * @param array<mixed>  $config
88
     * @param mixed         $configuration
89
     * @param ?EventManager $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 (
120
            $connection,
121
            $defaultConfig,
122
            $config,
123
            $configuration,
124
            $eventManager
125
        ): Connection {
126
            Assert::assertSame($configurationArg, $configuration);
127
            Assert::assertSame($eventManagerArg, $eventManager);
128
            Assert::assertSame(
129
                $params,
130
                array_replace_recursive($defaultConfig ?? [], $config ?? [])
131
            );
132
            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...
133
        };
134
135
        $factory = new ConnectionFactory($this->configurationManager, $doCreate);
136
137
        $this->assertSame(
138
            $connection,
139
            $factory->create($config, $configurationString ?? $configuration, $eventManager)
140
        );
141
    }
142
143
    /**
144
     * @return array<mixed>
145
     */
146
    public function getCreateWillReturnConfigurationData(): array
147
    {
148
        /** @var EventManager&MockObject $eventManager */
149
        $eventManager = $this->createMock(EventManager::class);
150
151
        /** @var Configuration&MockObject $configuration */
152
        $configuration = $this->createMock(Configuration::class);
153
154
        return [
155
            // Empty config test
156
            [
157
158
            ],
159
160
            // Config & Configuration
161
            [
162
                [],
163
                [
164
                    'host'     => 'localhost',
165
                    'port'     => 3306,
166
                    'user'     => 'username',
167
                    'password' => '',
168
                    'dbname'   => 'database',
169
                ],
170
                $configuration,
171
            ],
172
173
            // Sting configuration
174
            [
175
                [],
176
                [],
177
                'FooConfigService',
178
            ],
179
180
            // Config & Configuration
181
            [
182
                [
183
                    'host' => 'localhost',
184
                    'port' => 3306,
185
                    'user' => 'default_username',
186
                ],
187
                [
188
                    'host'     => 'new_replaced_hostname',
189
                    'user'     => 'username',
190
                    'password' => '234_^&%$sdfg&*(',
191
                    'dbname'   => 'database',
192
                ],
193
                $configuration,
194
            ],
195
196
            // Config, Configuration, EventManager
197
            [
198
                [
199
                    'port' => 999,
200
                ],
201
                [
202
                    'bar'      => 'foo',
203
                    'database' => 'hello',
204
                    'port'     => 1234,
205
                    'user'     => 'fred',
206
                ],
207
                $configuration,
208
                $eventManager,
209
            ],
210
211
        ];
212
    }
213
}
214