Skip to content

Commit

Permalink
Make DoctrineBundle compatible with ORM 2.9 (#1327)
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com>
  • Loading branch information
beberlei and ostrolucky committed May 9, 2021
1 parent 6595f6e commit da514a2
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 27 deletions.
9 changes: 7 additions & 2 deletions Command/Proxy/ClearMetadataCacheDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ protected function configure()

$this
->setName('doctrine:cache:clear-metadata')
->setDescription('Clears all metadata cache for an entity manager')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setDescription('Clears all metadata cache for an entity manager');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/ClearQueryCacheDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ protected function configure()

$this
->setName('doctrine:cache:clear-query')
->setDescription('Clears all query cache for an entity manager')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setDescription('Clears all query cache for an entity manager');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/ClearResultCacheDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ protected function configure()

$this
->setName('doctrine:cache:clear-result')
->setDescription('Clears result cache for an entity manager')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setDescription('Clears result cache for an entity manager');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/CollectionRegionDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ protected function configure()
parent::configure();

$this
->setName('doctrine:cache:clear-collection-region')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:cache:clear-collection-region');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/ConvertMappingDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ protected function configure()
{
parent::configure();
$this
->setName('doctrine:mapping:convert')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:mapping:convert');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/CreateSchemaDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ protected function configure()

$this
->setName('doctrine:schema:create')
->setDescription('Executes (or dumps) the SQL needed to generate the database schema')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setDescription('Executes (or dumps) the SQL needed to generate the database schema');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/DropSchemaDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ protected function configure()

$this
->setName('doctrine:schema:drop')
->setDescription('Executes (or dumps) the SQL needed to drop the current database schema')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setDescription('Executes (or dumps) the SQL needed to drop the current database schema');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/EnsureProductionSettingsDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ protected function configure()
parent::configure();

$this
->setName('doctrine:ensure-production-settings')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:ensure-production-settings');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/EntityRegionCacheDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ protected function configure()
parent::configure();

$this
->setName('doctrine:cache:clear-entity-region')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:cache:clear-entity-region');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/InfoDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class InfoDoctrineCommand extends InfoCommand
protected function configure()
{
$this
->setName('doctrine:mapping:info')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:mapping:info');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/QueryRegionCacheDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ protected function configure()
parent::configure();

$this
->setName('doctrine:cache:clear-query-region')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:cache:clear-query-region');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
7 changes: 6 additions & 1 deletion Command/Proxy/RunDqlDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ protected function configure()

$this
->setName('doctrine:query:dql')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
->setHelp(<<<EOT
The <info>%command.name%</info> command executes the given DQL query and
outputs the results:
Expand All @@ -39,6 +38,12 @@ protected function configure()
<info>php %command.full_name% "SELECT u FROM UserBundle:User u" --first-result=0 --max-result=30</info>
EOT
);

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/UpdateSchemaDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ protected function configure()
parent::configure();

$this
->setName('doctrine:schema:update')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:schema:update');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Command/Proxy/ValidateSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ protected function configure()
parent::configure();

$this
->setName('doctrine:schema:validate')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
->setName('doctrine:schema:validate');

if ($this->getDefinition()->hasOption('em')) {
return;
}

$this->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command');
}

/**
Expand Down
70 changes: 70 additions & 0 deletions Tests/Command/Proxy/InfoDoctrineCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Command\Proxy;

use Doctrine\Bundle\DoctrineBundle\Command\Proxy\InfoDoctrineCommand;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;

use function array_merge;

class InfoDoctrineCommandTest extends TestCase
{
public function testExecute(): void
{
$kernel = $this->setupKernelMocks();

$application = new Application($kernel);
$application->add(new InfoDoctrineCommand());

$command = $application->find('doctrine:mapping:info');

$commandTester = new CommandTester($command);
$commandTester->execute(
array_merge(['command' => $command->getName()])
);

$this->assertStringContainsString(
'You do not have any mapped Doctrine ORM entities',
$commandTester->getDisplay()
);
}

/**
* @return MockObject&Kernel
*/
private function setupKernelMocks(): MockObject
{
$configuration = new Configuration();
$configuration->setMetadataDriverImpl(new MappingDriverChain());

$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();

$manager = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
$manager->method('getConnection')->willReturn($connection);
$manager->method('getConfiguration')->willReturn($configuration);

$registry = $this->getMockBuilder(ManagerRegistry::class)->disableOriginalConstructor()->getMock();
$registry->method('getManager')->willReturn($manager);

$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
$container->method('get')->willReturn($registry);

$kernel = $this->getMockBuilder(Kernel::class);
$kernel->disableOriginalConstructor();
$kernel = $kernel->getMock();
$kernel->method('getBundles')->willReturn([]);
$kernel->method('getContainer')->willReturn($container);

return $kernel;
}
}
4 changes: 4 additions & 0 deletions Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\Bundle\DoctrineBundle\Command\Proxy\InfoDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\DbalTestKernel;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\EventManager;
Expand Down Expand Up @@ -72,6 +74,8 @@ public function testContainer(): void
$this->assertInstanceOf(ProxyCacheWarmer::class, $container->get('doctrine.orm.proxy_cache_warmer'));
$this->assertInstanceOf(ManagerRegistry::class, $container->get('doctrine'));
$this->assertInstanceOf(UniqueEntityValidator::class, $container->get('doctrine.orm.validator.unique'));
$this->assertInstanceOf(InfoDoctrineCommand::class, $container->get('doctrine.mapping_info_command'));
$this->assertInstanceOf(UpdateSchemaDoctrineCommand::class, $container->get('doctrine.schema_update_command'));

$this->assertSame($container->get('my.platform'), $container->get('doctrine.dbal.default_connection')->getDatabasePlatform());

Expand Down

0 comments on commit da514a2

Please sign in to comment.