Skip to content

Commit 4fc91e9

Browse files
authored
Add --purge-with-delete option to the doctrine:mongodb:fixtures:load command (#906)
Requires doctrine/data-fixtures 2.1+ doctrine/data-fixtures#544
1 parent 66e6ab5 commit 4fc91e9

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,6 @@ parameters:
8484
count: 1
8585
path: src/Command/LoadDataFixturesDoctrineODMCommand.php
8686

87-
-
88-
message: '#^Method Psr\\Log\\AbstractLogger@anonymous/src/Command/LoadDataFixturesDoctrineODMCommand\.php\:88\:\:log\(\) has parameter \$message with no type specified\.$#'
89-
identifier: missingType.parameter
90-
count: 1
91-
path: src/Command/LoadDataFixturesDoctrineODMCommand.php
92-
93-
-
94-
message: '#^Parameter \#1 \$dm of class Doctrine\\Common\\DataFixtures\\Executor\\MongoDBExecutor constructor expects Doctrine\\ODM\\MongoDB\\DocumentManager, Doctrine\\Persistence\\ObjectManager given\.$#'
95-
identifier: argument.type
96-
count: 1
97-
path: src/Command/LoadDataFixturesDoctrineODMCommand.php
98-
99-
-
100-
message: '#^Parameter \#1 \$dm of class Doctrine\\Common\\DataFixtures\\Purger\\MongoDBPurger constructor expects Doctrine\\ODM\\MongoDB\\DocumentManager\|null, Doctrine\\Persistence\\ObjectManager given\.$#'
101-
identifier: argument.type
102-
count: 1
103-
path: src/Command/LoadDataFixturesDoctrineODMCommand.php
104-
10587
-
10688
message: '#^Parameter \#1 \$application of static method Doctrine\\Bundle\\MongoDBBundle\\Command\\DoctrineODMCommand\:\:setApplicationDocumentManager\(\) expects Symfony\\Bundle\\FrameworkBundle\\Console\\Application, Symfony\\Component\\Console\\Application\|null given\.$#'
10789
identifier: argument.type

src/Command/LoadDataFixturesDoctrineODMCommand.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
use Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoaderInterface;
88
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
99
use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor;
10+
use Doctrine\Common\DataFixtures\Purger\MongoDBPurgeMode;
1011
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
12+
use Doctrine\ODM\MongoDB\DocumentManager;
1113
use Psr\Log\AbstractLogger;
1214
use Symfony\Component\Console\Input\InputInterface;
1315
use Symfony\Component\Console\Input\InputOption;
1416
use Symfony\Component\Console\Output\OutputInterface;
1517
use Symfony\Component\Console\Question\ConfirmationQuestion;
1618
use Symfony\Component\Console\Style\SymfonyStyle;
1719

20+
use function assert;
21+
use function class_exists;
1822
use function implode;
1923
use function sprintf;
2024

@@ -38,6 +42,7 @@ protected function configure(): void
3842
->addOption('group', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group (use with --services)')
3943
->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of flushing the database first.')
4044
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
45+
->addOption('purge-with-delete', null, InputOption::VALUE_NONE, 'Purge the database using deleteMany() instead of dropping and recreating the collections.')
4146
->setHelp(<<<'EOT'
4247
The <info>doctrine:mongodb:fixtures:load</info> command loads data fixtures from your application:
4348
@@ -50,13 +55,19 @@ protected function configure(): void
5055
You can also choose to load only fixtures that live in a certain group:
5156
5257
<info>php %command.full_name%</info> <comment>--group=group1</comment>
58+
59+
If the collection uses search indexes or encryption, you can use the <info>--purge-with-delete</info> option to keep the collections instead of dropping them when purging the database:
60+
61+
<info>php %command.full_name%</info> --purge-with-delete
5362
EOT
5463
);
5564
}
5665

5766
protected function execute(InputInterface $input, OutputInterface $output): int
5867
{
5968
$dm = $this->getManagerRegistry()->getManager($input->getOption('dm'));
69+
assert($dm instanceof DocumentManager);
70+
6071
$ui = new SymfonyStyle($input, $output);
6172

6273
if ($input->isInteractive() && ! $input->getOption('append')) {
@@ -82,15 +93,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8293
return 1;
8394
}
8495

85-
$purger = new MongoDBPurger($dm);
86-
$executor = new MongoDBExecutor($dm, $purger);
96+
$purger = new MongoDBPurger($dm);
97+
if ($input->getOption('purge-with-delete')) {
98+
if (! class_exists(MongoDBPurgeMode::class)) {
99+
$ui->error('The --purge-with-delete option requires doctrine/data-fixtures >= 2.1.0.');
87100

101+
return self::INVALID;
102+
}
103+
104+
// @phpstan-ignore method.notFound
105+
$purger->setPurgeMode(MongoDBPurgeMode::Delete);
106+
}
107+
108+
$executor = new MongoDBExecutor($dm, $purger);
88109
$executor->setLogger(new class ($output) extends AbstractLogger {
89110
public function __construct(private OutputInterface $output)
90111
{
91112
}
92113

93-
/** {@inheritDoc} */
114+
/**
115+
* @inheritdoc
116+
* @phpstan-ignore missingType.parameter
117+
*/
94118
public function log($level, $message, array $context = []): void
95119
{
96120
$this->output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message));

tests/Command/LoadDataFixturesDoctrineODMCommandTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
namespace Doctrine\Bundle\MongoDBBundle\Tests\Command;
66

77
use Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand;
8+
use Doctrine\Common\DataFixtures\Purger\MongoDBPurgeMode;
89
use Symfony\Bundle\FrameworkBundle\Console\Application;
910
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1011
use Symfony\Component\Console\Tester\CommandTester;
1112

13+
use function class_exists;
14+
1215
class LoadDataFixturesDoctrineODMCommandTest extends KernelTestCase
1316
{
1417
private LoadDataFixturesDoctrineODMCommand $command;
@@ -60,6 +63,22 @@ public function testExecute(): void
6063
$commandTester->execute([], ['interactive' => false]);
6164

6265
$output = $commandTester->getDisplay();
66+
$this->assertStringContainsString('purging database', $output);
67+
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures', $output);
68+
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures', $output);
69+
}
70+
71+
public function testExecutePurgeWithDelete(): void
72+
{
73+
if (! class_exists(MongoDBPurgeMode::class)) {
74+
$this->markTestSkipped('The --purge-with-delete option requires doctrine/data-fixtures >= 2.1.0.');
75+
}
76+
77+
$commandTester = new CommandTester($this->command);
78+
$commandTester->execute(['--purge-with-delete' => true], ['interactive' => false]);
79+
80+
$output = $commandTester->getDisplay();
81+
$this->assertStringContainsString('purging database', $output);
6382
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures', $output);
6483
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures', $output);
6584
}

0 commit comments

Comments
 (0)