silverstripe-testsession/tests/unit/TestSessionHTTPMiddlewareTest.php
Bernard Hamlin 1d8f112dd8 Refactor to separate 'applyState' and 'saveState' functions
`appyState` will now only read the test session data and update the
environment to relect that state. This is intended for things like
database connections and mailer classes. Actions that need to alter
state to apply it (eg creating a temp db) should be done in other
actions such as 'start'

`saveState` will only take an existing state object and persist it to a
file

Also addresses:

[Connect to test database on session load](https://github.com/silverstripe/silverstripe-testsession/pull/63)
[Skip Temp DB creation if state has no database prop](https://github.com/silverstripe/silverstripe-testsession/pull/64)
Prevent re-creating session file on end (original issue for this PR)
2019-05-17 15:46:39 +12:00

104 lines
2.6 KiB
PHP

<?php
namespace SilverStripe\TestSession\Tests\Unit;
use DateTime;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\TestSession\TestSessionEnvironment;
use SilverStripe\TestSession\TestSessionHTTPMiddleware;
use stdClass;
use SilverStripe\Control\Email\Mailer;
use SilverStripe\Core\Manifest\ModuleLoader;
class TestSessionHTTPMiddlewareTest extends SapphireTest
{
/**
* @var TestSessionEnvironment
*/
private $testSessionEnvironment;
protected $usesDatabase = true;
protected function setUp()
{
parent::setUp();
Injector::inst()->registerService(
$this->createMock(TestSessionEnvironment::class),
TestSessionEnvironment::class
);
$this->testSessionEnvironment = TestSessionEnvironment::singleton();
}
public function testProcessNoTestRunning()
{
$env = $this->testSessionEnvironment;
// setup expected calls on environment
$env->expects($this->never())
->method('getState');
$env->expects($this->never())
->method('applyState');
Injector::nest();
Injector::inst()->registerService(
$env,
TestSessionEnvironment::class
);
$middleware = new TestSessionHTTPMiddleware();
// Mock request
$session = new Session([]);
$request = new HTTPRequest('GET', '/');
$request->setSession($session);
$delegate = function () {
// noop
};
$middleware->process($request, $delegate);
Injector::unnest();
}
public function testProcessTestsRunning()
{
$state = new stdClass();
$env = $this->testSessionEnvironment;
$env->method('isRunningTests')
->willReturn(true);
// setup expected calls on environment
$env->expects($this->exactly(2))
->method('getState')
->willReturn($state);
$env->expects($this->once())
->method('applyState');
Injector::nest();
Injector::inst()->registerService(
$env,
TestSessionEnvironment::class
);
$middleware = new TestSessionHTTPMiddleware();
// Mock request
$session = new Session([]);
$request = new HTTPRequest('GET', '/');
$request->setSession($session);
$delegate = function () {
// noop
};
$middleware->process($request, $delegate);
Injector::unnest();
}
}