Passed
Push — master ( a47d37...49a564 )
by Alex
02:49 queued 10s
created

ConnectionTraitUnitTest::setPassword()   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 ConnectionTraitTests
9
{
10
11
    /**
12
     * Testing insertion method
13
     */
14
    public function testGetConnection(): void
15
    {
16
        // setupp
17
        $this->setDsn('dsn');
18
        $this->setUser('user');
19
        $this->setPassword('password');
20
        $mock = $this->getMock();
21
        $mock->setConnection(false);
22
23
        $mock->expects($this->once())
24
            ->method('constructConnection')
25
            ->willReturn($this->getPdoMock());
26
27
        // test body and assertionss
28
        $mock->getConnection();
29
    }
30
31
    /**
32
     * Asserting exception if dsn is not set
33
     */
34
    public function testDsnException(): void
35
    {
36
        // TODO join these tests in one with data provider
37
        // setup
38
        Conf::deleteConfigValue('default-db-connection/dsn');
39
        $this->setUser('user');
40
        $this->setPassword('password');
41
        $mock = $this->getMock();
42
        $mock->setConnection(false);
43
44
        // assertions
45
        $this->expectException(\Exception::class);
46
47
        // test body
48
        $mock->getConnection();
49
    }
50
51
    /**
52
     * Asserting exception if user is not set
53
     */
54
    public function testUserException(): void
55
    {
56
        // setup
57
        $this->setDsn('dsn');
58
        Conf::deleteConfigValue('default-db-connection/user');
59
        $this->setPassword('password');
60
        $mock = $this->getMock();
61
        $mock->setConnection(false);
62
63
        // assertions
64
        $this->expectException(\Exception::class);
65
66
        // test body
67
        $mock->getConnection();
68
    }
69
70
    /**
71
     * Asserting exception if password is not set
72
     */
73
    public function testPasswordException(): void
74
    {
75
        // setup
76
        $this->setDsn('dsn');
77
        $this->setUser('user');
78
        Conf::deleteConfigValue('default-db-connection/password');
79
        $mock = $this->getMock();
80
        $mock->setConnection(false);
81
82
        // assertions
83
        $this->expectException(\Exception::class);
84
85
        // test body
86
        $mock->getConnection();
87
    }
88
89
    /**
90
     * Testing data provider
91
     *
92
     * @return array testing data
93
     */
94
    public function getConnectionForArrayDataProvider(): array
95
    {
96
        return [
97
            // #0, the first case, two connections the first one is fetched
98
            [
99
                function (): void {
100
                    // setup method
101
                    $this->setConnection();
102
                    $this->setConnection('exact-connection');
103
                }
104
            ],
105
            // #1, the first case, two connections the second one is fetched
106
            [
107
                function (): void {
108
                    // setup method
109
                    $this->setConnection('exact-connection');
110
                    $this->setConnection();
111
                }
112
            ],
113
            // #2, the third case, connection was not found
114
            [
115
                function (): void {
116
                    // setup method
117
                    $this->setConnection('first-connection');
118
                    $this->setConnection('second-connection');
119
120
                    $this->expectException(\Exception::class);
121
                }
122
            ]
123
        ];
124
    }
125
126
    /**
127
     * Testing method
128
     *
129
     * @param callable $setup
130
     *            setup method
131
     * @dataProvider getConnectionForArrayDataProvider
132
     */
133
    public function testGetConnectionForArray(callable $setup): void
134
    {
135
        // setup and assertions
136
        $setup();
137
        $mock = $this->getMock();
138
        $mock->setConnection(false);
139
        $mock->expects($this->once())
140
            ->method('constructConnection')
141
            ->willReturn($this->getPdoMock());
142
143
        // test body
144
        $mock->getConnection([
145
            'exact-connection'
146
        ]);
147
    }
148
149
    /**
150
     * Data provider for the test testGetConnectionForArrayException
151
     *
152
     * @return array testing data
153
     */
154
    public function getConnectionForArrayExceptionDataProvider(): array
155
    {
156
        return [
157
            [
158
                [
159
                    'exact-connection'
160
                ]
161
            ],
162
            [
163
                new \stdClass()
164
            ]
165
        ];
166
    }
167
168
    /**
169
     * Testing exception for array type connection name
170
     *
171
     * @param mixed $connectionNAme
172
     *            connection name
173
     * @dataProvider getConnectionForArrayExceptionDataProvider
174
     */
175
    public function testGetConnectionForArrayException($connectionName): void
176
    {
177
        // TODO add snippet for testing exception with data provider
178
        // assertions
179
        $this->expectException(\Exception::class);
180
181
        // setup
182
        Conf::deleteConfigValue('exact-connection/dsn');
183
        $this->setConnection('first-connection');
184
        $this->setConnection('second-connection');
185
        $mock = $this->getMock();
186
        $mock->setConnection(false);
187
188
        // test body
189
        $mock->getConnection($connectionName);
190
    }
191
192
    /**
193
     * Testing method cached getConnection
194
     */
195
    public function testGetConnectionCached(): void
196
    {
197
        // setup
198
        $mock = $this->getMock();
199
        $mock->setConnection(new PdoCrudMock());
200
201
        // test body and assertions
202
        $this->assertInstanceOf(PdoCrudMock::class, $mock->getConnection('some-connection-wich-does-not-exists'));
203
    }
204
}
205