Просмотр исходного кода

[tests] multimanager test case when using few managers

Gediminas Morkevicius 14 лет назад
Родитель
Сommit
e2f91f4e58

+ 4 - 4
lib/Gedmo/Mapping/MappedEventSubscriber.php

@@ -105,14 +105,14 @@ abstract class MappedEventSubscriber implements EventSubscriber
      */
     public function getExtensionMetadataFactory(ObjectManager $objectManager)
     {
-        $class = get_class($objectManager);
-        if (!isset($this->extensionMetadataFactory[$class])) {
-            $this->extensionMetadataFactory[$class] = new ExtensionMetadataFactory(
+        $oid = spl_object_hash($objectManager);
+        if (!isset($this->extensionMetadataFactory[$oid])) {
+            $this->extensionMetadataFactory[$oid] = new ExtensionMetadataFactory(
                 $objectManager,
                 $this->getNamespace()
             );
         }
-        return $this->extensionMetadataFactory[$class];
+        return $this->extensionMetadataFactory[$oid];
     }
 
     /**

+ 1 - 1
tests/Gedmo/Mapping/Fixture/Yaml/User.php

@@ -10,7 +10,7 @@ class User
 
     private $username;
 
-    private $locale;
+    private $localeField;
     /**
      * Get id
      *

+ 105 - 0
tests/Gedmo/Mapping/MultiManagerMappingTest.php

@@ -0,0 +1,105 @@
+<?php
+
+namespace Gedmo\Mapping;
+
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\ORM\Mapping\Driver\DriverChain;
+use Doctrine\ORM\Mapping\Driver\YamlDriver;
+use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
+use Gedmo\Mapping\ExtensionMetadataFactory;
+use Tool\BaseTestCaseOM;
+
+/**
+ * These are mapping extension tests
+ *
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Mapping
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class MultiManagerMappingTest extends BaseTestCaseOM
+{
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
+    private $em1;
+
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
+    private $em2;
+
+    /**
+     * @var Doctrine\ODM\MongoDB\DocumentManager
+     */
+    private $dm1;
+
+    public function setUp()
+    {
+        parent::setUp();
+        // EM with standard annotation mapping
+        $this->em1 = $this->getMockSqliteEntityManager(array(
+            'Sluggable\Fixture\Article'
+        ));
+        // EM with yaml and annotation mapping
+        $reader = new AnnotationReader();
+        $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
+        $annotationDriver = new AnnotationDriver($reader);
+
+        $reader = new AnnotationReader();
+        $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
+        $annotationDriver2 = new AnnotationDriver($reader);
+
+        $yamlDriver = new YamlDriver(__DIR__.'/Driver/Yaml');
+
+        $chain = new DriverChain;
+        $chain->addDriver($annotationDriver, 'Translatable\Fixture');
+        $chain->addDriver($yamlDriver, 'Mapping\Fixture\Yaml');
+        $chain->addDriver($annotationDriver2, 'Gedmo\Translatable');
+
+        $this->em2 = $this->getMockSqliteEntityManager(array(
+            'Translatable\Fixture\PersonTranslation',
+            'Mapping\Fixture\Yaml\User',
+        ), $chain);
+        // DM with standard annotation mapping
+        $this->dm1 = $this->getMockDocumentManager('gedmo_extensions_test');
+    }
+
+    public function testTwoDiferentManager()
+    {
+        $meta = $this->dm1->getClassMetadata('Sluggable\Fixture\Document\Article');
+        $dmArticle = new \Sluggable\Fixture\Document\Article;
+        $dmArticle->setCode('code');
+        $dmArticle->setTitle('title');
+        $this->dm1->persist($dmArticle);
+        $this->dm1->flush();
+
+        $this->assertEquals('title-code', $dmArticle->getSlug());
+        $em1Article = new \Sluggable\Fixture\Article;
+        $em1Article->setCode('code');
+        $em1Article->setTitle('title');
+        $this->em1->persist($em1Article);
+        $this->em1->flush();
+
+        $this->assertEquals('title-code', $em1Article->getSlug());
+    }
+
+    public function testTwoSameManagers()
+    {
+        $em1Article = new \Sluggable\Fixture\Article;
+        $em1Article->setCode('code');
+        $em1Article->setTitle('title');
+        $this->em1->persist($em1Article);
+        $this->em1->flush();
+
+        $this->assertEquals('title-code', $em1Article->getSlug());
+
+        $user = new \Mapping\Fixture\Yaml\User;
+        $user->setUsername('user');
+        $user->setPassword('secret');
+        $this->em2->persist($user);
+        $this->em2->flush();
+
+        $this->assertEquals(1, $user->getId());
+    }
+}

+ 285 - 0
tests/Gedmo/Tool/BaseTestCaseOM.php

@@ -0,0 +1,285 @@
+<?php
+
+namespace Tool;
+
+// common
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\Common\EventManager;
+// orm specific
+use Doctrine\ORM\Mapping\Driver\Driver as MappingDriverORM;
+use Doctrine\ORM\Mapping\Driver\AnnotationDriver as AnnotationDriverORM;
+use Doctrine\ORM\EntityManager;
+use Doctrine\ORM\Tools\SchemaTool;
+// odm specific
+use Doctrine\ODM\MongoDB\Mapping\Driver\Driver as MappingDriverODM;
+use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver as AnnotationDriverODM;
+use Doctrine\ODM\MongoDB\DocumentManager;
+use Doctrine\MongoDB\Connection;
+// listeners
+use Gedmo\Translatable\TranslationListener;
+use Gedmo\Sluggable\SluggableListener;
+use Gedmo\Tree\TreeListener;
+use Gedmo\Timestampable\TimestampableListener;
+use Gedmo\Loggable\LoggableListener;
+
+/**
+ * Base test case contains common mock objects
+ * generation methods for multi object manager
+ * test cases
+ *
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo
+ * @subpackage BaseTestCase
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+abstract class BaseTestCaseOM extends \PHPUnit_Framework_TestCase
+{
+    /**
+     * @var EventManager
+     */
+    protected $evm;
+
+    /**
+     * Initialized document managers
+     *
+     * @var array
+     */
+    private $dms = array();
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function setUp()
+    {
+        if (!class_exists('Mongo')) {
+            $this->markTestSkipped('Missing Mongo extension.');
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function tearDown()
+    {
+        foreach ($this->dms as $dm) {
+            if ($dm) {
+                foreach ($dm->getDocumentDatabases() as $db) {
+                    foreach ($db->listCollections() as $collection) {
+                        $collection->drop();
+                    }
+                }
+                $dm->getConnection()->close();
+                $dm = null;
+            }
+        }
+    }
+
+	/**
+     * DocumentManager mock object together with
+     * annotation mapping driver and database
+     *
+     * @param string $dbName
+     * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
+     * @return DocumentManager
+     */
+    protected function getMockDocumentManager($dbName, MappingDriverODM $mappingDriver = null)
+    {
+        $conn = new Connection;
+        $config = $this->getMockAnnotatedODMMongoDBConfig($dbName, $mappingDriver);
+
+        $dm = null;
+        try {
+            $dm = DocumentManager::create($conn, $config, $this->getEventManager());
+            $dm->getConnection()->connect();
+        } catch (\MongoException $e) {
+            $this->markTestSkipped('Doctrine MongoDB ODM failed to connect');
+        }
+        return $dm;
+    }
+
+    /**
+     * DocumentManager mock object with
+     * annotation mapping driver
+     *
+     * @param string $dbName
+     * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
+     * @return DocumentManager
+     */
+    protected function getMockMappedDocumentManager($dbName, MappingDriverODM $mappingDriver = null)
+    {
+        $conn = $this->getMock('Doctrine\\MongoDB\\Connection');
+        $config = $this->getMockAnnotatedODMMongoDBConfig($dbName, $mappingDriver);
+
+        $dm = DocumentManager::create($conn, $config, $this->getEventManager());
+        return $dm;
+    }
+
+    /**
+     * EntityManager mock object together with
+     * annotation mapping driver and pdo_sqlite
+     * database in memory
+     *
+     * @param array $fixtures
+     * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
+     * @return EntityManager
+     */
+    protected function getMockSqliteEntityManager(array $fixtures, MappingDriverORM $mappingDriver = null)
+    {
+        $conn = array(
+            'driver' => 'pdo_sqlite',
+            'memory' => true,
+        );
+
+        $config = $this->getMockAnnotatedORMConfig($mappingDriver);
+        $em = EntityManager::create($conn, $config, $this->getEventManager());
+
+        $schema = array_map(function($class) use ($em) {
+            return $em->getClassMetadata($class);
+        }, $fixtures);
+
+        $schemaTool = new SchemaTool($em);
+        $schemaTool->dropSchema(array());
+        $schemaTool->createSchema($schema);
+        return $em;
+    }
+
+    /**
+     * EntityManager mock object with
+     * annotation mapping driver
+     *
+     * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
+     * @return EntityManager
+     */
+    protected function getMockMappedEntityManager(MappingDriverORM $mappingDriver = null)
+    {
+        $driver = $this->getMock('Doctrine\DBAL\Driver');
+        $driver->expects($this->once())
+            ->method('getDatabasePlatform')
+            ->will($this->returnValue($this->getMock('Doctrine\DBAL\Platforms\MySqlPlatform')));
+
+        $conn = $this->getMock('Doctrine\DBAL\Connection', array(), array(array(), $driver));
+        $conn->expects($this->once())
+            ->method('getEventManager')
+            ->will($this->returnValue($this->getEventManager()));
+
+        $config = $this->getMockAnnotatedORMConfig($mappingDriver);
+        $em = EntityManager::create($conn, $config);
+        return $em;
+    }
+
+    /**
+     * Build event manager
+     *
+     * @return EventManager
+     */
+    private function getEventManager()
+    {
+        if (is_null($this->evm)) {
+            $this->evm = new EventManager;
+            $this->evm->addEventSubscriber(new TreeListener);
+            $this->evm->addEventSubscriber(new SluggableListener);
+            $this->evm->addEventSubscriber(new LoggableListener);
+            $this->evm->addEventSubscriber(new TranslationListener);
+            $this->evm->addEventSubscriber(new TimestampableListener);
+        }
+        return $this->evm;
+    }
+
+    /**
+     * Get annotation mapping configuration
+     *
+     * @param string $dbName
+     * @param Doctrine\ODM\MongoDB\Mapping\Driver\Driver $mappingDriver
+     * @return Doctrine\ORM\Configuration
+     */
+    private function getMockAnnotatedODMMongoDBConfig($dbName, MappingDriverODM $mappingDriver = null)
+    {
+        $config = $this->getMock('Doctrine\\ODM\\MongoDB\\Configuration');
+        $config->expects($this->once())
+            ->method('getProxyDir')
+            ->will($this->returnValue(__DIR__.'/../../temp'));
+
+        $config->expects($this->once())
+            ->method('getProxyNamespace')
+            ->will($this->returnValue('Proxy'));
+
+        $config->expects($this->once())
+            ->method('getHydratorDir')
+            ->will($this->returnValue(__DIR__.'/../../temp'));
+
+        $config->expects($this->once())
+            ->method('getHydratorNamespace')
+            ->will($this->returnValue('Hydrator'));
+
+        $config->expects($this->any())
+            ->method('getDefaultDB')
+            ->will($this->returnValue($dbName));
+
+        $config->expects($this->once())
+            ->method('getAutoGenerateProxyClasses')
+            ->will($this->returnValue(true));
+
+        $config->expects($this->once())
+            ->method('getAutoGenerateHydratorClasses')
+            ->will($this->returnValue(true));
+
+        $config->expects($this->once())
+            ->method('getClassMetadataFactoryName')
+            ->will($this->returnValue('Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadataFactory'));
+
+        $config->expects($this->any())
+            ->method('getMongoCmd')
+            ->will($this->returnValue('$'));
+
+        if (is_null($mappingDriver)) {
+            $reader = new AnnotationReader();
+            $reader->setDefaultAnnotationNamespace('Doctrine\\ODM\\MongoDB\\Mapping\\');
+            $mappingDriver = new AnnotationDriverODM($reader);
+        }
+
+        $config->expects($this->any())
+            ->method('getMetadataDriverImpl')
+            ->will($this->returnValue($mappingDriver));
+
+        return $config;
+    }
+
+    /**
+     * Get annotation mapping configuration for ORM
+     *
+     * @param Doctrine\ORM\Mapping\Driver\Driver $mappingDriver
+     * @return Doctrine\ORM\Configuration
+     */
+    private function getMockAnnotatedORMConfig(MappingDriverORM $mappingDriver = null)
+    {
+        $config = $this->getMock('Doctrine\ORM\Configuration');
+        $config->expects($this->once())
+            ->method('getProxyDir')
+            ->will($this->returnValue(__DIR__.'/../../temp'));
+
+        $config->expects($this->once())
+            ->method('getProxyNamespace')
+            ->will($this->returnValue('Proxy'));
+
+        $config->expects($this->once())
+            ->method('getAutoGenerateProxyClasses')
+            ->will($this->returnValue(true));
+
+        $config->expects($this->once())
+            ->method('getClassMetadataFactoryName')
+            ->will($this->returnValue('Doctrine\\ORM\\Mapping\\ClassMetadataFactory'));
+
+        if (is_null($mappingDriver)) {
+            $reader = new AnnotationReader();
+            $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
+            $mappingDriver = new AnnotationDriverORM($reader);
+        }
+
+        $config->expects($this->any())
+            ->method('getMetadataDriverImpl')
+            ->will($this->returnValue($mappingDriver));
+
+        return $config;
+    }
+}