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 insert_final_newline = true
trim_trailing_whitespace = true trim_trailing_whitespace = true
[{*.yml,package.json}] [{*.yml,*.json}]
indent_size = 2 indent_size = 2
# The indent size used in the package.json file cannot be changed: # 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 language: php
sudo: false sudo: false
php:
- 5.5
env: env:
matrix: global:
- DB=MYSQL CORE_RELEASE=master - COMPOSER_ROOT_VERSION=2.0.x-dev
- CORE_RELEASE=master
matrix: matrix:
include: include:
- php: 5.6 - php: 5.6
env: DB=PGSQL CORE_RELEASE=master env:
- PHPUNIT_TEST=1
- DB=PGSQL
- php: 5.6 - 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: before_script:
- if [[ $PHPCS_TEST ]]; then pyrus install pear/PHP_CodeSniffer; fi
- phpenv rehash - phpenv rehash
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support - git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
- cd ~/builds/ss - cd ~/builds/ss
script: 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 <?php
use SilverStripe\ORM\DB; 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. // 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 * 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()}. // calling {@link TestSessionEnvironment::loadFromFile()}.
DB::connect($databaseConfig); DB::connect($databaseConfig);
$testEnv = Injector::inst()->get('TestSessionEnvironment'); $testEnv = TestSessionEnvironment::singleton();
if($testEnv->isRunningTests()) { if($testEnv->isRunningTests()) {
$testEnv->loadFromFile(); $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

@ -4,7 +4,10 @@
"description": "Support module for browser-based test sessions, e.g. for Behat behaviour testing", "description": "Support module for browser-based test sessions, e.g. for Behat behaviour testing",
"homepage": "http://silverstripe.org", "homepage": "http://silverstripe.org",
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"keywords": ["silverstripe", "testing"], "keywords": [
"silverstripe",
"testing"
],
"authors": [ "authors": [
{ {
"name": "SilverStripe", "name": "SilverStripe",
@ -17,7 +20,16 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.0.x-dev" "dev-master": "2.x-dev"
}
},
"scripts": {
"lint": "phpcs -s src/ tests/"
},
"autoload": {
"psr-4": {
"SilverStripe\\TestSession\\": "src/",
"SilverStripe\\TestSession\\Tests\\": "tests/"
} }
}, },
"minimum-stability": "dev" "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 <?php
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\Session;
@ -19,9 +21,12 @@ use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Security\RandomGenerator;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use LogicException;
use FilesystemIterator;
/** /**
* Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie. * 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(); parent::__construct();
$this->environment = Injector::inst()->get('TestSessionEnvironment'); $this->environment = TestSessionEnvironment::singleton();
} }
public function init() 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('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) public function Link($action = null)
@ -104,7 +109,7 @@ class TestSessionController extends Controller
if (!empty($params['globalTestSession'])) { if (!empty($params['globalTestSession'])) {
$id = null; $id = null;
} else { } else {
$generator = Injector::inst()->get('SilverStripe\\Security\\RandomGenerator'); $generator = Injector::inst()->get(RandomGenerator::class);
$id = substr($generator->randomToken(), 0, 10); $id = substr($generator->randomToken(), 0, 10);
Session::set('TestSessionId', $id); Session::set('TestSessionId', $id);
} }
@ -358,12 +363,6 @@ class TestSessionController extends Controller
return SapphireTest::using_temp_db(); 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 * @return ArrayList
*/ */

View File

@ -1,16 +1,23 @@
<?php <?php
namespace SilverStripe\TestSession;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\Session; 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\Injector\Injector;
use SilverStripe\Core\Object;
use SilverStripe\Dev\FixtureFactory; use SilverStripe\Dev\FixtureFactory;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\DatabaseAdmin; use SilverStripe\ORM\DatabaseAdmin;
use SilverStripe\ORM\FieldType\DBDatetime; 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. * 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. * 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. * @var int Optional identifier for the session.
@ -66,7 +76,7 @@ class TestSessionEnvironment extends Object
public function __construct($id = null) public function __construct($id = null)
{ {
parent::__construct(); $this->constructExtensions();
if ($id) { if ($id) {
$this->id = $id; $this->id = $id;
@ -288,11 +298,10 @@ class TestSessionEnvironment extends Object
$sql = file_get_contents($path); $sql = file_get_contents($path);
// Split into individual query commands, removing comments // Split into individual query commands, removing comments
$sqlCmds = array_filter( $sqlCmds = array_filter(preg_split(
preg_split('/;\n/', '/;\n/',
preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql) preg_replace(array('/^$\n/m', '/^(\/|#).*$\n/m'), '', $sql)
) ));
);
// Execute each query // Execute each query
foreach ($sqlCmds as $sqlCmd) { foreach ($sqlCmds as $sqlCmd) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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