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

Replace Doctrine subscriber by listener #54

Merged
merged 3 commits into from
Mar 24, 2024
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
23 changes: 13 additions & 10 deletions sources/Bundle/FeatureTogglesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Trompette\FeatureToggles\Bundle;

use Doctrine\ORM\Tools\ToolEvents;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -15,7 +16,7 @@
use Trompette\FeatureToggles\FeatureDefinition;
use Trompette\FeatureToggles\FeatureRegistry;
use Trompette\FeatureToggles\OnOffStrategy\OnOff;
use Trompette\FeatureToggles\ORM\SchemaSubscriber;
use Trompette\FeatureToggles\ORM\SchemaConfigurationListener;
use Trompette\FeatureToggles\PercentageStrategy\Percentage;
use Trompette\FeatureToggles\ToggleRouter;
use Trompette\FeatureToggles\ToggleRouterInterface;
Expand All @@ -31,7 +32,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->defineTogglingStrategies($config['doctrine_dbal_connection'], $container);
$this->defineToggleRouter($container);
$this->defineConsoleCommands($container);
$this->defineDoctrineEventSubscriber($container);
$this->defineDoctrineEventListener($container);
}

/**
Expand Down Expand Up @@ -118,14 +119,16 @@ private function defineConsoleCommands(ContainerBuilder $container): void
;
}

private function defineDoctrineEventSubscriber(ContainerBuilder $container): void
private function defineDoctrineEventListener(ContainerBuilder $container): void
{
$container
->register(SchemaSubscriber::class, SchemaSubscriber::class)
->addArgument(new Reference(OnOffStrategyConfigurationRepository::class))
->addArgument(new Reference(WhitelistStrategyConfigurationRepository::class))
->addArgument(new Reference(PercentageStrategyConfigurationRepository::class))
->addTag('doctrine.event_subscriber')
;
if (class_exists(ToolEvents::class)) {
$container
->register(SchemaConfigurationListener::class, SchemaConfigurationListener::class)
->addArgument(new Reference(OnOffStrategyConfigurationRepository::class))
->addArgument(new Reference(WhitelistStrategyConfigurationRepository::class))
->addArgument(new Reference(PercentageStrategyConfigurationRepository::class))
->addTag('doctrine.event_listener', ['event' => ToolEvents::postGenerateSchema])
;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Trompette\FeatureToggles\ORM;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
use Doctrine\ORM\Tools\ToolEvents;
use Trompette\FeatureToggles\DBAL\SchemaConfigurator;

final class SchemaSubscriber implements EventSubscriber
final class SchemaConfigurationListener
{
/**
* @var SchemaConfigurator[]
Expand All @@ -28,10 +26,4 @@ public function postGenerateSchema(GenerateSchemaEventArgs $eventArgs): void
$configurator->configureSchema($schema, $connection);
}
}

public function getSubscribedEvents(): array
{
// subscribe to event only if Doctrine ORM is installed
return class_exists(ToolEvents::class) ? [ToolEvents::postGenerateSchema] : [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\Trompette\FeatureToggles\ORM;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -10,9 +11,9 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Trompette\FeatureToggles\DBAL\SchemaConfigurator;
use Trompette\FeatureToggles\ORM\SchemaSubscriber;
use Trompette\FeatureToggles\ORM\SchemaConfigurationListener;

class SchemaSubscriberTest extends TestCase
class SchemaConfigurationListenerTest extends TestCase
{
use ProphecyTrait;

Expand All @@ -21,15 +22,18 @@ public function testSchemaIsConfiguredAfterGeneration(): void
$schema = new Schema();
$connection = DriverManager::getConnection(['url' => 'sqlite:///:memory:']);

$configurator = $this->prophesize(SchemaConfigurator::class);
$configurator->configureSchema($schema, $connection)->shouldBeCalled();

$connection->getEventManager()->addEventSubscriber(new SchemaSubscriber($configurator->reveal()));

$entityManager = $this->prophesize(EntityManagerInterface::class);
$entityManager->getConnection()->willReturn($connection);

$connection->getEventManager()->dispatchEvent(
$configurator = $this->prophesize(SchemaConfigurator::class);
$configurator->configureSchema($schema, $connection)->shouldBeCalled();

$eventManager = new EventManager();
$eventManager->addEventListener(
ToolEvents::postGenerateSchema,
new SchemaConfigurationListener($configurator->reveal())
);
$eventManager->dispatchEvent(
ToolEvents::postGenerateSchema,
new GenerateSchemaEventArgs($entityManager->reveal(), $schema)
);
Expand Down
Loading