Skip to content

Commit fb8fefc

Browse files
committed
Pass a PSR logger to LoadDataFixturesDoctrineCommand
Not doing so is deprecated. See doctrine/data-fixtures#462
1 parent a345e6c commit fb8fefc

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
],
2222
"require": {
2323
"php": "^7.4 || ^8.0",
24-
"doctrine/data-fixtures": "^1.3",
24+
"doctrine/data-fixtures": "^1.8",
2525
"doctrine/doctrine-bundle": "^2.2",
2626
"doctrine/orm": "^2.14.0 || ^3.0",
2727
"doctrine/persistence": "^2.4|^3.0",

config/services.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
<service id="doctrine.fixtures_load_command" class="Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand">
99
<argument type="service" id="doctrine.fixtures.loader" />
1010
<argument type="service" id="doctrine" />
11+
<argument type="collection"></argument>
12+
<argument type="service" id="logger" on-invalid="null"/>
1113
<tag name="console.command" command="doctrine:fixtures:load" />
14+
<tag name="monolog.logger" channel="doctrine" />
1215
</service>
1316

1417
<service id="doctrine.fixtures.loader" class="Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader" public="false">

src/Command/LoadDataFixturesDoctrineCommand.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
1313
use Doctrine\ORM\EntityManagerInterface;
1414
use Doctrine\Persistence\ManagerRegistry;
15+
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
@@ -34,9 +35,15 @@ class LoadDataFixturesDoctrineCommand extends DoctrineCommand
3435
/** @var PurgerFactory[] */
3536
private array $purgerFactories;
3637

38+
private ?LoggerInterface $logger;
39+
3740
/** @param PurgerFactory[] $purgerFactories */
38-
public function __construct(SymfonyFixturesLoader $fixturesLoader, ?ManagerRegistry $doctrine = null, array $purgerFactories = [])
39-
{
41+
public function __construct(
42+
SymfonyFixturesLoader $fixturesLoader,
43+
?ManagerRegistry $doctrine = null,
44+
array $purgerFactories = [],
45+
?LoggerInterface $logger = null
46+
) {
4047
if ($doctrine === null) {
4148
trigger_deprecation(
4249
'doctrine/fixtures-bundle',
@@ -51,6 +58,7 @@ public function __construct(SymfonyFixturesLoader $fixturesLoader, ?ManagerRegis
5158

5259
$this->fixturesLoader = $fixturesLoader;
5360
$this->purgerFactories = $purgerFactories;
61+
$this->logger = $logger;
5462
}
5563

5664
/** @return void */
@@ -134,7 +142,7 @@ private function doExecute(InputInterface $input, OutputInterface $output): int
134142
$input->getOption('purge-with-truncate'),
135143
);
136144
$executor = new ORMExecutor($em, $purger);
137-
$executor->setLogger(static function ($message) use ($ui): void {
145+
$executor->setLogger($this->logger ?? static function ($message) use ($ui): void {
138146
$ui->text(sprintf(' <comment>></comment> <info>%s</info>', $message));
139147
});
140148
$executor->execute($fixtures, $input->getOption('append'));

tests/Command/LoadDataFixturesDoctrineCommandTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader;
1010
use Doctrine\Persistence\ManagerRegistry;
1111
use PHPUnit\Framework\TestCase;
12+
use Psr\Log\NullLogger;
1213
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1314
use Symfony\Component\DependencyInjection\Container;
1415
use TypeError;
@@ -29,7 +30,7 @@ public function testInstantiatingWithoutManagerRegistry(): void
2930
$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.');
3031

3132
try {
32-
new LoadDataFixturesDoctrineCommand($loader);
33+
new LoadDataFixturesDoctrineCommand($loader, null, [], new NullLogger());
3334
} catch (TypeError $e) {
3435
$this->expectExceptionMessage(sprintf(
3536
PHP_VERSION_ID >= 80000 ?
@@ -49,6 +50,6 @@ public function testInstantiatingWithManagerRegistry(): void
4950
$registry = $this->createMock(ManagerRegistry::class);
5051
$loader = new SymfonyFixturesLoader(new Container());
5152

52-
new LoadDataFixturesDoctrineCommand($loader, $registry);
53+
new LoadDataFixturesDoctrineCommand($loader, $registry, [], new NullLogger());
5354
}
5455
}

tests/IntegrationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Doctrine\Persistence\ManagerRegistry;
2222
use LogicException;
2323
use PHPUnit\Framework\TestCase;
24+
use Psr\Log\NullLogger;
2425
use Symfony\Component\Console\Tester\CommandTester;
2526
use Symfony\Component\DependencyInjection\Alias;
2627
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -287,6 +288,8 @@ public function testRunCommandWithDefaultPurger(): void
287288
->setPublic(true)
288289
->setSynthetic(true);
289290

291+
$c->setDefinition('logger', new Definition(NullLogger::class));
292+
290293
$c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true));
291294

292295
$c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));
@@ -334,6 +337,8 @@ public function testRunCommandWithPurgeExclusions(): void
334337
->setPublic(true)
335338
->setSynthetic(true);
336339

340+
$c->setDefinition('logger', new Definition(NullLogger::class));
341+
337342
$c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true));
338343

339344
$c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));
@@ -386,6 +391,8 @@ public function testRunCommandWithCustomPurgerAndCustomEntityManager(): void
386391
->setSynthetic(true)
387392
->addTag(PurgerFactoryCompilerPass::PURGER_FACTORY_TAG, ['alias' => 'test']));
388393

394+
$c->setDefinition('logger', new Definition(NullLogger::class));
395+
389396
$c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));
390397
});
391398
$kernel->boot();
@@ -431,6 +438,8 @@ public function testRunCommandWithPurgeMode(): void
431438
->setPublic(true)
432439
->setSynthetic(true);
433440

441+
$c->setDefinition('logger', new Definition(NullLogger::class));
442+
434443
$c->setAlias('test.doctrine.fixtures.purger.orm_purger_factory', new Alias('doctrine.fixtures.purger.orm_purger_factory', true));
435444

436445
$c->setAlias('test.doctrine.fixtures_load_command', new Alias('doctrine.fixtures_load_command', true));

0 commit comments

Comments
 (0)