Allow setting use_test_session to false to stop using testsession module

This means it won't create a temporary database, and this can be up to
the developer to determine the database to connect to instead.

e.g. in your behat.yml:

```
default:
...

  extensions:
    ...
    SilverStripe\BehatExtension\Extension:
      use_test_session: false
```
This commit is contained in:
Sean Harvey 2014-10-08 19:20:55 +13:00
parent e1d42b89fe
commit 4d65b2af4e
4 changed files with 81 additions and 51 deletions

View File

@ -24,8 +24,8 @@ use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
*/ */
class SilverStripeAwareInitializer implements InitializerInterface class SilverStripeAwareInitializer implements InitializerInterface
{ {
private $databaseName; protected $databaseName;
/** /**
* @var Array * @var Array
@ -57,40 +57,38 @@ class SilverStripeAwareInitializer implements InitializerInterface
*/ */
protected $testSessionEnvironment; protected $testSessionEnvironment;
/** protected $frameworkPath;
* Initializes initializer.
*/
public function __construct($frameworkPath)
{
$this->bootstrap($frameworkPath);
protected $useTestSession;
protected $bootstrapInitialized = false;
protected $testSessionInitialized = false;
public function initTestSession() {
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL); file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
$testEnv = \Injector::inst()->get('TestSessionEnvironment'); $testEnv = \Injector::inst()->get('TestSessionEnvironment');
$testEnv->startTestSession(array( $testEnv->startTestSession();
'createDatabase' => true
));
$state = $testEnv->getState(); $state = $testEnv->getState();
$this->databaseName = $state->database; $this->databaseName = $state->database;
$this->testSessionEnvironment = $testEnv; $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() public function killTestSession() {
{ file_put_contents('php://stdout', "Killing test session environment...");
file_put_contents('php://stdout', "Killing test session environment...");
if($this->testSessionEnvironment) { if($this->testSessionEnvironment) {
$this->testSessionEnvironment->endTestSession(); $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. * Checks if initializer supports provided context.
@ -111,6 +109,7 @@ class SilverStripeAwareInitializer implements InitializerInterface
*/ */
public function initialize(ContextInterface $context) public function initialize(ContextInterface $context)
{ {
$context->setUseTestSession($this->useTestSession);
$context->setDatabase($this->databaseName); $context->setDatabase($this->databaseName);
$context->setAjaxSteps($this->ajaxSteps); $context->setAjaxSteps($this->ajaxSteps);
$context->setAjaxTimeout($this->ajaxTimeout); $context->setAjaxTimeout($this->ajaxTimeout);
@ -118,6 +117,26 @@ class SilverStripeAwareInitializer implements InitializerInterface
$context->setRegionMap($this->regionMap); $context->setRegionMap($this->regionMap);
$context->setAdminUrl($this->adminUrl); $context->setAdminUrl($this->adminUrl);
$context->setLoginUrl($this->loginUrl); $context->setLoginUrl($this->loginUrl);
if(!$this->bootstrapInitialized) {
$this->bootstrap();
$this->bootstrapInitialized = true;
}
if($this->useTestSession && !$this->testSessionInitialized) {
$this->initTestSession();
$this->testSessionInitialized = true;
}
}
public function setFrameworkPath($path)
{
$this->frameworkPath = $path;
}
public function setUseTestSession($bool)
{
$this->useTestSession = $bool;
} }
public function setAjaxSteps($ajaxSteps) public function setAjaxSteps($ajaxSteps)
@ -178,19 +197,16 @@ class SilverStripeAwareInitializer implements InitializerInterface
$this->regionMap = $regionMap; $this->regionMap = $regionMap;
} }
/** protected function bootstrap()
* @param String Absolute path to 'framework' module
*/
protected function bootstrap($frameworkPath)
{ {
file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL); file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL);
// Connect to database and build manifest // Connect to database and build manifest
$_GET['flush'] = 1; $_GET['flush'] = 1;
require_once $frameworkPath . '/core/Core.php'; require_once $this->frameworkPath . '/core/Core.php';
unset($_GET['flush']); unset($_GET['flush']);
// Remove the error handler so that PHPUnit can add its own // Remove the error handler so that PHPUnit can add its own
restore_error_handler(); restore_error_handler();
} }
} }

View File

@ -27,6 +27,9 @@ require_once 'vendor/autoload.php';
*/ */
class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface
{ {
protected $useTestSession;
protected $databaseName; protected $databaseName;
/** /**
@ -76,6 +79,10 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
$this->testSessionEnvironment = new \TestSessionEnvironment(); $this->testSessionEnvironment = new \TestSessionEnvironment();
} }
public function setUseTestSession($bool) {
$this->useTestSession = $bool;
}
public function setDatabase($databaseName) { public function setDatabase($databaseName) {
$this->databaseName = $databaseName; $this->databaseName = $databaseName;
} }
@ -180,29 +187,31 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
* @BeforeScenario * @BeforeScenario
*/ */
public function before(ScenarioEvent $event) { public function before(ScenarioEvent $event) {
if (!isset($this->databaseName)) { if($this->useTestSession) {
throw new \LogicException( if (!isset($this->databaseName)) {
'Context\'s $databaseName has to be set when implementing SilverStripeAwareContextInterface.' throw new \LogicException(
); 'Context\'s $databaseName has to be set when implementing SilverStripeAwareContextInterface.'
} );
}
$state = $this->getTestSessionState(); $state = $this->getTestSessionState();
$this->testSessionEnvironment->startTestSession($state); $this->testSessionEnvironment->startTestSession($state);
// Optionally import database // Optionally import database
if(!empty($state['importDatabasePath'])) { if(!empty($state['importDatabasePath'])) {
$this->testSessionEnvironment->importDatabase( $this->testSessionEnvironment->importDatabase(
$state['importDatabasePath'], $state['importDatabasePath'],
!empty($state['requireDefaultRecords']) ? $state['requireDefaultRecords'] : false !empty($state['requireDefaultRecords']) ? $state['requireDefaultRecords'] : false
); );
} else if(!empty($state['requireDefaultRecords']) && $state['requireDefaultRecords']) { } else if(!empty($state['requireDefaultRecords']) && $state['requireDefaultRecords']) {
$this->testSessionEnvironment->requireDefaultRecords(); $this->testSessionEnvironment->requireDefaultRecords();
} }
// Fixtures // Fixtures
$fixtureFile = (!empty($params['fixture'])) ? $params['fixture'] : null; $fixtureFile = (!empty($params['fixture'])) ? $params['fixture'] : null;
if($fixtureFile) { if($fixtureFile) {
$this->testSessionEnvironment->loadFixtureIntoDb($fixtureFile); $this->testSessionEnvironment->loadFixtureIntoDb($fixtureFile);
}
} }
if($screenSize = getenv('BEHAT_SCREEN_SIZE')) { 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.admin_url', $config['admin_url']);
$container->setParameter('behat.silverstripe_extension.login_url', $config['login_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.screenshot_path', $config['screenshot_path']);
$container->setParameter('behat.silverstripe_extension.use_test_session', $config['use_test_session']);
$container->setParameter('behat.silverstripe_extension.ajax_timeout', $config['ajax_timeout']); $container->setParameter('behat.silverstripe_extension.ajax_timeout', $config['ajax_timeout']);
if (isset($config['ajax_steps'])) { if (isset($config['ajax_steps'])) {
$container->setParameter('behat.silverstripe_extension.ajax_steps', $config['ajax_steps']); $container->setParameter('behat.silverstripe_extension.ajax_steps', $config['ajax_steps']);
@ -82,6 +83,9 @@ class Extension implements ExtensionInterface
{ {
$builder-> $builder->
children()-> children()->
scalarNode('use_test_session')->
defaultValue(true)->
end()->
scalarNode('framework_path')-> scalarNode('framework_path')->
defaultValue('framework')-> defaultValue('framework')->
end()-> end()->

View File

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