Przeglądaj źródła

[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.

Benjamin Eberlei 14 lat temu
rodzic
commit
682e83585b

+ 4 - 0
src/Symfony/Bundle/DoctrineAbstractBundle/DependencyInjection/AbstractDoctrineExtension.php

@@ -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. " .

+ 75 - 0
src/Symfony/Bundle/DoctrineBundle/Command/InfoDoctrineCommand.php

@@ -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);
+            }
+        }
+    }
+}

+ 40 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/Command/InfoDoctrineCommandTest.php

@@ -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);
+    }
+}

+ 1 - 30
src/Symfony/Bundle/DoctrineBundle/Tests/ContainerTest.php

@@ -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'));

+ 1 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/Bundles/YamlBundle/Entity/Test.php

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

+ 5 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/Bundles/YamlBundle/Resources/config/doctrine/metadata/orm/DoctrineBundle.Tests.DependencyInjection.Fixtures.Bundles.YamlBundle.Entity.Test.dcm.yml

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

+ 0 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/Fixtures/Bundles/YamlBundle/Resources/config/doctrine/metadata/orm/Fixtures.Bundles.YamlBundle.Entity.Test.yml


+ 35 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/TestCase.php

@@ -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;
+    }
 }