Skip to content

Commit

Permalink
refactor: make "database reset" mechanism extendable (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Oct 23, 2024
1 parent 4fb0b25 commit df568da
Show file tree
Hide file tree
Showing 26 changed files with 617 additions and 466 deletions.
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();
6 changes: 6 additions & 0 deletions 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\MongoSchemaResetter;

return static function (ContainerConfigurator $container): void {
$container->services()
Expand All @@ -12,5 +13,10 @@
abstract_arg('config'),
])
->tag('.foundry.persistence_strategy')
->set('.zenstruck_foundry.persistence.schema_resetter.mongo', MongoSchemaResetter::class)
->args([
abstract_arg('managers'),
])
->tag('.foundry.persistence.schema_resetter')
;
};
37 changes: 37 additions & 0 deletions config/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
use Zenstruck\Foundry\ORM\DoctrineOrmVersionGuesser;
use Zenstruck\Foundry\ORM\OrmV2PersistenceStrategy;
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;

return static function (ContainerConfigurator $container): void {
$container->services()
Expand All @@ -14,5 +19,37 @@
abstract_arg('config'),
])
->tag('.foundry.persistence_strategy')

->set('.zenstruck_foundry.persistence.database_resetter.orm.abstract', BaseOrmResetter::class)
->arg('$registry', service('doctrine'))
->arg('$managers', service('managers'))
->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')
;

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'),
])
;
}
};
7 changes: 7 additions & 0 deletions config/persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Zenstruck\Foundry\Persistence\PersistenceManager;
use Zenstruck\Foundry\Persistence\ResetDatabase\ResetDatabaseManager;

return static function (ContainerConfigurator $container): void {
$container->services()
->set('.zenstruck_foundry.persistence_manager', PersistenceManager::class)
->args([
tagged_iterator('.foundry.persistence_strategy'),
service('.zenstruck_foundry.persistence.reset_database_manager'),
])
->set('.zenstruck_foundry.persistence.reset_database_manager', ResetDatabaseManager::class)
->args([
tagged_iterator('.foundry.persistence.database_resetter'),
tagged_iterator('.foundry.persistence.schema_resetter'),
])
;
};
151 changes: 0 additions & 151 deletions phpstan-baseline.neon

This file was deleted.

3 changes: 3 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function __construct(
$this->instantiator = $instantiator;
}

/**
* @throws PersistenceNotAvailable
*/
public function persistence(): PersistenceManager
{
return $this->persistence ?? throw new PersistenceNotAvailable('No persistence managers configured. Note: persistence cannot be used in unit tests.');
Expand Down
39 changes: 0 additions & 39 deletions src/Mongo/MongoPersistenceStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,43 +89,4 @@ public function isEmbeddable(object $object): bool
{
return $this->objectManagerFor($object::class)->getClassMetadata($object::class)->isEmbeddedDocument;
}

public function resetDatabase(KernelInterface $kernel): void
{
// noop
}

public function resetSchema(KernelInterface $kernel): void
{
$application = self::application($kernel);

foreach ($this->managers() as $manager) {
try {
self::runCommand(
$application,
'doctrine:mongodb:schema:drop',
[
'--dm' => $manager,
]
);
} catch (\Exception) {
}

self::runCommand(
$application,
'doctrine:mongodb:schema:create',
[
'--dm' => $manager,
]
);
}
}

/**
* @return string[]
*/
private function managers(): array
{
return $this->config['reset']['document_managers'];
}
}
11 changes: 11 additions & 0 deletions src/Mongo/MongoResetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Mongo;

use Zenstruck\Foundry\Persistence\ResetDatabase\BeforeEachTestResetter;

interface MongoResetter extends BeforeEachTestResetter
{
}
38 changes: 38 additions & 0 deletions src/Mongo/MongoSchemaResetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Zenstruck\Foundry\Mongo;

use Symfony\Component\HttpKernel\KernelInterface;
use Zenstruck\Foundry\Persistence\SymfonyCommandRunner;

/**
* @internal
* @author Nicolas PHILIPPE <nikophil@gmail.com>
*/
final class MongoSchemaResetter implements MongoResetter
{
use SymfonyCommandRunner;

/**
* @param list<string> $managers
*/
public function __construct(private array $managers)
{
}

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

foreach ($this->managers as $manager) {
try {
self::runCommand($application, 'doctrine:mongodb:schema:drop', ['--dm' => $manager]);
} catch (\Exception) {
}

self::runCommand($application, 'doctrine:mongodb:schema:create', ['--dm' => $manager]);
}
}
}
Loading

0 comments on commit df568da

Please sign in to comment.