Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased Changes

## [0.2.0](https://github.com/cspray/database-test-case/releases/tag/0.2.0) - 2023-03-02

### Added

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

### Changed

Expand Down
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ services:
networks:
databasetestcase:

mysql:
build:
context: .
dockerfile: docker/mysql/Dockerfile
volumes:
- mysqldata:/var/lib/mysql
restart: unless-stopped
environment:
- MYSQL_DATABASE=mysql
- MYSQL_USER=mysql
- MYSQL_PASSWORD=mysql
- MYSQL_ROOT_PASSWORD=mysql
networks:
databasetestcase:

tests:
build:
context: .
Expand All @@ -21,6 +36,8 @@ services:
depends_on:
postgres:
condition: service_healthy
mysql:
condition: service_healthy
volumes:
- ./src:/app/src
- ./tests:/app/tests
Expand All @@ -34,4 +51,5 @@ networks:
databasetestcase:

volumes:
mysqldata:
pgdata:
7 changes: 7 additions & 0 deletions docker/mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM mysql:8-debian

COPY /resources/schemas/mysql.sql /docker-entrypoint-initdb.d/

HEALTHCHECK --interval=5s --start-period=7s --retries=5 --timeout=5s CMD mysqladmin ping -h localhost

USER mysql
2 changes: 1 addition & 1 deletion docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN apt-get update -y \
&& apt-get upgrade -y

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

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

Expand Down
5 changes: 5 additions & 0 deletions resources/schemas/mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE my_table (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
4 changes: 3 additions & 1 deletion src/PdoDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

enum PdoDriver : string{
case Postgresql = 'pdo_pgsql';
case Mysql = 'pdo_mysql';

public function getDsnIdentifier() : string {
return match ($this) {
self::Postgresql => 'pgsql'
self::Postgresql => 'pgsql',
self::Mysql => 'mysql'
};
}
}
38 changes: 38 additions & 0 deletions tests/Integration/MysqlPdoConnectionAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Cspray\DatabaseTestCase\Tests\Integration;

use Cspray\DatabaseTestCase\ConnectionAdapter;
use Cspray\DatabaseTestCase\ConnectionAdapterConfig;
use Cspray\DatabaseTestCase\PdoConnectionAdapter;
use Cspray\DatabaseTestCase\PdoDriver;
use PDO;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(PdoConnectionAdapter::class)]
class MysqlPdoConnectionAdapterTest extends ConnectionAdapterTestCase {

protected function getExpectedUnderlyingConnectionClassName() : string {
return \PDO::class;
}

protected function executeCountSql(string $table) : int {
$connection = self::getUnderlyingConnection();
assert($connection instanceof PDO);
return $connection->query('SELECT COUNT(*) AS "count" FROM ' . $table)
->fetch(PDO::FETCH_ASSOC)['count'];
}

protected static function getConnectionAdapter() : ConnectionAdapter {
return new PdoConnectionAdapter(
new ConnectionAdapterConfig(
'mysql',
'mysql',
3306,
'mysql',
'mysql'
),
PdoDriver::Mysql
);
}
}