5 * Contains \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest.
8 namespace Drupal\Tests\Core\DependencyInjection\Compiler;
10 use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
11 use Drupal\Tests\UnitTestCase;
12 use Symfony\Component\DependencyInjection\Alias;
13 use Symfony\Component\DependencyInjection\ContainerBuilder;
14 use Symfony\Component\DependencyInjection\Definition;
17 * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
18 * @group DependencyInjection
20 class BackendCompilerPassTest extends UnitTestCase {
23 * The tested backend compiler pass.
25 * @var \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
27 protected $backendPass;
32 protected function setUp() {
33 $this->backendPass = new BackendCompilerPass();
37 * Tests the process method.
39 * @param string $expected_class
40 * The expected used class.
41 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
44 * @dataProvider providerTestProcess
48 public function testProcess($expected_class, ContainerBuilder $container) {
49 $this->backendPass->process($container);
51 $this->assertEquals($expected_class, get_class($container->get('service')));
55 * Provides test data for testProcess().
59 public function providerTestProcess() {
61 // Add a container with no set default_backend.
62 $prefix = __NAMESPACE__ . '\\ServiceClass';
63 $service = (new Definition($prefix . 'Default'))->addTag('backend_overridable');
64 $container = $this->getMysqlContainer($service);
66 $data[] = [$prefix . 'Default', $container];
68 // Set the default_backend so the mysql service should be used.
69 $container = $this->getMysqlContainer($service);
70 $container->setParameter('default_backend', 'mysql');
71 $data[] = [$prefix . 'Mysql', $container];
73 // Configure a manual alias for the service, so ensure that it is not
74 // overridden by the default backend.
75 $container = $this->getMysqlContainer($service);
76 $container->setParameter('default_backend', 'mysql');
77 $container->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
78 $container->setAlias('service', new Alias('mariadb.service'));
79 $data[] = [$prefix . 'MariaDb', $container];
81 // Check the database driver is the default.
82 $container = $this->getSqliteContainer($service);
83 $data[] = [$prefix . 'Sqlite', $container];
86 $container = $this->getSqliteContainer($service);
87 $container->setParameter('default_backend', '');
88 $data[] = [$prefix . 'Default', $container];
94 * Creates a container with a sqlite database service in it.
96 * This is necessary because the container clone does not clone the parameter
97 * bag so the setParameter() call effects the parent container as well.
100 * @return \Symfony\Component\DependencyInjection\ContainerBuilder
102 protected function getSqliteContainer($service) {
103 $container = new ContainerBuilder();
104 $container->setDefinition('service', $service);
105 $container->setDefinition('sqlite.service', new Definition(__NAMESPACE__ . '\\ServiceClassSqlite'));
106 $mock = $this->getMockBuilder('Drupal\Core\Database\Driver\sqlite\Connection')->setMethods(NULL)->disableOriginalConstructor()->getMock();
107 $container->set('database', $mock);
112 * Creates a container with a mysql database service definition in it.
114 * This is necessary because the container clone does not clone the parameter
115 * bag so the setParameter() call effects the parent container as well.
118 * @return \Symfony\Component\DependencyInjection\ContainerBuilder
120 protected function getMysqlContainer($service) {
121 $container = new ContainerBuilder();
122 $container->setDefinition('service', $service);
123 $container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
129 class ServiceClassDefault {
132 class ServiceClassMysql extends ServiceClassDefault {
135 class ServiceClassMariaDb extends ServiceClassMysql {
138 class ServiceClassSqlite extends ServiceClassDefault {