Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: transform "migrate" testsuite into "reset database" testsuite #763

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,33 @@ 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)' || '' }}
test-reset-database:
name: Test reset database - 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/
nikophil marked this conversation as resolved.
Show resolved Hide resolved
/tests/Fixture/Maker/tmp/
8 changes: 4 additions & 4 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,
# only relevant for "migrate" testsuite
WITH_MIGRATION_CONFIGURATION_FILE="tests/Fixture/MigrationTests/configs/migration-configuration.php"
# test reset database with migrations,
# only relevant for "reset-database" testsuite
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
2 changes: 1 addition & 1 deletion phpunit
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ check_phpunit_version() {
}

### >> load env vars from .env files if not in CI and not from a composer script
if [ -z "${CI:-}" ] && [ -z "${COMPOSER_BINARY:-}" ] ; then
if [ -z "${CI:-}" ] ; then
source .env

if [ -f .env.local ]; then
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
162 changes: 162 additions & 0 deletions tests/Fixture/FoundryTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* 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 Zenstruck\Foundry\Persistence\PersistenceManager;
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangeCascadePersistOnLoadClassMetadataListener;
use Zenstruck\Foundry\ZenstruckFoundryBundle;

/**
* @author Nicolas PHILIPPE <nikophil@gmail.com>
*/
abstract class FoundryTestKernel extends Kernel
{
use MicroKernelTrait;

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

if (self::hasORM()) {
yield new DoctrineBundle();
}

if (self::hasMongo()) {
yield new DoctrineMongoDBBundle();
}

yield new ZenstruckFoundryBundle();

if (self::usesDamaDoctrineTestBundle()) {
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 (self::hasORM()) {
$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')
? [
'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 (self::hasMongo()) {
$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);
}

public static function hasORM(): bool
{
return (bool)\getenv('DATABASE_URL');
}

public static function hasMongo(): bool
{
return (bool)\getenv('MONGO_URL');
}

public static function usesMigrations(): bool
{
return \getenv('DATABASE_RESET_MODE') === 'migrate';
}

public static function usesDamaDoctrineTestBundle(): bool
{
return (bool)\getenv('USE_DAMA_DOCTRINE_TEST_BUNDLE');
}
}
Loading
Loading