diff --git a/src/SilverStripe/BehatExtension/Context/FixtureContext.php b/src/SilverStripe/BehatExtension/Context/FixtureContext.php index 3086943..d5c6e9c 100644 --- a/src/SilverStripe/BehatExtension/Context/FixtureContext.php +++ b/src/SilverStripe/BehatExtension/Context/FixtureContext.php @@ -183,7 +183,17 @@ class FixtureContext extends BehatContext if($class == 'File' || is_subclass_of($class, 'File')) { $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); + } } /** diff --git a/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php b/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php index 304c7e7..7881e19 100644 --- a/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php +++ b/src/SilverStripe/BehatExtension/Context/Initializer/SilverStripeAwareInitializer.php @@ -52,18 +52,41 @@ 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..."); + + $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 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