diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile index 8fbe6d0..01a4c23 100644 --- a/docker/php/Dockerfile +++ b/docker/php/Dockerfile @@ -1,4 +1,4 @@ -FROM php:8.2.1-zts-bullseye AS php +FROM php:8.3-zts-bullseye AS php RUN apt-get update -y \ && apt-get upgrade -y diff --git a/resources/schemas/sqlite.sql b/resources/schemas/sqlite.sql new file mode 100644 index 0000000..3961f95 --- /dev/null +++ b/resources/schemas/sqlite.sql @@ -0,0 +1,5 @@ +CREATE TABLE my_table ( + id INTEGER PRIMARY KEY, + name VARCHAR(255), + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL +); \ No newline at end of file diff --git a/src/AmpPostgresConnectionAdapter.php b/src/AmpPostgresConnectionAdapter.php index c37b39e..6400354 100644 --- a/src/AmpPostgresConnectionAdapter.php +++ b/src/AmpPostgresConnectionAdapter.php @@ -20,7 +20,7 @@ private function __construct( private readonly Closure $connectionFactory ) {} - public static function newConnectionFromConfig(ConnectionAdapterConfig $config) : self { + public static function fromConnectionConfig(ConnectionAdapterConfig $config) : self { return new self(fn() => connect( PostgresConfig::fromString(sprintf( 'db=%s host=%s port=%d user=%s pass=%s', @@ -33,7 +33,7 @@ public static function newConnectionFromConfig(ConnectionAdapterConfig $config) )); } - public static function existingConnection(PostgresLink $link) : self { + public static function fromExistingConnection(PostgresLink $link) : self { return new self(fn() => $link); } diff --git a/src/PdoConnectionAdapter.php b/src/PdoConnectionAdapter.php index 0fe4e03..e7e8072 100644 --- a/src/PdoConnectionAdapter.php +++ b/src/PdoConnectionAdapter.php @@ -2,12 +2,9 @@ namespace Cspray\DatabaseTestCase; -use Cspray\DatabaseTestCase\DatabaseRepresentation\Row; -use Cspray\DatabaseTestCase\DatabaseRepresentation\Table; -use Cspray\DatabaseTestCase\Exception\UnableToGetTable; +use Closure; use Cspray\DatabaseTestCase\Exception\MissingRequiredExtension; use PDO; -use PDOException; if (! extension_loaded('pdo')) { throw new MissingRequiredExtension('You must enable ext-pdo to use the ' . PdoConnectionAdapter::class); @@ -17,27 +14,25 @@ final class PdoConnectionAdapter extends AbstractConnectionAdapter { private ?PDO $connection = null; - public function __construct( - private readonly ConnectionAdapterConfig $adapterConfig, + private function __construct( + private readonly Closure $pdoSupplier, private readonly PdoDriver $pdoDriver ) {} + public static function fromConnectionConfig(ConnectionAdapterConfig $adapterConfig, PdoDriver $pdoDriver) : self { + return self::fromExistingConnection(new PDO($pdoDriver->dsn($adapterConfig)), $pdoDriver); + } + + public static function fromExistingConnection(PDO $pdo, PdoDriver $pdoDriver) : self { + return new self(static fn() => $pdo, $pdoDriver); + } + public function establishConnection() : void { - $this->connection = new PDO( - sprintf( - '%s:host=%s;port=%d;dbname=%s;user=%s;password=%s', - $this->pdoDriver->getDsnIdentifier(), - $this->adapterConfig->host, - $this->adapterConfig->port, - $this->adapterConfig->database, - $this->adapterConfig->user, - $this->adapterConfig->password - ) - ); + $this->connection = ($this->pdoSupplier)(); } public function onTestStart() : void { - $this->connection->query('START TRANSACTION'); + $this->connection->query($this->pdoDriver->startTransactionSql()); } public function onTestStop() : void { diff --git a/src/PdoDriver.php b/src/PdoDriver.php index 34e9eaf..07f6647 100644 --- a/src/PdoDriver.php +++ b/src/PdoDriver.php @@ -5,11 +5,39 @@ enum PdoDriver : string{ case Postgresql = 'pdo_pgsql'; case Mysql = 'pdo_mysql'; + case Sqlite = 'pdo_sqlite'; - public function getDsnIdentifier() : string { + public function dsn(ConnectionAdapterConfig $adapterConfig) : string { + return match ($this) { + self::Sqlite => sprintf( + 'sqlite:%s', + $adapterConfig->host + ), + default => sprintf( + '%s:host=%s;port=%d;dbname=%s;user=%s;password=%s', + $this->dsnIdentifier(), + $adapterConfig->host, + $adapterConfig->port, + $adapterConfig->database, + $adapterConfig->user, + $adapterConfig->password + ), + }; + + } + + private function dsnIdentifier() : string { return match ($this) { self::Postgresql => 'pgsql', - self::Mysql => 'mysql' + self::Mysql => 'mysql', + self::Sqlite => 'sqlite', + }; + } + + public function startTransactionSql() : string { + return match ($this) { + self::Sqlite => 'BEGIN TRANSACTION', + default => 'START TRANSACTION', }; } } \ No newline at end of file diff --git a/tests/Integration/AmpPostgresConnectionAdapterTest.php b/tests/Integration/AmpPostgresConnectionAdapterTest.php index 2adf338..7f891f3 100644 --- a/tests/Integration/AmpPostgresConnectionAdapterTest.php +++ b/tests/Integration/AmpPostgresConnectionAdapterTest.php @@ -22,6 +22,6 @@ protected function executeCountSql(string $table) : int { } protected static function getConnectionAdapter() : ConnectionAdapter { - return AmpPostgresConnectionAdapter::newConnectionFromConfig(new PostgresConnectionConfig()); + return AmpPostgresConnectionAdapter::fromConnectionConfig(new PostgresConnectionConfig()); } } \ No newline at end of file diff --git a/tests/Integration/ExistingPdoConnectionAdapterTest.php b/tests/Integration/ExistingPdoConnectionAdapterTest.php new file mode 100644 index 0000000..66c639d --- /dev/null +++ b/tests/Integration/ExistingPdoConnectionAdapterTest.php @@ -0,0 +1,30 @@ +query('SELECT COUNT(*) AS "count" FROM ' . $table) + ->fetch(PDO::FETCH_ASSOC)['count']; + } + + protected static function getConnectionAdapter() : ConnectionAdapter { + $pdo = new PDO('sqlite::memory:'); + $pdo->query(file_get_contents(dirname(__DIR__, 2) . '/resources/schemas/sqlite.sql')); + return PdoConnectionAdapter::fromExistingConnection($pdo, PdoDriver::Sqlite); + } +} \ No newline at end of file diff --git a/tests/Integration/MysqlPdoConnectionAdapterTest.php b/tests/Integration/MysqlPdoConnectionAdapterTest.php index 494826b..e727775 100644 --- a/tests/Integration/MysqlPdoConnectionAdapterTest.php +++ b/tests/Integration/MysqlPdoConnectionAdapterTest.php @@ -24,7 +24,7 @@ protected function executeCountSql(string $table) : int { } protected static function getConnectionAdapter() : ConnectionAdapter { - return new PdoConnectionAdapter( + return PdoConnectionAdapter::fromConnectionConfig( new ConnectionAdapterConfig( 'mysql', 'mysql', diff --git a/tests/Integration/PostgresPdoConnectionAdapterTest.php b/tests/Integration/PostgresPdoConnectionAdapterTest.php index 9bf87e2..c3959bd 100644 --- a/tests/Integration/PostgresPdoConnectionAdapterTest.php +++ b/tests/Integration/PostgresPdoConnectionAdapterTest.php @@ -14,7 +14,7 @@ class PostgresPdoConnectionAdapterTest extends ConnectionAdapterTestCase { protected static function getConnectionAdapter() : ConnectionAdapter { - return new PdoConnectionAdapter( + return PdoConnectionAdapter::fromConnectionConfig( new PostgresConnectionConfig(), PdoDriver::Postgresql );