BackendCompilerPassTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php
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() {
$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,
];
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\\Core\\Database\\Driver\\sqlite\\Connection')
->setMethods(NULL)
->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;
}
}
class ServiceClassDefault {
}
class ServiceClassMysql extends ServiceClassDefault {
}
class ServiceClassMariaDb extends ServiceClassMysql {
}
class ServiceClassSqlite extends ServiceClassDefault {
}