You are here

class DatabaseTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Database/DatabaseTest.php \Drupal\Tests\Core\Database\DatabaseTest
  2. 10 core/tests/Drupal/Tests/Core/Database/DatabaseTest.php \Drupal\Tests\Core\Database\DatabaseTest

@coversDefaultClass \Drupal\Core\Database\Database

@runTestsInSeparateProcesses @preserveGlobalState disabled

@group Database

Hierarchy

Expanded class hierarchy of DatabaseTest

File

core/tests/Drupal/Tests/Core/Database/DatabaseTest.php, line 19

Namespace

Drupal\Tests\Core\Database
View source
class DatabaseTest extends UnitTestCase {

  /**
   * A classloader to enable testing of contrib drivers.
   *
   * @var \Composer\Autoload\ClassLoader
   */
  protected $additionalClassloader;

  /**
   * Path to DRUPAL_ROOT.
   *
   * @var string
   */
  protected $root;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->additionalClassloader = new ClassLoader();
    $this->additionalClassloader
      ->register();

    // Mock the container so we don't need to mock drupal_valid_test_ua().
    // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
    $this->root = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
    $container = $this
      ->createMock(ContainerInterface::class);
    $container
      ->expects($this
      ->any())
      ->method('has')
      ->with('kernel')
      ->willReturn(TRUE);
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->with('site.path')
      ->willReturn('');
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::findDriverAutoloadDirectory
   * @dataProvider providerFindDriverAutoloadDirectory
   */
  public function testFindDriverAutoloadDirectory($expected, $namespace) {
    new Settings([
      'extension_discovery_scan_tests' => TRUE,
    ]);

    // The only module that provides a driver in core is a test module.
    $this
      ->assertSame($expected, Database::findDriverAutoloadDirectory($namespace, $this->root));
  }

  /**
   * Data provider for ::testFindDriverAutoloadDirectory().
   *
   * @return array
   */
  public function providerFindDriverAutoloadDirectory() {
    return [
      'core mysql' => [
        FALSE,
        'Drupal\\Core\\Database\\Driver\\mysql',
      ],
      'D8 custom fake' => [
        FALSE,
        'Drupal\\Driver\\Database\\corefake',
      ],
      'module mysql' => [
        'core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/',
        'Drupal\\driver_test\\Driver\\Database\\DrivertestMysql',
      ],
    ];
  }

  /**
   * @covers ::findDriverAutoloadDirectory
   * @dataProvider providerFindDriverAutoloadDirectoryException
   */
  public function testFindDriverAutoloadDirectoryException($expected_message, $namespace, $include_tests) {
    new Settings([
      'extension_discovery_scan_tests' => $include_tests,
    ]);
    if ($include_tests === FALSE) {

      // \Drupal\Core\Extension\ExtensionDiscovery::scan() needs
      // drupal_valid_test_ua().
      include $this->root . '/core/includes/bootstrap.inc';
    }
    $this
      ->expectException(\RuntimeException::class);
    $this
      ->expectExceptionMessage($expected_message);
    Database::findDriverAutoloadDirectory($namespace, $this->root);
  }

  /**
   * Data provider for ::testFindDriverAutoloadDirectoryException().
   *
   * @return array
   */
  public function providerFindDriverAutoloadDirectoryException() {
    return [
      'test module but tests not included' => [
        "Cannot find the module 'driver_test' for the database driver namespace 'Drupal\\driver_test\\Driver\\Database\\DrivertestMysql'",
        'Drupal\\driver_test\\Driver\\Database\\DrivertestMysql',
        FALSE,
      ],
      'non-existent driver in test module' => [
        "Cannot find the database driver namespace 'Drupal\\driver_test\\Driver\\Database\\sqlite' in module 'driver_test'",
        'Drupal\\driver_test\\Driver\\Database\\sqlite',
        TRUE,
      ],
      'non-existent module' => [
        "Cannot find the module 'does_not_exist' for the database driver namespace 'Drupal\\does_not_exist\\Driver\\Database\\mysql'",
        'Drupal\\does_not_exist\\Driver\\Database\\mysql',
        TRUE,
      ],
    ];
  }

  /**
   * Adds a database driver that uses the D8's Drupal\Driver\Database namespace.
   */
  protected function addD8CustomDrivers() {
    $this->additionalClassloader
      ->addPsr4("Drupal\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/corefake");
  }

  /**
   * Adds database drivers that are provided by modules.
   */
  protected function addModuleDrivers() {
    $this->additionalClassloader
      ->addPsr4("Drupal\\driver_test\\Driver\\Database\\DrivertestMysql\\", __DIR__ . "/../../../../../modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql");
    $this->additionalClassloader
      ->addPsr4("Drupal\\corefake\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefake");
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseTest::$additionalClassloader protected property A classloader to enable testing of contrib drivers.
DatabaseTest::$root protected property Path to DRUPAL_ROOT. Overrides UnitTestCase::$root
DatabaseTest::addD8CustomDrivers protected function Adds a database driver that uses the D8's Drupal\Driver\Database namespace.
DatabaseTest::addModuleDrivers protected function Adds database drivers that are provided by modules.
DatabaseTest::providerFindDriverAutoloadDirectory public function Data provider for ::testFindDriverAutoloadDirectory().
DatabaseTest::providerFindDriverAutoloadDirectoryException public function Data provider for ::testFindDriverAutoloadDirectoryException().
DatabaseTest::setUp protected function Overrides UnitTestCase::setUp
DatabaseTest::testFindDriverAutoloadDirectory public function @covers ::findDriverAutoloadDirectory @dataProvider providerFindDriverAutoloadDirectory
DatabaseTest::testFindDriverAutoloadDirectoryException public function @covers ::findDriverAutoloadDirectory @dataProvider providerFindDriverAutoloadDirectoryException
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::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.