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

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

http://github.com/fabpot/symfony
PHP | 270 lines | 216 code | 34 blank | 20 comment | 0 complexity | 7542f33646f7576370502abd6a92cc78 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 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(): void
  25. {
  26. parent::setUp();
  27. $this->extension = $this
  28. ->getMockBuilder('Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension')
  29. ->setMethods([
  30. 'getMappingResourceConfigDirectory',
  31. 'getObjectManagerElementName',
  32. 'getMappingObjectDefaultName',
  33. 'getMappingResourceExtension',
  34. 'getMetadataDriverClass',
  35. 'load',
  36. ])
  37. ->getMock()
  38. ;
  39. $this->extension->expects($this->any())
  40. ->method('getObjectManagerElementName')
  41. ->willReturnCallback(function ($name) {
  42. return 'doctrine.orm.'.$name;
  43. });
  44. }
  45. public function testFixManagersAutoMappingsWithTwoAutomappings()
  46. {
  47. $this->expectException('LogicException');
  48. $emConfigs = [
  49. 'em1' => [
  50. 'auto_mapping' => true,
  51. ],
  52. 'em2' => [
  53. 'auto_mapping' => true,
  54. ],
  55. ];
  56. $bundles = [
  57. 'FirstBundle' => 'My\FirstBundle',
  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 [
  68. [
  69. [ // no auto mapping on em1
  70. 'auto_mapping' => false,
  71. ],
  72. [ // no auto mapping on em2
  73. 'auto_mapping' => false,
  74. ],
  75. [],
  76. [],
  77. ],
  78. [
  79. [ // no auto mapping on em1
  80. 'auto_mapping' => false,
  81. ],
  82. [ // auto mapping enabled on em2
  83. 'auto_mapping' => true,
  84. ],
  85. [],
  86. [
  87. 'mappings' => [
  88. 'FirstBundle' => [
  89. 'mapping' => true,
  90. 'is_bundle' => true,
  91. ],
  92. 'SecondBundle' => [
  93. 'mapping' => true,
  94. 'is_bundle' => true,
  95. ],
  96. ],
  97. ],
  98. ],
  99. [
  100. [ // no auto mapping on em1, but it defines SecondBundle as own
  101. 'auto_mapping' => false,
  102. 'mappings' => [
  103. 'SecondBundle' => [
  104. 'mapping' => true,
  105. 'is_bundle' => true,
  106. ],
  107. ],
  108. ],
  109. [ // auto mapping enabled on em2
  110. 'auto_mapping' => true,
  111. ],
  112. [
  113. 'mappings' => [
  114. 'SecondBundle' => [
  115. 'mapping' => true,
  116. 'is_bundle' => true,
  117. ],
  118. ],
  119. ],
  120. [
  121. 'mappings' => [
  122. 'FirstBundle' => [
  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 = [
  137. 'em1' => $originalEm1,
  138. 'em2' => $originalEm2,
  139. ];
  140. $bundles = [
  141. 'FirstBundle' => 'My\FirstBundle',
  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([
  149. 'auto_mapping' => false,
  150. ], $expectedEm1));
  151. $this->assertEquals($newEmConfigs['em2'], array_merge([
  152. 'auto_mapping' => false,
  153. ], $expectedEm2));
  154. }
  155. public function providerBasicDrivers()
  156. {
  157. return [
  158. ['doctrine.orm.cache.apc.class', ['type' => 'apc']],
  159. ['doctrine.orm.cache.apcu.class', ['type' => 'apcu']],
  160. ['doctrine.orm.cache.array.class', ['type' => 'array']],
  161. ['doctrine.orm.cache.xcache.class', ['type' => 'xcache']],
  162. ['doctrine.orm.cache.wincache.class', ['type' => 'wincache']],
  163. ['doctrine.orm.cache.zenddata.class', ['type' => 'zenddata']],
  164. ['doctrine.orm.cache.redis.class', ['type' => 'redis'], ['setRedis']],
  165. ['doctrine.orm.cache.memcached.class', ['type' => 'memcached'], ['setMemcached']],
  166. ];
  167. }
  168. /**
  169. * @dataProvider providerBasicDrivers
  170. */
  171. public function testLoadBasicCacheDriver(string $class, array $config, array $expectedCalls = [])
  172. {
  173. $container = $this->createContainer();
  174. $cacheName = 'metadata_cache';
  175. $objectManager = [
  176. 'name' => 'default',
  177. 'metadata_cache_driver' => $config,
  178. ];
  179. $this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
  180. $this->assertTrue($container->hasDefinition('doctrine.orm.default_metadata_cache'));
  181. $definition = $container->getDefinition('doctrine.orm.default_metadata_cache');
  182. $defCalls = $definition->getMethodCalls();
  183. $expectedCalls[] = 'setNamespace';
  184. $actualCalls = array_column($defCalls, 0);
  185. $this->assertFalse($definition->isPublic());
  186. $this->assertEquals("%$class%", $definition->getClass());
  187. foreach (array_unique($expectedCalls) as $call) {
  188. $this->assertContains($call, $actualCalls);
  189. }
  190. }
  191. public function testServiceCacheDriver()
  192. {
  193. $cacheName = 'metadata_cache';
  194. $container = $this->createContainer();
  195. $definition = new Definition('%doctrine.orm.cache.apc.class%');
  196. $objectManager = [
  197. 'name' => 'default',
  198. 'metadata_cache_driver' => [
  199. 'type' => 'service',
  200. 'id' => 'service_driver',
  201. ],
  202. ];
  203. $container->setDefinition('service_driver', $definition);
  204. $this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
  205. $this->assertTrue($container->hasAlias('doctrine.orm.default_metadata_cache'));
  206. }
  207. public function testUnrecognizedCacheDriverException()
  208. {
  209. $this->expectException('InvalidArgumentException');
  210. $this->expectExceptionMessage('"unrecognized_type" is an unrecognized Doctrine cache driver.');
  211. $cacheName = 'metadata_cache';
  212. $container = $this->createContainer();
  213. $objectManager = [
  214. 'name' => 'default',
  215. 'metadata_cache_driver' => [
  216. 'type' => 'unrecognized_type',
  217. ],
  218. ];
  219. $this->invokeLoadCacheDriver($objectManager, $container, $cacheName);
  220. }
  221. protected function invokeLoadCacheDriver(array $objectManager, ContainerBuilder $container, $cacheName)
  222. {
  223. $method = new \ReflectionMethod($this->extension, 'loadObjectManagerCacheDriver');
  224. $method->setAccessible(true);
  225. $method->invokeArgs($this->extension, [$objectManager, $container, $cacheName]);
  226. }
  227. protected function createContainer(array $data = []): ContainerBuilder
  228. {
  229. return new ContainerBuilder(new ParameterBag(array_merge([
  230. 'kernel.bundles' => ['FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'],
  231. 'kernel.cache_dir' => __DIR__,
  232. 'kernel.container_class' => 'kernel',
  233. 'kernel.project_dir' => __DIR__,
  234. ], $data)));
  235. }
  236. }