public function ConnectionTest::testConnectionRouting in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest::testConnectionRouting()
- 9 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest::testConnectionRouting()
Tests that connections return appropriate connection objects.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Database/ ConnectionTest.php, line 19
Class
- ConnectionTest
- Tests of the core database system.
Namespace
Drupal\KernelTests\Core\DatabaseCode
public function testConnectionRouting() {
// Clone the primary credentials to a replica connection.
// Note this will result in two independent connection objects that happen
// to point to the same place.
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
$db1 = Database::getConnection('default', 'default');
$db2 = Database::getConnection('replica', 'default');
$this
->assertNotNull($db1, 'default connection is a real connection object.');
$this
->assertNotNull($db2, 'replica connection is a real connection object.');
$this
->assertNotSame($db1, $db2, 'Each target refers to a different connection.');
// Try to open those targets another time, that should return the same objects.
$db1b = Database::getConnection('default', 'default');
$db2b = Database::getConnection('replica', 'default');
$this
->assertSame($db1, $db1b, 'A second call to getConnection() returns the same object.');
$this
->assertSame($db2, $db2b, 'A second call to getConnection() returns the same object.');
// Try to open an unknown target.
$unknown_target = $this
->randomMachineName();
$db3 = Database::getConnection($unknown_target, 'default');
$this
->assertNotNull($db3, 'Opening an unknown target returns a real connection object.');
$this
->assertSame($db1, $db3, 'An unknown target opens the default connection.');
// Try to open that unknown target another time, that should return the same object.
$db3b = Database::getConnection($unknown_target, 'default');
$this
->assertSame($db3, $db3b, 'A second call to getConnection() returns the same object.');
}