Passed
Push — master ( 532142...a47d37 )
by Alex
02:22
created

ConnectionTraitUnitTest::setDsn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
namespace Mezon\PdoCrud\Tests;
3
4
use Mezon\Conf\Conf;
5
use Mezon\PdoCrud\PdoCrud;
6
use PHPUnit\Framework\TestCase;
7
8
class ConnectionTraitUnitTest extends TestCase
9
{
10
11
    /**
12
     * Method returns mock
13
     *
14
     * @return object mock
15
     */
16
    protected function getPdoMock(): object
17
    {
18
        return $this->getMockBuilder(PdoCrud::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

18
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(PdoCrud::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
19
            ->setMethods([
20
            'connect'
21
        ])
22
            ->getMock();
23
    }
24
25
    /**
26
     * Method returns mock
27
     *
28
     * @return object mock
29
     */
30
    protected function getMock(): object
31
    {
32
        return $this->getMockBuilder(TraitClient::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

32
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(TraitClient::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
33
            ->setMethods([
34
            'constructConnection'
35
        ])
36
            ->getMock();
37
    }
38
39
    /**
40
     * Method sets dsn
41
     *
42
     * @param string $dsn
43
     *            dsn
44
     * @param string $connectionName
45
     *            connection name
46
     */
47
    protected function setDsn(string $dsn, string $connectionName = 'default-db-connection'): void
48
    {
49
        Conf::setConfigValue($connectionName . '/dsn', $dsn);
0 ignored issues
show
Bug introduced by
$connectionName . '/dsn' of type string is incompatible with the type array expected by parameter $route of Mezon\Conf\Conf::setConfigValue(). ( Ignorable by Annotation )

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

49
        Conf::setConfigValue(/** @scrutinizer ignore-type */ $connectionName . '/dsn', $dsn);
Loading history...
50
    }
51
52
    /**
53
     * Method sets user
54
     *
55
     * @param string $user
56
     *            user
57
     * @param string $connectionName
58
     *            connection name
59
     */
60
    protected function setUser(string $user, string $connectionName = 'default-db-connection'): void
61
    {
62
        Conf::setConfigValue($connectionName . '/user', $user);
0 ignored issues
show
Bug introduced by
$connectionName . '/user' of type string is incompatible with the type array expected by parameter $route of Mezon\Conf\Conf::setConfigValue(). ( Ignorable by Annotation )

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

62
        Conf::setConfigValue(/** @scrutinizer ignore-type */ $connectionName . '/user', $user);
Loading history...
63
    }
64
65
    /**
66
     * Method sets password
67
     *
68
     * @param string $password
69
     *            password
70
     * @param string $connectionName
71
     *            connection name
72
     */
73
    protected function setPassword(string $password, string $connectionName = 'default-db-connection'): void
74
    {
75
        Conf::setConfigValue($connectionName . '/password', $password);
0 ignored issues
show
Bug introduced by
$connectionName . '/password' of type string is incompatible with the type array expected by parameter $route of Mezon\Conf\Conf::setConfigValue(). ( Ignorable by Annotation )

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

75
        Conf::setConfigValue(/** @scrutinizer ignore-type */ $connectionName . '/password', $password);
Loading history...
76
    }
77
78
    /**
79
     * Testing insertion method
80
     */
81
    public function testGetConnection(): void
82
    {
83
        // setupp
84
        $this->setDsn('dsn');
85
        $this->setUser('user');
86
        $this->setPassword('password');
87
        $mock = $this->getMock();
88
89
        $mock->expects($this->once())
90
            ->method('constructConnection')
91
            ->willReturn($this->getPdoMock());
92
93
        // test body and assertionss
94
        $mock->getConnection();
95
    }
96
97
    /**
98
     * Asserting exception if dsn is not set
99
     */
100
    public function testDsnException(): void
101
    {
102
        // TODO join these tests in one with data provider
103
        // setup
104
        Conf::deleteConfigValue('default-db-connection/dsn');
105
        $this->setUser('user');
106
        $this->setPassword('password');
107
        $mock = $this->getMock();
108
        $mock->setConnection(false);
109
110
        // assertions
111
        $this->expectException(\Exception::class);
112
113
        // test body
114
        $mock->getConnection();
115
    }
116
117
    /**
118
     * Asserting exception if user is not set
119
     */
120
    public function testUserException(): void
121
    {
122
        // setup
123
        $this->setDsn('dsn');
124
        Conf::deleteConfigValue('default-db-connection/user');
125
        $this->setPassword('password');
126
        $mock = $this->getMock();
127
        $mock->setConnection(false);
128
129
        // assertions
130
        $this->expectException(\Exception::class);
131
132
        // test body
133
        $mock->getConnection();
134
    }
135
136
    /**
137
     * Asserting exception if password is not set
138
     */
139
    public function testPasswordException(): void
140
    {
141
        // setup
142
        $this->setDsn('dsn');
143
        $this->setUser('user');
144
        Conf::deleteConfigValue('default-db-connection/password');
145
        $mock = $this->getMock();
146
        $mock->setConnection(false);
147
148
        // assertions
149
        $this->expectException(\Exception::class);
150
151
        // test body
152
        $mock->getConnection();
153
    }
154
155
    /**
156
     * Setting connection
157
     *
158
     * @param string $connectionName
159
     *            connection name
160
     */
161
    private function setConnection(string $connectionName = 'default-db-connection'): void
162
    {
163
        $this->setDsn('dsn', $connectionName);
164
        $this->setUser('user', $connectionName);
165
        $this->setPassword('password', $connectionName);
166
    }
167
168
    /**
169
     * Testing data provider
170
     *
171
     * @return array testing data
172
     */
173
    public function getConnectionForArrayDataProvider(): array
174
    {
175
        return [
176
            // #0, the first case, two connections the first one is fetched
177
            [
178
                function (): void {
179
                    // setup method
180
                    $this->setConnection();
181
                    $this->setConnection('exact-connection');
182
                }
183
            ],
184
            // #1, the first case, two connections the second one is fetched
185
            [
186
                function (): void {
187
                    // setup method
188
                    $this->setConnection('exact-connection');
189
                    $this->setConnection();
190
                }
191
            ],
192
            // #2, the third case, connection was not found
193
            [
194
                function (): void {
195
                    // setup method
196
                    $this->setConnection('first-connection');
197
                    $this->setConnection('second-connection');
198
199
                    $this->expectException(\Exception::class);
200
                }
201
            ]
202
        ];
203
    }
204
205
    /**
206
     * Testing method
207
     *
208
     * @param callable $setup
209
     *            setup method
210
     * @dataProvider getConnectionForArrayDataProvider
211
     */
212
    public function testGetConnectionForArray(callable $setup): void
213
    {
214
        // setup and assertions
215
        $setup();
216
        $mock = $this->getMock();
217
        $mock->setConnection(false);
218
        $mock->expects($this->once())
219
            ->method('constructConnection')
220
            ->willReturn($this->getPdoMock());
221
222
        // test body
223
        $mock->getConnection([
224
            'exact-connection'
225
        ]);
226
    }
227
228
    /**
229
     * Data provider for the test testGetConnectionForArrayException
230
     *
231
     * @return array testing data
232
     */
233
    public function getConnectionForArrayExceptionDataProvider(): array
234
    {
235
        return [
236
            [
237
                [
238
                    'exact-connection'
239
                ]
240
            ],
241
            [
242
                new \stdClass()
243
            ]
244
        ];
245
    }
246
247
    /**
248
     * Testing exception for array type connection name
249
     *
250
     * @param mixed $connectionNAme
251
     *            connection name
252
     * @dataProvider getConnectionForArrayExceptionDataProvider
253
     */
254
    public function testGetConnectionForArrayException($connectionName): void
255
    {
256
        // TODO add snippet for testing exception with data provider
257
        // assertions
258
        $this->expectException(\Exception::class);
259
260
        // setup
261
        Conf::deleteConfigValue('exact-connection/dsn');
262
        $this->setConnection('first-connection');
263
        $this->setConnection('second-connection');
264
        $mock = $this->getMock();
265
        $mock->setConnection(false);
266
267
        // test body
268
        $mock->getConnection($connectionName);
269
    }
270
271
    /**
272
     * Testing method cached getConnection
273
     */
274
    public function testGetConnectionCached(): void
275
    {
276
        // setup
277
        $mock = $this->getMock();
278
        $mock->setConnection(new PdoCrudMock());
279
280
        // test body and assertions
281
        $this->assertInstanceOf(PdoCrudMock::class, $mock->getConnection('some-connection-wich-does-not-exists'));
282
    }
283
}
284