PrefixInfoTest.php in Drupal 8
File
core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Database;
use Drupal\Core\Database\Database;
class PrefixInfoTest extends DatabaseTestBase {
public function testGetPrefixInfo() {
$driver = $this->connection
->driver();
if ($driver !== 'mysql') {
$this
->markTestSkipped("MySql tests can not run for driver '{$driver}'.");
}
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('extra', 'default', $connection_info['default']);
$db1_connection = Database::getConnection('default', 'default');
$db1_schema = $db1_connection
->schema();
$db2_connection = Database::getConnection('default', 'extra');
$method = new \ReflectionMethod($db1_schema, 'getPrefixInfo');
$method
->setAccessible(TRUE);
$db1_info = $method
->invoke($db1_schema);
$reflection = new \ReflectionObject($db2_connection);
$property = $reflection
->getProperty('connectionOptions');
$property
->setAccessible(TRUE);
$connection_info['default']['database'] = 'foobar';
$property
->setValue($db2_connection, $connection_info['default']);
$reflection_class = new \ReflectionClass(Database::class);
$property = $reflection_class
->getProperty('databaseInfo');
$property
->setAccessible(TRUE);
$info = $property
->getValue();
$info['extra']['default']['database'] = 'foobar';
$property
->setValue(NULL, $info);
$extra_info = Database::getConnectionInfo('extra');
$this
->assertSame('foobar', $extra_info['default']['database']);
$db2_schema = $db2_connection
->schema();
$db2_info = $method
->invoke($db2_schema);
$this
->assertNotSame($db2_info['database'], $db1_info['database']);
$this
->assertSame('foobar', $db2_info['database']);
Database::removeConnection('extra');
}
}
Classes
Name |
Description |
PrefixInfoTest |
Tests that the prefix info for a database schema is correct. |