Skip to content

Commit

Permalink
run tests for DoctrineStorage with MySQL, PostgreSQL, SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
craue committed Aug 29, 2023
1 parent 515871d commit 5c44c27
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 7 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
SYMFONY_REQUIRE: ${{ matrix.symfony }}
SYMFONY_DEPRECATIONS_HELPER: ${{ matrix.symfony-deprecations }}
PARAM_DB_DRIVER: ${{ matrix.db-driver }}
TEST_DB_FLAVOR_MYSQL_DSN: mysql://test@127.0.0.1/craue_form_flow
TEST_DB_FLAVOR_POSTGRESQL_DSN: pgsql://test:test@127.0.0.1/craue_form_flow_tests
TEST_DB_FLAVOR_SQLITE_DSN: sqlite://memory

strategy:
fail-fast: false
Expand Down Expand Up @@ -60,6 +63,25 @@ jobs:
stability: dev
allow-failure: true

services:
mysql:
image: mysql
env:
MYSQL_USER: test
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: craue_form_flow_tests
options: >-
--health-cmd "mysqladmin ping --silent"
postgres:
image: postgres
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: craue_form_flow_tests
options: >-
--health-cmd pg_isready
steps:
- name: checkout
uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Craue\FormFlowBundle\Tests\Storage;

use Craue\FormFlowBundle\Storage\DoctrineStorage;
use Craue\FormFlowBundle\Storage\StorageInterface;
use Craue\FormFlowBundle\Storage\StorageKeyGeneratorInterface;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;

Expand All @@ -15,22 +17,35 @@
* @copyright 2011-2023 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class DoctrineStorageTest extends AbstractStorageTest {
abstract class AbstractDoctrineStorageTest extends AbstractStorageTest {

/**
* {@inheritDoc}
* @var Connection
*/
protected function getStorageImplementation() {
private $conn;

abstract protected function getConnectionDsnEnvVar() : string;

protected function getConnection() : Connection {
return $this->conn;
}

protected function getStorageImplementation() : StorageInterface {
// TODO remove $configuration variable as soon as DBAL >= 4 is required
$configuration = new Configuration();

if (\method_exists($configuration, 'setSchemaManagerFactory')) {
$configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
}

$conn = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'memory' => true,
$dsnEnvVar = $this->getConnectionDsnEnvVar();

if (empty($_ENV[$dsnEnvVar])) {
$this->markTestSkipped(sprintf('Environment variable %s is not set.', $dsnEnvVar));
}

$this->conn = DriverManager::getConnection([
'url' => $_ENV[$dsnEnvVar],
], $configuration);

$generator = $this->createMock(StorageKeyGeneratorInterface::class);
Expand All @@ -40,7 +55,17 @@ protected function getStorageImplementation() {
->will($this->returnArgument(0))
;

return new DoctrineStorage($conn, $generator);
return new DoctrineStorage($this->conn, $generator);
}

protected function setUp() : void {
parent::setUp();

// ensure the table doesn't exist
$schemaManager = $this->conn->createSchemaManager();
if ($schemaManager->tablesExist([DoctrineStorage::TABLE])) {
$schemaManager->dropTable(DoctrineStorage::TABLE);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions Tests/Storage/DoctrineStorageMysqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Craue\FormFlowBundle\Tests\Storage;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2023 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class DoctrineStorageMysqlTest extends AbstractDoctrineStorageTest {

protected function getConnectionDsnEnvVar() : string {
return 'TEST_DB_FLAVOR_MYSQL_DSN';
}

}
18 changes: 18 additions & 0 deletions Tests/Storage/DoctrineStoragePostgresqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Craue\FormFlowBundle\Tests\Storage;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2023 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class DoctrineStoragePostgresqlTest extends AbstractDoctrineStorageTest {

protected function getConnectionDsnEnvVar() : string {
return 'TEST_DB_FLAVOR_POSTGRESQL_DSN';
}

}
18 changes: 18 additions & 0 deletions Tests/Storage/DoctrineStorageSqliteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Craue\FormFlowBundle\Tests\Storage;

/**
* @group unit
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2023 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class DoctrineStorageSqliteTest extends AbstractDoctrineStorageTest {

protected function getConnectionDsnEnvVar() : string {
return 'TEST_DB_FLAVOR_SQLITE_DSN';
}

}
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<php>
<env name="CACHE_DIR" value="/path/to/your/cache/" />
<env name="LOG_DIR" value="/path/to/your/log/" />
<env name="TEST_DB_FLAVOR_MYSQL_DSN" value="mysql://test@127.0.0.1/craue_form_flow" />
<env name="TEST_DB_FLAVOR_POSTGRESQL_DSN" value="pgsql://test:test@127.0.0.1/craue_form_flow_tests" />
<env name="TEST_DB_FLAVOR_SQLITE_DSN" value="sqlite://memory" />
</php>
-->
<listeners>
Expand Down

0 comments on commit 5c44c27

Please sign in to comment.