[DoctrineBundle] Add new Command doctrine:mapping:info that allows to check what entities are mapped and if their metadata is specified correctly. Added exception when a mapping/bundle directory does not exist.

This commit is contained in:
Benjamin Eberlei 2011-01-22 19:43:53 +01:00 committed by Fabien Potencier
parent 6eca31ce8f
commit 682e83585b
8 changed files with 161 additions and 30 deletions

View File

@ -249,6 +249,10 @@ abstract class AbstractDoctrineExtension extends Extension
"require at least the 'type', 'dir' and 'prefix' options.");
}
if (!file_exists($mappingConfig['dir'])) {
throw new \InvalidArgumentException("Specified non-existing directory '" . $mappingConfig['dir'] . "' as mapping source.");
}
if (!in_array($mappingConfig['type'], array('xml', 'yml', 'annotation', 'php', 'staticphp'))) {
throw new \InvalidArgumentException("Can only configure 'xml', 'yml', 'annotation', 'php' or ".
"'static-php' through the DoctrineBundle. Use your own bundle to configure other metadata drivers. " .

View File

@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineBundle\Command;
use Doctrine\ORM\Mapping\MappingException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Show information about mapped entities
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class InfoDoctrineCommand extends DoctrineCommand
{
protected function configure()
{
$this
->setName('doctrine:mapping:info')
->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command.')
->setDescription('Show basic information about all mapped entities.')
->setHelp(<<<EOT
The <info>doctrine:mapping:info</info> shows basic information about which
entities exist and possibly if their mapping information contains errors or not.
<info>./app/console doctrine:mapping:info</info>
If you are using multiple entitiy managers you can pick your choice with the <info>--em</info> option:
<info>./app/console doctrine:mapping:info --em=default</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$entityManagerName = $input->getOption('em') ?
$input->getOption('em') :
$this->container->getParameter('doctrine.orm.default_entity_manager');
-
$entityManagerService = sprintf('doctrine.orm.%s_entity_manager', $entityManagerName);
/* @var $entityManager Doctrine\ORM\EntityManager */
$entityManager = $this->container->get($entityManagerService);
$entityClassNames = $entityManager->getConfiguration()
->getMetadataDriverImpl()
->getAllClassNames();
$output->write(sprintf("Found %d entities mapped in entity manager '%s':",
count($entityClassNames), $entityManagerName), true);
foreach ($entityClassNames AS $entityClassName) {
try {
$cm = $entityManager->getClassMetadata($entityClassName);
$output->write("<info>[OK]</info> " . $entityClassName, true);
} catch(MappingException $e) {
$output->write("<error>[FAIL]</error> " . $entityClassName, true);
$output->write("<comment>" . $e->getMessage()."</comment>", true);
$output->write("", true);
}
}
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Symfony\Bundle\DoctrineBundle\Tests\Command;
use Symfony\Bundle\DoctrineBundle\Tests\TestCase;
use Symfony\Bundle\DoctrineBundle\Command\InfoDoctrineCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\StringInput;
class InfoDoctrineCommandTest extends TestCase
{
public function testAnnotationsBundle()
{
$input = new StringInput("doctrine:mapping:info");
$output = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
$output->expects($this->at(0))
->method('write')
->with($this->equalTo("Found 1 entities mapped in entity manager 'default':"), $this->equalTo(true));
$output->expects($this->at(1))
->method('write')
->with($this->equalTo("<info>[OK]</info> DoctrineBundle\Tests\DependencyInjection\Fixtures\Bundles\YamlBundle\Entity\Test"), $this->equalTo(true));
$testContainer = $this->createYamlBundleTestContainer();
$kernel = $this->getMock('Symfony\Component\HttpKernel\Kernel', array(), array(), '', false);
$kernel->expects($this->once())
->method('isBooted')
->will($this->returnValue( true ));
$kernel->expects($this->once())
->method('getBundles')
->will($this->returnValue(array()));
$kernel->expects($this->once())
->method('getContainer')
->will($this->returnValue($testContainer));
$application = new Application($kernel);
$cmd = new InfoDoctrineCommand();
$cmd->setApplication($application);
$cmd->run($input, $output);
}
}

View File

@ -11,40 +11,11 @@
namespace Symfony\Bundle\DoctrineBundle\Tests;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class ContainerTest extends TestCase
{
public function getContainer()
{
$container = new ContainerBuilder(new ParameterBag(array(
'kernel.bundles' => array('YamlBundle' => 'DoctrineBundle\Tests\DependencyInjection\Fixtures\Bundles\YamlBundle\YamlBundle'),
'kernel.cache_dir' => sys_get_temp_dir(),
)));
$loader = new DoctrineExtension();
$container->registerExtension($loader);
$loader->dbalLoad(array(array(
'connections' => array(
'default' => array(
'driver' => 'pdo_mysql',
'charset' => 'UTF-8',
'platform-service' => 'my.platform',
)
)
)), $container);
$loader->ormLoad(array('bundles' => array('YamlBundle' => array())), $container);
$container->setDefinition('my.platform', new \Symfony\Component\DependencyInjection\Definition('Doctrine\DBAL\Platforms\MySqlPlatform'));
return $container;
}
public function testContainer()
{
$container = $this->getContainer();
$container = $this->createYamlBundleTestContainer();
$this->assertInstanceOf('Doctrine\DBAL\Logging\DebugStack', $container->get('doctrine.dbal.logger.debug'));
$this->assertInstanceOf('Doctrine\DBAL\Logging\DebugStack', $container->get('doctrine.dbal.logger'));
$this->assertInstanceOf('Symfony\Bundle\DoctrineBundle\DataCollector\DoctrineDataCollector', $container->get('doctrine.data_collector'));

View File

@ -4,4 +4,5 @@ namespace DoctrineBundle\Tests\DependencyInjection\Fixtures\Bundles\YamlBundle\E
class Test
{
private $id;
}

View File

@ -0,0 +1,5 @@
DoctrineBundle\Tests\DependencyInjection\Fixtures\Bundles\YamlBundle\Entity\Test:
type: entity
id:
id:
type: integer

View File

@ -12,6 +12,10 @@
namespace Symfony\Bundle\DoctrineBundle\Tests;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class TestCase extends \PHPUnit_Framework_TestCase
{
@ -42,4 +46,35 @@ class TestCase extends \PHPUnit_Framework_TestCase
return EntityManager::create($params, $config);
}
public function createYamlBundleTestContainer()
{
$container = new ContainerBuilder(new ParameterBag(array(
'kernel.bundles' => array('YamlBundle' => 'DoctrineBundle\Tests\DependencyInjection\Fixtures\Bundles\YamlBundle\YamlBundle'),
'kernel.cache_dir' => sys_get_temp_dir(),
'kernel.root_dir' => __DIR__ . "/../../../../" // src dir
)));
$loader = new DoctrineExtension();
$container->registerExtension($loader);
$loader->dbalLoad(array(array(
'connections' => array(
'default' => array(
'driver' => 'pdo_mysql',
'charset' => 'UTF-8',
'platform-service' => 'my.platform',
)
)
)), $container);
$loader->ormLoad(array(
array(
'mappings' => array('YamlBundle' => array(
'type' => 'yml',
'dir' => __DIR__ . "/DependencyInjection/Fixtures/Bundles/YamlBundle/Resources/config/doctrine/metadata/orm",
'prefix' => 'DoctrineBundle\Tests\DependencyInjection\Fixtures\Bundles\YamlBundle',
)))), $container);
$container->setDefinition('my.platform', new \Symfony\Component\DependencyInjection\Definition('Doctrine\DBAL\Platforms\MySqlPlatform'));
return $container;
}
}