function BackendCompilerPassTest::testProcess

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest::testProcess()
  2. 8.9.x core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest::testProcess()
  3. 10 core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest::testProcess()

Tests the process method.

@covers ::process

File

core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php, line 40

Class

BackendCompilerPassTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21DependencyInjection%21Compiler%21BackendCompilerPass.php/class/BackendCompilerPass/11.x" title="Defines a compiler pass to allow automatic override per backend." class="local">\Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass</a> @group DependencyInjection

Namespace

Drupal\Tests\Core\DependencyInjection\Compiler

Code

public function testProcess() : void {
    // Add a container with no set default_backend.
    $prefix = __NAMESPACE__ . '\\ServiceClass';
    $service = (new Definition($prefix . 'Default'))->addTag('backend_overridable');
    $container = $this->getMysqlContainer($service);
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'Default', get_class($container->get('service')));
    // Set the default_backend so the mysql service should be used.
    $container = $this->getMysqlContainer($service);
    $container->setParameter('default_backend', 'mysql');
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'Mysql', get_class($container->get('service')));
    // Configure a manual alias for the service, so ensure that it is not
    // overridden by the default backend.
    $container = $this->getMysqlContainer($service);
    $container->setParameter('default_backend', 'mysql');
    $container->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
    $container->setAlias('service', new Alias('mariadb.service'));
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'MariaDb', get_class($container->get('service')));
    // Check the database driver is the default.
    $container = $this->getSqliteContainer($service);
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'Sqlite', get_class($container->get('service')));
    // Test the opt out.
    $container = $this->getSqliteContainer($service);
    $container->setParameter('default_backend', '');
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'Default', get_class($container->get('service')));
    // Set the mysql and the DrivertestMysql service, now the DrivertestMysql
    // service, as it is the driver override, should be used.
    $container = $this->getDrivertestMysqlContainer($service);
    $container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
    $container->setDefinition('DrivertestMysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassDrivertestMysql'));
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'DrivertestMysql', get_class($container->get('service')));
    // Set the mysql service, now the mysql service, as it is the database_type
    // override, should be used.
    $container = $this->getDrivertestMysqlContainer($service);
    $container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'Mysql', get_class($container->get('service')));
    // Set the DrivertestMysql service, now the DrivertestMysql service, as it
    // is the driver override, should be used.
    $container = $this->getDrivertestMysqlContainer($service);
    $container->setDefinition('DrivertestMysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassDrivertestMysql'));
    $this->backendPass
        ->process($container);
    $this->assertEquals($prefix . 'DrivertestMysql', get_class($container->get('service')));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.