Skip to content
This repository has been archived by the owner on Aug 13, 2018. It is now read-only.

Commit

Permalink
API change database session selection to setdb endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
michalochman committed Aug 7, 2012
1 parent e48151a commit 30f4bab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 53 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/ */
class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface class SilverStripeContext extends MinkContext implements SilverStripeAwareContextInterface
{ {
private $session_key; private $database_name;


protected $context; protected $context;
protected $fixtures; protected $fixtures;
Expand All @@ -46,9 +46,9 @@ public function __construct(array $parameters)
$this->context = $parameters; $this->context = $parameters;
} }


public function setSessionKey($session_key) public function setDatabase($database_name)
{ {
$this->session_key = $session_key; $this->database_name = $database_name;
} }


public function getFixture($data_object) public function getFixture($data_object)
Expand All @@ -70,15 +70,13 @@ public function getFixtures()
*/ */
public function before(ScenarioEvent $event) public function before(ScenarioEvent $event)
{ {
if (!isset($this->session_key)) { if (!isset($this->database_name)) {
throw new \LogicException('Context\'s $session_key has to be set when implementing SilverStripeAwareContextInterface.'); throw new \LogicException('Context\'s $database_name has to be set when implementing SilverStripeAwareContextInterface.');
} }


$selectsession_url = $this->joinUrlParts($this->context['base_url'], '/dev/tests/selectsession'); $setdb_url = $this->joinUrlParts($this->context['base_url'], '/dev/tests/setdb');
$this->getSession()->visit($selectsession_url); $setdb_url = sprintf('%s?database=%s', $setdb_url, $this->database_name);
$page = $this->getSession()->getPage(); $this->getSession()->visit($setdb_url);
$page->find('css', '#testSessionKey')->setValue($this->session_key);
$page->find('css', '#select-session')->click();
} }


/** /**
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,25 +24,20 @@
*/ */
class SilverStripeAwareInitializer implements InitializerInterface class SilverStripeAwareInitializer implements InitializerInterface
{ {
private $session_key; private $database_name;
private $session_file;


/** /**
* Initializes initializer. * Initializes initializer.
*/ */
public function __construct($framework_path, $framework_host) public function __construct($framework_path, $framework_host)
{ {
$this->bootstrap($framework_path, $framework_host); $this->bootstrap($framework_path, $framework_host);
$database_config = $this->initializeTempDb(); $this->database_name = $this->initializeTempDb();

$this->session_key = $this->generateSessionKey($database_config);
$this->session_file = $this->persistSession($database_config, $this->session_key);
} }


public function __destruct() public function __destruct()
{ {
$this->deleteTempDb(); $this->deleteTempDb();
$this->forgetSession();
} }


/** /**
Expand All @@ -64,7 +59,7 @@ public function supports(ContextInterface $context)
*/ */
public function initialize(ContextInterface $context) public function initialize(ContextInterface $context)
{ {
$context->setSessionKey($this->session_key); $context->setDatabase($this->database_name);
} }


protected function bootstrap($framework_path, $framework_host) protected function bootstrap($framework_path, $framework_host)
Expand All @@ -87,13 +82,8 @@ protected function initializeTempDb()
file_put_contents('php://stderr', 'Creating temp DB' . PHP_EOL); file_put_contents('php://stderr', 'Creating temp DB' . PHP_EOL);
$dbname = \SapphireTest::create_temp_db(); $dbname = \SapphireTest::create_temp_db();
\DB::set_alternative_database_name($dbname); \DB::set_alternative_database_name($dbname);
$database_config = array(
'databaseConfig' => array(
'database' => $dbname,
),
);


return json_encode($database_config); return $dbname;
} }


protected function deleteTempDb() protected function deleteTempDb()
Expand All @@ -102,31 +92,4 @@ protected function deleteTempDb()
\SapphireTest::kill_temp_db(); \SapphireTest::kill_temp_db();
\DB::set_alternative_database_name(null); \DB::set_alternative_database_name(null);
} }

protected function generateSessionKey($database_config)
{
return sha1(sprintf('%s%s', $database_config, microtime(true)));
}

protected function persistSession($database_config, $session_key)
{
file_put_contents('php://stderr', 'Saving testSessionKey file' . PHP_EOL);
$temp_dir = '/tmp';
$test_sessions_dir = $temp_dir . DIRECTORY_SEPARATOR . 'testsessions';
if (!file_exists($test_sessions_dir)) {
mkdir($test_sessions_dir);
}
$test_session_file = $test_sessions_dir . DIRECTORY_SEPARATOR . $session_key;
file_put_contents($test_session_file, $database_config);

return $test_session_file;
}

protected function forgetSession()
{
file_put_contents('php://stderr', 'Removing testSessionKey file' . PHP_EOL);
if (file_exists($this->session_file)) {
unlink($this->session_file);
}
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface SilverStripeAwareContextInterface
/** /**
* Sets SilverStripe instance. * Sets SilverStripe instance.
* *
* @param String $session_key testSessionKey used in TEMP_FOLDER/testsessions/<testSessionKey> file * @param String $database_name Temp database name
*/ */
public function setSessionKey($session_key); public function setDatabase($database_name);
} }

0 comments on commit 30f4bab

Please sign in to comment.