Skip to content

Commit

Permalink
wip fix decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jan 3, 2025
1 parent 1252d1e commit 7bf6331
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 33 deletions.
2 changes: 1 addition & 1 deletion 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 Down
48 changes: 24 additions & 24 deletions config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@
->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'))
->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.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'))
// ->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')
->args([
service('.inner'),
])
;
}
// 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')
// ->args([
// service('.inner'),
// ])
// ;
// }
};
59 changes: 51 additions & 8 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 <[email protected]>
Expand Down Expand Up @@ -249,19 +251,60 @@ 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'])
;
// $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->registerChild(OrmResetter::class, '.zenstruck_foundry.persistence.database_resetter.orm.abstract')
->setClass(
match ($resetMode) {
ResetDatabaseMode::SCHEMA => SchemaDatabaseResetter::class,
ResetDatabaseMode::MIGRATE => MigrateDatabaseResetter::class,
}
)
->addTag('.foundry.persistence.database_resetter')
->addTag('.foundry.persistence.schema_resetter');


// $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->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}");
}
->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'))
->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')
->args([
service('.inner'),
])
;
}
*/

if (isset($bundles['DoctrineMongoDBBundle'])) {
$configurator->import('../config/mongo.php');
Expand Down
34 changes: 34 additions & 0 deletions tests/Fixture/ResetDatabase/OrmResetterDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 $calledBeforeFirstTest = false;

Check failure on line 14 in tests/Fixture/ResetDatabase/OrmResetterDecorator.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Property Zenstruck\Foundry\Tests\Fixture\ResetDatabase\OrmResetterDecorator::$calledBeforeFirstTest has no type specified.
public static $calledBeforeEachTest = false;

Check failure on line 15 in tests/Fixture/ResetDatabase/OrmResetterDecorator.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Property Zenstruck\Foundry\Tests\Fixture\ResetDatabase\OrmResetterDecorator::$calledBeforeEachTest has no type specified.

public function __construct(
private OrmResetter $decorated
) {}

public function resetBeforeFirstTest(KernelInterface $kernel): void
{
$this->decorated->resetBeforeFirstTest($kernel);

self::$calledBeforeFirstTest = true;
}

public function resetBeforeEachTest(KernelInterface $kernel): void
{
$this->decorated->resetBeforeEachTest($kernel);

self::$calledBeforeEachTest = true;
}
}
1 change: 1 addition & 0 deletions tests/Fixture/ResetDatabase/ResetDatabaseTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
}

$c->register(GlobalInvokableService::class);
$c->register(OrmResetterDecorator::class)->setAutowired(true)->setAutoconfigured(true);
}

public static function useMigrations(): bool
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/ResetDatabase/ResetDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Zenstruck\Foundry\Tests\Fixture\EntityInAnotherSchema\Article;
use Zenstruck\Foundry\Tests\Fixture\Factories\Document\GenericDocumentFactory;
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\GenericEntityFactory;
use Zenstruck\Foundry\Tests\Fixture\ResetDatabase\OrmResetterDecorator;
use Zenstruck\Foundry\Tests\Integration\RequiresORM;

use function Zenstruck\Foundry\Persistence\persist;
Expand Down Expand Up @@ -104,4 +105,13 @@ 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_reset_mechanism(): void
{
self::assertTrue(OrmResetterDecorator::$calledBeforeFirstTest);
self::assertTrue(OrmResetterDecorator::$calledBeforeEachTest);
}
}

0 comments on commit 7bf6331

Please sign in to comment.