Skip to content

Commit 2625da9

Browse files
authored
Test credentials as string without password (#20)
1 parent cd1e0e5 commit 2625da9

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/Credentials.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@ public function toString(): string
138138
$this->dbName
139139
);
140140

141+
if (strpos($asString, ':@') === 0) {
142+
return rawurldecode(
143+
substr($asString, 2)
144+
);
145+
}
146+
141147
return rawurldecode(
142-
str_replace(':@', '', $asString)
148+
str_replace(':@', '@', $asString)
143149
);
144150
}
145151
}

tests/CredentialsTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DriftPHP Project
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author David Llop <[email protected]>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\DBAL\Tests;
17+
18+
use Drift\DBAL\Credentials;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Class ConnectionTest.
23+
*/
24+
class CredentialsTest extends TestCase
25+
{
26+
/**
27+
* Test that credentials works as expected with all the fields filled.
28+
*/
29+
public function testCredentialsCanBeConvertedToString()
30+
{
31+
$credentials = new Credentials('127.0.0.1', '3306', 'user', 'password', 'database');
32+
33+
$this->assertEquals('user:[email protected]:3306/database', $credentials->toString());
34+
}
35+
36+
/**
37+
* Test that credentials works as expected without login information.
38+
*/
39+
public function testCredentialsCanBeConvertedToStringWithoutLogin()
40+
{
41+
$credentials = new Credentials('127.0.0.1', '3306', '', '', 'database');
42+
43+
$this->assertEquals('127.0.0.1:3306/database', $credentials->toString());
44+
}
45+
46+
/**
47+
* Test that credentials works as expected without password.
48+
*/
49+
public function testCredentialsCanBeConvertedToStringWithoutPassword()
50+
{
51+
$credentials = new Credentials('127.0.0.1', '3306', 'user', '', 'database');
52+
53+
$this->assertEquals('[email protected]:3306/database', $credentials->toString());
54+
}
55+
}

0 commit comments

Comments
 (0)