Skip to content

Commit 8c3ab2a

Browse files
authored
Merge pull request #421 from craue/tests-with-databases
run tests with MySQL, PostgreSQL, SQLite
2 parents 515871d + 1fae5f6 commit 8c3ab2a

27 files changed

Lines changed: 249 additions & 70 deletions

.github/workflows/tests.yml

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ jobs:
1616
env:
1717
SYMFONY_REQUIRE: ${{ matrix.symfony }}
1818
SYMFONY_DEPRECATIONS_HELPER: ${{ matrix.symfony-deprecations }}
19-
PARAM_DB_DRIVER: ${{ matrix.db-driver }}
19+
DB_DSN_MYSQL: mysql://test:test@127.0.0.1/craue_form_flow_tests
20+
DB_DSN_POSTGRESQL: pgsql://test:test@127.0.0.1/craue_form_flow_tests
21+
DB_DSN_SQLITE: sqlite:///sqlite.db
2022

2123
strategy:
2224
fail-fast: false
@@ -31,11 +33,6 @@ jobs:
3133
-
3234
php: '7.3'
3335
symfony: '4.4.*'
34-
-
35-
php: '7.3'
36-
symfony: '4.4.*'
37-
note: sqlite
38-
db-driver: pdo_sqlite
3936
-
4037
php: '7.4'
4138
symfony: '5.4.*'
@@ -60,6 +57,36 @@ jobs:
6057
stability: dev
6158
allow-failure: true
6259

60+
services:
61+
mysql:
62+
image: mysql:${{ (matrix.php == '7.3' && '5.7') || '8.0' }}
63+
env:
64+
MYSQL_USER: test
65+
MYSQL_PASSWORD: test
66+
MYSQL_DATABASE: craue_form_flow_tests
67+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
68+
options: >-
69+
--health-cmd "mysqladmin ping --silent"
70+
--health-interval 5s
71+
--health-timeout 5s
72+
--health-retries 5
73+
ports:
74+
- 3306:3306
75+
76+
postgres:
77+
image: postgres
78+
env:
79+
POSTGRES_USER: test
80+
POSTGRES_PASSWORD: test
81+
POSTGRES_DB: craue_form_flow_tests
82+
options: >-
83+
--health-cmd pg_isready
84+
--health-interval 5s
85+
--health-timeout 5s
86+
--health-retries 5
87+
ports:
88+
- 5432:5432
89+
6390
steps:
6491
- name: checkout
6592
uses: actions/checkout@v3
@@ -68,9 +95,9 @@ jobs:
6895
uses: shivammathur/setup-php@v2
6996
with:
7097
php-version: ${{ matrix.php }}
71-
extensions: mysql, redis
98+
extensions: pdo_mysql, pdo_pgsql, pdo_sqlite
7299
coverage: pcov
73-
ini-values: memory_limit=-1
100+
ini-values: memory_limit=-1, variables_order="EGPCS"
74101
tools: flex
75102
env:
76103
fail-fast: true # interrupt on extension setup error
@@ -91,7 +118,25 @@ jobs:
91118
composer-options: --prefer-dist
92119

93120
- name: run PHPUnit
94-
run: vendor/bin/phpunit -v --coverage-clover build/logs/clover.xml
121+
run: vendor/bin/phpunit -v --coverage-clover build/logs/clover.xml --exclude-group run-with-multiple-databases-only
122+
123+
- name: run PHPUnit with MySQL
124+
if: always()
125+
run: vendor/bin/phpunit -v --coverage-clover build/logs/clover-mysql.xml --group run-with-multiple-databases,run-with-multiple-databases-only
126+
env:
127+
DB_FLAVOR: mysql
128+
129+
- name: run PHPUnit with PostgreSQL
130+
if: always()
131+
run: vendor/bin/phpunit -v --coverage-clover build/logs/clover-postgresql.xml --group run-with-multiple-databases,run-with-multiple-databases-only
132+
env:
133+
DB_FLAVOR: postgresql
134+
135+
- name: run PHPUnit with SQLite
136+
if: always()
137+
run: vendor/bin/phpunit -v --coverage-clover build/logs/clover-sqlite.xml --group run-with-multiple-databases,run-with-multiple-databases-only
138+
env:
139+
DB_FLAVOR: sqlite
95140

96141
- name: upload code coverage data
97142
if: github.repository == 'craue/CraueFormFlowBundle'
@@ -101,4 +146,4 @@ jobs:
101146
run: |
102147
unset SYMFONY_REQUIRE
103148
composer global require php-coveralls/php-coveralls
104-
php-coveralls --coverage_clover=build/logs/clover.xml -v
149+
php-coveralls -v --coverage_clover "build/logs/clover*.xml"

Storage/SerializableFile.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,30 @@ public static function isSupported($file) {
7070
return $file instanceof UploadedFile;
7171
}
7272

73+
public function __serialize() : array {
74+
return [
75+
'content' => $this->content,
76+
'type' => $this->type,
77+
'clientOriginalName' => $this->clientOriginalName,
78+
'clientMimeType' => $this->clientMimeType,
79+
];
80+
}
81+
82+
public function __unserialize(array $data) : void {
83+
// TODO remove for 4.0
84+
// handle representation of object which got serialized before `__serialize` method was added
85+
if (count(array_diff(array_keys($data), ["\x00*\x00content", "\x00*\x00type", "\x00*\x00clientOriginalName", "\x00*\x00clientMimeType"])) === 0) {
86+
$this->content = $data["\x00*\x00content"];
87+
$this->type = $data["\x00*\x00type"];
88+
$this->clientOriginalName = $data["\x00*\x00clientOriginalName"];
89+
$this->clientMimeType = $data["\x00*\x00clientMimeType"];
90+
return;
91+
}
92+
93+
$this->content = $data['content'];
94+
$this->type = $data['type'];
95+
$this->clientOriginalName = $data['clientOriginalName'];
96+
$this->clientMimeType = $data['clientMimeType'];
97+
}
98+
7399
}

Tests/ConcurrentFlowsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/**
66
* @group integration
7+
* @group run-with-multiple-databases
78
*
89
* @author Christian Raue <christian.raue@gmail.com>
910
* @copyright 2011-2023 Christian Raue

Tests/CreateTopicFlowTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @group integration
9+
* @group run-with-multiple-databases
910
*
1011
* @author Christian Raue <christian.raue@gmail.com>
1112
* @copyright 2011-2023 Christian Raue

Tests/CreateVehicleFlowTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @group integration
9+
* @group run-with-multiple-databases
910
*
1011
* @author Christian Raue <christian.raue@gmail.com>
1112
* @copyright 2011-2023 Christian Raue

Tests/Demo1FlowTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @group integration
9+
* @group run-with-multiple-databases
910
*
1011
* @author Christian Raue <christian.raue@gmail.com>
1112
* @copyright 2011-2023 Christian Raue

Tests/IntegrationTestBundle/DependencyInjection/Compiler/DoctrineStorageCompilerPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Craue\FormFlowBundle\Tests\IntegrationTestBundle\DependencyInjection\Compiler;
44

5-
use Craue\FormFlowBundle\Storage\DoctrineStorage;
65
use Symfony\Component\Config\FileLocator;
76
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
87
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -20,7 +19,7 @@
2019
class DoctrineStorageCompilerPass implements CompilerPassInterface {
2120

2221
public function process(ContainerBuilder $container) : void {
23-
if ($container->getParameter('db.driver') !== null) {
22+
if ($container->has('doctrine.dbal.default_connection')) {
2423
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config'));
2524
$loader->load('doctrine_storage.xml');
2625

Tests/IntegrationTestBundle/DependencyInjection/StorageImplementationTest.php

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

55
use Craue\FormFlowBundle\Storage\DoctrineStorage;
66
use Craue\FormFlowBundle\Storage\SessionStorage;
7-
use Craue\FormFlowBundle\Storage\StorageInterface;
8-
use Craue\FormFlowBundle\Tests\IntegrationTestBundle\DependencyInjection\Compiler\DoctrineStorageCompilerPass;
97
use Craue\FormFlowBundle\Tests\IntegrationTestCase;
108

119
/**
1210
* @group integration
11+
* @group run-without-database
12+
* @group run-with-multiple-databases
1313
*
1414
* @author Christian Raue <christian.raue@gmail.com>
1515
* @copyright 2011-2023 Christian Raue
@@ -24,9 +24,8 @@ class StorageImplementationTest extends IntegrationTestCase {
2424
* may silently fail leading to a wrong implementation being used in tests.
2525
*/
2626
public function testUseCorrectStorageImplementation() {
27-
$dbDriver = static::$kernel->getContainer()->getParameter('db.driver');
2827
$storage = $this->getService('craue.form.flow.storage');
29-
$expectedClass = $dbDriver !== null ? DoctrineStorage::class : SessionStorage::class;
28+
$expectedClass = empty($_ENV['DB_DSN']) ? SessionStorage::class : DoctrineStorage::class;
3029
$this->assertInstanceOf($expectedClass, $storage);
3130
}
3231

Tests/IntegrationTestBundle/Resources/config/doctrine_storage.xml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,8 @@
1414
<argument type="service" id="request_stack" />
1515
</service>
1616

17-
<service id="integrationTestBundle.doctrineConnection" class="Doctrine\DBAL\Connection" public="false">
18-
<factory class="Doctrine\DBAL\DriverManager" method="getConnection" />
19-
<argument type="collection">
20-
<argument key="charset">UTF8</argument>
21-
<argument key="driver">%db.driver%</argument>
22-
<argument key="host">%db.host%</argument>
23-
<argument key="port">%db.port%</argument>
24-
<argument key="dbname">%db.name%</argument>
25-
<argument key="user">%db.user%</argument>
26-
<argument key="password">%db.password%</argument>
27-
<argument key="path">%db.path%</argument>
28-
</argument>
29-
</service>
30-
3117
<service id="craue.form.flow.storage.doctrine" class="Craue\FormFlowBundle\Storage\DoctrineStorage" public="false">
32-
<argument type="service" id="integrationTestBundle.doctrineConnection" />
18+
<argument type="service" id="doctrine.dbal.default_connection" />
3319
<argument type="service" id="craue.form.flow.storageKeyGenerator" />
3420
</service>
3521

Tests/IntegrationTestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ protected static function createKernel(array $options = []) : KernelInterface {
4040
$environment = $options['environment'] ?? self::ENV_FLOWS_WITH_AUTOCONFIGURATION;
4141
$configFile = $options['config'] ?? sprintf('config_%s.yml', $environment);
4242

43+
// ensure different caches are used for each database flavor
44+
if (!empty($_ENV['DB_FLAVOR'])) {
45+
$environment .= '_' . $_ENV['DB_FLAVOR'];
46+
$_ENV['DB_DSN'] = $_ENV['DB_DSN_' . strtoupper($_ENV['DB_FLAVOR'])];
47+
}
48+
4349
return new AppKernel($environment, $configFile);
4450
}
4551

0 commit comments

Comments
 (0)