diff --git a/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php b/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php index f9abf99..4042364 100644 --- a/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php +++ b/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php @@ -24,13 +24,8 @@ use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface; */ class SilverStripeAwareInitializer implements InitializerInterface { - - /** - * @var bool - */ - protected $createTempDatabase; - - protected $databaseName; + + private $databaseName; /** * @var Array @@ -62,36 +57,40 @@ class SilverStripeAwareInitializer implements InitializerInterface */ protected $testSessionEnvironment; - protected $frameworkPath; + /** + * Initializes initializer. + */ + public function __construct($frameworkPath) + { + $this->bootstrap($frameworkPath); - public function initTestSession() { - $this->bootstrap($this->frameworkPath); + file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL); - if($this->createTempDatabase) { - file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL); + $testEnv = \Injector::inst()->get('TestSessionEnvironment'); + $testEnv->startTestSession(array( + 'createDatabase' => true + )); - $testEnv = \Injector::inst()->get('TestSessionEnvironment'); - $testEnv->startTestSession(); + $state = $testEnv->getState(); - $state = $testEnv->getState(); - $this->databaseName = $state->database; - $this->testSessionEnvironment = $testEnv; + $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, 'killTestSession')); - } - } + register_shutdown_function(array($this, '__destruct')); + } - public function killTestSession() { - file_put_contents('php://stdout', "Killing test session environment..."); + public function __destruct() + { + 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. @@ -112,7 +111,6 @@ 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); @@ -120,18 +118,6 @@ 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) @@ -207,4 +193,4 @@ class SilverStripeAwareInitializer implements InitializerInterface // Remove the error handler so that PHPUnit can add its own restore_error_handler(); } -} +} \ No newline at end of file diff --git a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php index 0a4d216..fce6d9f 100644 --- a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php +++ b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php @@ -27,9 +27,6 @@ require_once 'vendor/autoload.php'; */ class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface { - - protected $createTempDatabase; - protected $databaseName; /** @@ -79,10 +76,6 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex $this->testSessionEnvironment = new \TestSessionEnvironment(); } - public function setCreateTempDatabase($bool) { - $this->createTempDatabase = $bool; - } - public function setDatabase($databaseName) { $this->databaseName = $databaseName; } @@ -187,31 +180,29 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex * @BeforeScenario */ public function before(ScenarioEvent $event) { - if($this->createTempDatabase) { - if (!isset($this->databaseName)) { - throw new \LogicException( - 'Context\'s $databaseName has to be set when implementing SilverStripeAwareContextInterface.' - ); - } + 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')) { diff --git a/src/SilverStripe/BehatExtension/Extension.php b/src/SilverStripe/BehatExtension/Extension.php index e2cf836..b8ea3f7 100644 --- a/src/SilverStripe/BehatExtension/Extension.php +++ b/src/SilverStripe/BehatExtension/Extension.php @@ -54,7 +54,6 @@ 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']); @@ -83,9 +82,6 @@ class Extension implements ExtensionInterface { $builder-> children()-> - scalarNode('create_temp_database')-> - defaultValue(true)-> - end()-> scalarNode('framework_path')-> defaultValue('framework')-> end()-> diff --git a/src/SilverStripe/BehatExtension/services/silverstripe.yml b/src/SilverStripe/BehatExtension/services/silverstripe.yml index b5dc968..756e4d0 100644 --- a/src/SilverStripe/BehatExtension/services/silverstripe.yml +++ b/src/SilverStripe/BehatExtension/services/silverstripe.yml @@ -10,16 +10,15 @@ 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%]] @@ -34,4 +33,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 } \ No newline at end of file