mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
Merge pull request #49 from open-sausages/pulls/4.0/app-object
[WIP] App object refactor
This commit is contained in:
commit
787ba1cc67
34
.travis.yml
34
.travis.yml
@ -1,34 +0,0 @@
|
||||
language: php
|
||||
|
||||
sudo: false
|
||||
|
||||
env:
|
||||
global:
|
||||
- COMPOSER_ROOT_VERSION=2.0.x-dev
|
||||
- CORE_RELEASE=master
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- php: 5.6
|
||||
env:
|
||||
- PHPUNIT_TEST=1
|
||||
- DB=PGSQL
|
||||
- php: 5.6
|
||||
env:
|
||||
- PHPUNIT_TEST=1
|
||||
- DB=MYSQL
|
||||
- php: 5.6
|
||||
env:
|
||||
- PHPCS_TEST=1
|
||||
- DB=MYSQL
|
||||
|
||||
before_script:
|
||||
- if [[ $PHPCS_TEST ]]; then pyrus install pear/PHP_CodeSniffer; fi
|
||||
- phpenv rehash
|
||||
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
|
||||
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
|
||||
- cd ~/builds/ss
|
||||
|
||||
script:
|
||||
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit testsession/tests/; fi
|
||||
- if [[ $PHPCS_TEST ]]; then (cd testsession && composer run-script lint); fi
|
28
_config.php
28
_config.php
@ -10,21 +10,21 @@ TestSessionEnvironment::singleton()->loadFromFile();
|
||||
* This closure will run every time a Resque_Event is forked (just before it is forked, so it applies to the parent
|
||||
* and child process).
|
||||
*/
|
||||
if(class_exists('Resque_Event') && class_exists('SSResqueRun')) {
|
||||
Resque_Event::listen('beforeFork', function($data) {
|
||||
global $databaseConfig;
|
||||
if (class_exists('Resque_Event') && class_exists('SSResqueRun')) {
|
||||
Resque_Event::listen('beforeFork', function ($data) {
|
||||
$databaseConfig = DB::getConfig();
|
||||
|
||||
// Reconnect to the database - this may connect to the old DB first, but is required because these processes
|
||||
// are long-lived, and MySQL connections often get closed in between worker runs. We need to connect before
|
||||
// calling {@link TestSessionEnvironment::loadFromFile()}.
|
||||
DB::connect($databaseConfig);
|
||||
// Reconnect to the database - this may connect to the old DB first, but is required because these processes
|
||||
// are long-lived, and MySQL connections often get closed in between worker runs. We need to connect before
|
||||
// calling {@link TestSessionEnvironment::loadFromFile()}.
|
||||
DB::connect($databaseConfig);
|
||||
|
||||
$testEnv = TestSessionEnvironment::singleton();
|
||||
$testEnv = TestSessionEnvironment::singleton();
|
||||
|
||||
if($testEnv->isRunningTests()) {
|
||||
$testEnv->loadFromFile();
|
||||
} else {
|
||||
$testEnv->endTestSession();
|
||||
}
|
||||
});
|
||||
if ($testEnv->isRunningTests()) {
|
||||
$testEnv->loadFromFile();
|
||||
} else {
|
||||
$testEnv->endTestSession();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -4,10 +4,8 @@ namespace SilverStripe\TestSession;
|
||||
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\Session;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Dev\Deprecation;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\CheckboxField;
|
||||
use SilverStripe\Forms\DatetimeField;
|
||||
@ -18,6 +16,7 @@ use SilverStripe\Forms\FormAction;
|
||||
use SilverStripe\Forms\HiddenField;
|
||||
use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\ORM\ArrayList;
|
||||
use SilverStripe\ORM\Connect\TempDatabase;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
use SilverStripe\Security\Permission;
|
||||
@ -104,14 +103,14 @@ class TestSessionController extends Controller
|
||||
*/
|
||||
public function start()
|
||||
{
|
||||
$params = $this->request->requestVars();
|
||||
$params = $this->getRequest()->requestVars();
|
||||
|
||||
if (!empty($params['globalTestSession'])) {
|
||||
$id = null;
|
||||
} else {
|
||||
$generator = Injector::inst()->get(RandomGenerator::class);
|
||||
$id = substr($generator->randomToken(), 0, 10);
|
||||
Session::set('TestSessionId', $id);
|
||||
$this->getRequest()->getSession()->set('TestSessionId', $id);
|
||||
}
|
||||
|
||||
// Convert datetime from form object into a single string
|
||||
@ -181,14 +180,15 @@ class TestSessionController extends Controller
|
||||
throw new LogicException('No query parameters detected');
|
||||
}
|
||||
|
||||
$sessionStates = (array)Session::get('_TestSessionController.BrowserSessionState');
|
||||
$session = $this->getRequest()->getSession();
|
||||
$sessionStates = (array)$session->get('_TestSessionController.BrowserSessionState');
|
||||
|
||||
foreach ($newSessionStates as $k => $v) {
|
||||
Session::set($k, $v);
|
||||
$session->set($k, $v);
|
||||
}
|
||||
|
||||
// Track which state we're setting so we can unset later in end()
|
||||
Session::set('_TestSessionController.BrowserSessionState', array_merge($sessionStates, $newSessionStates));
|
||||
$session->set('_TestSessionController.BrowserSessionState', array_merge($sessionStates, $newSessionStates));
|
||||
}
|
||||
|
||||
public function StartForm()
|
||||
@ -316,8 +316,9 @@ class TestSessionController extends Controller
|
||||
|
||||
$this->extend('onBeforeClear');
|
||||
|
||||
if (SapphireTest::using_temp_db()) {
|
||||
SapphireTest::empty_temp_db();
|
||||
$tempDB = new TempDatabase();
|
||||
if ($tempDB->isUsed()) {
|
||||
$tempDB->clearAllData();
|
||||
}
|
||||
|
||||
if (isset($_SESSION['_testsession_codeblocks'])) {
|
||||
@ -341,17 +342,17 @@ class TestSessionController extends Controller
|
||||
}
|
||||
|
||||
$this->environment->endTestSession();
|
||||
Session::clear('TestSessionId');
|
||||
$session = Controller::curr()->getRequest()->getSession();
|
||||
$session->clear('TestSessionId');
|
||||
|
||||
// Clear out all PHP session states which have been set previously
|
||||
if ($sessionStates = Session::get('_TestSessionController.BrowserSessionState')) {
|
||||
if ($sessionStates = $session->get('_TestSessionController.BrowserSessionState')) {
|
||||
foreach ($sessionStates as $k => $v) {
|
||||
Session::clear($k);
|
||||
$session->clear($k);
|
||||
}
|
||||
Session::clear('_TestSessionController');
|
||||
$session->clear('_TestSessionController');
|
||||
}
|
||||
|
||||
|
||||
return $this->renderWith('TestSession_end');
|
||||
}
|
||||
|
||||
@ -360,7 +361,8 @@ class TestSessionController extends Controller
|
||||
*/
|
||||
public function isTesting()
|
||||
{
|
||||
return SapphireTest::using_temp_db();
|
||||
$tempDB = new TempDatabase();
|
||||
return $tempDB->isUsed();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -394,7 +396,7 @@ class TestSessionController extends Controller
|
||||
$templates = array();
|
||||
|
||||
if (!$path) {
|
||||
$path = $this->config()->database_templates_path;
|
||||
$path = $this->config()->get('database_templates_path');
|
||||
}
|
||||
|
||||
// TODO Remove once we can set BASE_PATH through the config layer
|
||||
|
@ -2,21 +2,21 @@
|
||||
|
||||
namespace SilverStripe\TestSession;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\Session;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Core\Config\Configurable;
|
||||
use SilverStripe\Core\Extensible;
|
||||
use SilverStripe\Core\Injector\Injectable;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Dev\FixtureFactory;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\Connect\TempDatabase;
|
||||
use SilverStripe\ORM\DatabaseAdmin;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
use SilverStripe\Versioned\Versioned;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use Exception;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
@ -77,19 +77,23 @@ class TestSessionEnvironment
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->constructExtensions();
|
||||
|
||||
if ($id) {
|
||||
$this->id = $id;
|
||||
} else {
|
||||
Session::start();
|
||||
}
|
||||
}
|
||||
|
||||
public function init(HTTPRequest $request)
|
||||
{
|
||||
if (!$this->id) {
|
||||
$request->getSession()->init();
|
||||
// $_SESSION != Session::get() in some execution paths, suspect Controller->pushCurrent()
|
||||
// as part of the issue, easiest resolution is to use session directly for now
|
||||
$this->id = (isset($_SESSION['TestSessionId'])) ? $_SESSION['TestSessionId'] : null;
|
||||
$this->id = $request->getSession()->get('TestSessionId');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return String Absolute path to the file persisting our state.
|
||||
* @return string Absolute path to the file persisting our state.
|
||||
*/
|
||||
public function getFilePath()
|
||||
{
|
||||
@ -191,7 +195,7 @@ class TestSessionEnvironment
|
||||
$this->extend('onBeforeApplyState', $state);
|
||||
|
||||
// back up source
|
||||
global $databaseConfig;
|
||||
$databaseConfig = DB::getConfig();
|
||||
$this->oldDatabaseName = $databaseConfig['database'];
|
||||
|
||||
// Load existing state from $this->state into $state, if there is any
|
||||
@ -239,7 +243,8 @@ class TestSessionEnvironment
|
||||
|
||||
if (!$dbExists) {
|
||||
// Create a new one with a randomized name
|
||||
$dbName = SapphireTest::create_temp_db();
|
||||
$tempDB = new TempDatabase();
|
||||
$dbName = $tempDB->build();
|
||||
|
||||
$state->database = $dbName; // In case it's changed by the call to SapphireTest::create_temp_db();
|
||||
|
||||
@ -351,9 +356,12 @@ class TestSessionEnvironment
|
||||
|
||||
$this->applyState($json);
|
||||
} catch (Exception $e) {
|
||||
throw new \Exception("A test session appears to be in progress, but we can't retrieve the details. "
|
||||
. "Try removing the " . $this->getFilePath() . " file. Inner "
|
||||
. "error: " . $e->getMessage());
|
||||
throw new Exception(
|
||||
"A test session appears to be in progress, but we can't retrieve the details.\n"
|
||||
. "Try removing the " . $this->getFilePath() . " file.\n"
|
||||
. "Inner error: " . $e->getMessage() . "\n"
|
||||
. "Stacktrace: " . $e->getTraceAsString()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -385,7 +393,8 @@ class TestSessionEnvironment
|
||||
{
|
||||
$this->extend('onBeforeEndTestSession');
|
||||
|
||||
if (SapphireTest::using_temp_db()) {
|
||||
$tempDB = new TempDatabase();
|
||||
if ($tempDB->isUsed()) {
|
||||
$state = $this->getState();
|
||||
$dbConn = DB::get_schema();
|
||||
$dbExists = $dbConn->databaseExists($state->database);
|
||||
@ -396,8 +405,6 @@ class TestSessionEnvironment
|
||||
}
|
||||
// End test session mode
|
||||
$this->resetDatabaseName();
|
||||
|
||||
SapphireTest::set_is_running_test(false);
|
||||
}
|
||||
|
||||
$this->removeStateFile();
|
||||
@ -443,9 +450,9 @@ class TestSessionEnvironment
|
||||
public function resetDatabaseName()
|
||||
{
|
||||
if ($this->oldDatabaseName) {
|
||||
global $databaseConfig;
|
||||
|
||||
$databaseConfig = DB::getConfig();
|
||||
$databaseConfig['database'] = $this->oldDatabaseName;
|
||||
DB::setConfig($databaseConfig);
|
||||
|
||||
$conn = DB::get_conn();
|
||||
|
||||
|
@ -2,17 +2,15 @@
|
||||
|
||||
namespace SilverStripe\TestSession;
|
||||
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\Email\Email;
|
||||
use SilverStripe\Control\Email\Mailer;
|
||||
use SilverStripe\ORM\DataModel;
|
||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Control\Session;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Control\RequestFilter;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Control\HTTPResponse;
|
||||
use SilverStripe\Control\RequestFilter;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
|
||||
/**
|
||||
* Sets state previously initialized through {@link TestSessionController}.
|
||||
@ -29,9 +27,10 @@ class TestSessionRequestFilter implements RequestFilter
|
||||
$this->testSessionEnvironment = TestSessionEnvironment::singleton();
|
||||
}
|
||||
|
||||
public function preRequest(HTTPRequest $request, Session $session, DataModel $model)
|
||||
public function preRequest(HTTPRequest $request)
|
||||
{
|
||||
$isRunningTests = $this->testSessionEnvironment->isRunningTests();
|
||||
$this->testSessionEnvironment->init($request);
|
||||
if (!$isRunningTests) {
|
||||
return;
|
||||
}
|
||||
@ -59,7 +58,7 @@ class TestSessionRequestFilter implements RequestFilter
|
||||
$file = $testState->stubfile;
|
||||
if (!Director::isLive() && $file && file_exists($file)) {
|
||||
// Connect to the database so the included code can interact with it
|
||||
global $databaseConfig;
|
||||
$databaseConfig = DB::getConfig();
|
||||
if ($databaseConfig) {
|
||||
DB::connect($databaseConfig);
|
||||
}
|
||||
@ -68,7 +67,7 @@ class TestSessionRequestFilter implements RequestFilter
|
||||
}
|
||||
}
|
||||
|
||||
public function postRequest(HTTPRequest $request, HTTPResponse $response, DataModel $model)
|
||||
public function postRequest(HTTPRequest $request, HTTPResponse $response)
|
||||
{
|
||||
if (!$this->testSessionEnvironment->isRunningTests()) {
|
||||
return;
|
||||
@ -76,7 +75,7 @@ class TestSessionRequestFilter implements RequestFilter
|
||||
|
||||
// Store PHP session
|
||||
$state = $this->testSessionEnvironment->getState();
|
||||
$state->session = Session::get_all();
|
||||
$state->session = $request->getSession()->getAll();
|
||||
$this->testSessionEnvironment->applyState($state);
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\TestSession\Tests;
|
||||
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\TestSession\TestSessionStubCodeWriter;
|
||||
|
||||
class TestSessionStubCodeWriterTest extends SapphireTest
|
||||
{
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
public function testWritesHeaderOnNewFile()
|
||||
{
|
||||
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
|
||||
$writer = new TestSessionStubCodeWriter($file);
|
||||
$writer->write('foo();', false);
|
||||
$this->assertFileExists($file);
|
||||
$this->assertEquals(
|
||||
file_get_contents($writer->getFilePath()),
|
||||
"<?php\nfoo();\n"
|
||||
);
|
||||
}
|
||||
|
||||
public function testWritesWithAppendOnExistingFile()
|
||||
{
|
||||
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
|
||||
$writer = new TestSessionStubCodeWriter($file);
|
||||
$writer->write('foo();', false);
|
||||
$writer->write('bar();', false);
|
||||
$this->assertFileExists($file);
|
||||
$this->assertEquals(
|
||||
file_get_contents($writer->getFilePath()),
|
||||
"<?php\nfoo();\nbar();\n"
|
||||
);
|
||||
}
|
||||
|
||||
public function testReset()
|
||||
{
|
||||
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
|
||||
$writer = new TestSessionStubCodeWriter($file);
|
||||
$writer->write('foo();', false);
|
||||
$this->assertFileExists($file);
|
||||
$writer->reset();
|
||||
$this->assertFileNotExists($file);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user