Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 0 additions & 18 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,6 @@ parameters:
count: 1
path: src/Command/LoadDataFixturesDoctrineODMCommand.php

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

-
message: '#^Parameter \#1 \$dm of class Doctrine\\Common\\DataFixtures\\Executor\\MongoDBExecutor constructor expects Doctrine\\ODM\\MongoDB\\DocumentManager, Doctrine\\Persistence\\ObjectManager given\.$#'
identifier: argument.type
count: 1
path: src/Command/LoadDataFixturesDoctrineODMCommand.php

-
message: '#^Parameter \#1 \$dm of class Doctrine\\Common\\DataFixtures\\Purger\\MongoDBPurger constructor expects Doctrine\\ODM\\MongoDB\\DocumentManager\|null, Doctrine\\Persistence\\ObjectManager given\.$#'
identifier: argument.type
count: 1
path: src/Command/LoadDataFixturesDoctrineODMCommand.php

-
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\.$#'
identifier: argument.type
Expand Down
30 changes: 27 additions & 3 deletions src/Command/LoadDataFixturesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
use Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoaderInterface;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\Common\DataFixtures\Executor\MongoDBExecutor;
use Doctrine\Common\DataFixtures\Purger\MongoDBPurgeMode;
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 class_exists;
use function implode;
use function sprintf;

Expand All @@ -38,6 +42,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, 'Purge the database using deleteMany() instead of dropping and recreating the collections.')
->setHelp(<<<'EOT'
The <info>doctrine:mongodb:fixtures:load</info> command loads data fixtures from your application:

Expand All @@ -50,13 +55,19 @@
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')) {
Expand All @@ -82,15 +93,28 @@
return 1;
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/Command/LoadDataFixturesDoctrineODMCommand.php#L99

Added line #L99 was not covered by tests

return self::INVALID;

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

View check run for this annotation

Codecov / codecov/patch

src/Command/LoadDataFixturesDoctrineODMCommand.php#L101

Added line #L101 was not covered by tests
}

// @phpstan-ignore method.notFound
$purger->setPurgeMode(MongoDBPurgeMode::Delete);
}

$executor = new MongoDBExecutor($dm, $purger);
$executor->setLogger(new class ($output) extends AbstractLogger {
public function __construct(private OutputInterface $output)
{
}

/** {@inheritDoc} */
/**
* @inheritdoc
* @phpstan-ignore missingType.parameter
*/
public function log($level, $message, array $context = []): void
{
$this->output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message));
Expand Down
19 changes: 19 additions & 0 deletions tests/Command/LoadDataFixturesDoctrineODMCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Doctrine\Bundle\MongoDBBundle\Tests\Command;

use Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand;
use Doctrine\Common\DataFixtures\Purger\MongoDBPurgeMode;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

use function class_exists;

class LoadDataFixturesDoctrineODMCommandTest extends KernelTestCase
{
private LoadDataFixturesDoctrineODMCommand $command;
Expand Down Expand Up @@ -60,6 +63,22 @@ public function testExecute(): void
$commandTester->execute([], ['interactive' => false]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('purging database', $output);
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures', $output);
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures', $output);
}

public function testExecutePurgeWithDelete(): void
{
if (! class_exists(MongoDBPurgeMode::class)) {
$this->markTestSkipped('The --purge-with-delete option requires doctrine/data-fixtures >= 2.1.0.');
}

$commandTester = new CommandTester($this->command);
$commandTester->execute(['--purge-with-delete' => true], ['interactive' => false]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('purging database', $output);
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures', $output);
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures', $output);
}
Expand Down