PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Tests/DependencyInjection/DoctrineExtensionTest.php

https://gitlab.com/TouirMohamedMarwen/Symfony2
PHP | 284 lines | 215 code | 34 blank | 35 comment | 0 complexity | cf9f99ae990aaf645437fcb04a53a3b0 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Tests\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  14. /**
  15. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  16. */
  17. class DoctrineExtensionTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var \Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension
  21. */
  22. private $extension;
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $this->extension = $this
  27. ->getMockBuilder('Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension')
  28. ->setMethods(array(
  29. 'getMappingResourceConfigDirectory',
  30. 'getObjectManagerElementName',
  31. 'getMappingObjectDefaultName',
  32. 'getMappingResourceExtension',
  33. 'load',
  34. ))
  35. ->getMock()
  36. ;
  37. $this->extension->expects($this->any())
  38. ->method('getObjectManagerElementName')
  39. ->will($this->returnCallback(function ($name) {
  40. return 'doctrine.orm.'.$name;
  41. }));
  42. }
  43. /**
  44. * @expectedException LogicException
  45. */
  46. public function testFixManagersAutoMappingsWithTwoAutomappings()
  47. {
  48. $emConfigs = array(
  49. 'em1'=> array(
  50. 'auto_mapping' => true
  51. ),
  52. 'em2'=> array(
  53. 'auto_mapping' => true
  54. ),
  55. );
  56. $bundles = array(
  57. 'FristBundle'=> 'My\FristBundle',
  58. 'SecondBundle'=> 'My\SecondBundle',
  59. );
  60. $reflection = new \ReflectionClass(get_class($this->extension));
  61. $method = $reflection->getMethod('fixManagersAutoMappings');
  62. $method->setAccessible(true);
  63. $method->invoke($this->extension, $emConfigs, $bundles);
  64. }
  65. public function getAutomappingData()
  66. {
  67. return array(
  68. array(
  69. array( // no auto mapping on em1
  70. 'auto_mapping' => false
  71. ),
  72. array( // no auto mapping on em2
  73. 'auto_mapping' => false
  74. ),
  75. array(),
  76. array()
  77. ),
  78. array(
  79. array( // no auto mapping on em1
  80. 'auto_mapping' => false
  81. ),
  82. array( // auto mapping enabled on em2
  83. 'auto_mapping' => true
  84. ),
  85. array(),
  86. array(
  87. 'mappings' => array(
  88. 'FristBundle' => array(
  89. 'mapping' => true,
  90. 'is_bundle' => true
  91. ),
  92. 'SecondBundle' => array(
  93. 'mapping' => true,
  94. 'is_bundle' => true
  95. )
  96. )
  97. )
  98. ),
  99. array(
  100. array( // no auto mapping on em1, but it defines SecondBundle as own
  101. 'auto_mapping' => false,
  102. 'mappings' => array(
  103. 'SecondBundle' => array(
  104. 'mapping' => true,
  105. 'is_bundle' => true
  106. )
  107. )
  108. ),
  109. array( // auto mapping enabled on em2
  110. 'auto_mapping' => true
  111. ),
  112. array(
  113. 'mappings' => array(
  114. 'SecondBundle' => array(
  115. 'mapping' => true,
  116. 'is_bundle' => true
  117. )
  118. )
  119. ),
  120. array(
  121. 'mappings' => array(
  122. 'FristBundle' => array(
  123. 'mapping' => true,
  124. 'is_bundle' => true
  125. )
  126. )
  127. )
  128. )
  129. );
  130. }
  131. /**
  132. * @dataProvider getAutomappingData
  133. */
  134. public function testFixManagersAutoMappings(array $originalEm1, array $originalEm2, array $expectedEm1, array $expectedEm2)
  135. {
  136. $emConfigs = array(
  137. 'em1'=> $originalEm1,
  138. 'em2'=> $originalEm2,
  139. );
  140. $bundles = array(
  141. 'FristBundle'=> 'My\FristBundle',
  142. 'SecondBundle'=> 'My\SecondBundle',
  143. );
  144. $reflection = new \ReflectionClass(get_class($this->extension));
  145. $method = $reflection->getMethod('fixManagersAutoMappings');
  146. $method->setAccessible(true);
  147. $newEmConfigs = $method->invoke($this->extension, $emConfigs, $bundles);
  148. $this->assertEquals($newEmConfigs["em1"], array_merge(array(
  149. 'auto_mapping' => false
  150. ), $expectedEm1));
  151. $this->assertEquals($newEmConfigs["em2"], array_merge(array(
  152. 'auto_mapping' => false
  153. ), $expectedEm2));
  154. }
  155. public function providerBasicDrivers()
  156. {
  157. return array(
  158. array('doctrine.orm.cache.apc.class', array('type' => 'apc')),
  159. array('doctrine.orm.cache.array.class', array('type' => 'array')),
  160. array('doctrine.orm.cache.xcache.class', array('type' => 'xcache')),
  161. array('doctrine.orm.cache.wincache.class', array('type' => 'wincache')),
  162. array('doctrine.orm.cache.zenddata.class', array('type' => 'zenddata')),
  163. array('doctrine.orm.cache.redis.class', array('type' => 'redis'), array('setRedis')),
  164. array('doctrine.orm.cache.memcache.class', array('type' => 'memcache'), array('setMemcache')),
  165. array('doctrine.orm.cache.memcached.class', array('type' => 'memcached'), array('setMemcached')),
  166. );
  167. }
  168. /**
  169. * @param string $class
  170. * @param array $config
  171. *
  172. * @dataProvider providerBasicDrivers
  173. */
  174. public function testLoadBasicCacheDriver($class, array $config, array $expectedCalls = array())
  175. {
  176. $container = $this->createContainer();
  177. $cacheName = 'metadata_cache';
  178. $objectManager = array(
  179. 'name' => 'default',
  180. 'metadata_cache_driver' => $config,
  181. );
  182. $this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
  183. $this->assertTrue($container->hasDefinition('doctrine.orm.default_metadata_cache'));
  184. $definition = $container->getDefinition('doctrine.orm.default_metadata_cache');
  185. $defCalls = $definition->getMethodCalls();
  186. $expectedCalls[] = 'setNamespace';
  187. $actualCalls = array_map(function ($call) {
  188. return $call[0];
  189. }, $defCalls);
  190. $this->assertFalse($definition->isPublic());
  191. $this->assertEquals("%$class%", $definition->getClass());
  192. foreach (array_unique($expectedCalls) as $call) {
  193. $this->assertContains($call, $actualCalls);
  194. }
  195. }
  196. public function testServiceCacheDriver()
  197. {
  198. $cacheName = 'metadata_cache';
  199. $container = $this->createContainer();
  200. $definition = new Definition('%doctrine.orm.cache.apc.class%');
  201. $objectManager = array(
  202. 'name' => 'default',
  203. 'metadata_cache_driver' => array(
  204. 'type' => 'service',
  205. 'id' => 'service_driver',
  206. ),
  207. );
  208. $container->setDefinition('service_driver', $definition);
  209. $this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
  210. $this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache'));
  211. }
  212. /**
  213. * @expectedException InvalidArgumentException
  214. * @expectedExceptionMessage "unrecognized_type" is an unrecognized Doctrine cache driver.
  215. */
  216. public function testUnrecognizedCacheDriverException()
  217. {
  218. $cacheName = 'metadata_cache';
  219. $container = $this->createContainer();
  220. $objectManager = array(
  221. 'name' => 'default',
  222. 'metadata_cache_driver' => array(
  223. 'type' => 'unrecognized_type',
  224. ),
  225. );
  226. $this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
  227. }
  228. protected function invokeLoadCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName)
  229. {
  230. $method = new \ReflectionMethod($this->extension, 'loadObjectManagerCacheDriver');
  231. $method->setAccessible(true);
  232. $method->invokeArgs($this->extension, array($objectManager, $container, $cacheName));
  233. }
  234. /**
  235. * @param array $data
  236. *
  237. * @return \Symfony\Component\DependencyInjection\ContainerBuilder
  238. */
  239. protected function createContainer(array $data = array())
  240. {
  241. return new ContainerBuilder(new ParameterBag(array_merge(array(
  242. 'kernel.bundles' => array('FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'),
  243. 'kernel.cache_dir' => __DIR__,
  244. 'kernel.debug' => false,
  245. 'kernel.environment' => 'test',
  246. 'kernel.name' => 'kernel',
  247. 'kernel.root_dir' => __DIR__,
  248. ), $data)));
  249. }
  250. }