Skip to content

Commit c5d4865

Browse files
authored
Add support for MySQL in PdoConnectionAdapter (#10)
1 parent 8e7f8fb commit c5d4865

7 files changed

Lines changed: 75 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased Changes
99

10+
## [0.2.0](https://github.com/cspray/database-test-case/releases/tag/0.2.0) - 2023-03-02
11+
1012
### Added
1113

1214
- Introduces a `Cspray\DatabaseTestCase\AbstractConnectionAdapter` for implementing functionality common across all `Cspray\DatabaseTestCase\ConnectionAdapter` implementations.
1315
- Provides the `Cspray\DatabaseTestCase\AmpPostgresConnectionAdapter` for working with the amphp/postgres library.
16+
- Adds support for MySQL in `Cspray\DatabaseTestCase\PdoConnecitonAdapter`. The enum `Cspray\DatabaseTestCase\PdoDriver` has been updated to include this new option.
1417

1518
### Changed
1619

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ services:
1313
networks:
1414
databasetestcase:
1515

16+
mysql:
17+
build:
18+
context: .
19+
dockerfile: docker/mysql/Dockerfile
20+
volumes:
21+
- mysqldata:/var/lib/mysql
22+
restart: unless-stopped
23+
environment:
24+
- MYSQL_DATABASE=mysql
25+
- MYSQL_USER=mysql
26+
- MYSQL_PASSWORD=mysql
27+
- MYSQL_ROOT_PASSWORD=mysql
28+
networks:
29+
databasetestcase:
30+
1631
tests:
1732
build:
1833
context: .
@@ -21,6 +36,8 @@ services:
2136
depends_on:
2237
postgres:
2338
condition: service_healthy
39+
mysql:
40+
condition: service_healthy
2441
volumes:
2542
- ./src:/app/src
2643
- ./tests:/app/tests
@@ -34,4 +51,5 @@ networks:
3451
databasetestcase:
3552

3653
volumes:
54+
mysqldata:
3755
pgdata:

docker/mysql/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM mysql:8-debian
2+
3+
COPY /resources/schemas/mysql.sql /docker-entrypoint-initdb.d/
4+
5+
HEALTHCHECK --interval=5s --start-period=7s --retries=5 --timeout=5s CMD mysqladmin ping -h localhost
6+
7+
USER mysql

docker/php/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN apt-get update -y \
44
&& apt-get upgrade -y
55

66
RUN apt-get install git libsodium-dev libzip-dev libpq-dev -y
7-
RUN docker-php-ext-install sodium zip pdo pdo_pgsql pgsql
7+
RUN docker-php-ext-install sodium zip pdo pdo_pgsql pdo_mysql pgsql
88

99
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
1010

resources/schemas/mysql.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE my_table (
2+
id INT PRIMARY KEY AUTO_INCREMENT,
3+
name VARCHAR(255),
4+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
5+
);

src/PdoDriver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
enum PdoDriver : string{
66
case Postgresql = 'pdo_pgsql';
7+
case Mysql = 'pdo_mysql';
78

89
public function getDsnIdentifier() : string {
910
return match ($this) {
10-
self::Postgresql => 'pgsql'
11+
self::Postgresql => 'pgsql',
12+
self::Mysql => 'mysql'
1113
};
1214
}
1315
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Cspray\DatabaseTestCase\Tests\Integration;
4+
5+
use Cspray\DatabaseTestCase\ConnectionAdapter;
6+
use Cspray\DatabaseTestCase\ConnectionAdapterConfig;
7+
use Cspray\DatabaseTestCase\PdoConnectionAdapter;
8+
use Cspray\DatabaseTestCase\PdoDriver;
9+
use PDO;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
12+
#[CoversClass(PdoConnectionAdapter::class)]
13+
class MysqlPdoConnectionAdapterTest extends ConnectionAdapterTestCase {
14+
15+
protected function getExpectedUnderlyingConnectionClassName() : string {
16+
return \PDO::class;
17+
}
18+
19+
protected function executeCountSql(string $table) : int {
20+
$connection = self::getUnderlyingConnection();
21+
assert($connection instanceof PDO);
22+
return $connection->query('SELECT COUNT(*) AS "count" FROM ' . $table)
23+
->fetch(PDO::FETCH_ASSOC)['count'];
24+
}
25+
26+
protected static function getConnectionAdapter() : ConnectionAdapter {
27+
return new PdoConnectionAdapter(
28+
new ConnectionAdapterConfig(
29+
'mysql',
30+
'mysql',
31+
3306,
32+
'mysql',
33+
'mysql'
34+
),
35+
PdoDriver::Mysql
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)