PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Doctrine/Tests/ORM/ConfigurationTest.php

https://bitbucket.org/aagraz/doctrine2
PHP | 288 lines | 232 code | 44 blank | 12 comment | 0 complexity | bba11973021a3d431d6cc7c2e707ee27 MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\ORM;
  3. use Doctrine\ORM\Mapping as AnnotationNamespace;
  4. use Doctrine\ORM\Configuration;
  5. use Doctrine\ORM\ORMException;
  6. use ReflectionClass;
  7. use PHPUnit_Framework_TestCase;
  8. require_once __DIR__ . '/../TestInit.php';
  9. /**
  10. * Tests for the Configuration object
  11. * @author Marco Pivetta <ocramius@gmail.com>
  12. */
  13. class ConfigurationTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @var Configuration
  17. */
  18. private $configuration;
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $this->configuration = new Configuration();
  23. }
  24. public function testSetGetProxyDir()
  25. {
  26. $this->assertSame(null, $this->configuration->getProxyDir()); // defaults
  27. $this->configuration->setProxyDir(__DIR__);
  28. $this->assertSame(__DIR__, $this->configuration->getProxyDir());
  29. }
  30. public function testSetGetAutoGenerateProxyClasses()
  31. {
  32. $this->assertSame(true, $this->configuration->getAutoGenerateProxyClasses()); // defaults
  33. $this->configuration->setAutoGenerateProxyClasses(false);
  34. $this->assertSame(false, $this->configuration->getAutoGenerateProxyClasses());
  35. }
  36. public function testSetGetProxyNamespace()
  37. {
  38. $this->assertSame(null, $this->configuration->getProxyNamespace()); // defaults
  39. $this->configuration->setProxyNamespace(__NAMESPACE__);
  40. $this->assertSame(__NAMESPACE__, $this->configuration->getProxyNamespace());
  41. }
  42. public function testSetGetMetadataDriverImpl()
  43. {
  44. $this->assertSame(null, $this->configuration->getMetadataDriverImpl()); // defaults
  45. $metadataDriver = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
  46. $this->configuration->setMetadataDriverImpl($metadataDriver);
  47. $this->assertSame($metadataDriver, $this->configuration->getMetadataDriverImpl());
  48. }
  49. public function testNewDefaultAnnotationDriver()
  50. {
  51. $paths = array(__DIR__);
  52. $reflectionClass = new ReflectionClass(__NAMESPACE__ . '\ConfigurationTestAnnotationReaderChecker');
  53. $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths, false);
  54. $reader = $annotationDriver->getReader();
  55. $annotation = $reader->getMethodAnnotation(
  56. $reflectionClass->getMethod('namespacedAnnotationMethod'),
  57. 'Doctrine\ORM\Mapping\PrePersist'
  58. );
  59. $this->assertInstanceOf('Doctrine\ORM\Mapping\PrePersist', $annotation);
  60. $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths);
  61. $reader = $annotationDriver->getReader();
  62. $annotation = $reader->getMethodAnnotation(
  63. $reflectionClass->getMethod('simpleAnnotationMethod'),
  64. 'Doctrine\ORM\Mapping\PrePersist'
  65. );
  66. $this->assertInstanceOf('Doctrine\ORM\Mapping\PrePersist', $annotation);
  67. }
  68. public function testSetGetEntityNamespace()
  69. {
  70. $this->configuration->addEntityNamespace('TestNamespace', __NAMESPACE__);
  71. $this->assertSame(__NAMESPACE__, $this->configuration->getEntityNamespace('TestNamespace'));
  72. $namespaces = array('OtherNamespace' => __NAMESPACE__);
  73. $this->configuration->setEntityNamespaces($namespaces);
  74. $this->assertSame($namespaces, $this->configuration->getEntityNamespaces());
  75. $this->setExpectedException('Doctrine\ORM\ORMException');
  76. $this->configuration->getEntityNamespace('NonExistingNamespace');
  77. }
  78. public function testSetGetQueryCacheImpl()
  79. {
  80. $this->assertSame(null, $this->configuration->getQueryCacheImpl()); // defaults
  81. $queryCacheImpl = $this->getMock('Doctrine\Common\Cache\Cache');
  82. $this->configuration->setQueryCacheImpl($queryCacheImpl);
  83. $this->assertSame($queryCacheImpl, $this->configuration->getQueryCacheImpl());
  84. }
  85. public function testSetGetHydrationCacheImpl()
  86. {
  87. $this->assertSame(null, $this->configuration->getHydrationCacheImpl()); // defaults
  88. $queryCacheImpl = $this->getMock('Doctrine\Common\Cache\Cache');
  89. $this->configuration->setHydrationCacheImpl($queryCacheImpl);
  90. $this->assertSame($queryCacheImpl, $this->configuration->getHydrationCacheImpl());
  91. }
  92. public function testSetGetMetadataCacheImpl()
  93. {
  94. $this->assertSame(null, $this->configuration->getMetadataCacheImpl()); // defaults
  95. $queryCacheImpl = $this->getMock('Doctrine\Common\Cache\Cache');
  96. $this->configuration->setMetadataCacheImpl($queryCacheImpl);
  97. $this->assertSame($queryCacheImpl, $this->configuration->getMetadataCacheImpl());
  98. }
  99. public function testAddGetNamedQuery()
  100. {
  101. $dql = 'SELECT u FROM User u';
  102. $this->configuration->addNamedQuery('QueryName', $dql);
  103. $this->assertSame($dql, $this->configuration->getNamedQuery('QueryName'));
  104. $this->setExpectedException('Doctrine\ORM\ORMException');
  105. $this->configuration->getNamedQuery('NonExistingQuery');
  106. }
  107. public function testAddGetNamedNativeQuery()
  108. {
  109. $sql = 'SELECT * FROM user';
  110. $rsm = $this->getMock('Doctrine\ORM\Query\ResultSetMapping');
  111. $this->configuration->addNamedNativeQuery('QueryName', $sql, $rsm);
  112. $fetched = $this->configuration->getNamedNativeQuery('QueryName');
  113. $this->assertSame($sql, $fetched[0]);
  114. $this->assertSame($rsm, $fetched[1]);
  115. $this->setExpectedException('Doctrine\ORM\ORMException');
  116. $this->configuration->getNamedQuery('NonExistingQuery');
  117. }
  118. public function ensureProductionSettings()
  119. {
  120. $cache = $this->getMock('Doctrine\Common\Cache\Cache');
  121. $this->configuration->setAutoGenerateProxyClasses(true);
  122. try {
  123. $this->configuration->ensureProductionSettings();
  124. $this->fail('Didn\'t check all production settings');
  125. } catch (ORMException $e) {}
  126. $this->configuration->setQueryCacheImpl($cache);
  127. try {
  128. $this->configuration->ensureProductionSettings();
  129. $this->fail('Didn\'t check all production settings');
  130. } catch (ORMException $e) {}
  131. $this->configuration->setMetadataCacheImpl($cache);
  132. try {
  133. $this->configuration->ensureProductionSettings();
  134. $this->fail('Didn\'t check all production settings');
  135. } catch (ORMException $e) {}
  136. $this->configuration->setAutoGenerateProxyClasses(false);
  137. $this->configuration->ensureProductionSettings();
  138. }
  139. public function testAddGetCustomStringFunction()
  140. {
  141. $this->configuration->addCustomStringFunction('FunctionName', __CLASS__);
  142. $this->assertSame(__CLASS__, $this->configuration->getCustomStringFunction('FunctionName'));
  143. $this->assertSame(null, $this->configuration->getCustomStringFunction('NonExistingFunction'));
  144. $this->configuration->setCustomStringFunctions(array('OtherFunctionName' => __CLASS__));
  145. $this->assertSame(__CLASS__, $this->configuration->getCustomStringFunction('OtherFunctionName'));
  146. $this->setExpectedException('Doctrine\ORM\ORMException');
  147. $this->configuration->addCustomStringFunction('concat', __CLASS__);
  148. }
  149. public function testAddGetCustomNumericFunction()
  150. {
  151. $this->configuration->addCustomNumericFunction('FunctionName', __CLASS__);
  152. $this->assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('FunctionName'));
  153. $this->assertSame(null, $this->configuration->getCustomNumericFunction('NonExistingFunction'));
  154. $this->configuration->setCustomNumericFunctions(array('OtherFunctionName' => __CLASS__));
  155. $this->assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('OtherFunctionName'));
  156. $this->setExpectedException('Doctrine\ORM\ORMException');
  157. $this->configuration->addCustomNumericFunction('abs', __CLASS__);
  158. }
  159. public function testAddGetCustomDatetimeFunction()
  160. {
  161. $this->configuration->addCustomDatetimeFunction('FunctionName', __CLASS__);
  162. $this->assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('FunctionName'));
  163. $this->assertSame(null, $this->configuration->getCustomDatetimeFunction('NonExistingFunction'));
  164. $this->configuration->setCustomDatetimeFunctions(array('OtherFunctionName' => __CLASS__));
  165. $this->assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('OtherFunctionName'));
  166. $this->setExpectedException('Doctrine\ORM\ORMException');
  167. $this->configuration->addCustomDatetimeFunction('date_add', __CLASS__);
  168. }
  169. public function testAddGetCustomHydrationMode()
  170. {
  171. $this->assertSame(null, $this->configuration->getCustomHydrationMode('NonExisting'));
  172. $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
  173. $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
  174. }
  175. public function testSetCustomHydrationModes()
  176. {
  177. $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
  178. $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
  179. $this->configuration->setCustomHydrationModes(
  180. array(
  181. 'AnotherHydrationModeName' => __CLASS__
  182. )
  183. );
  184. $this->assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
  185. $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
  186. }
  187. public function testSetGetClassMetadataFactoryName()
  188. {
  189. $this->assertSame('Doctrine\ORM\Mapping\ClassMetadataFactory', $this->configuration->getClassMetadataFactoryName());
  190. $this->configuration->setClassMetadataFactoryName(__CLASS__);
  191. $this->assertSame(__CLASS__, $this->configuration->getClassMetadataFactoryName());
  192. }
  193. public function testAddGetFilters()
  194. {
  195. $this->assertSame(null, $this->configuration->getFilterClassName('NonExistingFilter'));
  196. $this->configuration->addFilter('FilterName', __CLASS__);
  197. $this->assertSame(__CLASS__, $this->configuration->getFilterClassName('FilterName'));
  198. }
  199. public function setDefaultRepositoryClassName()
  200. {
  201. $this->assertSame('Doctrine\ORM\EntityRepository', $this->configuration->getDefaultRepositoryClassName());
  202. $repositoryClass = 'Doctrine\Tests\Models\DDC753\DDC753CustomRepository';
  203. $this->configuration->setDefaultRepositoryClassName($repositoryClass);
  204. $this->assertSame($repositoryClass, $this->configuration->getDefaultRepositoryClassName());
  205. $this->setExpectedException('Doctrine\ORM\ORMException');
  206. $this->configuration->setDefaultRepositoryClassName(__CLASS__);
  207. }
  208. public function testSetGetNamingStrategy()
  209. {
  210. $this->assertInstanceOf('Doctrine\ORM\Mapping\NamingStrategy', $this->configuration->getNamingStrategy());
  211. $namingStrategy = $this->getMock('Doctrine\ORM\Mapping\NamingStrategy');
  212. $this->configuration->setNamingStrategy($namingStrategy);
  213. $this->assertSame($namingStrategy, $this->configuration->getNamingStrategy());
  214. }
  215. public function testSetGetQuoteStrategy()
  216. {
  217. $this->assertInstanceOf('Doctrine\ORM\Mapping\QuoteStrategy', $this->configuration->getQuoteStrategy());
  218. $quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\QuoteStrategy');
  219. $this->configuration->setQuoteStrategy($quoteStrategy);
  220. $this->assertSame($quoteStrategy, $this->configuration->getQuoteStrategy());
  221. }
  222. /**
  223. * @group DDC-1955
  224. */
  225. public function testSetGetEntityListenerResolver()
  226. {
  227. $this->assertInstanceOf('Doctrine\ORM\Mapping\EntityListenerResolver', $this->configuration->getEntityListenerResolver());
  228. $this->assertInstanceOf('Doctrine\ORM\Mapping\DefaultEntityListenerResolver', $this->configuration->getEntityListenerResolver());
  229. $resolver = $this->getMock('Doctrine\ORM\Mapping\EntityListenerResolver');
  230. $this->configuration->setEntityListenerResolver($resolver);
  231. $this->assertSame($resolver, $this->configuration->getEntityListenerResolver());
  232. }
  233. }
  234. class ConfigurationTestAnnotationReaderChecker
  235. {
  236. /** @PrePersist */
  237. public function simpleAnnotationMethod()
  238. {
  239. }
  240. /** @AnnotationNamespace\PrePersist */
  241. public function namespacedAnnotationMethod()
  242. {
  243. }
  244. }