Skip to content

Instantly share code, notes, and snippets.

@WengerK
Last active November 4, 2021 13:16
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 WengerK/1a4db460cd5a6376ca393794b70f2493 to your computer and use it in GitHub Desktop.
Save WengerK/1a4db460cd5a6376ca393794b70f2493 to your computer and use it in GitHub Desktop.
Behat on Drupal - clearing Data Between Scenarios

Article Ressources - Behat on Drupal - clearing Data Between Scenarios

This is the Gist repository for my article Behat on Drupal - clearing Data Between Scenarios.

Be aware that this article has been wrote for the Blog of Antistatique — Web Agency in Lausanne, Switzerland. A place where I work as Full Stack Web Developer.

Feel free to read it the full article on Medium or check it out on Antistatique.

Content of this gist :

  • behat.yml : The minimal configuration to use the context.
  • DatabaseContext.php : The Behat Context that deal with database dumps.
default:
suites:
default:
contexts:
- Drupal\Behat\Context\Drupal\DatabaseContext:
dump_path: '%paths.base%/dump/behat'
<?php
namespace Drupal\Behat\Context\Drupal;
use Drupal\Component\Utility\Random;
use Drupal\DrupalExtension\Context\RawDrupalContext;
/**
* Defines Database features from the specific context.
*/
class DatabaseContext extends RawDrupalContext {
/**
* The dump path directory from the root.
*
* @var string
*/
public $dumpPath;
/**
* The last dump created.
*
* @var string
*/
protected static $dump = NULL;
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct($dump_path) {
// Create the directory if not already exists.
if (!is_dir($dump_path)) {
mkdir($dump_path, 0777, TRUE);
}
$this->dumpPath = $dump_path;
}
/**
* Generate a dump of the currente database state.
*
* @BeforeScenario @preserveDatabase
*/
public function preserveDatabase() {
$random = new Random();
self::$dump = $this->dumpTo($random->name(10, TRUE));
}
/**
* Reload a dump of the previous database state.
*
* @AfterScenario @preserveDatabase
*/
public function reloadDatabase() {
$this->loadFrom(self::$dump);
}
/**
* Dump the current database into the given filename.
*
* @param string $filename
* The filename.
*
* @throws \Exception
*
* @return string
* The dump file path.
*/
private function dumpTo($filename) {
if (!is_dir($this->dumpPath)) {
throw new \Exception(sprintf('The dump directory "%s" does not exists.', $this->dumpPath));
}
$file_and_path = $this->dumpPath . \DIRECTORY_SEPARATOR . $filename . '.sql';
print sprintf("\e[0;34mSQL dump: %s\e[0m\n", $file_and_path);
exec("../vendor/bin/drush sql-dump --result-file={$file_and_path} -y");
return $file_and_path;
}
/**
* Reload the database file.
*
* @param string $file_and_path
* The filename & path.
*
* @throws \Exception
*/
private function loadFrom($file_and_path) {
if (!is_file($file_and_path)) {
throw new \Exception(sprintf('The dump file "%s" does not exists.', $file_and_path));
}
exec("../vendor/bin/drush sql-cli < {$file_and_path}");
unlink($file_and_path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment