Merge pull request #23 from madmatt/pulls/TestSessionEnvironment

Update SilverStripeAwareInitializer to use new TestSessionEnvironment class
This commit is contained in:
Ingo Schommer 2014-02-09 16:49:40 +13:00
commit 0308dee0dc
2 changed files with 36 additions and 19 deletions

View File

@ -183,7 +183,17 @@ class FixtureContext extends BehatContext
if($class == 'File' || is_subclass_of($class, 'File')) { if($class == 'File' || is_subclass_of($class, 'File')) {
$fields = $this->prepareAsset($class, $id, $fields); $fields = $this->prepareAsset($class, $id, $fields);
} }
$this->fixtureFactory->createObject($class, $id, $fields);
// We should check if this fixture object already exists - if it does, we update it. If not, we create it
if($existingFixture = $this->fixtureFactory->get($class, $id)) {
// Merge existing data with new data, and create new object to replace existing object
foreach($fields as $k => $v) {
$existingFixture->$k = $v;
}
$existingFixture->write();
} else {
$this->fixtureFactory->createObject($class, $id, $fields);
}
} }
/** /**

View File

@ -52,18 +52,41 @@ class SilverStripeAwareInitializer implements InitializerInterface
*/ */
protected $screenshotPath; protected $screenshotPath;
/**
* @var object {@link TestSessionEnvironment}
*/
protected $testSessionEnvironment;
/** /**
* Initializes initializer. * Initializes initializer.
*/ */
public function __construct($frameworkPath) public function __construct($frameworkPath)
{ {
$this->bootstrap($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() public function __destruct()
{ {
$this->deleteTempDb(); file_put_contents('php://stdout', "Killing test session environment...");
$this->testSessionEnvironment->endTestSession();
file_put_contents('php://stdout', " done!" . PHP_EOL);
} }
/** /**
@ -158,20 +181,4 @@ class SilverStripeAwareInitializer implements InitializerInterface
// 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();
} }
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);
}
} }