Passed
Push — master ( 0e82a5...532142 )
by Alex
02:14
created

ConnectionTraitUnitTest::setUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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