Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0042c80

Browse files
connecting to mysql using PDO, all the trst are passing
1 parent 2147e26 commit 0042c80

File tree

6 files changed

+47
-9
lines changed

6 files changed

+47
-9
lines changed

‎Test/Unit/DatabaseConnectionTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public function testItCanConnectToDatabaseWithPdoApi()
2222
$credentials = $this->getCredentials('pdo');
2323
$pdo = (new PDOConnection($credentials))->connect();
2424
self::assertInstanceOf(DatabaseConnectionInterface::class, $pdo, 'It is an instance of');
25+
return $pdo;
26+
}
27+
28+
/**
29+
* @param DatabaseConnectionInterface $handler Interface
30+
* @depends testItCanConnectToDatabaseWithPdoApi
31+
*/
32+
public function testItIsAValidPdoConnection(DatabaseConnectionInterface $handler)
33+
{
34+
self::assertInstanceOf(\PDO::class, $handler->getConnection());
2535
}
2636

2737
private function getCredentials(string $type)

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "code-architect/bug-tracking-app",
33
"require": {
4-
"ext-json": "*"
4+
"ext-json": "*",
5+
"ext-pdo": "*"
56
},
67
"require-dev": {
78
"phpunit/phpunit": "^7",

‎index.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@
33
require_once __DIR__.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php';
44

55
require_once __DIR__.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Exception'.DIRECTORY_SEPARATOR.'exception.php';
6+
7+
$test = new \App\Database\PDOConnection( [
8+
'driver' => 'mysql',
9+
'host' => 'localhost',
10+
'db_name' => 'bug_app',
11+
'db_username' => 'root',
12+
'db_user_password' => '',
13+
'default_fetch' => PDO::FETCH_OBJ
14+
]);
15+
var_dump($test->connect());

‎src/Config/database.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'host' => 'localhost',
77
'db_name' => 'bug_app',
88
'db_username' => 'root',
9-
'db-user_password' => '',
9+
'db_user_password' => '',
1010
'default_fetch' => PDO::FETCH_OBJ
1111
]
1212
];

‎src/Database/AbstractConnection.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public function __construct(array $credentials)
2121
'Database connection credentials are not mapped correctly, required key: %s',
2222
implode(',', static::REQUIRED_CONNECTION_KEYS)
2323
)
24-
2524
);
2625
}
2726
}
@@ -31,4 +30,6 @@ private function credentialsHaveRequiredKeys(array $credentials): bool
3130
$matches = array_intersect(static::REQUIRED_CONNECTION_KEYS, array_keys($credentials));
3231
return count($matches) === count(static::REQUIRED_CONNECTION_KEYS);
3332
}
33+
34+
abstract protected function parseCredentials(array $credentials): array ;
3435
}

‎src/Database/PDOConnection.php‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
2-
3-
42
namespace App\Database;
5-
6-
3+
useApp\Exception\DatabaseConnectionException;
4+
usePDOException, PDO;
75
use App\Contracts\DatabaseConnectionInterface;
86

97
class PDOConnection extends AbstractConnection implements DatabaseConnectionInterface
@@ -13,17 +11,35 @@ class PDOConnection extends AbstractConnection implements DatabaseConnectionInte
1311
'host',
1412
'db_name',
1513
'db_username',
16-
'db-user_password',
14+
'db_user_password',
1715
'default_fetch'
1816
];
1917

2018
public function connect(): PDOConnection
2119
{
20+
$credentials = $this->parseCredentials($this->credentials);
21+
try{
22+
$this->conn = new PDO(...$credentials);
23+
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
24+
$this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, $this->credentials['default_fetch'] );
25+
}catch (PDOException $e)
26+
{
27+
throw new DatabaseConnectionException($e->getMessage(),$this->credentials, 500);
28+
}
2229
return $this;
2330
}
2431

2532
public function getConnection()
2633
{
27-
// TODO: Implement getConnection() method.
34+
return $this->conn;
35+
}
36+
37+
protected function parseCredentials(array $credentials): array
38+
{
39+
$dsn = sprintf(
40+
'%s:host=%s;dbname=%s',
41+
$credentials['driver'], $credentials['host'], $credentials['db_name']
42+
);
43+
return [$dsn, $credentials['db_username'], $credentials['db_user_password']];
2844
}
2945
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /