Skip to content

Commit

Permalink
fix: simplify reset database extension
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jan 5, 2025
1 parent 951ef94 commit 82d1f29
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 30 deletions.
4 changes: 3 additions & 1 deletion config/mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Zenstruck\Foundry\Mongo\MongoPersistenceStrategy;
use Zenstruck\Foundry\Mongo\MongoResetter;
use Zenstruck\Foundry\Mongo\MongoSchemaResetter;

return static function (ContainerConfigurator $container): void {
Expand All @@ -13,7 +14,8 @@
abstract_arg('config'),
])
->tag('.foundry.persistence_strategy')
->set('.zenstruck_foundry.persistence.schema_resetter.mongo', MongoSchemaResetter::class)

->set(MongoResetter::class, MongoSchemaResetter::class)
->args([
abstract_arg('managers'),
])
Expand Down
20 changes: 4 additions & 16 deletions config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use Zenstruck\Foundry\ORM\OrmV3PersistenceStrategy;
use Zenstruck\Foundry\ORM\ResetDatabase\BaseOrmResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\DamaDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\SchemaDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\MigrateDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmResetter;

return static function (ContainerConfigurator $container): void {
$container->services()
Expand All @@ -26,27 +25,16 @@
->arg('$connections', service('connections'))
->abstract()

->set('.zenstruck_foundry.persistence.database_resetter.orm.schema', SchemaDatabaseResetter::class)
->parent('.zenstruck_foundry.persistence.database_resetter.orm.abstract')
->tag('.foundry.persistence.database_resetter')
->tag('.foundry.persistence.schema_resetter')

->set('.zenstruck_foundry.persistence.database_resetter.orm.migrate', MigrateDatabaseResetter::class)
->arg('$configurations', abstract_arg('configurations'))
->set(OrmResetter::class, /* class to be defined thanks to the configuration */)
->parent('.zenstruck_foundry.persistence.database_resetter.orm.abstract')
->tag('.foundry.persistence.database_resetter')
->tag('.foundry.persistence.schema_resetter')
;

if (\class_exists(StaticDriver::class)) {
$container->services()
->set('.zenstruck_foundry.persistence.database_resetter.orm.schema.dama', DamaDatabaseResetter::class)
->decorate('.zenstruck_foundry.persistence.database_resetter.orm.schema')
->args([
service('.inner'),
])
->set('.zenstruck_foundry.persistence.database_resetter.orm.migrate.dama', DamaDatabaseResetter::class)
->decorate('.zenstruck_foundry.persistence.database_resetter.orm.migrate')
->set('.zenstruck_foundry.persistence.database_resetter.orm.dama', DamaDatabaseResetter::class)
->decorate(OrmResetter::class, priority: 10)
->args([
service('.inner'),
])
Expand Down
29 changes: 16 additions & 13 deletions src/ZenstruckFoundryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use Zenstruck\Foundry\Mongo\MongoResetter;
use Zenstruck\Foundry\Object\Instantiator;
use Zenstruck\Foundry\ORM\ResetDatabase\MigrateDatabaseResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmResetter;
use Zenstruck\Foundry\ORM\ResetDatabase\ResetDatabaseMode;
use Zenstruck\Foundry\ORM\ResetDatabase\SchemaDatabaseResetter;

/**
* @author Kevin Bond <kevinbond@gmail.com>
Expand Down Expand Up @@ -249,18 +251,21 @@ public function loadExtension(array $config, ContainerConfigurator $configurator
->replaceArgument('$connections', $config['orm']['reset']['connections'])
;

$container->getDefinition('.zenstruck_foundry.persistence.database_resetter.orm.migrate')
->replaceArgument('$configurations', $config['orm']['reset']['migrations']['configurations'])
;

/** @var ResetDatabaseMode $resetMode */
$resetMode = $config['orm']['reset']['mode'];
$toRemove = ResetDatabaseMode::SCHEMA === $resetMode ? ResetDatabaseMode::MIGRATE->value : ResetDatabaseMode::SCHEMA->value;

$container->removeDefinition(".zenstruck_foundry.persistence.database_resetter.orm.{$toRemove}.dama");
$container->removeDefinition(".zenstruck_foundry.persistence.database_resetter.orm.{$toRemove}");

$container->setAlias(OrmResetter::class, ".zenstruck_foundry.persistence.database_resetter.orm.{$resetMode->value}");
$container->getDefinition(OrmResetter::class)
->setClass(
match ($resetMode) {
ResetDatabaseMode::SCHEMA => SchemaDatabaseResetter::class,
ResetDatabaseMode::MIGRATE => MigrateDatabaseResetter::class,
}
);

if ($resetMode === ResetDatabaseMode::MIGRATE) {
$container->getDefinition(OrmResetter::class)
->replaceArgument('$configurations', $config['orm']['reset']['migrations']['configurations'])
;
}
}

if (isset($bundles['DoctrineMongoDBBundle'])) {
Expand All @@ -270,11 +275,9 @@ public function loadExtension(array $config, ContainerConfigurator $configurator
->replaceArgument(1, $config['mongo'])
;

$container->getDefinition('.zenstruck_foundry.persistence.schema_resetter.mongo')
$container->getDefinition(MongoResetter::class)
->replaceArgument(0, $config['mongo']['reset']['document_managers'])
;

$container->setAlias(MongoResetter::class, '.zenstruck_foundry.persistence.schema_resetter.mongo');
}
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Fixture/ResetDatabase/MongoResetterDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Tests\Fixture\ResetDatabase;

use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\HttpKernel\KernelInterface;
use Zenstruck\Foundry\Mongo\MongoResetter;

#[AsDecorator(MongoResetter::class)]
final class MongoResetterDecorator implements MongoResetter
{
public static bool $calledBeforeEachTest = false;

public function __construct(
private MongoResetter $decorated
) {
}

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

$this->decorated->resetBeforeEachTest($kernel);
}
}
40 changes: 40 additions & 0 deletions tests/Fixture/ResetDatabase/OrmResetterDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Tests\Fixture\ResetDatabase;

use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\HttpKernel\KernelInterface;
use Zenstruck\Foundry\ORM\ResetDatabase\OrmResetter;

#[AsDecorator(OrmResetter::class)]
final class OrmResetterDecorator implements OrmResetter
{
public static bool $calledBeforeFirstTest = false;
public static bool $calledBeforeEachTest = false;

public function __construct(
private OrmResetter $decorated
) {}

public function resetBeforeFirstTest(KernelInterface $kernel): void
{
self::$calledBeforeFirstTest = true;

$this->decorated->resetBeforeFirstTest($kernel);
}

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

$this->decorated->resetBeforeEachTest($kernel);
}

public static function reset(): void
{
self::$calledBeforeFirstTest = false;
self::$calledBeforeEachTest = false;
}
}
8 changes: 8 additions & 0 deletions tests/Fixture/ResetDatabase/ResetDatabaseTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,13 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
}

$c->register(GlobalInvokableService::class);

if (self::hasORM()) {
$c->register(OrmResetterDecorator::class)->setAutowired(true)->setAutoconfigured(true);
}

if (self::hasMongo()) {
$c->register(MongoResetterDecorator::class)->setAutowired(true)->setAutoconfigured(true);
}
}
}
56 changes: 56 additions & 0 deletions tests/Integration/ResetDatabase/ResetDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Zenstruck\Foundry\Tests\Fixture\EntityInAnotherSchema\Article;
use Zenstruck\Foundry\Persistence\PersistenceManager;
use Zenstruck\Foundry\Tests\Fixture\Factories\Document\GenericDocumentFactory;
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\MongoResetterDecorator;
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\OrmResetterDecorator;
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory;
use Zenstruck\Foundry\Tests\Fixture\FoundryTestKernel;

Expand Down Expand Up @@ -98,4 +101,57 @@ public function can_create_object_in_another_schema(): void
persist(Article::class, ['title' => 'Hello World!']);
repository(Article::class)->assert()->count(1);
}

/**
* @test
*/
public function can_extend_orm_reset_mechanism_first(): void
{
if (!ResetDatabaseTestKernel::hasORM()) {

Check failure on line 110 in tests/Integration/ResetDatabase/ResetDatabaseTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to static method hasORM() on an unknown class Zenstruck\Foundry\Tests\Integration\ResetDatabase\ResetDatabaseTestKernel.
self::markTestSkipped('ORM needed.');
}

self::assertTrue(OrmResetterDecorator::$calledBeforeFirstTest);

if (PersistenceManager::isOrmOnly() && ResetDatabaseTestKernel::usesDamaDoctrineTestBundle()) {

Check failure on line 116 in tests/Integration/ResetDatabase/ResetDatabaseTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to static method usesDamaDoctrineTestBundle() on an unknown class Zenstruck\Foundry\Tests\Integration\ResetDatabase\ResetDatabaseTestKernel.
// in this case, the resetBeforeEachTest() method is never called
self::assertFalse(OrmResetterDecorator::$calledBeforeEachTest);
} else {
self::assertTrue(OrmResetterDecorator::$calledBeforeEachTest);
}

OrmResetterDecorator::reset();
}

/**
* @test
* @depends can_extend_orm_reset_mechanism_first
*/
public function can_extend_orm_reset_mechanism_second(): void
{
if (!ResetDatabaseTestKernel::hasORM()) {

Check failure on line 132 in tests/Integration/ResetDatabase/ResetDatabaseTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to static method hasORM() on an unknown class Zenstruck\Foundry\Tests\Integration\ResetDatabase\ResetDatabaseTestKernel.
self::markTestSkipped('ORM needed.');
}

self::assertFalse(OrmResetterDecorator::$calledBeforeFirstTest);

if (PersistenceManager::isOrmOnly() && ResetDatabaseTestKernel::usesDamaDoctrineTestBundle()) {

Check failure on line 138 in tests/Integration/ResetDatabase/ResetDatabaseTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to static method usesDamaDoctrineTestBundle() on an unknown class Zenstruck\Foundry\Tests\Integration\ResetDatabase\ResetDatabaseTestKernel.
// in this case, the resetBeforeEachTest() method is never called
self::assertFalse(OrmResetterDecorator::$calledBeforeEachTest);
} else {
self::assertTrue(OrmResetterDecorator::$calledBeforeEachTest);
}
}

/**
* @test
*/
public function can_extend_mongo_reset_mechanism_first(): void
{
if (!ResetDatabaseTestKernel::hasMongo()) {

Check failure on line 151 in tests/Integration/ResetDatabase/ResetDatabaseTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Call to static method hasMongo() on an unknown class Zenstruck\Foundry\Tests\Integration\ResetDatabase\ResetDatabaseTestKernel.
self::markTestSkipped('Mongo needed.');
}

self::assertTrue(MongoResetterDecorator::$calledBeforeEachTest);
}
}

0 comments on commit 82d1f29

Please sign in to comment.