View source
<?php
namespace Drupal\Tests\Core\DependencyInjection\Compiler;
use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
class BackendCompilerPassTest extends UnitTestCase {
protected $backendPass;
protected function setUp() : void {
$this->backendPass = new BackendCompilerPass();
}
public function testProcess($expected_class, ContainerBuilder $container) {
$this->backendPass
->process($container);
$this
->assertEquals($expected_class, get_class($container
->get('service')));
}
public function providerTestProcess() {
$data = [];
$prefix = __NAMESPACE__ . '\\ServiceClass';
$service = (new Definition($prefix . 'Default'))
->addTag('backend_overridable');
$container = $this
->getMysqlContainer($service);
$data[] = [
$prefix . 'Default',
$container,
];
$container = $this
->getMysqlContainer($service);
$container
->setParameter('default_backend', 'mysql');
$data[] = [
$prefix . 'Mysql',
$container,
];
$container = $this
->getMysqlContainer($service);
$container
->setParameter('default_backend', 'mysql');
$container
->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
$container
->setAlias('service', new Alias('mariadb.service'));
$data[] = [
$prefix . 'MariaDb',
$container,
];
$container = $this
->getSqliteContainer($service);
$data[] = [
$prefix . 'Sqlite',
$container,
];
$container = $this
->getSqliteContainer($service);
$container
->setParameter('default_backend', '');
$data[] = [
$prefix . 'Default',
$container,
];
$container = $this
->getDrivertestMysqlContainer($service);
$container
->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
$container
->setDefinition('DrivertestMysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassDrivertestMysql'));
$data[] = [
$prefix . 'DrivertestMysql',
$container,
];
$container = $this
->getDrivertestMysqlContainer($service);
$container
->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
$data[] = [
$prefix . 'Mysql',
$container,
];
$container = $this
->getDrivertestMysqlContainer($service);
$container
->setDefinition('DrivertestMysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassDrivertestMysql'));
$data[] = [
$prefix . 'DrivertestMysql',
$container,
];
return $data;
}
protected function getSqliteContainer($service) {
$container = new ContainerBuilder();
$container
->setDefinition('service', $service);
$container
->setDefinition('sqlite.service', new Definition(__NAMESPACE__ . '\\ServiceClassSqlite'));
$mock = $this
->getMockBuilder('Drupal\\sqlite\\Driver\\Database\\sqlite\\Connection')
->onlyMethods([])
->disableOriginalConstructor()
->getMock();
$container
->set('database', $mock);
return $container;
}
protected function getMysqlContainer($service) {
$container = new ContainerBuilder();
$container
->setDefinition('service', $service);
$container
->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
return $container;
}
protected function getDrivertestMysqlContainer($service) {
$container = new ContainerBuilder();
$container
->setDefinition('service', $service);
$mock = $this
->getMockBuilder('Drupal\\driver_test\\Driver\\Database\\DrivertestMysql\\Connection')
->onlyMethods([])
->disableOriginalConstructor()
->getMock();
$container
->set('database', $mock);
return $container;
}
}
class ServiceClassDefault {
}
class ServiceClassMysql extends ServiceClassDefault {
}
class ServiceClassMariaDb extends ServiceClassMysql {
}
class ServiceClassSqlite extends ServiceClassDefault {
}
class ServiceClassDrivertestMysql extends ServiceClassDefault {
}