3 namespace Doctrine\Tests\ORM;
5 use Doctrine\ORM\Mapping as AnnotationNamespace;
6 use Doctrine\ORM\Configuration;
7 use Doctrine\ORM\ORMException;
9 use PHPUnit_Framework_TestCase;
11 require_once __DIR__ . '/../TestInit.php';
14 * Tests for the Configuration object
15 * @author Marco Pivetta <ocramius@gmail.com>
17 class ConfigurationTest extends PHPUnit_Framework_TestCase
22 private $configuration;
24 protected function setUp()
27 $this->configuration = new Configuration();
30 public function testSetGetProxyDir()
32 $this->assertSame(null, $this->configuration->getProxyDir()); // defaults
34 $this->configuration->setProxyDir(__DIR__);
35 $this->assertSame(__DIR__, $this->configuration->getProxyDir());
38 public function testSetGetAutoGenerateProxyClasses()
40 $this->assertSame(true, $this->configuration->getAutoGenerateProxyClasses()); // defaults
42 $this->configuration->setAutoGenerateProxyClasses(false);
43 $this->assertSame(false, $this->configuration->getAutoGenerateProxyClasses());
46 public function testSetGetProxyNamespace()
48 $this->assertSame(null, $this->configuration->getProxyNamespace()); // defaults
50 $this->configuration->setProxyNamespace(__NAMESPACE__);
51 $this->assertSame(__NAMESPACE__, $this->configuration->getProxyNamespace());
54 public function testSetGetMetadataDriverImpl()
56 $this->assertSame(null, $this->configuration->getMetadataDriverImpl()); // defaults
58 $metadataDriver = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
59 $this->configuration->setMetadataDriverImpl($metadataDriver);
60 $this->assertSame($metadataDriver, $this->configuration->getMetadataDriverImpl());
63 public function testNewDefaultAnnotationDriver()
65 $paths = array(__DIR__);
66 $reflectionClass = new ReflectionClass(__NAMESPACE__ . '\ConfigurationTestAnnotationReaderChecker');
68 $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths, false);
69 $reader = $annotationDriver->getReader();
70 $annotation = $reader->getMethodAnnotation(
71 $reflectionClass->getMethod('namespacedAnnotationMethod'),
72 'Doctrine\ORM\Mapping\PrePersist'
74 $this->assertInstanceOf('Doctrine\ORM\Mapping\PrePersist', $annotation);
76 $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths);
77 $reader = $annotationDriver->getReader();
78 $annotation = $reader->getMethodAnnotation(
79 $reflectionClass->getMethod('simpleAnnotationMethod'),
80 'Doctrine\ORM\Mapping\PrePersist'
82 $this->assertInstanceOf('Doctrine\ORM\Mapping\PrePersist', $annotation);
85 public function testSetGetEntityNamespace()
87 $this->configuration->addEntityNamespace('TestNamespace', __NAMESPACE__);
88 $this->assertSame(__NAMESPACE__, $this->configuration->getEntityNamespace('TestNamespace'));
89 $namespaces = array('OtherNamespace' => __NAMESPACE__);
90 $this->configuration->setEntityNamespaces($namespaces);
91 $this->assertSame($namespaces, $this->configuration->getEntityNamespaces());
92 $this->setExpectedException('Doctrine\ORM\ORMException');
93 $this->configuration->getEntityNamespace('NonExistingNamespace');
96 public function testSetGetQueryCacheImpl()
98 $this->assertSame(null, $this->configuration->getQueryCacheImpl()); // defaults
99 $queryCacheImpl = $this->getMock('Doctrine\Common\Cache\Cache');
100 $this->configuration->setQueryCacheImpl($queryCacheImpl);
101 $this->assertSame($queryCacheImpl, $this->configuration->getQueryCacheImpl());
104 public function testSetGetHydrationCacheImpl()
106 $this->assertSame(null, $this->configuration->getHydrationCacheImpl()); // defaults
107 $queryCacheImpl = $this->getMock('Doctrine\Common\Cache\Cache');
108 $this->configuration->setHydrationCacheImpl($queryCacheImpl);
109 $this->assertSame($queryCacheImpl, $this->configuration->getHydrationCacheImpl());
112 public function testSetGetMetadataCacheImpl()
114 $this->assertSame(null, $this->configuration->getMetadataCacheImpl()); // defaults
115 $queryCacheImpl = $this->getMock('Doctrine\Common\Cache\Cache');
116 $this->configuration->setMetadataCacheImpl($queryCacheImpl);
117 $this->assertSame($queryCacheImpl, $this->configuration->getMetadataCacheImpl());
120 public function testAddGetNamedQuery()
122 $dql = 'SELECT u FROM User u';
123 $this->configuration->addNamedQuery('QueryName', $dql);
124 $this->assertSame($dql, $this->configuration->getNamedQuery('QueryName'));
125 $this->setExpectedException('Doctrine\ORM\ORMException');
126 $this->configuration->getNamedQuery('NonExistingQuery');
129 public function testAddGetNamedNativeQuery()
131 $sql = 'SELECT * FROM user';
132 $rsm = $this->getMock('Doctrine\ORM\Query\ResultSetMapping');
133 $this->configuration->addNamedNativeQuery('QueryName', $sql, $rsm);
134 $fetched = $this->configuration->getNamedNativeQuery('QueryName');
135 $this->assertSame($sql, $fetched[0]);
136 $this->assertSame($rsm, $fetched[1]);
137 $this->setExpectedException('Doctrine\ORM\ORMException');
138 $this->configuration->getNamedQuery('NonExistingQuery');
141 public function ensureProductionSettings()
143 $cache = $this->getMock('Doctrine\Common\Cache\Cache');
144 $this->configuration->setAutoGenerateProxyClasses(true);
147 $this->configuration->ensureProductionSettings();
148 $this->fail('Didn\'t check all production settings');
149 } catch (ORMException $e) {}
151 $this->configuration->setQueryCacheImpl($cache);
154 $this->configuration->ensureProductionSettings();
155 $this->fail('Didn\'t check all production settings');
156 } catch (ORMException $e) {}
158 $this->configuration->setMetadataCacheImpl($cache);
161 $this->configuration->ensureProductionSettings();
162 $this->fail('Didn\'t check all production settings');
163 } catch (ORMException $e) {}
165 $this->configuration->setAutoGenerateProxyClasses(false);
166 $this->configuration->ensureProductionSettings();
169 public function testAddGetCustomStringFunction()
171 $this->configuration->addCustomStringFunction('FunctionName', __CLASS__);
172 $this->assertSame(__CLASS__, $this->configuration->getCustomStringFunction('FunctionName'));
173 $this->assertSame(null, $this->configuration->getCustomStringFunction('NonExistingFunction'));
174 $this->configuration->setCustomStringFunctions(array('OtherFunctionName' => __CLASS__));
175 $this->assertSame(__CLASS__, $this->configuration->getCustomStringFunction('OtherFunctionName'));
176 $this->setExpectedException('Doctrine\ORM\ORMException');
177 $this->configuration->addCustomStringFunction('concat', __CLASS__);
180 public function testAddGetCustomNumericFunction()
182 $this->configuration->addCustomNumericFunction('FunctionName', __CLASS__);
183 $this->assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('FunctionName'));
184 $this->assertSame(null, $this->configuration->getCustomNumericFunction('NonExistingFunction'));
185 $this->configuration->setCustomNumericFunctions(array('OtherFunctionName' => __CLASS__));
186 $this->assertSame(__CLASS__, $this->configuration->getCustomNumericFunction('OtherFunctionName'));
187 $this->setExpectedException('Doctrine\ORM\ORMException');
188 $this->configuration->addCustomNumericFunction('abs', __CLASS__);
191 public function testAddGetCustomDatetimeFunction()
193 $this->configuration->addCustomDatetimeFunction('FunctionName', __CLASS__);
194 $this->assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('FunctionName'));
195 $this->assertSame(null, $this->configuration->getCustomDatetimeFunction('NonExistingFunction'));
196 $this->configuration->setCustomDatetimeFunctions(array('OtherFunctionName' => __CLASS__));
197 $this->assertSame(__CLASS__, $this->configuration->getCustomDatetimeFunction('OtherFunctionName'));
198 $this->setExpectedException('Doctrine\ORM\ORMException');
199 $this->configuration->addCustomDatetimeFunction('date_add', __CLASS__);
202 public function testAddGetCustomHydrationMode()
204 $this->assertSame(null, $this->configuration->getCustomHydrationMode('NonExisting'));
205 $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
206 $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
209 public function testSetCustomHydrationModes()
211 $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
212 $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
214 $this->configuration->setCustomHydrationModes(
216 'AnotherHydrationModeName' => __CLASS__
220 $this->assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
221 $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
224 public function testSetGetClassMetadataFactoryName()
226 $this->assertSame('Doctrine\ORM\Mapping\ClassMetadataFactory', $this->configuration->getClassMetadataFactoryName());
227 $this->configuration->setClassMetadataFactoryName(__CLASS__);
228 $this->assertSame(__CLASS__, $this->configuration->getClassMetadataFactoryName());
231 public function testAddGetFilters()
233 $this->assertSame(null, $this->configuration->getFilterClassName('NonExistingFilter'));
234 $this->configuration->addFilter('FilterName', __CLASS__);
235 $this->assertSame(__CLASS__, $this->configuration->getFilterClassName('FilterName'));
238 public function setDefaultRepositoryClassName()
240 $this->assertSame('Doctrine\ORM\EntityRepository', $this->configuration->getDefaultRepositoryClassName());
241 $repositoryClass = 'Doctrine\Tests\Models\DDC753\DDC753CustomRepository';
242 $this->configuration->setDefaultRepositoryClassName($repositoryClass);
243 $this->assertSame($repositoryClass, $this->configuration->getDefaultRepositoryClassName());
244 $this->setExpectedException('Doctrine\ORM\ORMException');
245 $this->configuration->setDefaultRepositoryClassName(__CLASS__);
248 public function testSetGetNamingStrategy()
250 $this->assertInstanceOf('Doctrine\ORM\Mapping\NamingStrategy', $this->configuration->getNamingStrategy());
251 $namingStrategy = $this->getMock('Doctrine\ORM\Mapping\NamingStrategy');
252 $this->configuration->setNamingStrategy($namingStrategy);
253 $this->assertSame($namingStrategy, $this->configuration->getNamingStrategy());
256 public function testSetGetQuoteStrategy()
258 $this->assertInstanceOf('Doctrine\ORM\Mapping\QuoteStrategy', $this->configuration->getQuoteStrategy());
259 $quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\QuoteStrategy');
260 $this->configuration->setQuoteStrategy($quoteStrategy);
261 $this->assertSame($quoteStrategy, $this->configuration->getQuoteStrategy());
265 class ConfigurationTestAnnotationReaderChecker
268 public function simpleAnnotationMethod()
272 /** @AnnotationNamespace\PrePersist */
273 public function namespacedAnnotationMethod()