mirror of
https://github.com/silverstripe/silverstripe-behat-extension
synced 2024-10-22 17:05:32 +02:00
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:
parent
e1d42b89fe
commit
4d65b2af4e
@ -24,8 +24,8 @@ use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
|
||||
*/
|
||||
class SilverStripeAwareInitializer implements InitializerInterface
|
||||
{
|
||||
|
||||
private $databaseName;
|
||||
|
||||
protected $databaseName;
|
||||
|
||||
/**
|
||||
* @var Array
|
||||
@ -57,40 +57,38 @@ class SilverStripeAwareInitializer implements InitializerInterface
|
||||
*/
|
||||
protected $testSessionEnvironment;
|
||||
|
||||
/**
|
||||
* Initializes initializer.
|
||||
*/
|
||||
public function __construct($frameworkPath)
|
||||
{
|
||||
$this->bootstrap($frameworkPath);
|
||||
protected $frameworkPath;
|
||||
|
||||
protected $useTestSession;
|
||||
|
||||
protected $bootstrapInitialized = false;
|
||||
|
||||
protected $testSessionInitialized = false;
|
||||
|
||||
public function initTestSession() {
|
||||
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
|
||||
|
||||
$testEnv = \Injector::inst()->get('TestSessionEnvironment');
|
||||
$testEnv->startTestSession(array(
|
||||
'createDatabase' => true
|
||||
));
|
||||
$testEnv->startTestSession();
|
||||
|
||||
$state = $testEnv->getState();
|
||||
|
||||
$this->databaseName = $state->database;
|
||||
$this->testSessionEnvironment = $testEnv;
|
||||
|
||||
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 +109,7 @@ class SilverStripeAwareInitializer implements InitializerInterface
|
||||
*/
|
||||
public function initialize(ContextInterface $context)
|
||||
{
|
||||
$context->setUseTestSession($this->useTestSession);
|
||||
$context->setDatabase($this->databaseName);
|
||||
$context->setAjaxSteps($this->ajaxSteps);
|
||||
$context->setAjaxTimeout($this->ajaxTimeout);
|
||||
@ -118,6 +117,26 @@ class SilverStripeAwareInitializer implements InitializerInterface
|
||||
$context->setRegionMap($this->regionMap);
|
||||
$context->setAdminUrl($this->adminUrl);
|
||||
$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)
|
||||
@ -178,19 +197,16 @@ class SilverStripeAwareInitializer implements InitializerInterface
|
||||
$this->regionMap = $regionMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param String Absolute path to 'framework' module
|
||||
*/
|
||||
protected function bootstrap($frameworkPath)
|
||||
protected function bootstrap()
|
||||
{
|
||||
file_put_contents('php://stdout', 'Bootstrapping' . PHP_EOL);
|
||||
|
||||
// Connect to database and build manifest
|
||||
$_GET['flush'] = 1;
|
||||
require_once $frameworkPath . '/core/Core.php';
|
||||
require_once $this->frameworkPath . '/core/Core.php';
|
||||
unset($_GET['flush']);
|
||||
|
||||
// Remove the error handler so that PHPUnit can add its own
|
||||
restore_error_handler();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,9 @@ require_once 'vendor/autoload.php';
|
||||
*/
|
||||
class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface
|
||||
{
|
||||
|
||||
protected $useTestSession;
|
||||
|
||||
protected $databaseName;
|
||||
|
||||
/**
|
||||
@ -76,6 +79,10 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
|
||||
$this->testSessionEnvironment = new \TestSessionEnvironment();
|
||||
}
|
||||
|
||||
public function setUseTestSession($bool) {
|
||||
$this->useTestSession = $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->useTestSession) {
|
||||
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')) {
|
||||
|
@ -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.use_test_session', $config['use_test_session']);
|
||||
$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('use_test_session')->
|
||||
defaultValue(true)->
|
||||
end()->
|
||||
scalarNode('framework_path')->
|
||||
defaultValue('framework')->
|
||||
end()->
|
||||
|
@ -10,15 +10,16 @@ parameters:
|
||||
behat.silverstripe_extension.admin_url: ~
|
||||
behat.silverstripe_extension.login_url: ~
|
||||
behat.silverstripe_extension.screenshot_path: ~
|
||||
behat.silverstripe_extension.use_test_session: 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%]]
|
||||
- [setUseTestSession, [%behat.silverstripe_extension.use_test_session%]]
|
||||
- [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 }
|
||||
|
Loading…
Reference in New Issue
Block a user