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()
This commit is contained in:
madmatt 2014-02-05 11:57:46 +13:00
parent fe77d1dacb
commit d61760ddc6

View File

@ -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);
}
}