2018-12-18 02:24:59 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\TestSession;
|
|
|
|
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2019-01-08 04:36:20 +01:00
|
|
|
use SilverStripe\ORM\Queries\SQLUpdate;
|
2018-12-18 02:24:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The session state keeps some metadata about the current test session.
|
|
|
|
* This may allow the client (Behat) to get some insight into the
|
|
|
|
* server side affairs (e.g. if the server is handling some number requests at the moment).
|
|
|
|
*
|
|
|
|
* The client side (Behat) must not use this class straightforwardly, but rather
|
|
|
|
* rely on the API of {@see TestSessionEnvironment} or {@see TestSessionController}.
|
2019-01-08 04:36:20 +01:00
|
|
|
*
|
|
|
|
* @property int PendingRequests keeps information about how many requests are in progress
|
|
|
|
* @property float LastResponseTimestamp microtime of the last response made by the server
|
2018-12-18 02:24:59 +01:00
|
|
|
*/
|
|
|
|
class TestSessionState extends DataObject
|
|
|
|
{
|
2019-01-08 04:36:20 +01:00
|
|
|
private static $table_name = 'TestSessionState';
|
|
|
|
|
2018-12-18 02:24:59 +01:00
|
|
|
private static $db = [
|
|
|
|
'PendingRequests' => 'Int',
|
|
|
|
'LastResponseTimestamp' => 'Decimal(14, 0)'
|
|
|
|
];
|
2019-01-08 04:36:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Increments TestSessionState.PendingRequests number by 1
|
|
|
|
* to indicate we have one more request in progress
|
|
|
|
*/
|
|
|
|
public static function incrementState()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
|
|
|
|
->addWhere(['ID' => 1])
|
|
|
|
->assignSQL('"PendingRequests"', '"PendingRequests" + 1');
|
|
|
|
|
|
|
|
$update->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrements TestSessionState.PendingRequests number by 1
|
|
|
|
* to indicate we have one more request in progress.
|
|
|
|
* Also updates TestSessionState.LastResponseTimestamp
|
|
|
|
* to the current timestamp.
|
|
|
|
*/
|
|
|
|
public static function decrementState()
|
|
|
|
{
|
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
|
|
|
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
|
|
|
|
->addWhere(['ID' => 1])
|
|
|
|
->assignSQL('"PendingRequests"', '"PendingRequests" - 1')
|
2019-01-10 03:30:39 +01:00
|
|
|
->assign('"LastResponseTimestamp"', self::millitime());
|
2019-01-08 04:36:20 +01:00
|
|
|
|
|
|
|
$update->execute();
|
|
|
|
}
|
2019-01-09 23:48:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns unix timestamp in milliseconds
|
|
|
|
*
|
|
|
|
* @return float milliseconds since 1970
|
|
|
|
*/
|
2019-01-10 03:30:39 +01:00
|
|
|
public static function millitime()
|
2019-01-09 23:48:59 +01:00
|
|
|
{
|
|
|
|
return round(microtime(true) * 1000);
|
|
|
|
}
|
2018-12-18 02:24:59 +01:00
|
|
|
}
|