BackendCompilerPassTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest.
  5. */
  6. namespace Drupal\Tests\Core\DependencyInjection\Compiler;
  7. use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
  8. use Drupal\Tests\UnitTestCase;
  9. use Symfony\Component\DependencyInjection\Alias;
  10. use Symfony\Component\DependencyInjection\ContainerBuilder;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. /**
  13. * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
  14. * @group DependencyInjection
  15. */
  16. class BackendCompilerPassTest extends UnitTestCase {
  17. /**
  18. * The tested backend compiler pass.
  19. *
  20. * @var \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
  21. */
  22. protected $backendPass;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function setUp() {
  27. $this->backendPass = new BackendCompilerPass();
  28. }
  29. /**
  30. * Tests the process method.
  31. *
  32. * @param string $expected_class
  33. * The expected used class.
  34. * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
  35. * The container.
  36. *
  37. * @dataProvider providerTestProcess
  38. *
  39. * @covers ::process
  40. */
  41. public function testProcess($expected_class, ContainerBuilder $container) {
  42. $this->backendPass->process($container);
  43. $this->assertEquals($expected_class, get_class($container->get('service')));
  44. }
  45. /**
  46. * Provides test data for testProcess().
  47. *
  48. * @return array
  49. */
  50. public function providerTestProcess() {
  51. $data = [];
  52. // Add a container with no set default_backend.
  53. $prefix = __NAMESPACE__ . '\\ServiceClass';
  54. $service = (new Definition($prefix . 'Default'))->addTag('backend_overridable');
  55. $container = $this->getMysqlContainer($service);
  56. $data[] = [$prefix . 'Default', $container];
  57. // Set the default_backend so the mysql service should be used.
  58. $container = $this->getMysqlContainer($service);
  59. $container->setParameter('default_backend', 'mysql');
  60. $data[] = [$prefix . 'Mysql', $container];
  61. // Configure a manual alias for the service, so ensure that it is not
  62. // overridden by the default backend.
  63. $container = $this->getMysqlContainer($service);
  64. $container->setParameter('default_backend', 'mysql');
  65. $container->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
  66. $container->setAlias('service', new Alias('mariadb.service'));
  67. $data[] = [$prefix . 'MariaDb', $container];
  68. // Check the database driver is the default.
  69. $container = $this->getSqliteContainer($service);
  70. $data[] = [$prefix . 'Sqlite', $container];
  71. // Test the opt out.
  72. $container = $this->getSqliteContainer($service);
  73. $container->setParameter('default_backend', '');
  74. $data[] = [$prefix . 'Default', $container];
  75. return $data;
  76. }
  77. /**
  78. * Creates a container with a sqlite database service in it.
  79. *
  80. * This is necessary because the container clone does not clone the parameter
  81. * bag so the setParameter() call effects the parent container as well.
  82. *
  83. * @param $service
  84. *
  85. * @return \Symfony\Component\DependencyInjection\ContainerBuilder
  86. */
  87. protected function getSqliteContainer($service) {
  88. $container = new ContainerBuilder();
  89. $container->setDefinition('service', $service);
  90. $container->setDefinition('sqlite.service', new Definition(__NAMESPACE__ . '\\ServiceClassSqlite'));
  91. $mock = $this->getMockBuilder('Drupal\Core\Database\Driver\sqlite\Connection')->setMethods(NULL)->disableOriginalConstructor()->getMock();
  92. $container->set('database', $mock);
  93. return $container;
  94. }
  95. /**
  96. * Creates a container with a mysql database service definition in it.
  97. *
  98. * This is necessary because the container clone does not clone the parameter
  99. * bag so the setParameter() call effects the parent container as well.
  100. *
  101. * @param $service
  102. *
  103. * @return \Symfony\Component\DependencyInjection\ContainerBuilder
  104. */
  105. protected function getMysqlContainer($service) {
  106. $container = new ContainerBuilder();
  107. $container->setDefinition('service', $service);
  108. $container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
  109. return $container;
  110. }
  111. }
  112. class ServiceClassDefault {
  113. }
  114. class ServiceClassMysql extends ServiceClassDefault {
  115. }
  116. class ServiceClassMariaDb extends ServiceClassMysql {
  117. }
  118. class ServiceClassSqlite extends ServiceClassDefault {
  119. }