App object refactor

This commit is contained in:
Damian Mooyman 2017-06-08 18:01:46 +12:00
parent be78e58ff6
commit 1ab843776d
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
5 changed files with 29 additions and 114 deletions

View File

@ -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

View File

@ -4,10 +4,8 @@ namespace SilverStripe\TestSession;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\Session;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField; use SilverStripe\Forms\DatetimeField;
@ -104,14 +102,14 @@ class TestSessionController extends Controller
*/ */
public function start() public function start()
{ {
$params = $this->request->requestVars(); $params = $this->getRequest()->requestVars();
if (!empty($params['globalTestSession'])) { if (!empty($params['globalTestSession'])) {
$id = null; $id = null;
} else { } else {
$generator = Injector::inst()->get(RandomGenerator::class); $generator = Injector::inst()->get(RandomGenerator::class);
$id = substr($generator->randomToken(), 0, 10); $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 // Convert datetime from form object into a single string
@ -181,14 +179,15 @@ class TestSessionController extends Controller
throw new LogicException('No query parameters detected'); 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) { 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() // 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() public function StartForm()
@ -341,17 +340,17 @@ class TestSessionController extends Controller
} }
$this->environment->endTestSession(); $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 // 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) { foreach ($sessionStates as $k => $v) {
Session::clear($k); $session->clear($k);
} }
Session::clear('_TestSessionController'); $session->clear('_TestSessionController');
} }
return $this->renderWith('TestSession_end'); return $this->renderWith('TestSession_end');
} }
@ -394,7 +393,7 @@ class TestSessionController extends Controller
$templates = array(); $templates = array();
if (!$path) { 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 // TODO Remove once we can set BASE_PATH through the config layer

View File

@ -3,6 +3,7 @@
namespace SilverStripe\TestSession; namespace SilverStripe\TestSession;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible; use SilverStripe\Core\Extensible;
@ -77,11 +78,15 @@ class TestSessionEnvironment
public function __construct($id = null) public function __construct($id = null)
{ {
$this->constructExtensions(); $this->constructExtensions();
if ($id) { if ($id) {
$this->id = $id; $this->id = $id;
} else { }
Session::start(); }
public function init(HTTPRequest $request)
{
if (!$this->id) {
$request->getSession()->start();
// $_SESSION != Session::get() in some execution paths, suspect Controller->pushCurrent() // $_SESSION != Session::get() in some execution paths, suspect Controller->pushCurrent()
// as part of the issue, easiest resolution is to use session directly for now // as part of the issue, easiest resolution is to use session directly for now
$this->id = (isset($_SESSION['TestSessionId'])) ? $_SESSION['TestSessionId'] : null; $this->id = (isset($_SESSION['TestSessionId'])) ? $_SESSION['TestSessionId'] : null;

View File

@ -2,17 +2,16 @@
namespace SilverStripe\TestSession; namespace SilverStripe\TestSession;
use SilverStripe\Control\Director;
use SilverStripe\Control\Email\Email; use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer; 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\HTTPRequest;
use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\RequestFilter;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;
/** /**
* Sets state previously initialized through {@link TestSessionController}. * Sets state previously initialized through {@link TestSessionController}.
@ -29,9 +28,10 @@ class TestSessionRequestFilter implements RequestFilter
$this->testSessionEnvironment = TestSessionEnvironment::singleton(); $this->testSessionEnvironment = TestSessionEnvironment::singleton();
} }
public function preRequest(HTTPRequest $request, Session $session, DataModel $model) public function preRequest(HTTPRequest $request)
{ {
$isRunningTests = $this->testSessionEnvironment->isRunningTests(); $isRunningTests = $this->testSessionEnvironment->isRunningTests();
$this->testSessionEnvironment->init($request);
if (!$isRunningTests) { if (!$isRunningTests) {
return; return;
} }
@ -68,7 +68,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()) { if (!$this->testSessionEnvironment->isRunningTests()) {
return; return;

View File

@ -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);
}
}