Skip to content

Commit

Permalink
Merge pull request #38 from open-sausages/pulls/4.0/namespace-everything
Browse files Browse the repository at this point in the history
Upgrade for framework namespacing
  • Loading branch information
chillu authored Sep 8, 2016
2 parents 3b06f5f + 1bedade commit 7b91881
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
2 changes: 2 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use SilverStripe\ORM\DB;
use SilverStripe\Core\Injector\Injector;


// Determine whether there is a testsession currently running, and if so - setup the persistent details for it.
Injector::inst()->get('TestSessionEnvironment')->loadFromFile();
Expand Down
26 changes: 23 additions & 3 deletions code/TestSessionController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
<?php

use SilverStripe\ORM\DB;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\Session;
use SilverStripe\Control\SS_HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;

/**
* Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie.
Expand Down Expand Up @@ -51,7 +70,8 @@ public function init()
&& (Director::isDev() || Director::isTest() || Director::is_cli() || Permission::check("ADMIN"))
);
if (!$canAccess) {
return Security::permissionFailure($this);
Security::permissionFailure($this);
return;
}

Requirements::javascript('framework/thirdparty/jquery/jquery.js');
Expand Down Expand Up @@ -84,7 +104,7 @@ public function start()
if (!empty($params['globalTestSession'])) {
$id = null;
} else {
$generator = Injector::inst()->get('RandomGenerator');
$generator = Injector::inst()->get('SilverStripe\\Security\\RandomGenerator');
$id = substr($generator->randomToken(), 0, 10);
Session::set('TestSessionId', $id);
}
Expand Down
25 changes: 17 additions & 8 deletions code/TestSessionEnvironment.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?php

use SilverStripe\Control\Director;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Object;
use SilverStripe\Dev\FixtureFactory;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DatabaseAdmin;
use SilverStripe\ORM\Versioning\Versioned;


/**
* Responsible for starting and finalizing test sessions.
* Since these session span across multiple requests, session information is persisted
Expand Down Expand Up @@ -120,6 +127,7 @@ public function getId()
* onBeforeApplyState() and onAfterApplyState(). See the {@link self::applyState()} method for more.
*
* @param array $state An array of test state options to write.
* @param mixed $id
*/
public function startTestSession($state = null, $id = null)
{
Expand Down Expand Up @@ -163,15 +171,14 @@ public function updateTestSession($state)
* You can extend this by creating an Extension object and implementing either onBeforeApplyState() or
* onAfterApplyState() to add your own test state handling in.
*
* @param mixed $state
* @throws LogicException
* @throws InvalidArgumentException
*/
public function applyState($state)
{
$this->extend('onBeforeApplyState', $state);

$database = (isset($state->database)) ? $state->database : null;

// back up source
global $databaseConfig;
$this->oldDatabaseName = $databaseConfig['database'];
Expand Down Expand Up @@ -244,7 +251,7 @@ public function applyState($state)
$mailer = (isset($state->mailer)) ? $state->mailer : null;

if ($mailer) {
if (!class_exists($mailer) || !is_subclass_of($mailer, 'Mailer')) {
if (!class_exists($mailer) || !is_subclass_of($mailer, 'SilverStripe\\Control\\Email\\Mailer')) {
throw new InvalidArgumentException(sprintf(
'Class "%s" is not a valid class, or subclass of Mailer',
$mailer
Expand Down Expand Up @@ -272,7 +279,7 @@ public function applyState($state)
* Import the database
*
* @param String $path Absolute path to a SQL dump (include DROP TABLE commands)
* @return void
* @param bool $requireDefaultRecords
*/
public function importDatabase($path, $requireDefaultRecords = false)
{
Expand Down Expand Up @@ -309,6 +316,8 @@ public function requireDefaultRecords()
/**
* Sliented as if the file already exists by another process, we don't want
* to modify.
*
* @param mixed $state
*/
public function saveState($state)
{
Expand Down Expand Up @@ -367,11 +376,11 @@ public function endTestSession()

if (SapphireTest::using_temp_db()) {
$state = $this->getState();
$dbConn = DB::getConn();
$dbConn = DB::get_schema();
$dbExists = $dbConn->databaseExists($state->database);
if($dbExists) {
// Clean up temp database
$dbConn->dropDatabase();
$dbConn->dropDatabase($state->database);
file_put_contents('php://stdout', "Deleted temp database: $state->database" . PHP_EOL);
}
// End test session mode
Expand Down Expand Up @@ -406,8 +415,8 @@ public function loadFixtureIntoDb($fixtureFile)
throw new LogicException("Fixture file must be inside the tests subfolder of one of your modules.");
}

$factory = Injector::inst()->create('FixtureFactory');
$fixture = Injector::inst()->create('YamlFixture', $fixtureFile);
$factory = Injector::inst()->create('SilverStripe\\Dev\\FixtureFactory');
$fixture = Injector::inst()->create('SilverStripe\\Dev\\YamlFixture', $fixtureFile);
$fixture->writeInto($factory);

$state = $this->getState();
Expand Down
12 changes: 10 additions & 2 deletions code/TestSessionRequestFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
use SilverStripe\ORM\DataModel;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\DB;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Control\SS_HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director;
use SilverStripe\Control\SS_HTTPResponse;
use SilverStripe\Control\RequestFilter;


/**
* Sets state previously initialized through {@link TestSessionController}.
Expand Down Expand Up @@ -36,8 +44,8 @@ public function preRequest(SS_HTTPRequest $request, Session $session, DataModel
// Register mailer
if (isset($testState->mailer)) {
$mailer = $testState->mailer;
Injector::inst()->registerService(new $mailer(), 'Mailer');
Config::inst()->update("Email", "send_all_emails_to", null);
Injector::inst()->registerService(new $mailer(), 'SilverStripe\\Control\\Email\\Mailer');
Config::inst()->update("SilverStripe\\Control\\Email\\Email", "send_all_emails_to", null);
}

// Allows inclusion of a PHP file, usually with procedural commands
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/TestSessionStubCodeWriterTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use SilverStripe\Dev\SapphireTest;

class TestSessionStubCodeWriterTest extends SapphireTest
{

Expand Down

0 comments on commit 7b91881

Please sign in to comment.