AbstractDriverTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Tests\Bridge\Doctrine\Mapping\Driver;
  11. abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testFindMappingFile()
  14. {
  15. $driver = $this->getDriver(array(
  16. 'MyNamespace\MySubnamespace\EntityFoo' => 'foo',
  17. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  18. ));
  19. touch($filename = $this->dir.'/Foo'.$this->getFileExtension());
  20. $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo')));
  21. }
  22. public function testFindMappingFileInSubnamespace()
  23. {
  24. $driver = $this->getDriver(array(
  25. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  26. ));
  27. touch($filename = $this->dir.'/Foo.Bar'.$this->getFileExtension());
  28. $this->assertEquals($filename, $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo\Bar')));
  29. }
  30. public function testFindMappingFileNamespacedFoundFileNotFound()
  31. {
  32. $this->setExpectedException(
  33. 'Doctrine\ORM\Mapping\MappingException',
  34. "No mapping file found named '".$this->dir."/Foo".$this->getFileExtension()."' for class 'MyNamespace\MySubnamespace\Entity\Foo'."
  35. );
  36. $driver = $this->getDriver(array(
  37. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  38. ));
  39. $this->invoke($driver, '_findMappingFile', array('MyNamespace\MySubnamespace\Entity\Foo'));
  40. }
  41. public function testFindMappingNamespaceNotFound()
  42. {
  43. $this->setExpectedException(
  44. 'Doctrine\ORM\Mapping\MappingException',
  45. "No mapping file found named 'Foo".$this->getFileExtension()."' for class 'MyOtherNamespace\MySubnamespace\Entity\Foo'."
  46. );
  47. $driver = $this->getDriver(array(
  48. 'MyNamespace\MySubnamespace\Entity' => $this->dir,
  49. ));
  50. $this->invoke($driver, '_findMappingFile', array('MyOtherNamespace\MySubnamespace\Entity\Foo'));
  51. }
  52. protected function setUp()
  53. {
  54. if (!class_exists('Doctrine\\Common\\Version')) {
  55. $this->markTestSkipped('Doctrine is not available.');
  56. }
  57. $this->dir = sys_get_temp_dir().'/abstract_driver_test';
  58. @mkdir($this->dir, 0777, true);
  59. }
  60. protected function tearDown()
  61. {
  62. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dir), \RecursiveIteratorIterator::CHILD_FIRST);
  63. foreach ($iterator as $path) {
  64. if ($path->isDir()) {
  65. @rmdir($path);
  66. } else {
  67. @unlink($path);
  68. }
  69. }
  70. @rmdir($this->dir);
  71. }
  72. abstract protected function getFileExtension();
  73. abstract protected function getDriver(array $paths = array());
  74. private function setField($obj, $field, $value)
  75. {
  76. $ref = new \ReflectionProperty($obj, $field);
  77. $ref->setAccessible(true);
  78. $ref->setValue($obj, $value);
  79. }
  80. private function invoke($obj, $method, array $args = array())
  81. {
  82. $ref = new \ReflectionMethod($obj, $method);
  83. $ref->setAccessible(true);
  84. return $ref->invokeArgs($obj, $args);
  85. }
  86. }