|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\PdoCrud\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Mezon\Conf\Conf; |
|
5
|
|
|
|
|
6
|
|
|
class ConnectionTraitUnitTest extends \PHPUnit\Framework\TestCase |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Method returns mock |
|
11
|
|
|
* |
|
12
|
|
|
* @return object mock |
|
13
|
|
|
*/ |
|
14
|
|
|
protected function getPdoMock(): object |
|
15
|
|
|
{ |
|
16
|
|
|
return $this->getMockBuilder(\Mezon\PdoCrud\PdoCrud::class) |
|
|
|
|
|
|
17
|
|
|
->setMethods([ |
|
18
|
|
|
'connect' |
|
19
|
|
|
]) |
|
20
|
|
|
->getMock(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Method returns mock |
|
25
|
|
|
* |
|
26
|
|
|
* @return object mock |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function getMock(): object |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->getMockBuilder(TraitClient::class) |
|
|
|
|
|
|
31
|
|
|
->setMethods([ |
|
32
|
|
|
'constructConnection' |
|
33
|
|
|
]) |
|
34
|
|
|
->getMock(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Method sets dsn |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $dsn dsn |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function setDsn(string $dsn):void{ |
|
43
|
|
|
Conf::setConfigValue('default-db-connection/dsn', $dsn); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Method sets user |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $user user |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function setUser(string $user):void{ |
|
52
|
|
|
Conf::setConfigValue('default-db-connection/user', $user); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Method sets password |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $password password |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function setPassword(string $password):void{ |
|
61
|
|
|
Conf::setConfigValue('default-db-connection/password', $password); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Testing insertion method |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testGetConnection(): void |
|
68
|
|
|
{ |
|
69
|
|
|
// setupp |
|
70
|
|
|
$this->setDsn('dsn'); |
|
71
|
|
|
$this->setUser('user'); |
|
72
|
|
|
$this->setPassword('password'); |
|
73
|
|
|
$mock = $this->getMock(); |
|
74
|
|
|
|
|
75
|
|
|
$mock->expects($this->once()) |
|
76
|
|
|
->method('constructConnection') |
|
77
|
|
|
->willReturn($this->getPdoMock()); |
|
78
|
|
|
|
|
79
|
|
|
// test body and assertionss |
|
80
|
|
|
$mock->getConnection(); // creating connection object |
|
81
|
|
|
$mock->getConnection(); // getting created object |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Asserting exception if dsn is not set |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testDsnException(): void |
|
88
|
|
|
{ |
|
89
|
|
|
// setup |
|
90
|
|
|
\Mezon\Conf\Conf::deleteConfigValue('default-db-connection/dsn'); |
|
91
|
|
|
$this->setUser('user'); |
|
92
|
|
|
$this->setPassword('password'); |
|
93
|
|
|
$mock = $this->getMock(); |
|
94
|
|
|
$mock->setConnection(false); |
|
95
|
|
|
|
|
96
|
|
|
// assertions |
|
97
|
|
|
$this->expectException(\Exception::class); |
|
98
|
|
|
|
|
99
|
|
|
// test body |
|
100
|
|
|
$mock->getConnection(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Asserting exception if user is not set |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testUserException(): void |
|
107
|
|
|
{ |
|
108
|
|
|
// setup |
|
109
|
|
|
$this->setDsn('dsn'); |
|
110
|
|
|
\Mezon\Conf\Conf::deleteConfigValue('default-db-connection/user'); |
|
111
|
|
|
$this->setPassword('password'); |
|
112
|
|
|
$mock = $this->getMock(); |
|
113
|
|
|
$mock->setConnection(false); |
|
114
|
|
|
|
|
115
|
|
|
// assertions |
|
116
|
|
|
$this->expectException(\Exception::class); |
|
117
|
|
|
|
|
118
|
|
|
// test body |
|
119
|
|
|
$mock->getConnection(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Asserting exception if password is not set |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testPasswordException(): void |
|
126
|
|
|
{ |
|
127
|
|
|
// setup |
|
128
|
|
|
$this->setDsn('dsn'); |
|
129
|
|
|
$this->setUser('user'); |
|
130
|
|
|
\Mezon\Conf\Conf::deleteConfigValue('default-db-connection/password'); |
|
131
|
|
|
$mock = $this->getMock(); |
|
132
|
|
|
$mock->setConnection(false); |
|
133
|
|
|
|
|
134
|
|
|
// assertions |
|
135
|
|
|
$this->expectException(\Exception::class); |
|
136
|
|
|
|
|
137
|
|
|
// test body |
|
138
|
|
|
$mock->getConnection(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
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.