API Namespace and update for ss 4.0

This commit is contained in:
Damian Mooyman 2017-04-21 11:58:27 +12:00 committed by Sam Minnée
parent 04fe1e5ee8
commit beda9e158f
20 changed files with 137 additions and 82 deletions

View File

@ -10,7 +10,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[{*.yml,package.json}]
[{*.yml,*.json}]
indent_size = 2
# The indent size used in the package.json file cannot be changed:

View File

@ -1,28 +1,34 @@
# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details
language: php
sudo: false
php:
- 5.5
env:
matrix:
- DB=MYSQL CORE_RELEASE=master
global:
- COMPOSER_ROOT_VERSION=2.0.x-dev
- CORE_RELEASE=master
matrix:
include:
- php: 5.6
env: DB=PGSQL CORE_RELEASE=master
env:
- PHPUNIT_TEST=1
- DB=PGSQL
- php: 5.6
env: DB=MYSQL CORE_RELEASE=master
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:
- vendor/bin/phpunit testsession/tests/unit/
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit testsession/tests/; fi
- if [[ $PHPCS_TEST ]]; then (cd testsession && composer run-script lint); fi

6
.upgrade.yml Normal file
View File

@ -0,0 +1,6 @@
mappings:
TestSessionController: SilverStripe\TestSession\TestSessionController
TestSessionEnvironment: SilverStripe\TestSession\TestSessionEnvironment
TestSessionRequestFilter: SilverStripe\TestSession\TestSessionRequestFilter
TestSessionStubCodeWriter: SilverStripe\TestSession\TestSessionStubCodeWriter
TestSessionStubCodeWriterTest: SilverStripe\TestSession\Tests\TestSessionStubCodeWriterTest

View File

@ -1,11 +1,10 @@
<?php
use SilverStripe\ORM\DB;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\TestSession\TestSessionEnvironment;
// Determine whether there is a testsession currently running, and if so - setup the persistent details for it.
Injector::inst()->get('TestSessionEnvironment')->loadFromFile();
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
@ -20,7 +19,7 @@ if(class_exists('Resque_Event') && class_exists('SSResqueRun')) {
// calling {@link TestSessionEnvironment::loadFromFile()}.
DB::connect($databaseConfig);
$testEnv = Injector::inst()->get('TestSessionEnvironment');
$testEnv = TestSessionEnvironment::singleton();
if($testEnv->isRunningTests()) {
$testEnv->loadFromFile();

View File

@ -1,8 +0,0 @@
---
Name: testsessionprocessors
---
SilverStripe\Core\Injector\Injector:
RequestProcessor:
properties:
filters:
- '%$TestSessionRequestFilter'

8
_config/processors.yml Normal file
View File

@ -0,0 +1,8 @@
---
Name: testsessionprocessors
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\RequestProcessor:
properties:
filters:
- '%$SilverStripe\TestSession\TestSessionRequestFilter'

8
_config/services.yml Normal file
View File

@ -0,0 +1,8 @@
---
Name: testsessionservices
---
SilverStripe\Core\Injector\Injector:
SilverStripe\TestSession\TestSessionEnvironment:
class: SilverStripe\TestSession\TestSessionEnvironment
# shorthand alias for FQN
TestSessionEnvironment: %$SilverStripe\TestSession\TestSessionEnvironment

View File

@ -1,24 +1,36 @@
{
"name": "silverstripe/testsession",
"type": "silverstripe-module",
"description": "Support module for browser-based test sessions, e.g. for Behat behaviour testing",
"homepage": "http://silverstripe.org",
"license": "BSD-3-Clause",
"keywords": ["silverstripe", "testing"],
"authors": [
{
"name": "SilverStripe",
"homepage": "http://silverstripe.com"
}
],
"require": {
"composer/installers": "*",
"silverstripe/framework": "^4@dev"
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"minimum-stability": "dev"
"name": "silverstripe/testsession",
"type": "silverstripe-module",
"description": "Support module for browser-based test sessions, e.g. for Behat behaviour testing",
"homepage": "http://silverstripe.org",
"license": "BSD-3-Clause",
"keywords": [
"silverstripe",
"testing"
],
"authors": [
{
"name": "SilverStripe",
"homepage": "http://silverstripe.com"
}
],
"require": {
"composer/installers": "*",
"silverstripe/framework": "^4@dev"
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
},
"scripts": {
"lint": "phpcs -s src/ tests/"
},
"autoload": {
"psr-4": {
"SilverStripe\\TestSession\\": "src/",
"SilverStripe\\TestSession\\Tests\\": "tests/"
}
},
"minimum-stability": "dev"
}

10
phpcs.xml.dist Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>
<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
</rule>
</ruleset>

View File

@ -1,5 +1,7 @@
<?php
namespace SilverStripe\TestSession;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\Session;
@ -19,9 +21,12 @@ use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Permission;
use SilverStripe\Security\RandomGenerator;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use LogicException;
use FilesystemIterator;
/**
* Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie.
@ -56,7 +61,7 @@ class TestSessionController extends Controller
{
parent::__construct();
$this->environment = Injector::inst()->get('TestSessionEnvironment');
$this->environment = TestSessionEnvironment::singleton();
}
public function init()
@ -75,7 +80,7 @@ class TestSessionController extends Controller
}
Requirements::javascript('http://code.jquery.com/jquery-1.7.2.min.js');
Requirements::javascript('testsession/javascript/testsession.js');
Requirements::javascript('testsession/client/js/testsession.js');
}
public function Link($action = null)
@ -104,7 +109,7 @@ class TestSessionController extends Controller
if (!empty($params['globalTestSession'])) {
$id = null;
} else {
$generator = Injector::inst()->get('SilverStripe\\Security\\RandomGenerator');
$generator = Injector::inst()->get(RandomGenerator::class);
$id = substr($generator->randomToken(), 0, 10);
Session::set('TestSessionId', $id);
}
@ -358,12 +363,6 @@ class TestSessionController extends Controller
return SapphireTest::using_temp_db();
}
public function setState($data)
{
Deprecation::notice('3.1', 'TestSessionController::setState() is no longer used, please use '
. 'TestSessionEnvironment instead.');
}
/**
* @return ArrayList
*/

View File

@ -1,16 +1,23 @@
<?php
namespace SilverStripe\TestSession;
use SilverStripe\Control\Director;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
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\FieldType\DBDatetime;
use SilverStripe\ORM\Versioning\Versioned;
use SilverStripe\Versioned\Versioned;
use InvalidArgumentException;
use LogicException;
use Exception;
use stdClass;
/**
* Responsible for starting and finalizing test sessions.
@ -36,8 +43,11 @@ use SilverStripe\ORM\Versioning\Versioned;
*
* See {@link $state} for default information stored in the test session.
*/
class TestSessionEnvironment extends Object
class TestSessionEnvironment
{
use Injectable;
use Configurable;
use Extensible;
/**
* @var int Optional identifier for the session.
@ -66,7 +76,7 @@ class TestSessionEnvironment extends Object
public function __construct($id = null)
{
parent::__construct();
$this->constructExtensions();
if ($id) {
$this->id = $id;
@ -288,11 +298,10 @@ class TestSessionEnvironment extends Object
$sql = file_get_contents($path);
// Split into individual query commands, removing comments
$sqlCmds = array_filter(
preg_split('/;\n/',
preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql)
)
);
$sqlCmds = array_filter(preg_split(
'/;\n/',
preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql)
));
// Execute each query
foreach ($sqlCmds as $sqlCmd) {
@ -380,7 +389,7 @@ class TestSessionEnvironment extends Object
$state = $this->getState();
$dbConn = DB::get_schema();
$dbExists = $dbConn->databaseExists($state->database);
if($dbExists) {
if ($dbExists) {
// Clean up temp database
$dbConn->dropDatabase($state->database);
file_put_contents('php://stdout', "Deleted temp database: $state->database" . PHP_EOL);

View File

@ -1,24 +1,24 @@
<?php
namespace SilverStripe\TestSession;
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\Core\Config\Config;
use SilverStripe\Control\Director;
use SilverStripe\Control\RequestFilter;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
/**
* Sets state previously initialized through {@link TestSessionController}.
*/
class TestSessionRequestFilter implements RequestFilter
{
/**
* @var TestSessionEnvironment
*/
@ -26,12 +26,13 @@ class TestSessionRequestFilter implements RequestFilter
public function __construct()
{
$this->testSessionEnvironment = Injector::inst()->get('TestSessionEnvironment');
$this->testSessionEnvironment = TestSessionEnvironment::singleton();
}
public function preRequest(HTTPRequest $request, Session $session, DataModel $model)
{
if (!$this->testSessionEnvironment->isRunningTests()) {
$isRunningTests = $this->testSessionEnvironment->isRunningTests();
if (!$isRunningTests) {
return;
}
@ -45,8 +46,8 @@ class TestSessionRequestFilter implements RequestFilter
// Register mailer
if (isset($testState->mailer)) {
$mailer = $testState->mailer;
Injector::inst()->registerService(new $mailer(), 'SilverStripe\\Control\\Email\\Mailer');
Config::inst()->update("SilverStripe\\Control\\Email\\Email", "send_all_emails_to", null);
Injector::inst()->registerService(new $mailer(), Mailer::class);
Email::config()->set("send_all_emails_to", null);
}
// Allows inclusion of a PHP file, usually with procedural commands

View File

@ -1,11 +1,13 @@
<?php
namespace SilverStripe\TestSession;
/**
* Writes PHP to a file which can be included in SilverStripe runs on existence.
* The generated file is included in page execution through {@link TestSessionRequestFilter}.
*/
class TestSessionStubCodeWriter
{
/**
* @var boolean Add debug statements to the generated PHP about
* the generator's origin code location.

View File

@ -1,9 +1,9 @@
<% if State %>
<% if $State %>
<p>
<a href="#" onclick="document.getElementById('state').style.display = 'block'; return false;">Show testing state</a>
<ul id="state" style="display: none;">
<% loop State %>
<li><strong>$Name:</strong> $Value</li>
<% loop $State %>
<li><strong>$Name:</strong> $Value</li>
<% end_loop %>
</ul>
</p>

View File

@ -5,7 +5,7 @@
<% base_tag %>
$MetaTags
<% require css('framework/client/dist/styles/debug.css') %>
<% require css('testsession/css/styles.css') %>
<% require css('testsession/client/styles/styles.css') %>
</head>
<body>
<div class="info">

View File

@ -5,7 +5,7 @@
<% base_tag %>
$MetaTags
<% require css('framework/client/dist/styles/debug.css') %>
<% require css('testsession/css/styles.css') %>
<% require css('testsession/client/styles/styles.css') %>
</head>
<body>
<div class="info">

View File

@ -5,7 +5,7 @@
<% base_tag %>
$MetaTags
<% require css('framework/client/dist/styles/debug.css') %>
<% require css('testsession/css/styles.css') %>
<% require css('testsession/client/styles/styles.css') %>
</head>
<body>
<div class="info">

View File

@ -1,6 +1,9 @@
<?php
namespace SilverStripe\TestSession\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\TestSession\TestSessionStubCodeWriter;
class TestSessionStubCodeWriterTest extends SapphireTest
{