function TestSetupTraitTest::testChangeDatabasePrefix

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php \Drupal\Tests\Core\Test\TestSetupTraitTest::testChangeDatabasePrefix()
  2. 10 core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php \Drupal\Tests\Core\Test\TestSetupTraitTest::testChangeDatabasePrefix()
  3. 11.x core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php \Drupal\Tests\Core\Test\TestSetupTraitTest::testChangeDatabasePrefix()

Tests the SIMPLETEST_DB environment variable is used.

@covers ::changeDatabasePrefix

File

core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php, line 27

Class

TestSetupTraitTest
Tests the TestSetupTrait trait.

Namespace

Drupal\Tests\Core\Test

Code

public function testChangeDatabasePrefix() {
    $root = dirname(__FILE__, 7);
    putenv('SIMPLETEST_DB=pgsql://user:pass@127.0.0.1/db');
    $connection_info = Database::convertDbUrlToConnectionInfo('mysql://user:pass@localhost/db', $root);
    Database::addConnectionInfo('default', 'default', $connection_info);
    $this->assertEquals('mysql', Database::getConnectionInfo()['default']['driver']);
    $this->assertEquals('localhost', Database::getConnectionInfo()['default']['host']);
    // Create a mock for testing the trait and set a few properties that are
    // used to avoid unnecessary set up.
    $test_setup = $this->getMockForTrait(TestSetupTrait::class);
    $reflection = new \ReflectionClass($test_setup);
    $property = $reflection->getProperty('databasePrefix');
    $property->setAccessible(TRUE);
    $property->setValue($test_setup, 'testDbPrefix');
    $property = $reflection->getProperty('root');
    $property->setAccessible(TRUE);
    $property->setValue($test_setup, $root);
    $method = new \ReflectionMethod(get_class($test_setup), 'changeDatabasePrefix');
    $method->setAccessible(TRUE);
    $method->invoke($test_setup);
    // Ensure that SIMPLETEST_DB defines the default database connection after
    // calling \Drupal\Core\Test\TestSetupTrait::changeDatabasePrefix().
    $this->assertEquals('pgsql', Database::getConnectionInfo()['default']['driver']);
    $this->assertEquals('127.0.0.1', Database::getConnectionInfo()['default']['host']);
}

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