Issue #2553533 by alexpott, dawehner, amateescu, hussainweb: KernelTestBaseTNG™ is not cleaning up after itself

8.0.x
webchick 2015-08-25 15:26:12 -07:00
parent 8f94d83897
commit d6d3f6a6ca
2 changed files with 49 additions and 18 deletions

View File

@ -591,6 +591,20 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
$this->kernel->shutdown();
}
// Remove all prefixed tables.
$original_connection_info = Database::getConnectionInfo('simpletest_original_default');
$original_prefix = $original_connection_info['default']['prefix']['default'];
$test_connection_info = Database::getConnectionInfo('default');
$test_prefix = $test_connection_info['default']['prefix']['default'];
if ($original_prefix != $test_prefix) {
$tables = Database::getConnection()->schema()->findTables('%');
foreach ($tables as $table) {
if (Database::getConnection()->schema()->dropTable($table)) {
unset($tables[$table]);
}
}
}
// Free up memory: Own properties.
$this->classLoader = NULL;
$this->vfsRoot = NULL;
@ -618,13 +632,20 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
$this->container = NULL;
new Settings(array());
parent::tearDown();
}
/**
* @after
*
* Additional tear down method to close the connection at the end.
*/
public function tearDownCloseDatabaseConnection() {
// Destroy the database connection, which for example removes the memory
// from sqlite in memory.
foreach (Database::getAllConnectionInfo() as $key => $targets) {
Database::removeConnection($key);
}
parent::tearDown();
}
/**

View File

@ -7,6 +7,7 @@
namespace Drupal\KernelTests;
use Drupal\Core\Database\Database;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
@ -52,26 +53,10 @@ class KernelTestBaseTest extends KernelTestBase {
* @covers ::getDatabaseConnectionInfo
*/
public function testGetDatabaseConnectionInfoWithOutManualSetDbUrl() {
$this->setUp();
$options = $this->container->get('database')->getConnectionOptions();
$this->assertSame($this->databasePrefix, $options['prefix']['default']);
}
/**
* @covers ::getDatabaseConnectionInfo
*/
public function testGetDatabaseConnectionInfoWithManualSetDbUrl() {
if (!file_exists('/tmp')) {
$this->markTestSkipped();
}
putenv('SIMPLETEST_DB=sqlite://localhost//tmp/test2.sqlite');
$this->setUp();
$options = $this->container->get('database')->getConnectionOptions();
$this->assertNotEqual('', $options['prefix']['default']);
}
/**
* @covers ::setUp
*/
@ -216,4 +201,29 @@ class KernelTestBaseTest extends KernelTestBase {
$this->assertRegExp($expected, (string) $output);
}
/**
* {@inheritdoc}
*/
protected function tearDown() {
parent::tearDown();
// Check that all tables of the test instance have been deleted. At this
// point the original database connection is restored so we need to prefix
// the tables.
$connection = Database::getConnection();
if ($connection->databaseType() != 'sqlite') {
$tables = $connection->schema()->findTables($this->databasePrefix . '%');
$this->assertTrue(empty($tables), 'All test tables have been removed.');
}
else {
$result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", array(
':type' => 'table',
':table_name' => '%',
':pattern' => 'sqlite_%',
))->fetchAllKeyed(0, 0);
$this->assertTrue(empty($result), 'All test tables have been removed.');
}
}
}