Skip to content

Commit 3c33706

Browse files
author
Willem Stuursma-Ruwen
authored
Merge pull request #44 from mvorisek/gh_allow_to_test_with_db_with_pass
Allow to test /w DB /w password
2 parents 7e9419c + 3e63027 commit 3c33706

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ env:
1616
- REDIS_URIS=redis://localhost:63791,redis://localhost:63792,redis://localhost:63793
1717
- MYSQL_DSN="mysql:host=localhost;dbname=test"
1818
- MYSQL_USER=travis
19+
- MYSQL_PASSWORD=
1920
- PGSQL_DSN="pgsql:host=localhost;dbname=test;"
2021
- PGSQL_USER=postgres
22+
- PGSQL_PASSWORD=
2123

2224
services:
2325
- memcached

tests/mutex/MutexConcurrencyTest.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*
1616
* - MEMCACHE_HOST
1717
* - REDIS_URIS - a comma separated list of redis:// URIs.
18-
* - MYSQL_DSN, MYSQL_USER
19-
* - PGSQL_DSN, PGSQL_USER
18+
* - MYSQL_DSN, MYSQL_USER, MYSQL_PASSWORD
19+
* - PGSQL_DSN, PGSQL_USER, PGSQL_PASSWORD
2020
*
2121
* @author Markus Malkusch <[email protected]>
2222
* @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations
@@ -49,13 +49,14 @@ public static function tearDownAfterClass(): void
4949
*
5050
* @param string $dsn The DSN.
5151
* @param string $user The user.
52+
* @param string $password The password.
5253
*
5354
* @return \PDO The PDO.
5455
*/
55-
private function getPDO(string $dsn, string $user): \PDO
56+
private function getPDO(string $dsn, string $user, string $password): \PDO
5657
{
5758
if ($this->pdo === null) {
58-
$this->pdo = new \PDO($dsn, $user);
59+
$this->pdo = new \PDO($dsn, $user, $password);
5960
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
6061
}
6162

@@ -133,8 +134,8 @@ function (int $increment) use ($filename): int {
133134
];
134135
}, $this->provideMutexFactories());
135136

136-
$addPDO = function ($dsn, $user, $vendor) use (&$cases) {
137-
$pdo = $this->getPDO($dsn, $user);
137+
$addPDO = function ($dsn, $user, $password, $vendor) use (&$cases) {
138+
$pdo = $this->getPDO($dsn, $user, $password);
138139

139140
$options = ['mysql' => 'engine=InnoDB'];
140141
$option = $options[$vendor] ?? '';
@@ -148,12 +149,12 @@ function (int $increment) use ($filename): int {
148149
$this->pdo = null;
149150

150151
$cases[$vendor] = [
151-
function ($increment) use ($dsn, $user) {
152+
function ($increment) use ($dsn, $user, $password) {
152153
// This prevents using a closed connection from a child.
153154
if ($increment == 0) {
154155
$this->pdo = null;
155156
}
156-
$pdo = $this->getPDO($dsn, $user);
157+
$pdo = $this->getPDO($dsn, $user, $password);
157158
$id = 1;
158159
$select = $pdo->prepare('SELECT counter FROM counter WHERE id = ? FOR UPDATE');
159160
$select->execute([$id]);
@@ -166,9 +167,9 @@ function ($increment) use ($dsn, $user) {
166167

167168
return $counter;
168169
},
169-
function ($timeout = 3) use ($dsn, $user) {
170+
function ($timeout = 3) use ($dsn, $user, $password) {
170171
$this->pdo = null;
171-
$pdo = $this->getPDO($dsn, $user);
172+
$pdo = $this->getPDO($dsn, $user, $password);
172173

173174
return new TransactionalMutex($pdo, $timeout);
174175
}
@@ -178,13 +179,15 @@ function ($timeout = 3) use ($dsn, $user) {
178179
if (getenv('MYSQL_DSN')) {
179180
$dsn = getenv('MYSQL_DSN');
180181
$user = getenv('MYSQL_USER');
181-
$addPDO($dsn, $user, 'mysql');
182+
$password = getenv('MYSQL_PASSWORD');
183+
$addPDO($dsn, $user, $password, 'mysql');
182184
}
183185

184186
if (getenv('PGSQL_DSN')) {
185187
$dsn = getenv('PGSQL_DSN');
186188
$user = getenv('PGSQL_USER');
187-
$addPDO($dsn, $user, 'postgres');
189+
$password = getenv('PGSQL_PASSWORD');
190+
$addPDO($dsn, $user, $password, 'postgres');
188191
}
189192

190193
return $cases;
@@ -306,7 +309,7 @@ function (string $uri): Redis {
306309

307310
if (getenv('MYSQL_DSN')) {
308311
$cases['MySQLMutex'] = [function ($timeout = 3): Mutex {
309-
$pdo = new \PDO(getenv('MYSQL_DSN'), getenv('MYSQL_USER'));
312+
$pdo = new \PDO(getenv('MYSQL_DSN'), getenv('MYSQL_USER'), getenv('MYSQL_USER'));
310313
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
311314

312315
return new MySQLMutex($pdo, 'test', $timeout);
@@ -315,7 +318,7 @@ function (string $uri): Redis {
315318

316319
if (getenv('PGSQL_DSN')) {
317320
$cases['PgAdvisoryLockMutex'] = [function (): Mutex {
318-
$pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'));
321+
$pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'), getenv('PGSQL_PASSWORD'));
319322
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
320323

321324
return new PgAdvisoryLockMutex($pdo, 'test');

tests/mutex/MutexTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function ($uri) {
152152

153153
if (getenv('MYSQL_DSN')) {
154154
$cases['MySQLMutex'] = [function (): Mutex {
155-
$pdo = new \PDO(getenv('MYSQL_DSN'), getenv('MYSQL_USER'));
155+
$pdo = new \PDO(getenv('MYSQL_DSN'), getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'));
156156
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
157157

158158
return new MySQLMutex($pdo, 'test' . time(), self::TIMEOUT);
@@ -161,7 +161,7 @@ function ($uri) {
161161

162162
if (getenv('PGSQL_DSN')) {
163163
$cases['PgAdvisoryLockMutex'] = [function (): Mutex {
164-
$pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'));
164+
$pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'), getenv('PGSQL_PASSWORD'));
165165
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
166166

167167
return new PgAdvisoryLockMutex($pdo, 'test');

tests/mutex/TransactionalMutexTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public function testRollbackAfterFailedCommitFails()
182182
*
183183
* - MYSQL_DSN
184184
* - MYSQL_USER
185+
* - MYSQL_PASSWORD
185186
*
186187
* @return \PDO The MySQL PDO.
187188
*/
@@ -193,7 +194,8 @@ private function buildMySqlPdo()
193194

194195
$dsn = getenv('MYSQL_DSN');
195196
$user = getenv('MYSQL_USER');
196-
$pdo = new \PDO($dsn, $user);
197+
$password = getenv('MYSQL_PASSWORD');
198+
$pdo = new \PDO($dsn, $user, $password);
197199

198200
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
199201

0 commit comments

Comments
 (0)