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 Sep 3, 2024
1 parent 3eebbf9 commit 3257095
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 132 deletions.
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DATABASE_URL="mysql://root:[email protected]:3307/foundry_test?serverVersion=5.7.42"
MONGO_URL="mongodb://127.0.0.1:27018/dbName?compressors=disabled&gssapiServiceName=mongodb"
DATABASE_RESET_MODE="schema"
USE_DAMA_DOCTRINE_TEST_BUNDLE="0"
PHPUNIT_VERSION="9"
70 changes: 57 additions & 13 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 }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.use-dama == 1 && contains(matrix.database, 'sql') && ' (dama)' || '' }}${{ !contains(matrix.database, 'sql') && '' || matrix.use-migrate == 1 && ' (migrate)' || ' (schema)' }}
name: P:${{ matrix.php }}, S:${{ matrix.symfony }}, D:${{ matrix.database }}, PU:${{ matrix.phpunit }}${{ matrix.deps == 'lowest' && ' (lowest)' || '' }}${{ matrix.use-dama == 1 && contains(matrix.database, 'sql') && ' (dama)' || '' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -18,7 +18,6 @@ jobs:
symfony: [ 6.4.*, 7.0.*, 7.1.* ]
database: [ mysql, mongo ]
use-dama: [ 1 ]
use-migrate: [ 0 ]
phpunit: [ 9 ]
exclude:
- php: 8.1
Expand All @@ -31,70 +30,60 @@ jobs:
symfony: '*'
database: none
use-dama: 1
use-migrate: 0
phpunit: 9
- php: 8.3
deps: highest
symfony: '*'
database: mysql|mongo
use-dama: 1
use-migrate: 0
phpunit: 9
- php: 8.3
deps: highest
symfony: '*'
database: pgsql|mongo
use-dama: 1
use-migrate: 0
phpunit: 9
- php: 8.3
deps: highest
symfony: '*'
database: pgsql
use-dama: 0
use-migrate: 0
phpunit: 9
- php: 8.3
deps: highest
symfony: '*'
database: sqlite
use-dama: 0
use-migrate: 0
phpunit: 9
- php: 8.3
deps: lowest
symfony: '*'
database: sqlite
use-dama: 0
use-migrate: 0
phpunit: 9
- php: 8.3
deps: lowest
symfony: '*'
database: mysql
use-dama: 1
use-migrate: 0
phpunit: 9
- php: 8.3
deps: highest
symfony: '*'
database: mysql
use-dama: 1
use-migrate: 1
phpunit: 9
- php: 8.3
deps: highest
symfony: '*'
database: mysql|mongo
use-dama: 1
use-migrate: 0
phpunit: 10
- php: 8.3
deps: highest
symfony: '*'
database: mysql|mongo
use-dama: 1
use-migrate: 0
phpunit: 11
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' || contains(matrix.database, 'sqlite') && 'sqlite:///%kernel.project_dir%/var/data.db' || '' }}
Expand Down Expand Up @@ -145,8 +134,63 @@ jobs:
- name: Test
run: ./phpunit
shell: bash

test-reset-database-with-migration:
name: Test migration - D:${{ matrix.database }}, ${{ matrix.use-dama == 1 && ' (dama)' || '' }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
database: [ mysql, pgsql, sqlite ]
use-dama: [ 0, 1 ]
exclude:
- database: sqlite
use-dama: 0
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 }}
PHPUNIT_VERSION: 9
services:
postgres:
image: ${{ contains(matrix.database, 'pgsql') && 'postgres:15' || '' }}
env:
DATABASE_RESET_MODE: ${{ matrix.use-migrate == 1 && 'migrate' || 'schema' }}
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
Expand Down
26 changes: 13 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,7 +64,12 @@ 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
Expand Down
4 changes: 2 additions & 2 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<?php

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Zenstruck\Foundry\Tests\Fixtures\Kernel;
use Zenstruck\Foundry\Tests\Fixture\TestKernel;

require_once __DIR__ . '/../tests/bootstrap.php';

$application = new Application(new Kernel('test', true));
$application = new Application(new TestKernel('test', true));
$application->run();
19 changes: 6 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,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
3 changes: 2 additions & 1 deletion phpunit-10.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
</php>
<testsuites>
<testsuite name="zenstruck/foundry Test Suite">
<testsuite name="zenstruck/foundry Test Suite" >
<directory>tests</directory>
<exclude>tests/Integration/Migration/ResetDatabaseWithMigrationTest.php</exclude>
</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
44 changes: 0 additions & 44 deletions tests/Fixture/CustomMigrations/Version20240611065130.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\Tests\Fixture\EdgeCases\Migrate\ORM\EntityInAnotherSchema;
namespace Zenstruck\Foundry\Tests\Fixture\EntityInAnotherSchema;

use Doctrine\ORM\Mapping as ORM;
use Zenstruck\Foundry\Tests\Fixture\Model\Base;

/**
* 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.
*
* @see https://github.com/zenstruck/foundry/issues/618
*/
#[ORM\Entity]
#[ORM\Table(name: 'article', schema: 'cms')]
class Article extends Base
Expand Down
17 changes: 5 additions & 12 deletions tests/Fixture/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use DAMA\DoctrineTestBundle\DAMADoctrineTestBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle;
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
Expand Down Expand Up @@ -44,7 +43,6 @@ public function registerBundles(): iterable

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

if (\getenv('MONGO_URL')) {
Expand Down Expand Up @@ -74,7 +72,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
],
'orm' => [
'reset' => [
'mode' => \getenv('DATABASE_RESET_MODE') ?: AbstractORMPersistenceStrategy::RESET_MODE_SCHEMA,
'mode' => AbstractORMPersistenceStrategy::RESET_MODE_SCHEMA,
],
],
]);
Expand Down Expand Up @@ -105,27 +103,22 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
],
]);

if (AbstractORMPersistenceStrategy::RESET_MODE_MIGRATE === \getenv('DATABASE_RESET_MODE')) {
// doctrine only handles different schema with postgres
if (str_starts_with(\getenv('DATABASE_URL'), 'postgresql')) {
$c->loadFromExtension('doctrine', [
'orm' => [
'mappings' => [
'Migrate' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/tests/Fixture/EdgeCases/Migrate/ORM',
'prefix' => 'Zenstruck\Foundry\Tests\Fixture\EdgeCases\Migrate\ORM',
'dir' => '%kernel.project_dir%/tests/Fixture/EntityInAnotherSchema',
'prefix' => 'Zenstruck\Foundry\Tests\Fixture\EntityInAnotherSchema',
'alias' => 'Migrate',
],
],
],
]);
}

$c->loadFromExtension('doctrine_migrations', [
'migrations_paths' => [
'Zenstruck\\Foundry\\Tests\\Fixture\\Migrations' => '%kernel.project_dir%/tests/Fixture/Migrations',
],
]);
}

if (\getenv('MONGO_URL')) {
Expand Down
Loading

0 comments on commit 3257095

Please sign in to comment.