BackendCompilerPassTest.php in Zircon Profile 8.0
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 = array();
$prefix = __NAMESPACE__ . '\\ServiceClass';
$service = (new Definition($prefix . 'Default'))
->addTag('backend_overridable');
$container = $this
->getMysqlContainer($service);
$data[] = array(
$prefix . 'Default',
$container,
);
$container = $this
->getMysqlContainer($service);
$container
->setParameter('default_backend', 'mysql');
$data[] = array(
$prefix . 'Mysql',
$container,
);
$container = clone $container;
$container
->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
$container
->setAlias('service', new Alias('mariadb.service'));
$data[] = array(
$prefix . 'MariaDb',
$container,
);
$container = $this
->getSqliteContainer($service);
$data[] = array(
$prefix . 'Sqlite',
$container,
);
$container = $this
->getSqliteContainer($service);
$container
->setParameter('default_backend', '');
$data[] = array(
$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 {
}