1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Test class for DbUser. |
4
|
|
|
* |
5
|
|
|
* @author Stéphane Monnot <[email protected]> |
6
|
|
|
* @license MIT http://mit-license.org/ |
7
|
|
|
*/ |
8
|
|
|
namespace Shinbuntu\DbUser\Tests\Units; |
9
|
|
|
|
10
|
|
|
use atoum; |
11
|
|
|
use mock\Doctrine\DBAL\Connection; |
12
|
|
|
use Shinbuntu\DbUser\DbUser as testedClass; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Test class for DbUser. |
16
|
|
|
* |
17
|
|
|
* @author Stéphane Monnot <[email protected]> |
18
|
|
|
* @license MIT http://mit-license.org/ |
19
|
|
|
*/ |
20
|
|
|
class DbUser extends atoum |
21
|
|
|
{ |
22
|
|
|
private $connection = null; |
23
|
|
|
|
24
|
|
|
private function getConnection() |
25
|
|
|
{ |
26
|
|
|
if ($this->connection !== null) { |
27
|
|
|
return $this->connection; |
28
|
|
|
} |
29
|
|
|
$this->mockGenerator->shuntParentClassCalls(); |
30
|
|
|
$this->mockGenerator->orphanize('__construct'); |
31
|
|
|
$this->mockGenerator->orphanize('__construct'); |
32
|
|
|
|
33
|
|
|
$this->connection = new Connection(); |
34
|
|
|
$this->connection->getMockController()->connect = function () { |
35
|
|
|
}; |
36
|
|
|
$this->connection->getMockController()->fetchColumn = function () { |
37
|
|
|
}; |
38
|
|
|
$this->connection->getMockController()->quote = function ($input) { |
39
|
|
|
return '"'.addslashes($input).'"'; |
40
|
|
|
}; |
41
|
|
|
|
42
|
|
|
$this->mockGenerator->unshuntParentClassCalls(); |
43
|
|
|
|
44
|
|
|
return $this->connection; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test createUserQuery method. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function testCreateUserQuery() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$connection = $this->getConnection(); |
55
|
|
|
|
56
|
|
|
$this |
57
|
|
|
->if($this->newTestedInstance($connection)) |
58
|
|
|
->string($this->testedInstance->createUserQuery('test_username', '!super_secure_password$')) |
59
|
|
|
->isEqualTo('CREATE USER test_username@localhost IDENTIFIED BY "!super_secure_password$";'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test dropUser method. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function testDropUserQuery() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$connection = $this->getConnection(); |
70
|
|
|
|
71
|
|
|
$this |
72
|
|
|
->if($this->newTestedInstance($connection)) |
73
|
|
|
->string($this->testedInstance->dropUserQuery('test_username', '!super_secure_password$')) |
74
|
|
|
->isEqualTo('DROP USER test_username@localhost;'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test userExistQuery method. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function testUserExistQuery() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$connection = $this->getConnection(); |
85
|
|
|
|
86
|
|
|
$this |
87
|
|
|
->if($this->newTestedInstance($connection)) |
88
|
|
|
->string($this->testedInstance->userExistQuery('test_username')) |
89
|
|
|
->isEqualTo('SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = "test_username");'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test flushPrivilegesQuery method. |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function testFlushPrivilegesQuery() |
98
|
|
|
{ |
99
|
|
|
$connection = $this->getConnection(); |
100
|
|
|
|
101
|
|
|
$this |
102
|
|
|
->if($this->newTestedInstance($connection)) |
103
|
|
|
->string($this->testedInstance->flushPrivilegesQuery()) |
104
|
|
|
->isEqualTo('FLUSH PRIVILEGES;'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Test changePrivilegesQuery method. |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function testChangePrivilegesQuery() |
113
|
|
|
{ |
114
|
|
|
$connection = $this->getConnection(); |
115
|
|
|
|
116
|
|
|
$this |
117
|
|
|
->if($this->newTestedInstance($connection)) |
118
|
|
|
->string( |
119
|
|
|
$this->testedInstance->changePrivilegesQuery( |
120
|
|
|
testedClass::PRIVILEGE_STATEMENT_GRANT, |
121
|
|
|
'test_username', |
122
|
|
|
[ |
123
|
|
|
testedClass::PRIVILEGE_CREATE, |
124
|
|
|
testedClass::PRIVILEGE_UPDATE, |
125
|
|
|
testedClass::PRIVILEGE_DELETE, |
126
|
|
|
], |
127
|
|
|
'test_database', |
128
|
|
|
'test_table' |
129
|
|
|
)) |
130
|
|
|
->isEqualTo('GRANT CREATE, UPDATE, DELETE ON test_database.test_table TO "test_username"@localhost;') |
131
|
|
|
|
132
|
|
|
->string( |
133
|
|
|
$this->testedInstance->changePrivilegesQuery( |
134
|
|
|
testedClass::PRIVILEGE_STATEMENT_GRANT, |
135
|
|
|
'test_username', |
136
|
|
|
testedClass::PRIVILEGE_CREATE, |
137
|
|
|
'test_database', |
138
|
|
|
'test_table' |
139
|
|
|
)) |
140
|
|
|
->isEqualTo('GRANT CREATE ON test_database.test_table TO "test_username"@localhost;'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Fake Tests for doctrine to ignore in coverage percentage. |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function testDoctrine() |
149
|
|
|
{ |
150
|
|
|
$connection = $this->getConnection(); |
151
|
|
|
|
152
|
|
|
$this |
153
|
|
|
->if($this->newTestedInstance($connection)) |
154
|
|
|
->variable($this->testedInstance->flushPrivileges()) |
155
|
|
|
->isEqualTo(true) |
156
|
|
|
|
157
|
|
|
->variable($this->testedInstance->revokePrivileges('test_username')) |
158
|
|
|
->isEqualTo(true) |
159
|
|
|
|
160
|
|
|
->variable($this->testedInstance->grantPrivileges('test_username')) |
161
|
|
|
->isEqualTo(true) |
162
|
|
|
|
163
|
|
|
->variable($this->testedInstance->userExist('test_username')) |
164
|
|
|
->isNull() |
165
|
|
|
|
166
|
|
|
->variable($this->testedInstance->dropUser('test_username')) |
167
|
|
|
->isEqualTo(true) |
168
|
|
|
|
169
|
|
|
->variable($this->testedInstance->createUser('test_username', 'test_password')) |
170
|
|
|
->isEqualTo(true); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.