Skip to content

Commit e72afac

Browse files
committed
Revert to --purge-with-delete
1 parent 2ff49b7 commit e72afac

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,11 @@ parameters:
8585
path: src/Command/LoadDataFixturesDoctrineODMCommand.php
8686

8787
-
88-
message: '#^Method Psr\\Log\\AbstractLogger@anonymous/src/Command/LoadDataFixturesDoctrineODMCommand\.php\:88\:\:log\(\) has parameter \$message with no type specified\.$#'
88+
message: '#^Method Psr\\Log\\AbstractLogger@anonymous/src/Command/LoadDataFixturesDoctrineODMCommand\.php\:109\:\:log\(\) has parameter \$message with no type specified\.$#'
8989
identifier: missingType.parameter
9090
count: 1
9191
path: src/Command/LoadDataFixturesDoctrineODMCommand.php
9292

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-
10593
-
10694
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\.$#'
10795
identifier: argument.type

src/Command/LoadDataFixturesDoctrineODMCommand.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
use Doctrine\Common\DataFixtures\Purger\MongoDBPurgeMode;
1111
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
1212
use Doctrine\ODM\MongoDB\DocumentManager;
13-
use InvalidArgumentException;
1413
use Psr\Log\AbstractLogger;
1514
use Symfony\Component\Console\Input\InputInterface;
1615
use Symfony\Component\Console\Input\InputOption;
1716
use Symfony\Component\Console\Output\OutputInterface;
1817
use Symfony\Component\Console\Question\ConfirmationQuestion;
1918
use Symfony\Component\Console\Style\SymfonyStyle;
2019

21-
use function array_column;
2220
use function assert;
2321
use function class_exists;
2422
use function implode;
@@ -44,7 +42,7 @@ protected function configure(): void
4442
->addOption('group', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group (use with --services)')
4543
->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of flushing the database first.')
4644
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
47-
->addOption('purge', null, InputOption::VALUE_OPTIONAL, 'Purge the database before loading the fixtures. If set to "delete", collections will be kept and documents deleted instead of dropping the collections.', null, self::getPurgeModes(...))
45+
->addOption('purge-with-delete', null, InputOption::VALUE_NONE, 'Purge the database using deleteMany() instead of dropping and recreating the collections.')
4846
->setHelp(<<<'EOT'
4947
The <info>doctrine:mongodb:fixtures:load</info> command loads data fixtures from your application:
5048
@@ -58,9 +56,9 @@ protected function configure(): void
5856
5957
<info>php %command.full_name%</info> <comment>--group=group1</comment>
6058
61-
If the collection uses search indexes or encryption, you can use the <info>--purge=delete</info> option to keep the collections instead of dropping them when purging the database:
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:
6260
63-
<info>php %command.full_name%</info> --purge=delete
61+
<info>php %command.full_name%</info> --purge-with-delete
6462
EOT
6563
);
6664
}
@@ -72,13 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7270

7371
$ui = new SymfonyStyle($input, $output);
7472

75-
if ($input->hasOption('purge')) {
76-
if ($input->getOption('append')) {
77-
$ui->error('The --purge option cannot be used with the --append option.');
78-
79-
return self::INVALID;
80-
}
81-
} elseif ($input->isInteractive() && ! $input->getOption('append')) {
73+
if ($input->isInteractive() && ! $input->getOption('append')) {
8274
$helper = $this->getHelper('question');
8375
$question = new ConfirmationQuestion('Careful, database will be purged. Do you want to continue (y/N) ?', false);
8476

@@ -102,19 +94,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10294
}
10395

10496
$purger = new MongoDBPurger($dm);
105-
$purge = $input->getOption('purge');
106-
if ($purge) {
97+
if ($input->getOption('purge-with-delete')) {
10798
if (! class_exists(MongoDBPurgeMode::class)) {
10899
$ui->error('The --purge-with-delete option requires doctrine/data-fixtures >= 2.1.0.');
109100

110101
return self::INVALID;
111102
}
112103

113-
$purger->setPurgeMode(MongoDBPurgeMode::tryFrom($purge) ?? throw new InvalidArgumentException('Invalid purge mode: ' . $purge));
104+
$purger->setPurgeMode(MongoDBPurgeMode::Delete);
114105
}
115106

116107
$executor = new MongoDBExecutor($dm, $purger);
117-
118108
$executor->setLogger(new class ($output) extends AbstractLogger {
119109
public function __construct(private OutputInterface $output)
120110
{
@@ -130,9 +120,4 @@ public function log($level, $message, array $context = []): void
130120

131121
return 0;
132122
}
133-
134-
private static function getPurgeModes(): array
135-
{
136-
return class_exists(MongoDBPurgeMode::class) ? array_column(MongoDBPurgeMode::cases(), 'value') : [];
137-
}
138123
}

tests/Command/LoadDataFixturesDoctrineODMCommandTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ public function testExecute(): void
6060
$commandTester->execute([], ['interactive' => false]);
6161

6262
$output = $commandTester->getDisplay();
63+
$this->assertStringContainsString('purging database', $output);
64+
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures', $output);
65+
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures', $output);
66+
}
67+
68+
public function testExecutePurgeWithDelete(): void
69+
{
70+
$commandTester = new CommandTester($this->command);
71+
$commandTester->execute(['--purge-with-delete'], ['interactive' => false]);
72+
73+
$output = $commandTester->getDisplay();
74+
$this->assertStringContainsString('purging database', $output);
6375
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures', $output);
6476
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures', $output);
6577
}

0 commit comments

Comments
 (0)