Merge pull request #75 from halkyon/disable_create_database

Allow setting create_temp_database to false to stop temp db creation
This commit is contained in:
Mateusz U 2014-10-09 09:30:46 +13:00
commit 276b5298f5
4 changed files with 79 additions and 51 deletions

View File

@ -24,8 +24,13 @@ use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
*/
class SilverStripeAwareInitializer implements InitializerInterface
{
private $databaseName;
/**
* @var bool
*/
protected $createTempDatabase;
protected $databaseName;
/**
* @var Array
@ -57,40 +62,36 @@ class SilverStripeAwareInitializer implements InitializerInterface
*/
protected $testSessionEnvironment;
/**
* Initializes initializer.
*/
public function __construct($frameworkPath)
{
$this->bootstrap($frameworkPath);
protected $frameworkPath;
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
public function initTestSession() {
$this->bootstrap($this->frameworkPath);
$testEnv = \Injector::inst()->get('TestSessionEnvironment');
$testEnv->startTestSession(array(
'createDatabase' => true
));
if($this->createTempDatabase) {
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
$state = $testEnv->getState();
$testEnv = \Injector::inst()->get('TestSessionEnvironment');
$testEnv->startTestSession();
$this->databaseName = $state->database;
$this->testSessionEnvironment = $testEnv;
$state = $testEnv->getState();
$this->databaseName = $state->database;
$this->testSessionEnvironment = $testEnv;
file_put_contents('php://stdout', "Temp Database: $this->databaseName" . PHP_EOL . PHP_EOL);
file_put_contents('php://stdout', "Temp Database: $this->databaseName" . PHP_EOL . PHP_EOL);
register_shutdown_function(array($this, '__destruct'));
}
register_shutdown_function(array($this, 'killTestSession'));
}
}
public function __destruct()
{
file_put_contents('php://stdout', "Killing test session environment...");
public function killTestSession() {
file_put_contents('php://stdout', "Killing test session environment...");
if($this->testSessionEnvironment) {
$this->testSessionEnvironment->endTestSession();
}
if($this->testSessionEnvironment) {
$this->testSessionEnvironment->endTestSession();
}
file_put_contents('php://stdout', " done!" . PHP_EOL);
}
file_put_contents('php://stdout', " done!" . PHP_EOL);
}
/**
* Checks if initializer supports provided context.
@ -111,6 +112,7 @@ class SilverStripeAwareInitializer implements InitializerInterface
*/
public function initialize(ContextInterface $context)
{
$context->setCreateTempDatabase($this->createTempDatabase);
$context->setDatabase($this->databaseName);
$context->setAjaxSteps($this->ajaxSteps);
$context->setAjaxTimeout($this->ajaxTimeout);
@ -118,6 +120,18 @@ class SilverStripeAwareInitializer implements InitializerInterface
$context->setRegionMap($this->regionMap);
$context->setAdminUrl($this->adminUrl);
$context->setLoginUrl($this->loginUrl);
$this->initTestSession();
}
public function setFrameworkPath($path)
{
$this->frameworkPath = $path;
}
public function setCreateTempDatabase($bool)
{
$this->createTempDatabase = $bool;
}
public function setAjaxSteps($ajaxSteps)
@ -193,4 +207,4 @@ class SilverStripeAwareInitializer implements InitializerInterface
// Remove the error handler so that PHPUnit can add its own
restore_error_handler();
}
}
}

View File

@ -27,6 +27,9 @@ require_once 'vendor/autoload.php';
*/
class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface
{
protected $createTempDatabase;
protected $databaseName;
/**
@ -76,6 +79,10 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
$this->testSessionEnvironment = new \TestSessionEnvironment();
}
public function setCreateTempDatabase($bool) {
$this->createTempDatabase = $bool;
}
public function setDatabase($databaseName) {
$this->databaseName = $databaseName;
}
@ -180,29 +187,31 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
* @BeforeScenario
*/
public function before(ScenarioEvent $event) {
if (!isset($this->databaseName)) {
throw new \LogicException(
'Context\'s $databaseName has to be set when implementing SilverStripeAwareContextInterface.'
);
}
if($this->createTempDatabase) {
if (!isset($this->databaseName)) {
throw new \LogicException(
'Context\'s $databaseName has to be set when implementing SilverStripeAwareContextInterface.'
);
}
$state = $this->getTestSessionState();
$this->testSessionEnvironment->startTestSession($state);
$state = $this->getTestSessionState();
$this->testSessionEnvironment->startTestSession($state);
// Optionally import database
if(!empty($state['importDatabasePath'])) {
$this->testSessionEnvironment->importDatabase(
$state['importDatabasePath'],
!empty($state['requireDefaultRecords']) ? $state['requireDefaultRecords'] : false
);
} else if(!empty($state['requireDefaultRecords']) && $state['requireDefaultRecords']) {
$this->testSessionEnvironment->requireDefaultRecords();
}
// Optionally import database
if(!empty($state['importDatabasePath'])) {
$this->testSessionEnvironment->importDatabase(
$state['importDatabasePath'],
!empty($state['requireDefaultRecords']) ? $state['requireDefaultRecords'] : false
);
} else if(!empty($state['requireDefaultRecords']) && $state['requireDefaultRecords']) {
$this->testSessionEnvironment->requireDefaultRecords();
}
// Fixtures
$fixtureFile = (!empty($params['fixture'])) ? $params['fixture'] : null;
if($fixtureFile) {
$this->testSessionEnvironment->loadFixtureIntoDb($fixtureFile);
// Fixtures
$fixtureFile = (!empty($params['fixture'])) ? $params['fixture'] : null;
if($fixtureFile) {
$this->testSessionEnvironment->loadFixtureIntoDb($fixtureFile);
}
}
if($screenSize = getenv('BEHAT_SCREEN_SIZE')) {

View File

@ -54,6 +54,7 @@ class Extension implements ExtensionInterface
$container->setParameter('behat.silverstripe_extension.admin_url', $config['admin_url']);
$container->setParameter('behat.silverstripe_extension.login_url', $config['login_url']);
$container->setParameter('behat.silverstripe_extension.screenshot_path', $config['screenshot_path']);
$container->setParameter('behat.silverstripe_extension.create_temp_database', $config['create_temp_database']);
$container->setParameter('behat.silverstripe_extension.ajax_timeout', $config['ajax_timeout']);
if (isset($config['ajax_steps'])) {
$container->setParameter('behat.silverstripe_extension.ajax_steps', $config['ajax_steps']);
@ -82,6 +83,9 @@ class Extension implements ExtensionInterface
{
$builder->
children()->
scalarNode('create_temp_database')->
defaultValue(true)->
end()->
scalarNode('framework_path')->
defaultValue('framework')->
end()->

View File

@ -10,15 +10,16 @@ parameters:
behat.silverstripe_extension.admin_url: ~
behat.silverstripe_extension.login_url: ~
behat.silverstripe_extension.screenshot_path: ~
behat.silverstripe_extension.create_temp_database: true
behat.silverstripe_extension.module:
behat.silverstripe_extension.region_map: ~
behat.silverstripe_extension.context.path_suffix: tests/behat/features/
services:
behat.silverstripe_extension.context.initializer:
class: %behat.silverstripe_extension.context.initializer.class%
arguments:
- %behat.silverstripe_extension.framework_path%
calls:
- [setFrameworkPath, [%behat.silverstripe_extension.framework_path%]]
- [setCreateTempDatabase, [%behat.silverstripe_extension.create_temp_database%]]
- [setAjaxSteps, [%behat.silverstripe_extension.ajax_steps%]]
- [setAjaxTimeout, [%behat.silverstripe_extension.ajax_timeout%]]
- [setAdminUrl, [%behat.silverstripe_extension.admin_url%]]
@ -33,4 +34,4 @@ services:
- %behat.silverstripe_extension.context.namespace_suffix%
- %behat.context.class%
tags:
- { name: behat.context.class_guesser, priority: 10 }
- { name: behat.context.class_guesser, priority: 10 }