Skip to content

Instantly share code, notes, and snippets.

@haringsrob
Created September 19, 2019 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haringsrob/faa6cd6f260cc67f8d9a791152358ba0 to your computer and use it in GitHub Desktop.
Save haringsrob/faa6cd6f260cc67f8d9a791152358ba0 to your computer and use it in GitHub Desktop.
Drupal specific database refreshing trait.
<?php
namespace OpenEuropa\Site\Tests\Behat\FeatureTraits;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Testwork\Hook\Scope\AfterSuiteScope;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use Drupal\Core\Database\Database;
/**
* Trait DatabaseRefreshingTrait.
*
* This trait adds some extra load to the tests, however it provides more
* consistent results and less need for managing cleanups.
*/
trait DatabaseRefreshingTrait {
/**
* A local cache property which tracks the first run status.
*
* @var bool
*/
public static $isFirstRun = TRUE;
/**
* Creates the initial backup when starting to run tests.
*
* @param \Behat\Testwork\Hook\Scope\BeforeSuiteScope $beforeSuiteScope
* The before suite scope.
*
* @BeforeSuite
*/
public static function saveDbBeforeSuite(BeforeSuiteScope $beforeSuiteScope): void {
echo 'DatabaseRefreshingTrait: Saving db to temporary file.' . PHP_EOL;
$dbConfig = self::getDatabaseInfo();
$file = $beforeSuiteScope->getEnvironment()
->getSuite()
->getSetting('paths');
$file = $file[0] . '/dump-behat-sql.sql';
exec("mysqldump --user={$dbConfig['user']} --password={$dbConfig['pass']} --host={$dbConfig['host']} {$dbConfig['name']} --result-file={$file} 2>&1");
}
/**
* Restores the db before a new scenario is running.
*
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $beforeScenarioScope
* The before scenario scope.
*
* @BeforeScenario
*/
public static function restoreStateBeforeScenario(BeforeScenarioScope $beforeScenarioScope): void {
// Avoid this from running on the first scenario.
if (self::$isFirstRun) {
self::$isFirstRun = FALSE;
return;
}
echo 'DatabaseRefreshingTrait: Restoring db for next scenario.' . PHP_EOL;
$dbConfig = self::getDatabaseInfo();
$file = $beforeScenarioScope->getEnvironment()
->getSuite()
->getSetting('paths');
$file = $file[0] . '/dump-behat-sql.sql';
exec("mysql --user={$dbConfig['user']} --password={$dbConfig['pass']} --host={$dbConfig['host']} {$dbConfig['name']} < {$file} 2>&1");
}
/**
* Restores once more after all tests are done.
*
* @param \Behat\Testwork\Hook\Scope\AfterSuiteScope $afterSuiteScope
* The after suite scope.
*
* @AfterSuite
*/
public static function restoreStateAfterSuite(AfterSuiteScope $afterSuiteScope): void {
echo 'DatabaseRefreshingTrait: Restoring db after test suite.' . PHP_EOL;
$dbConfig = self::getDatabaseInfo();
$file = $afterSuiteScope->getEnvironment()
->getSuite()
->getSetting('paths');
$file = $file[0] . '/dump-behat-sql.sql';
exec("mysql --user={$dbConfig['user']} --password={$dbConfig['pass']} --host={$dbConfig['host']} {$dbConfig['name']} < {$file} 2>&1");
exec("rm $file");
}
/**
* Gets the database info.
*
* @return array
* An array with 4 keys: user, pass, host, name.
*/
private static function getDatabaseInfo(): array {
$settings = Database::getConnectionInfo('default');
$settings = reset($settings);
return [
'user' => $settings['username'],
'pass' => $settings['password'],
'host' => $settings['host'],
'name' => $settings['database'],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment