Skip to content

Commit

Permalink
Pass a PSR logger to LoadDataFixturesDoctrineCommand
Browse files Browse the repository at this point in the history
Not doing so is deprecated.
See doctrine/data-fixtures#462
  • Loading branch information
greg0ire committed Nov 6, 2024
1 parent a345e6c commit af42133
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<service id="doctrine.fixtures_load_command" class="Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand">
<argument type="service" id="doctrine.fixtures.loader" />
<argument type="service" id="doctrine" />
<argument type="collection"></argument>
<argument type="service" id="logger" on-invalid="null"/>
<tag name="console.command" command="doctrine:fixtures:load" />
</service>

Expand Down
14 changes: 11 additions & 3 deletions src/Command/LoadDataFixturesDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -51,6 +58,7 @@ public function __construct(SymfonyFixturesLoader $fixturesLoader, ?ManagerRegis

$this->fixturesLoader = $fixturesLoader;
$this->purgerFactories = $purgerFactories;
$this->logger = $logger;
}

/** @return void */
Expand Down Expand Up @@ -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(' <comment>></comment> <info>%s</info>', $message));
});
$executor->execute($fixtures, $input->getOption('append'));
Expand Down
5 changes: 3 additions & 2 deletions tests/Command/LoadDataFixturesDoctrineCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ?
Expand All @@ -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());
}
}
9 changes: 9 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit af42133

Please sign in to comment.