Bladeren bron

[mapping] more metadata factory tests, references #134

gediminasm 13 jaren geleden
bovenliggende
commit
ff51d6bd3d

+ 0 - 85
tests/Gedmo/Mapping/CompatibilityMappingTest.php

@@ -1,85 +0,0 @@
-<?php
-
-namespace Gedmo\Mapping;
-
-use Mapping\Fixture\Compatibility\Article;
-use Doctrine\Common\Annotations\AnnotationReader;
-use Doctrine\Common\Annotations\CachedReader;
-use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
-use Doctrine\Common\Cache\ArrayCache;
-
-/**
- * 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 CompatibilityMappingTest extends \PHPUnit_Framework_TestCase
-{
-    const ARTICLE = "Mapping\Fixture\Compatibility\Article";
-
-    private $em;
-    private $timestampable;
-
-    public function setUp()
-    {
-        if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0RC4-DEV', '>=')) {
-            $this->markTestSkipped('Doctrine common is 2.1.0RC4-DEV version, skipping.');
-        } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) {
-            $reader = new AnnotationReader();
-            $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
-            $reader->setIgnoreNotImportedAnnotations(true);
-            $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo');
-            $reader->setEnableParsePhpImports(false);
-            $reader->setAutoloadAnnotations(true);
-            $reader = new CachedReader(
-                new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
-            );
-        } else {
-            $reader = new AnnotationReader();
-            $reader->setAutoloadAnnotations(true);
-            $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo');
-            $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
-        }
-        $config = new \Doctrine\ORM\Configuration();
-        $config->setProxyDir(TESTS_TEMP_DIR);
-        $config->setProxyNamespace('Gedmo\Mapping\Proxy');
-        $config->setMetadataDriverImpl(new AnnotationDriver($reader));
-
-        $conn = array(
-            'driver' => 'pdo_sqlite',
-            'memory' => true,
-        );
-
-        //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
-
-        $evm = new \Doctrine\Common\EventManager();
-        $this->timestampable = new \Gedmo\Timestampable\TimestampableListener();
-        $evm->addEventSubscriber($this->timestampable);
-        $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
-
-        $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
-        $schemaTool->dropSchema(array());
-        $schemaTool->createSchema(array(
-            $this->em->getClassMetadata(self::ARTICLE)
-        ));
-    }
-
-    public function testNoCacheImplementationMapping()
-    {
-        $test = new Article;
-        $test->setTitle('test');
-        $this->em->persist($test);
-        $this->em->flush();
-        // assertion checks if configuration is read correctly without cache driver
-        $conf = $this->timestampable->getConfiguration(
-            $this->em,
-            self::ARTICLE
-        );
-        $this->assertArrayHasKey('create', $conf);
-        $this->assertArrayHasKey('update', $conf);
-
-    }
-}

+ 1 - 1
tests/Gedmo/Mapping/FactoryMetadataTest.php

@@ -12,7 +12,7 @@ use Mapping\Fixture\Unmapped\Timestampable;
 * @link http://www.gediminasm.org
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
-class FactoryMetadataTest extends \PHPUnit_Framework_TestCase
+class CustomDriverTest extends \PHPUnit_Framework_TestCase
 {
     public function setUp()
     {

+ 99 - 0
tests/Gedmo/Mapping/MetadataFactory/ForcedMetadataTest.php

@@ -0,0 +1,99 @@
+<?php
+
+use Doctrine\ORM\Mapping\ClassMetadata;
+use Mapping\Fixture\Unmapped\Timestampable;
+
+/**
+* These are mapping tests for tree extension
+*
+* @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 ForcedMetadataTest extends \PHPUnit_Framework_TestCase
+{
+    public function setUp()
+    {
+        $config = new \Doctrine\ORM\Configuration();
+        $config->setProxyDir(TESTS_TEMP_DIR);
+        $config->setProxyNamespace('Gedmo\Mapping\Proxy');
+        $config->setMetadataDriverImpl(
+            new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($_ENV['annotation_reader'])
+        );
+
+        $conn = array(
+            'driver' => 'pdo_sqlite',
+            'memory' => true,
+        );
+
+        $evm = new \Doctrine\Common\EventManager();
+        $this->timestampable = new \Gedmo\Timestampable\TimestampableListener();
+        $this->timestampable->setAnnotationReader($_ENV['annotation_reader']);
+        $evm->addEventSubscriber($this->timestampable);
+        $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
+    }
+
+    private function prepare()
+    {
+        $cmf = $this->em->getMetadataFactory();
+        $metadata = new ClassMetadata('Mapping\Fixture\Unmapped\Timestampable');
+        $id = array();
+        $id['fieldName'] = 'id';
+        $id['type'] = 'integer';
+        $id['nullable'] = false;
+        $id['columnName'] = 'id';
+        $id['id'] = true;
+
+        $metadata->mapField($id);
+
+        $created = array();
+        $created['fieldName'] = 'created';
+        $created['type'] = 'datetime';
+        $created['nullable'] = false;
+        $created['columnName'] = 'created';
+
+        $metadata->mapField($created);
+        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
+        $metadata->setIdGenerator(new \Doctrine\ORM\Id\IdentityGenerator(null));
+        $cmf->setMetadataFor('Mapping\Fixture\Unmapped\Timestampable', $metadata);
+
+        // trigger loadClassMetadata event
+        $evm = $this->em->getEventManager();
+        $eventArgs = new \Doctrine\ORM\Event\LoadClassMetadataEventArgs($metadata, $this->em);
+        $evm->dispatchEvent(\Doctrine\ORM\Events::loadClassMetadata, $eventArgs);
+
+        $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
+        $schemaTool->dropSchema(array());
+        $schemaTool->createSchema(array(
+            $this->em->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable'),
+        ));
+    }
+
+    /**
+     * @test
+     */
+    public function shouldWork()
+    {
+        $this->prepare();
+
+        $meta = $this->em->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable');
+        // driver falls back to annotation driver
+        $conf = $this->timestampable->getConfiguration(
+            $this->em,
+            'Mapping\Fixture\Unmapped\Timestampable'
+        );
+        $this->assertTrue(isset($conf['create']));
+
+        $test = new Timestampable;
+        $this->em->persist($test);
+        $this->em->flush();
+
+        $id = $this->em
+            ->getClassMetadata('Mapping\Fixture\Unmapped\Timestampable')
+            ->getReflectionProperty('id')
+            ->getValue($test)
+        ;
+        $this->assertFalse(empty($id));
+    }
+}