Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / ConfigurationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM;
4
5 use Doctrine\ORM\Mapping as AnnotationNamespace;
6 use Doctrine\ORM\Configuration;
7 use Doctrine\ORM\ORMException;
8 use ReflectionClass;
9 use PHPUnit_Framework_TestCase;
10
11 require_once __DIR__ . '/../TestInit.php';
12
13 /**
14  * Tests for the Configuration object
15  * @author Marco Pivetta <ocramius@gmail.com>
16  */
17 class ConfigurationTest extends PHPUnit_Framework_TestCase
18 {
19     /**
20      * @var Configuration
21      */
22     private $configuration;
23
24     protected function setUp()
25     {
26         parent::setUp();
27         $this->configuration = new Configuration();
28     }
29
30     public function testSetGetProxyDir()
31     {
32         $this->assertSame(null, $this->configuration->getProxyDir()); // defaults
33
34         $this->configuration->setProxyDir(__DIR__);
35         $this->assertSame(__DIR__, $this->configuration->getProxyDir());
36     }
37
38     public function testSetGetAutoGenerateProxyClasses()
39     {
40         $this->assertSame(true, $this->configuration->getAutoGenerateProxyClasses()); // defaults
41
42         $this->configuration->setAutoGenerateProxyClasses(false);
43         $this->assertSame(false, $this->configuration->getAutoGenerateProxyClasses());
44     }
45
46     public function testSetGetProxyNamespace()
47     {
48         $this->assertSame(null, $this->configuration->getProxyNamespace()); // defaults
49
50         $this->configuration->setProxyNamespace(__NAMESPACE__);
51         $this->assertSame(__NAMESPACE__, $this->configuration->getProxyNamespace());
52     }
53
54     public function testSetGetMetadataDriverImpl()
55     {
56         $this->assertSame(null, $this->configuration->getMetadataDriverImpl()); // defaults
57
58         $metadataDriver = $this->getMock('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver');
59         $this->configuration->setMetadataDriverImpl($metadataDriver);
60         $this->assertSame($metadataDriver, $this->configuration->getMetadataDriverImpl());
61     }
62
63     public function testNewDefaultAnnotationDriver()
64     {
65         $paths = array(__DIR__);
66         $reflectionClass = new ReflectionClass(__NAMESPACE__ . '\ConfigurationTestAnnotationReaderChecker');
67
68         $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths, false);
69         $reader = $annotationDriver->getReader();
70         $annotation = $reader->getMethodAnnotation(
71             $reflectionClass->getMethod('namespacedAnnotationMethod'),
72             'Doctrine\ORM\Mapping\PrePersist'
73         );
74         $this->assertInstanceOf('Doctrine\ORM\Mapping\PrePersist', $annotation);
75
76         $annotationDriver = $this->configuration->newDefaultAnnotationDriver($paths);
77         $reader = $annotationDriver->getReader();
78         $annotation = $reader->getMethodAnnotation(
79             $reflectionClass->getMethod('simpleAnnotationMethod'),
80             'Doctrine\ORM\Mapping\PrePersist'
81         );
82         $this->assertInstanceOf('Doctrine\ORM\Mapping\PrePersist', $annotation);
83     }
84
85     public function testSetGetEntityNamespace()
86     {
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');
94     }
95
96     public function testSetGetQueryCacheImpl()
97     {
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());
102     }
103
104     public function testSetGetHydrationCacheImpl()
105     {
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());
110     }
111
112     public function testSetGetMetadataCacheImpl()
113     {
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());
118     }
119
120     public function testAddGetNamedQuery()
121     {
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');
127     }
128
129     public function testAddGetNamedNativeQuery()
130     {
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');
139     }
140
141     public function ensureProductionSettings()
142     {
143         $cache = $this->getMock('Doctrine\Common\Cache\Cache');
144         $this->configuration->setAutoGenerateProxyClasses(true);
145
146         try {
147             $this->configuration->ensureProductionSettings();
148             $this->fail('Didn\'t check all production settings');
149         } catch (ORMException $e) {}
150
151         $this->configuration->setQueryCacheImpl($cache);
152
153         try {
154             $this->configuration->ensureProductionSettings();
155             $this->fail('Didn\'t check all production settings');
156         } catch (ORMException $e) {}
157
158         $this->configuration->setMetadataCacheImpl($cache);
159
160         try {
161             $this->configuration->ensureProductionSettings();
162             $this->fail('Didn\'t check all production settings');
163         } catch (ORMException $e) {}
164
165         $this->configuration->setAutoGenerateProxyClasses(false);
166         $this->configuration->ensureProductionSettings();
167     }
168
169     public function testAddGetCustomStringFunction()
170     {
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__);
178     }
179
180     public function testAddGetCustomNumericFunction()
181     {
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__);
189     }
190
191     public function testAddGetCustomDatetimeFunction()
192     {
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__);
200     }
201
202     public function testAddGetCustomHydrationMode()
203     {
204         $this->assertSame(null, $this->configuration->getCustomHydrationMode('NonExisting'));
205         $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
206         $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
207     }
208
209     public function testSetCustomHydrationModes()
210     {
211         $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
212         $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
213
214         $this->configuration->setCustomHydrationModes(
215             array(
216                 'AnotherHydrationModeName' => __CLASS__
217             )
218         );
219
220         $this->assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
221         $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
222     }
223
224     public function testSetGetClassMetadataFactoryName()
225     {
226         $this->assertSame('Doctrine\ORM\Mapping\ClassMetadataFactory', $this->configuration->getClassMetadataFactoryName());
227         $this->configuration->setClassMetadataFactoryName(__CLASS__);
228         $this->assertSame(__CLASS__, $this->configuration->getClassMetadataFactoryName());
229     }
230
231     public function testAddGetFilters()
232     {
233         $this->assertSame(null, $this->configuration->getFilterClassName('NonExistingFilter'));
234         $this->configuration->addFilter('FilterName', __CLASS__);
235         $this->assertSame(__CLASS__, $this->configuration->getFilterClassName('FilterName'));
236     }
237
238     public function setDefaultRepositoryClassName()
239     {
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__);
246     }
247
248     public function testSetGetNamingStrategy()
249     {
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());
254     }
255
256     public function testSetGetQuoteStrategy()
257     {
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());
262     }
263 }
264
265 class ConfigurationTestAnnotationReaderChecker
266 {
267     /** @PrePersist */
268     public function simpleAnnotationMethod()
269     {
270     }
271
272     /** @AnnotationNamespace\PrePersist */
273     public function namespacedAnnotationMethod()
274     {
275     }
276 }