Skip to content

Commit

Permalink
tests: transform "migrate" testsuite into "reset database" testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jan 5, 2025
1 parent 482fcdd commit 01485a7
Show file tree
Hide file tree
Showing 24 changed files with 537 additions and 498 deletions.
31 changes: 20 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,32 @@ jobs:
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)' || '' }}
name: Test migration - D:${{ matrix.database }} ${{ matrix.use-dama == 1 && ' (dama)' || '' }} ${{ matrix.reset-database-mode == 'migrate' && ' (migrate)' || '' }} ${{ 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 ]
database: [ mysql, pgsql, sqlite, mysql|mongo ]
use-dama: [ 0, 1 ]
with-migration-configuration-file:
- ''
- 'tests/Fixture/MigrationTests/configs/migration-configuration.php'
- 'tests/Fixture/MigrationTests/configs/migration-configuration-transactional.php'
reset-database-mode: [ schema, migrate ]
migration-configuration-file: ['no', 'migration-configuration', 'migration-configuration-transactional']
include:
- { database: mongo, migration-configuration-file: 'no', use-dama: 0, reset-database-mode: schema }
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'
migration-configuration-file: 'migration-configuration-transactional'
- reset-database-mode: schema
migration-configuration-file: 'migration-configuration'
- reset-database-mode: schema
migration-configuration-file: 'migration-configuration-transactional'
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: ''
MONGO_URL: ${{ contains(matrix.database, 'mongo') && 'mongodb://127.0.0.1:27017/dbName?compressors=disabled&gssapiServiceName=mongodb' || '' }}
USE_DAMA_DOCTRINE_TEST_BUNDLE: ${{ matrix.use-dama == 1 && 1 || 0 }}
WITH_MIGRATION_CONFIGURATION_FILE: ${{ matrix.with-migration-configuration-file }}
PHPUNIT_VERSION: 9
DATABASE_RESET_MODE: ${{ matrix.reset-database-mode == 1 && 1 || 0 }}
MIGRATION_CONFIGURATION_FILE: ${{ matrix.migration-configuration-file == 'no' && '' || format('tests/Fixture/MigrationTests/configs/{0}.php', matrix.migration-configuration-file) }}
PHPUNIT_VERSION: 11
services:
postgres:
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
Expand All @@ -125,6 +130,10 @@ jobs:
--health-interval 10s
--health-timeout 5s
--health-retries 5
mongo:
image: ${{ contains(matrix.database, 'mongo') && 'mongo:4' || '' }}
ports:
- 27017:27017
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand All @@ -149,7 +158,7 @@ jobs:
run: sudo /etc/init.d/mysql start

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

test-with-paratest:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@
/docker/.makefile
/.env.local
/docker-compose.override.yaml
/tests/Fixture/MigrationTests/Migrations/
/tests/Fixture/Maker/tmp/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ $ ./phpunit
# run "migrate" testsuite (with "migrate" reset database strategy)
$ composer test-migrate
# or
$ ./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php
$ ./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
```

### Overriding the default configuration
Expand All @@ -74,9 +74,9 @@ 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,
# test reset database with configuration migration,
# only relevant for "migrate" testsuite
WITH_MIGRATION_CONFIGURATION_FILE="tests/Fixture/MigrationTests/configs/migration-configuration.php"
MIGRATION_CONFIGURATION_FILE="tests/Fixture/MigrationTests/configs/migration-configuration.php"

# run test suite with postgreSQL
$ vendor/bin/phpunit
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@
},
"scripts": {
"test": [
"@test-schema",
"@test-migrate"
"@test-main",
"@test-reset-database"
],
"test-schema": "./phpunit",
"test-migrate": "./phpunit --testsuite migrate --bootstrap tests/bootstrap-migrate.php"
"test-main": "./phpunit --testsuite main",
"test-reset-database": "./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php"
},
"scripts-descriptions": {
"test-schema": "Test with schema reset",
"test-migrate": "Test with migrations reset"
"test-main": "Main test suite",
"test-reset-database": "Test reset database test suite"
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
6 changes: 3 additions & 3 deletions phpunit-10.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Integration/ResetDatabase</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
<testsuite name="reset-database">
<directory>tests/Integration/ResetDatabase</directory>
</testsuite>
</testsuites>
<source ignoreSuppressionOfDeprecations="true">
Expand Down
2 changes: 1 addition & 1 deletion phpunit-paratest.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Integration/ResetDatabase</exclude>
</testsuite>
</testsuites>
<source ignoreSuppressionOfDeprecations="true">
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
<testsuites>
<testsuite name="main">
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
<exclude>tests/Integration/ResetDatabase</exclude>
</testsuite>
<testsuite name="migrate">
<file>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</file>
<testsuite name="reset-database">
<directory>tests/Integration/ResetDatabase</directory>
</testsuite>
</testsuites>

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/ResetDatabase/SchemaDatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function resetBeforeFirstTest(KernelInterface $kernel): void
$this->createSchema($application);
}

public function doResetBeforeEachTest(KernelInterface $kernel): void
protected function doResetBeforeEachTest(KernelInterface $kernel): void
{
$application = application($kernel);

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/EntityInAnotherSchema/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/**
* Create custom "cms" schema ({@see Article}) to ensure "migrate" mode is still working with multiple schemas.
* Note: this entity is added to mapping only for PostgreSQ, as it is the only supported DBMS which handles multiple schemas.
* Note: this entity is added to mapping only for PostgreSQL, as it is the only supported DBMS which handles multiple schemas.
*
* @see https://github.com/zenstruck/foundry/issues/618
*/
Expand Down
143 changes: 143 additions & 0 deletions tests/Fixture/FoundryTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\Tests\Fixture;

use DAMA\DoctrineTestBundle\DAMADoctrineTestBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Zenstruck\Foundry\Persistence\PersistenceManager;
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangeCascadePersistOnLoadClassMetadataListener;
use Zenstruck\Foundry\ZenstruckFoundryBundle;

/**
* @author Nicolas PHILIPPE <[email protected]>
*/
abstract class FoundryTestKernel extends Kernel
{
use MicroKernelTrait;

public function registerBundles(): iterable
{
yield new FrameworkBundle();

if (\getenv('DATABASE_URL')) {
yield new DoctrineBundle();
}

if (\getenv('MONGO_URL')) {
yield new DoctrineMongoDBBundle();
}

yield new ZenstruckFoundryBundle();

if (\getenv('USE_DAMA_DOCTRINE_TEST_BUNDLE')) {
yield new DAMADoctrineTestBundle();
}
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
{
$c->loadFromExtension('framework', [
'http_method_override' => false,
'secret' => 'S3CRET',
'router' => ['utf8' => true],
'test' => true,
]);

if (\getenv('DATABASE_URL')) {
$c->loadFromExtension('doctrine', [
'dbal' => ['url' => '%env(resolve:DATABASE_URL)%', 'use_savepoints' => true],
'orm' => [
'auto_generate_proxy_classes' => true,
'auto_mapping' => true,
'mappings' => [
'Entity' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/tests/Fixture/Entity',
'prefix' => 'Zenstruck\Foundry\Tests\Fixture\Entity',
'alias' => 'Entity',
],
'Model' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/tests/Fixture/Model',
'prefix' => 'Zenstruck\Foundry\Tests\Fixture\Model',
'alias' => 'Model',
],

// postgres acts weirdly with multiple schemas
// @see https://github.com/doctrine/DoctrineBundle/issues/548
...(\str_starts_with(\getenv('DATABASE_URL') ?: '', 'postgresql') // @phpstan-ignore ternary.alwaysTrue
? [
'EntityInAnotherSchema' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/tests/Fixture/EntityInAnotherSchema',
'prefix' => 'Zenstruck\Foundry\Tests\Fixture\EntityInAnotherSchema',
'alias' => 'Migrate',
],
]
: []
),
],
'controller_resolver' => ['auto_mapping' => false],
],
]);

$c->register(ChangeCascadePersistOnLoadClassMetadataListener::class)
->setAutowired(true)
->setAutoconfigured(true);
$c->setAlias(PersistenceManager::class, '.zenstruck_foundry.persistence_manager')
->setPublic(true);
}

if (\getenv('MONGO_URL')) {
$c->loadFromExtension('doctrine_mongodb', [
'connections' => [
'default' => ['server' => '%env(resolve:MONGO_URL)%'],
],
'default_database' => 'mongo',
'document_managers' => [
'default' => [
'auto_mapping' => true,
'mappings' => [
'Document' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/tests/Fixture/Document',
'prefix' => 'Zenstruck\Foundry\Tests\Fixture\Document',
'alias' => 'Document',
],
'Model' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/tests/Fixture/Model',
'prefix' => 'Zenstruck\Foundry\Tests\Fixture\Model',
'alias' => 'Model',
],
],
],
],
]);
}

$c->register('logger', NullLogger::class);
}
}
Loading

0 comments on commit 01485a7

Please sign in to comment.