From d61760ddc603faf031697cc3937087612b22d3a6 Mon Sep 17 00:00:00 2001 From: madmatt Date: Wed, 5 Feb 2014 11:57:46 +1300 Subject: [PATCH] Update SilverStripeAwareInitializer to use new TestSessionEnvironment class. This change is designed to be merged in at the same time as the matching testsession PR (silverstripe-labs/silverstripe-testsession#7) is merged in, as it relies on changes introduced in that PR to function. This updates the behat-extension to use the new file-based capabilities of the testsession module. Instead of creating a temp database, it will create an entire testsession in the initializer, and then continue on as per normal. When Behat runs end, the cleanup code will completely remove the testsession state, instead of just removing the temp database. This may mean in the future that you can hook into the TestSessionEnvironment via extensions, and connect to other test services during a testsession, then cleanup after yourself on ending a test session. API CHANGES: - Remove SilverStripeAwareInitializer::initializeTempDb() - Remove SilverStripeAwareInitializer::deleteTempDb() --- .../SilverStripeAwareInitializer.php | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php b/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php index 304c7e7..e1c85b6 100644 --- a/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php +++ b/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php @@ -52,18 +52,42 @@ class SilverStripeAwareInitializer implements InitializerInterface */ protected $screenshotPath; + /** + * @var object {@link TestSessionEnvironment} + */ + protected $testSessionEnvironment; + /** * Initializes initializer. */ public function __construct($frameworkPath) { $this->bootstrap($frameworkPath); - $this->databaseName = $this->initializeTempDb(); + + file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL); + + $testEnv = \Injector::inst()->get('TestSessionEnvironment'); + + $testEnv->startTestSession(array( + 'createDatabase' => true + )); + + $state = $testEnv->getState(); + $this->databaseName = $state->database; + + $this->testSessionEnvironment = $testEnv; + + file_put_contents('php://stdout', "Temp Database: $this->databaseName" . PHP_EOL . PHP_EOL); } public function __destruct() { - $this->deleteTempDb(); + file_put_contents('php://stdout', "Killing test session environment..."); + + $testEnv = \Injector::inst()->get('TestSessionEnvironment'); + $testEnv->endTestSession(); + + file_put_contents('php://stdout', " done!" . PHP_EOL); } /** @@ -158,20 +182,4 @@ class SilverStripeAwareInitializer implements InitializerInterface // Remove the error handler so that PHPUnit can add its own restore_error_handler(); } - - protected function initializeTempDb() - { - $dbname = \SapphireTest::create_temp_db(); - file_put_contents('php://stdout', "Creating temp DB $dbname" . PHP_EOL); - \DB::set_alternative_database_name($dbname); - - return $dbname; - } - - protected function deleteTempDb() - { - file_put_contents('php://stdout', "Killing temp DB" . PHP_EOL); - \SapphireTest::kill_temp_db(); - \DB::set_alternative_database_name(null); - } } \ No newline at end of file