Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate some unused DoctrineODMCommand methods #693

Merged
merged 3 commits into from
Sep 1, 2021
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
77 changes: 72 additions & 5 deletions Command/DoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,83 @@
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
use Doctrine\Persistence\ObjectManager;
use InvalidArgumentException;
use LogicException;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

use function assert;
use function sprintf;
use function str_replace;
use function strtolower;
use function trigger_deprecation;

use const DIRECTORY_SEPARATOR;

/**
* Base class for Doctrine ODM console commands to extend.
*
* @internal since version 5.0
*/
abstract class DoctrineODMCommand extends Command implements ContainerAwareInterface
abstract class DoctrineODMCommand extends Command
{
use ContainerAwareTrait;
/** @var ContainerInterface|null */
protected $container;

/** @var ManagerRegistry|null */
private $managerRegistry;

public function __construct(?ManagerRegistry $registry = null)
{
parent::__construct(null);
parent::__construct();

$this->managerRegistry = $registry;
}

/**
* @deprecated since version 4.4
*/
public function setContainer(?ContainerInterface $container = null)
{
trigger_deprecation(
'doctrine/mongodb-odm-bundle',
'4.4',
'The "%s" method is deprecated and will be dropped in DoctrineMongoDBBundle 5.0.',
__METHOD__
);

$this->container = $container;
}

/**
* @deprecated since version 4.4
*
* @return ContainerInterface
*
* @throws LogicException
*/
protected function getContainer()
{
trigger_deprecation(
'doctrine/mongodb-odm-bundle',
'4.4',
'The "%s" method is deprecated and will be dropped in DoctrineMongoDBBundle 5.0.',
__METHOD__
);

if ($this->container === null) {
$application = $this->getApplication();
if ($application === null) {
throw new LogicException('The container cannot be retrieved as the application instance is not yet set.');
}

assert($application instanceof Application);

$this->container = $application->getKernel()->getContainer();
}

return $this->container;
}

Expand All @@ -59,10 +99,19 @@ public static function setApplicationDocumentManager(Application $application, $
}

/**
* @deprecated since version 4.4
*
* @return ObjectManager[]
*/
protected function getDoctrineDocumentManagers()
{
trigger_deprecation(
'doctrine/mongodb-odm-bundle',
'4.4',
'The "%s" method is deprecated and will be dropped in DoctrineMongoDBBundle 5.0.',
__METHOD__
);

return $this->getManagerRegistry()->getManagers();
}

Expand All @@ -82,12 +131,21 @@ protected function getManagerRegistry()
}

/**
* @deprecated since version 4.4
*
* @param string $bundleName
*
* @return Bundle
*/
protected function findBundle($bundleName)
{
trigger_deprecation(
'doctrine/mongodb-odm-bundle',
'4.4',
'The "%s" method is deprecated and will be dropped in DoctrineMongoDBBundle 5.0.',
__METHOD__
);

$foundBundle = false;

$application = $this->getApplication();
Expand All @@ -113,12 +171,21 @@ protected function findBundle($bundleName)
/**
* Transform classname to a path $foundBundle substract it to get the destination
*
* @deprecated since version 4.4
*
* @param Bundle $bundle
*
* @return string
*/
protected function findBasePathForBundle($bundle)
{
trigger_deprecation(
'doctrine/mongodb-odm-bundle',
'4.4',
'The "%s" method is deprecated and will be dropped in DoctrineMongoDBBundle 5.0.',
__METHOD__
);

$path = str_replace('\\', DIRECTORY_SEPARATOR, $bundle->getNamespace());
$search = str_replace('\\', DIRECTORY_SEPARATOR, $bundle->getPath());
$destination = str_replace(DIRECTORY_SEPARATOR . $path, '', $search, $c);
Expand Down
4 changes: 2 additions & 2 deletions Command/InfoDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ protected function configure()
The <info>doctrine:mongodb:mapping:info</info> shows basic information about which
documents exist and possibly if their mapping information contains errors or not.

<info>./app/console doctrine:mongodb:mapping:info</info>
<info>./bin/console doctrine:mongodb:mapping:info</info>

If you are using multiple document managers you can pick your choice with the <info>--dm</info> option:

<info>./app/console doctrine:mongodb:mapping:info --dm=default</info>
<info>./bin/console doctrine:mongodb:mapping:info --dm=default</info>
EOT
);
}
Expand Down
78 changes: 78 additions & 0 deletions Tests/Command/CommandTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\Command;

use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\CommandBundle;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures;
use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;

use function sys_get_temp_dir;

final class CommandTestKernel extends Kernel
{
use MicroKernelTrait;

public function registerBundles(): array
{
return [
new FrameworkBundle(),
new DoctrineMongoDBBundle(),
new CommandBundle(),
];
}

/**
* @param RouteConfigurator|RouteCollectionBuilder $routes
*/
public function configureRoutes($routes): void
{
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
$container->loadFromExtension('framework', [
'secret' => 'foo',
'router' => ['utf8' => false],
]);

$container->loadFromExtension('doctrine_mongodb', [
'connections' => ['default' => []],
'document_managers' => [
'command_test' => [
'connection' => 'default',
'mappings' => ['CommandBundle' => null],
],
'command_test_without_documents' => ['connection' => 'default'],
],
]);

$container
->autowire(UserFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG, ['group' => 'test_group']);

$container
->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
}

public function getCacheDir(): string
{
return sys_get_temp_dir() . '/doctrine_mongodb_odm_bundle';
}

public function getLogDir(): string
{
return sys_get_temp_dir();
}
}
42 changes: 42 additions & 0 deletions Tests/Command/InfoDoctrineODMCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\Command;

use Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\Document\User;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Throwable;

final class InfoDoctrineODMCommandTest extends KernelTestCase
{
public function testExecute(): void
{
$kernel = new CommandTestKernel('test', false);
$application = new Application($kernel);

$command = $application->find('doctrine:mongodb:mapping:info');
$commandTester = new CommandTester($command);
$commandTester->execute(['--dm' => 'command_test']);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Found 1 documents mapped in document manager command_test', $output);
$this->assertStringContainsString(User::class, $output);
}

public function testExecuteWithDocumentManagerWithoutDocuments(): void
{
$kernel = new CommandTestKernel('test', false);
$application = new Application($kernel);

$command = $application->find('doctrine:mongodb:mapping:info');
$commandTester = new CommandTester($command);

$this->expectException(Throwable::class);
$this->expectExceptionMessage('You do not have any mapped Doctrine MongoDB ODM documents for any of your bundles. Create a class inside the Document namespace of any of your bundles and provide mapping information for it with Annotations directly in the classes doc blocks or with XML in your bundles Resources/config/doctrine/metadata/mongodb directory');

$commandTester->execute(['--dm' => 'command_test_without_documents']);
}
}
58 changes: 47 additions & 11 deletions Tests/Command/LoadDataFixturesDoctrineODMCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,63 @@
namespace Doctrine\Bundle\MongoDBBundle\Tests\Command;

use Doctrine\Bundle\MongoDBBundle\Command\LoadDataFixturesDoctrineODMCommand;
use Doctrine\Bundle\MongoDBBundle\Loader\SymfonyFixturesLoaderInterface;
use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

class LoadDataFixturesDoctrineODMCommandTest extends TestCase
class LoadDataFixturesDoctrineODMCommandTest extends KernelTestCase
{
/** @var LoadDataFixturesDoctrineODMCommand */
private $command;

protected function setUp(): void
{
$registry = $this->createMock(ManagerRegistry::class);
$kernel = $this->createMock(KernelInterface::class);
$loader = $this->createMock(SymfonyFixturesLoaderInterface::class);
$kernel = new CommandTestKernel('test', false);
$application = new Application($kernel);

$this->command = new LoadDataFixturesDoctrineODMCommand($registry, $kernel, $loader);
$this->command = $application->find('doctrine:mongodb:fixtures:load');
}

public function testCommandIsEnabledWithDependency(): void
public function testIsInteractiveByDefault(): void
{
$this->assertTrue($this->command->isEnabled());
$commandTester = new CommandTester($this->command);
$commandTester->execute([]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Careful, database will be purged. Do you want to continue (y/N) ?', $output);
}

public function testGroup(): void
{
$commandTester = new CommandTester($this->command);
$commandTester->execute([
'--group' => ['test_group'],
], ['interactive' => false]);

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

public function testNonExistingGroup(): void
{
$commandTester = new CommandTester($this->command);
$commandTester->execute([
'--group' => ['non_existing_group'],
], ['interactive' => false]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('Could not find any fixture services to load in the groups', $output);
$this->assertStringContainsString('(non_existing_group)', $output);
}

public function testExecute(): void
{
$commandTester = new CommandTester($this->command);
$commandTester->execute([], ['interactive' => false]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\UserFixtures', $output);
$this->assertStringContainsString('loading Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures\OtherFixtures', $output);
}
}
11 changes: 11 additions & 0 deletions Tests/Fixtures/CommandBundle/CommandBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CommandBundle extends Bundle
{
}
15 changes: 15 additions & 0 deletions Tests/Fixtures/CommandBundle/DataFixtures/OtherFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\Fixtures\CommandBundle\DataFixtures;

use Doctrine\Bundle\MongoDBBundle\Fixture\ODMFixtureInterface;
use Doctrine\Persistence\ObjectManager;

final class OtherFixtures implements ODMFixtureInterface
{
public function load(ObjectManager $manager): void
{
}
}
Loading