Skip to content

Commit

Permalink
refactor: extract reset:migration tests in another testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Dec 4, 2024
1 parent d629c13 commit f632bab
Show file tree
Hide file tree
Showing 23 changed files with 517 additions and 232 deletions.
10 changes: 8 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# DBMS can be changed by adding the following in .env.local:
#DATABASE_URL="postgresql://zenstruck:[email protected]:5433/zenstruck_foundry?serverVersion=15"
#DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_URL="mysql://root:[email protected]:3307/foundry_test?serverVersion=5.7.42"

# Mongo ca be disabled with the following in .env.local:
# MONGO_URL=""
MONGO_URL="mongodb://127.0.0.1:27018/dbName?compressors=disabled&gssapiServiceName=mongodb"
DATABASE_RESET_MODE="schema"

USE_DAMA_DOCTRINE_TEST_BUNDLE="0"
USE_FOUNDRY_PHPUNIT_EXTENSION="0"
PHPUNIT_VERSION="9"
PHPUNIT_VERSION="9" # allowed values: 9, 10, 11
68 changes: 64 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
tests:
name: P:${{ matrix.php }}, S:${{ matrix.symfony }}, D:${{ matrix.database }}, PU:${{ matrix.phpunit || 9 }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.without-dama == 0 && contains(matrix.database, 'sql') && ' (dama)' || '' }}${{ !contains(matrix.database, 'sql') && '' || matrix.use-migrate == 1 && ' (migrate)' || ' (schema)' }}${{ matrix.use-phpunit-extension == 1 && ' (phpunit extension)' || '' }}
name: P:${{ matrix.php }}, S:${{ matrix.symfony }}, D:${{ matrix.database }}, PU:${{ matrix.phpunit || 9 }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.without-dama == 0 && contains(matrix.database, 'sql') && ' (dama)' || '' }}${{ matrix.use-phpunit-extension == 1 && ' (phpunit extension)' || '' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -20,7 +20,6 @@ jobs:
# default values:
# deps: [ highest ]
# without-dama: [ 0 ]
# use-migrate: [ 0 ]
# use-phpunit-extension: [ 0 ]
# phpunit: [ 9 ]

Expand All @@ -35,7 +34,6 @@ jobs:
- {php: 8.3, symfony: '*', database: sqlite, without-dama: 1}
- {php: 8.3, symfony: '*', database: sqlite, without-dama: 1, deps: lowest}
- {php: 8.3, symfony: '*', database: mysql, deps: lowest}
- {php: 8.3, symfony: '*', database: mysql, use-migrate: 1}
- {php: 8.3, symfony: '*', database: mysql|mongo, phpunit: 10}
- {php: 8.3, symfony: '*', database: mysql|mongo, phpunit: 11}
- {php: 8.3, symfony: '*', database: mysql|mongo, use-phpunit-extension: 1, phpunit: 11}
Expand All @@ -46,7 +44,6 @@ jobs:
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.without-dama == 0 && contains(matrix.database, 'sql') && 1 || 0 }}
USE_FOUNDRY_PHPUNIT_EXTENSION: ${{ matrix.use-phpunit-extension || 0 }}
PHPUNIT_VERSION: ${{ matrix.phpunit || 9 }}
DATABASE_RESET_MODE: ${{ matrix.use-migrate == 1 && 'migrate' || 'schema' }}
services:
postgres:
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
Expand Down Expand Up @@ -92,6 +89,69 @@ jobs:
run: ./phpunit
shell: bash

test-reset-database-with-migration:
name: Test migration - D:${{ matrix.database }} ${{ matrix.use-dama == 1 && ' (dama)' || '' }} ${{ contains(matrix.with-migration-configuration-file, 'transactional') && '(configuration file transactional)' || contains(matrix.with-migration-configuration-file, 'configuration') && '(configuration file)' || '' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
database: [ mysql, pgsql, sqlite ]
use-dama: [ 0, 1 ]
with-migration-configuration-file:
- ''
- 'tests/Fixture/MigrationTests/configs/migration-configuration.php'
- 'tests/Fixture/MigrationTests/configs/migration-configuration-transactional.php'
exclude:
# there is currently a bug with MySQL and transactional migrations
- database: mysql
with-migration-configuration-file: 'tests/Fixture/MigrationTests/configs/migration-configuration-transactional.php'
env:
DATABASE_URL: ${{ contains(matrix.database, 'mysql') && 'mysql://root:root@localhost:3306/foundry?serverVersion=5.7.42' || contains(matrix.database, 'pgsql') && 'postgresql://root:root@localhost:5432/foundry?serverVersion=15' || 'sqlite:///%kernel.project_dir%/var/data.db' }}
MONGO_URL: ''
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.use-dama == 1 && 1 || 0 }}
WITH_MIGRATION_CONFIGURATION_FILE: ${{ matrix.with-migration-configuration-file }}
PHPUNIT_VERSION: 9
services:
postgres:
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: foundry
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
coverage: none
tools: flex

- name: Install dependencies
uses: ramsey/composer-install@v2
with:
dependency-versions: highest
composer-options: --prefer-dist
env:
SYMFONY_REQUIRE: 7.1.*

- name: Set up MySQL
if: contains(matrix.database, 'mysql')
run: sudo /etc/init.d/mysql start

- name: Test
run: ./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php
shell: bash

code-coverage:
name: Code Coverage
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
/docker/.makefile
/.env.local
/docker-compose.override.yaml
/tests/Fixture/Migrations/
/tests/Fixture/MigrationTests/Migrations/
/tests/Fixture/Maker/tmp/
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,15 @@ $ docker compose up --detach
# install dependencies
$ composer update

# run test suite with all available permutations
$ composer test

# run only one permutation
# run main testsuite (with "schema" reset database strategy)
$ composer test-schema
# or
$ ./phpunit

# run test suite with dama/doctrine-test-bundle
$ USE_DAMA_DOCTRINE_TEST_BUNDLE=1 vendor/bin/phpunit

# run test suite with postgreSQL instead of MySQL
$ DATABASE_URL="postgresql://zenstruck:[email protected]:5433/zenstruck_foundry?serverVersion=15" vendor/bin/phpunit

# run test suite with another PHPUnit version
$ PHPUNIT_VERSION=10 vendor/bin/phpunit
# run "migrate" testsuite (with "migrate" reset database strategy)
$ composer test-migrate
# or
$ ./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php
```

### Overriding the default configuration
Expand All @@ -69,11 +64,20 @@ You can override default environment variables by creating a `.env.local` file,

```bash
# .env.local
DATABASE_URL="postgresql://zenstruck:[email protected]:5433/zenstruck_foundry?serverVersion=15" # enables postgreSQL instead of MySQL

# change the database to postgreSQL...
DATABASE_URL="postgresql://zenstruck:[email protected]:5433/zenstruck_foundry?serverVersion=15"
# ...or to SQLite
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"

MONGO_URL="" # disables Mongo
USE_DAMA_DOCTRINE_TEST_BUNDLE="1" # enables dama/doctrine-test-bundle
PHPUNIT_VERSION="11" # possible values: 9, 10, 11, 11.4

# test reset database with configuration migration,
# only relevant for "migrate" testsuite
WITH_MIGRATION_CONFIGURATION_FILE="tests/Fixture/MigrationTests/configs/migration-configuration.php"

# run test suite with postgreSQL
$ vendor/bin/phpunit
```
Expand Down
26 changes: 12 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@
"Zenstruck\\Foundry\\": "src/",
"Zenstruck\\Foundry\\Psalm\\": "utils/psalm"
},
"files": ["src/functions.php", "src/Persistence/functions.php", "src/phpunit_helper.php"]
"files": [
"src/functions.php",
"src/Persistence/functions.php",
"src/phpunit_helper.php",
"src/symfony_console.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down Expand Up @@ -77,22 +82,15 @@
},
"scripts": {
"test": [
"@test-schema-no-dama",
"@test-migrate-no-dama",
"@test-schema-dama",
"@test-migrate-dama"
"@test-schema",
"@test-migrate"
],
"test-schema-no-dama": "DATABASE_RESET_MODE=schema USE_DAMA_DOCTRINE_TEST_BUNDLE=0 ./phpunit",
"test-migrate-no-dama": "DATABASE_RESET_MODE=migrate USE_DAMA_DOCTRINE_TEST_BUNDLE=0 ./phpunit",
"test-schema-dama": "DATABASE_RESET_MODE=schema USE_DAMA_DOCTRINE_TEST_BUNDLE=1 ./phpunit",
"test-migrate-dama": "DATABASE_RESET_MODE=migrate USE_DAMA_DOCTRINE_TEST_BUNDLE=1 ./phpunit"
"test-schema": "./phpunit",
"test-migrate": "./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php"
},
"scripts-descriptions": {
"test": "Run all test permutations",
"test-schema-no-dama": "Test with schema reset (no dama/doctrine-test-bundle)",
"test-migrate-no-dama": "Test with migrations reset (no dama/doctrine-test-bundle)",
"test-schema-dama": "Test with schema reset and dama/doctrine-test-bundle",
"test-migrate-dama": "Test with migrations reset and dama/doctrine-test-bundle"
"test-schema": "Test with schema reset",
"test-migrate": "Test with migrations reset"
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
9 changes: 7 additions & 2 deletions phpunit-10.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
colors="true"
failOnRisky="true"
failOnWarning="true"
cacheDirectory=".phpunit.cache">
cacheDirectory=".phpunit.cache"
defaultTestSuite="main">
<php>
<ini name="error_reporting" value="-1"/>
<server name="KERNEL_CLASS" value="Zenstruck\Foundry\Tests\Fixture\TestKernel"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
</php>
<testsuites>
<testsuite name="zenstruck/foundry Test Suite">
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
</testsuite>
</testsuites>
<source ignoreSuppressionOfDeprecations="true">
Expand Down
7 changes: 6 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
colors="true"
failOnRisky="true"
failOnWarning="true"
defaultTestSuite="main"
>
<php>
<ini name="error_reporting" value="-1" />
Expand All @@ -16,8 +17,12 @@
</php>

<testsuites>
<testsuite name="zenstruck/foundry Test Suite">
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
</testsuite>
</testsuites>

Expand Down
12 changes: 6 additions & 6 deletions src/Mongo/MongoSchemaResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
namespace Zenstruck\Foundry\Mongo;

use Symfony\Component\HttpKernel\KernelInterface;
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner;

use function Zenstruck\Foundry\application;
use function Zenstruck\Foundry\runCommand;

/**
* @internal
* @author Nicolas PHILIPPE <nikophil@gmail.com>
*/
final class MongoSchemaResetter implements MongoResetter
{
use SymfonyCommandRunner;

/**
* @param list<string> $managers
*/
Expand All @@ -33,15 +33,15 @@ public function __construct(private array $managers)

public function resetBeforeEachTest(KernelInterface $kernel): void
{
$application = self::application($kernel);
$application = application($kernel);

foreach ($this->managers as $manager) {
try {
self::runCommand($application, 'doctrine:mongodb:schema:drop', ['--dm' => $manager]);
runCommand($application, "doctrine:mongodb:schema:drop --dm={$manager}");
} catch (\Exception) {
}

self::runCommand($application, 'doctrine:mongodb:schema:create', ['--dm' => $manager]);
runCommand($application, "doctrine:mongodb:schema:create --dm={$manager}");
}
}
}
51 changes: 33 additions & 18 deletions src/ORM/ResetDatabase/BaseOrmResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;

use Zenstruck\Foundry\ORM\DoctrineOrmVersionGuesser;

use function Zenstruck\Foundry\runCommand;

/**
* @author Nicolas PHILIPPE <nikophil@gmail.com>
* @internal
*/
abstract class BaseOrmResetter
abstract class BaseOrmResetter implements OrmResetter
{
use SymfonyCommandRunner;
private static bool $inFirstTest = true;

/**
* @param list<string> $managers
Expand All @@ -30,6 +35,19 @@ public function __construct(
) {
}

final public function resetBeforeEachTest(KernelInterface $kernel): void
{
if (self::$inFirstTest) {
self::$inFirstTest = false;

return;
}

$this->doResetBeforeEachTest($kernel);
}

abstract protected function doResetBeforeEachTest(KernelInterface $kernel): void;

final protected function dropAndResetDatabase(Application $application): void
{
foreach ($this->connections as $connectionName) {
Expand All @@ -39,29 +57,26 @@ final protected function dropAndResetDatabase(Application $application): void

if ($databasePlatform instanceof SQLitePlatform) {
// we don't need to create the sqlite database - it's created when the schema is created
// let's only drop the .db file

$dbPath = $connection->getParams()['path'] ?? null;
$fs = new Filesystem();
if (DoctrineOrmVersionGuesser::isOrmV3() && $dbPath && $fs->exists($dbPath)) {
(new Filesystem())->remove($dbPath);
}

continue;
}

if ($databasePlatform instanceof PostgreSQLPlatform) {
// let's drop all connections to the database to be able to drop it
self::runCommand(
$application,
'dbal:run-sql',
[
'--connection' => $connectionName,
'sql' => 'SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid()',
],
canFail: true,
);
$sql = 'SELECT pid, pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid()';
runCommand($application, "dbal:run-sql --connection={$connectionName} '{$sql}'", canFail: true);
}

self::runCommand(
$application,
'doctrine:database:drop',
['--connection' => $connectionName, '--force' => true, '--if-exists' => true]
);
runCommand($application, "doctrine:database:drop --connection={$connectionName} --force --if-exists");

self::runCommand($application, 'doctrine:database:create', ['--connection' => $connectionName]);
runCommand($application, "doctrine:database:create --connection={$connectionName}");
}
}
}
Loading

0 comments on commit f632bab

Please sign in to comment.