AbstractDriverTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\ORM\Mapping\Symfony;
  20. /**
  21. * @group DDC-1418
  22. */
  23. abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
  24. {
  25. public function testFindMappingFile()
  26. {
  27. $driver = $this->getDriver(array(
  28. 'MyNamespace\MySubnamespace\EntityFoo' => 'foo',
  29. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  30. ));
  31. touch($filename = $this->dir.'/Foo'.$this->getFileExtension());
  32. $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo')));
  33. }
  34. public function testFindMappingFileInSubnamespace()
  35. {
  36. $driver = $this->getDriver(array(
  37. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  38. ));
  39. touch($filename = $this->dir.'/Foo.Bar'.$this->getFileExtension());
  40. $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo\Bar')));
  41. }
  42. public function testFindMappingFileNamespacedFoundFileNotFound()
  43. {
  44. $this->setExpectedException(
  45. 'Doctrine\ORM\Mapping\MappingException',
  46. "No mapping file found named '".$this->dir."/Foo".$this->getFileExtension()."' for class 'MyNamespace\MySubnamespace\Entity\Foo'."
  47. );
  48. $driver = $this->getDriver(array(
  49. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  50. ));
  51. $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo'));
  52. }
  53. public function testFindMappingNamespaceNotFound()
  54. {
  55. $this->setExpectedException(
  56. 'Doctrine\ORM\Mapping\MappingException',
  57. "No mapping file found named 'Foo".$this->getFileExtension()."' for class 'MyOtherNamespace\MySubnamespace\Entity\Foo'."
  58. );
  59. $driver = $this->getDriver(array(
  60. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  61. ));
  62. $this->invoke($driver, '_findMappingFile', array('MyOtherNamespace\MySubnamespace\Entity\Foo'));
  63. }
  64. protected function setUp()
  65. {
  66. $this->dir = sys_get_temp_dir().'/abstract_driver_test';
  67. @mkdir($this->dir, 0777, true);
  68. }
  69. protected function tearDown()
  70. {
  71. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dir), \RecursiveIteratorIterator::CHILD_FIRST);
  72. foreach ($iterator as $path) {
  73. if ($path->isDir()) {
  74. @rmdir($path);
  75. } else {
  76. @unlink($path);
  77. }
  78. }
  79. @rmdir($this->dir);
  80. }
  81. abstract protected function getFileExtension();
  82. abstract protected function getDriver(array $paths = array());
  83. private function setField($obj, $field, $value)
  84. {
  85. $ref = new \ReflectionProperty($obj, $field);
  86. $ref->setAccessible(true);
  87. $ref->setValue($obj, $value);
  88. }
  89. private function invoke($obj, $method, array $args = array()) {
  90. $ref = new \ReflectionMethod($obj, $method);
  91. $ref->setAccessible(true);
  92. return $ref->invokeArgs($obj, $args);
  93. }
  94. }