浏览代码

[DoctrineBundle] added some tests

Johannes Schmitt 14 年之前
父节点
当前提交
b70d7a7462

+ 5 - 3
src/Symfony/Bundle/DoctrineBundle/Mapping/Driver/XmlDriver.php

@@ -69,12 +69,14 @@ class XmlDriver extends BaseXmlDriver
                 continue;
             }
 
-            $subPath = strtr(substr($className, strlen($prefix)+1), '\\', '.');
-            if (file_exists($filename = $path.'/'.$subPath.$this->fileExtension)) {
+            $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
+            if (file_exists($filename)) {
                 return $filename;
             }
+
+            throw MappingException::mappingFileNotFound($className, $filename);
         }
 
-        throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\')).$this->_fileExtension);
+        throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->_fileExtension);
     }
 }

+ 5 - 3
src/Symfony/Bundle/DoctrineBundle/Mapping/Driver/YamlDriver.php

@@ -69,12 +69,14 @@ class YamlDriver extends BaseYamlDriver
                 continue;
             }
 
-            $subPath = strtr(substr($className, strlen($prefix)+1), '\\', '.');
-            if (file_exists($filename = $path.'/'.$subPath.$this->fileExtension)) {
+            $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
+            if (file_exists($filename)) {
                 return $filename;
             }
+
+            throw MappingException::mappingFileNotFound($className, $filename);
         }
 
-        throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\')).$this->_fileExtension);
+        throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->_fileExtension);
     }
 }

+ 93 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/Mapping/Driver/AbstractDriverTest.php

@@ -0,0 +1,93 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineBundle\Tests\Mapping\Driver;
+
+abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
+{
+    public function testFindMappingFile()
+    {
+        $driver = $this->getDriver(array(
+            'MyNamespace\MyBundle\EntityFoo' => 'foo',
+            'MyNamespace\MyBundle\Entity' => $this->dir,
+        ));
+
+        touch($filename = $this->dir.'/Foo'.$this->getFileExtension());
+        $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MyBundle\Entity\Foo')));
+    }
+
+    public function testFindMappingFileInSubnamespace()
+    {
+        $driver = $this->getDriver(array(
+            'MyNamespace\MyBundle\Entity' => $this->dir,
+        ));
+
+        touch($filename = $this->dir.'/Foo.Bar'.$this->getFileExtension());
+        $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MyBundle\Entity\Foo\Bar')));
+    }
+
+    public function testFindMappingFileNamespacedFoundFileNotFound()
+    {
+        $this->setExpectedException(
+            'Doctrine\ORM\Mapping\MappingException',
+            "No mapping file found named '".$this->dir."/Foo".$this->getFileExtension()."' for class 'MyNamespace\MyBundle\Entity\Foo'."
+        );
+
+        $driver = $this->getDriver(array(
+            'MyNamespace\MyBundle\Entity' => $this->dir,
+        ));
+
+        $this->invoke($driver, '_findMappingFile', array('MyNamespace\MyBundle\Entity\Foo'));
+    }
+
+    public function testFindMappingNamespaceNotFound()
+    {
+        $this->setExpectedException(
+            'Doctrine\ORM\Mapping\MappingException',
+            "No mapping file found named 'Foo".$this->getFileExtension()."' for class 'MyOtherNamespace\MyBundle\Entity\Foo'."
+        );
+
+        $driver = $this->getDriver(array(
+            'MyNamespace\MyBundle\Entity' => $this->dir,
+        ));
+
+        $this->invoke($driver, '_findMappingFile', array('MyOtherNamespace\MyBundle\Entity\Foo'));
+    }
+
+    protected function setUp()
+    {
+        $this->dir = sys_get_temp_dir().'/abstract_driver_test';
+        @mkdir($this->dir, 0777, true);
+    }
+
+    protected function tearDown()
+    {
+        $iter = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dir), \RecursiveIteratorIterator::CHILD_FIRST);
+
+        foreach ($iter as $path) {
+            if ($path->isDir()) {
+                @rmdir($path);
+            } else {
+                @unlink($path);
+            }
+        }
+
+        @rmdir($this->dir);
+    }
+
+    abstract protected function getFileExtension();
+    abstract protected function getDriver(array $paths = array());
+
+    private function setField($obj, $field, $value)
+    {
+        $ref = new \ReflectionProperty($obj, $field);
+        $ref->setAccessible(true);
+        $ref->setValue($obj, $value);
+    }
+
+    private function invoke($obj, $method, array $args = array()) {
+        $ref = new \ReflectionMethod($obj, $method);
+        $ref->setAccessible(true);
+
+        return $ref->invokeArgs($obj, $args);
+    }
+}

+ 18 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/Mapping/Driver/XmlDriverTest.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineBundle\Tests\Mapping\Driver;
+
+use Symfony\Bundle\DoctrineBundle\Mapping\Driver\XmlDriver;
+
+class XmlDriverTest extends AbstractDriverTest
+{
+    protected function getFileExtension()
+    {
+        return '.orm.xml';
+    }
+
+    protected function getDriver(array $paths = array())
+    {
+        return new XmlDriver($paths);
+    }
+}

+ 18 - 0
src/Symfony/Bundle/DoctrineBundle/Tests/Mapping/Driver/YamlDriverTest.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Symfony\Bundle\DoctrineBundle\Tests\Mapping\Driver;
+
+use Symfony\Bundle\DoctrineBundle\Mapping\Driver\YamlDriver;
+
+class YamlDriverTest extends AbstractDriverTest
+{
+    protected function getFileExtension()
+    {
+        return '.orm.yml';
+    }
+
+    protected function getDriver(array $paths = array())
+    {
+        return new YamlDriver($paths);
+    }
+}