You are here

class BackendCompilerPassTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest

@coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass @group DependencyInjection

Hierarchy

Expanded class hierarchy of BackendCompilerPassTest

File

core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php, line 20
Contains \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest.

Namespace

Drupal\Tests\Core\DependencyInjection\Compiler
View source
class BackendCompilerPassTest extends UnitTestCase {

  /**
   * The tested backend compiler pass.
   *
   * @var \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
   */
  protected $backendPass;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->backendPass = new BackendCompilerPass();
  }

  /**
   * Tests the process method.
   *
   * @param string $expected_class
   *   The expected used class.
   * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
   *   The container.
   *
   * @dataProvider providerTestProcess
   *
   * @covers ::process
   */
  public function testProcess($expected_class, ContainerBuilder $container) {
    $this->backendPass
      ->process($container);
    $this
      ->assertEquals($expected_class, get_class($container
      ->get('service')));
  }

  /**
   * Provides test data for testProcess().
   *
   * @return array
   */
  public function providerTestProcess() {
    $data = [];

    // Add a container with no set default_backend.
    $prefix = __NAMESPACE__ . '\\ServiceClass';
    $service = (new Definition($prefix . 'Default'))
      ->addTag('backend_overridable');
    $container = $this
      ->getMysqlContainer($service);
    $data[] = [
      $prefix . 'Default',
      $container,
    ];

    // Set the default_backend so the mysql service should be used.
    $container = $this
      ->getMysqlContainer($service);
    $container
      ->setParameter('default_backend', 'mysql');
    $data[] = [
      $prefix . 'Mysql',
      $container,
    ];

    // 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'));
    $data[] = [
      $prefix . 'MariaDb',
      $container,
    ];

    // Check the database driver is the default.
    $container = $this
      ->getSqliteContainer($service);
    $data[] = [
      $prefix . 'Sqlite',
      $container,
    ];

    // Test the opt out.
    $container = $this
      ->getSqliteContainer($service);
    $container
      ->setParameter('default_backend', '');
    $data[] = [
      $prefix . 'Default',
      $container,
    ];
    return $data;
  }

  /**
   * Creates a container with a sqlite database service in it.
   *
   * This is necessary because the container clone does not clone the parameter
   * bag so the setParameter() call effects the parent container as well.
   *
   * @param $service
   *
   * @return \Symfony\Component\DependencyInjection\ContainerBuilder
   */
  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;
  }

  /**
   * Creates a container with a mysql database service definition in it.
   *
   * This is necessary because the container clone does not clone the parameter
   * bag so the setParameter() call effects the parent container as well.
   *
   * @param $service
   *
   * @return \Symfony\Component\DependencyInjection\ContainerBuilder
   */
  protected function getMysqlContainer($service) {
    $container = new ContainerBuilder();
    $container
      ->setDefinition('service', $service);
    $container
      ->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
    return $container;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackendCompilerPassTest::$backendPass protected property The tested backend compiler pass.
BackendCompilerPassTest::getMysqlContainer protected function Creates a container with a mysql database service definition in it.
BackendCompilerPassTest::getSqliteContainer protected function Creates a container with a sqlite database service in it.
BackendCompilerPassTest::providerTestProcess public function Provides test data for testProcess().
BackendCompilerPassTest::setUp protected function Overrides UnitTestCase::setUp
BackendCompilerPassTest::testProcess public function Tests the process method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.