class DatabaseTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Database/DatabaseTest.php \Drupal\Tests\Core\Database\DatabaseTest
  2. 8.9.x 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 23

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() : void {
        parent::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(__DIR__, 6);
        $databaseDriverList = new DatabaseDriverList($this->root, 'database_driver', new NullBackend('database_driver'));
        $container = $this->createMock(ContainerInterface::class);
        $container->expects($this->any())
            ->method('has')
            ->willReturnMap([
            [
                'kernel',
                TRUE,
            ],
            [
                'extension.list.database_driver',
                TRUE,
            ],
        ]);
        $container->expects($this->any())
            ->method('get')
            ->with('extension.list.database_driver')
            ->willReturn($databaseDriverList);
        $container->expects($this->any())
            ->method('getParameter')
            ->with('site.path')
            ->willReturn('');
        \Drupal::setContainer($container);
    }
    
    /**
     * @covers ::findDriverAutoloadDirectory
     * @dataProvider providerFindDriverAutoloadDirectory
     * @group legacy
     */
    public function testFindDriverAutoloadDirectory($expected, $namespace, $include_test_drivers) : void {
        $this->expectDeprecation('Drupal\\Core\\Database\\Database::findDriverAutoloadDirectory() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use DatabaseDriverList::getList() instead. See https://www.drupal.org/node/3258175');
        // The only module that provides a driver in core is a test module.
        if (!$expected) {
            $this->expectException(UnknownExtensionException::class);
            Database::findDriverAutoloadDirectory($namespace, $this->root, $include_test_drivers);
        }
        else {
            $this->assertSame($expected, Database::findDriverAutoloadDirectory($namespace, $this->root, $include_test_drivers));
        }
    }
    
    /**
     * Data provider for ::testFindDriverAutoloadDirectory().
     *
     * @return array
     */
    public static function providerFindDriverAutoloadDirectory() {
        return [
            'core mysql' => [
                'core/modules/mysql/src/Driver/Database/mysql/',
                'Drupal\\mysql\\Driver\\Database\\mysql',
                FALSE,
            ],
            'D8 custom fake' => [
                FALSE,
                'Drupal\\Driver\\Database\\CoreFake',
                TRUE,
            ],
            'module mysql' => [
                'core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/',
                'Drupal\\driver_test\\Driver\\Database\\DrivertestMysql',
                TRUE,
            ],
        ];
    }
    
    /**
     * @covers ::findDriverAutoloadDirectory
     * @dataProvider providerFindDriverAutoloadDirectoryException
     * @group legacy
     */
    public function testFindDriverAutoloadDirectoryException($expected_message, $namespace, $include_tests) : void {
        $this->expectDeprecation('Drupal\\Core\\Database\\Database::findDriverAutoloadDirectory() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use DatabaseDriverList::getList() instead. See https://www.drupal.org/node/3258175');
        $this->expectException(UnknownExtensionException::class);
        $this->expectExceptionMessage($expected_message);
        Database::findDriverAutoloadDirectory($namespace, $this->root, $include_tests);
    }
    
    /**
     * Data provider for ::testFindDriverAutoloadDirectoryException().
     *
     * @return array
     */
    public static function providerFindDriverAutoloadDirectoryException() {
        return [
            'test module but tests not included' => [
                "The database_driver Drupal\\driver_test\\Driver\\Database\\DrivertestMysql does not exist.",
                'Drupal\\driver_test\\Driver\\Database\\DrivertestMysql',
                FALSE,
            ],
            'non-existent driver in test module' => [
                "The database_driver Drupal\\driver_test\\Driver\\Database\\sqlite does not exist.",
                'Drupal\\driver_test\\Driver\\Database\\sqlite',
                TRUE,
            ],
            'non-existent module' => [
                "The database_driver Drupal\\does_not_exist\\Driver\\Database\\mysql does not exist.",
                '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/core_fake/src/Driver/Database/CoreFake");
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
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 static function Data provider for ::testFindDriverAutoloadDirectory().
DatabaseTest::providerFindDriverAutoloadDirectoryException public static function Data provider for ::testFindDriverAutoloadDirectoryException().
DatabaseTest::setUp protected function Overrides UnitTestCase::setUp
DatabaseTest::testFindDriverAutoloadDirectory public function @covers ::findDriverAutoloadDirectory
@dataProvider providerFindDriverAutoloadDirectory
@group legacy
DatabaseTest::testFindDriverAutoloadDirectoryException public function @covers ::findDriverAutoloadDirectory
@dataProvider providerFindDriverAutoloadDirectoryException
@group legacy
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.