DoctrineExtensionTest.php 9.3 KB

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