From 5d0c3ebee69bb0afcb7fbf9f469a35f87e0b32c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Wed, 6 Nov 2024 10:55:31 +0100 Subject: [PATCH] Pass a PSR logger to LoadDataFixturesDoctrineCommand Not doing so is deprecated. See https://github.com/doctrine/data-fixtures/pull/462 --- config/services.xml | 2 ++ src/Command/LoadDataFixturesDoctrineCommand.php | 14 +++++++++++--- .../LoadDataFixturesDoctrineCommandTest.php | 5 +++-- tests/IntegrationTest.php | 9 +++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/config/services.xml b/config/services.xml index 12d7ea52..4952c1dd 100644 --- a/config/services.xml +++ b/config/services.xml @@ -8,6 +8,8 @@ + + diff --git a/src/Command/LoadDataFixturesDoctrineCommand.php b/src/Command/LoadDataFixturesDoctrineCommand.php index 6ca2c78d..a6876f7e 100644 --- a/src/Command/LoadDataFixturesDoctrineCommand.php +++ b/src/Command/LoadDataFixturesDoctrineCommand.php @@ -12,6 +12,7 @@ use Doctrine\Common\DataFixtures\Executor\ORMExecutor; use Doctrine\ORM\EntityManagerInterface; use Doctrine\Persistence\ManagerRegistry; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -34,9 +35,15 @@ class LoadDataFixturesDoctrineCommand extends DoctrineCommand /** @var PurgerFactory[] */ private array $purgerFactories; + private ?LoggerInterface $logger; + /** @param PurgerFactory[] $purgerFactories */ - public function __construct(SymfonyFixturesLoader $fixturesLoader, ?ManagerRegistry $doctrine = null, array $purgerFactories = []) - { + public function __construct( + SymfonyFixturesLoader $fixturesLoader, + ?ManagerRegistry $doctrine = null, + array $purgerFactories = [], + ?LoggerInterface $logger = null + ) { if ($doctrine === null) { trigger_deprecation( 'doctrine/fixtures-bundle', @@ -51,6 +58,7 @@ public function __construct(SymfonyFixturesLoader $fixturesLoader, ?ManagerRegis $this->fixturesLoader = $fixturesLoader; $this->purgerFactories = $purgerFactories; + $this->logger = $logger; } /** @return void */ @@ -134,7 +142,7 @@ private function doExecute(InputInterface $input, OutputInterface $output): int $input->getOption('purge-with-truncate'), ); $executor = new ORMExecutor($em, $purger); - $executor->setLogger(static function ($message) use ($ui): void { + $executor->setLogger($this->logger ?? static function ($message) use ($ui): void { $ui->text(sprintf(' > %s', $message)); }); $executor->execute($fixtures, $input->getOption('append')); diff --git a/tests/Command/LoadDataFixturesDoctrineCommandTest.php b/tests/Command/LoadDataFixturesDoctrineCommandTest.php index 1d274554..e9fca9c5 100644 --- a/tests/Command/LoadDataFixturesDoctrineCommandTest.php +++ b/tests/Command/LoadDataFixturesDoctrineCommandTest.php @@ -9,6 +9,7 @@ use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader; use Doctrine\Persistence\ManagerRegistry; use PHPUnit\Framework\TestCase; +use Psr\Log\NullLogger; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\DependencyInjection\Container; use TypeError; @@ -29,7 +30,7 @@ public function testInstantiatingWithoutManagerRegistry(): void $this->expectDeprecation('Since doctrine/fixtures-bundle 3.2: Argument 2 of Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand::__construct() expects an instance of Doctrine\Persistence\ManagerRegistry, not passing it will throw a \TypeError in DoctrineFixturesBundle 4.0.'); try { - new LoadDataFixturesDoctrineCommand($loader); + new LoadDataFixturesDoctrineCommand($loader, null, [], new NullLogger()); } catch (TypeError $e) { $this->expectExceptionMessage(sprintf( PHP_VERSION_ID >= 80000 ? @@ -49,6 +50,6 @@ public function testInstantiatingWithManagerRegistry(): void $registry = $this->createMock(ManagerRegistry::class); $loader = new SymfonyFixturesLoader(new Container()); - new LoadDataFixturesDoctrineCommand($loader, $registry); + new LoadDataFixturesDoctrineCommand($loader, $registry, [], new NullLogger()); } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 19e67fc7..a3adecad 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -21,6 +21,7 @@ use Doctrine\Persistence\ManagerRegistry; use LogicException; use PHPUnit\Framework\TestCase; +use Psr\Log\NullLogger; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -287,6 +288,8 @@ public function testRunCommandWithDefaultPurger(): void ->setPublic(true) ->setSynthetic(true); + $c->setDefinition('logger', new Definition(NullLogger::class)); + $c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true)); $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true)); @@ -334,6 +337,8 @@ public function testRunCommandWithPurgeExclusions(): void ->setPublic(true) ->setSynthetic(true); + $c->setDefinition('logger', new Definition(NullLogger::class)); + $c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true)); $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true)); @@ -386,6 +391,8 @@ public function testRunCommandWithCustomPurgerAndCustomEntityManager(): void ->setSynthetic(true) ->addTag(PurgerFactoryCompilerPass::PURGER_FACTORY_TAG, ['alias' => 'test'])); + $c->setDefinition('logger', new Definition(NullLogger::class)); + $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true)); }); $kernel->boot(); @@ -431,6 +438,8 @@ public function testRunCommandWithPurgeMode(): void ->setPublic(true) ->setSynthetic(true); + $c->setDefinition('logger', new Definition(NullLogger::class)); + $c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true)); $c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));