Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/Command/LoadDataFixturesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

declare(strict_types=1);
Expand All @@ -8,14 +8,17 @@
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor;
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
use Doctrine\ODM\MongoDB\DocumentManager;
use Psr\Log\AbstractLogger;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;

use function assert;
use function implode;
use function method_exists;
use function sprintf;

/**
Expand All @@ -38,6 +41,7 @@
->addOption('group', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group (use with --services)')
->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of flushing the database first.')
->addOption('dm', null, InputOption::VALUE_REQUIRED, 'The document manager to use for this command.')
->addOption('purge-with-delete', null, InputOption::VALUE_NONE, 'Use deleteMany() to purge the collections instead of dropping them. This is useful to keep the collections, their metadata and their indexes, but it is slower than dropping them.')
->setHelp(<<<'EOT'
The <info>doctrine:mongodb:fixtures:load</info> command loads data fixtures from your application:

Expand All @@ -50,16 +54,28 @@
You can also choose to load only fixtures that live in a certain group:

<info>php %command.full_name%</info> <comment>--group=group1</comment>

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:

<info>php %command.full_name%</info> --purge-with-delete
EOT
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$dm = $this->getManagerRegistry()->getManager($input->getOption('dm'));
assert($dm instanceof DocumentManager);

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

if ($input->isInteractive() && ! $input->getOption('append')) {
if ($input->getOption('purge-with-delete')) {
if ($input->getOption('append')) {
$ui->error('The --purge-with-delete option cannot be used with the --append option.');

Check warning on line 74 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/LoadDataFixturesDoctrineODMCommand.php#L73-L74

Added lines #L73 - L74 were not covered by tests

return self::INVALID;

Check warning on line 76 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/LoadDataFixturesDoctrineODMCommand.php#L76

Added line #L76 was not covered by tests
}
} elseif ($input->isInteractive() && ! $input->getOption('append')) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Careful, database will be purged. Do you want to continue (y/N) ?', false);

Expand All @@ -82,7 +98,17 @@
return 1;
}

$purger = new MongoDBPurger($dm);
$purger = new MongoDBPurger($dm);
if ($input->getOption('purge-with-delete')) {
if (! method_exists($purger, 'setPurgeMode')) {

Check failure on line 103 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Call to function method_exists() with Doctrine\Common\DataFixtures\Purger\MongoDBPurger and 'setPurgeMode' will always evaluate to false.
$ui->error('The --purge-with-delete option requires doctrine/data-fixtures >= 2.1.0.');

Check warning on line 104 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/LoadDataFixturesDoctrineODMCommand.php#L103-L104

Added lines #L103 - L104 were not covered by tests

return self::INVALID;

Check warning on line 106 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/LoadDataFixturesDoctrineODMCommand.php#L106

Added line #L106 was not covered by tests
}

$purger->setPurgeMode(MongoDBPurger::PURGE_MODE_DELETE);

Check failure on line 109 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Access to undefined constant Doctrine\Common\DataFixtures\Purger\MongoDBPurger::PURGE_MODE_DELETE.

Check warning on line 109 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/LoadDataFixturesDoctrineODMCommand.php#L109

Added line #L109 was not covered by tests
}

$executor = new MongoDBExecutor($dm, $purger);

$executor->setLogger(new class ($output) extends AbstractLogger {
Expand All @@ -91,7 +117,7 @@
}

/** {@inheritDoc} */
public function log($level, $message, array $context = []): void

Check failure on line 120 in src/Command/LoadDataFixturesDoctrineODMCommand.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Method Psr\Log\AbstractLogger@anonymous/src/Command/LoadDataFixturesDoctrineODMCommand.php:114::log() has parameter $message with no type specified.
{
$this->output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message));
}
Expand Down
Loading