-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from open-sausages/pulls/2.2/pending-requests-…
…awaited ADD / TestSessionState initial implementation
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace SilverStripe\TestSession; | ||
|
||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\ORM\Queries\SQLUpdate; | ||
|
||
/** | ||
* 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}. | ||
* | ||
* @property int PendingRequests keeps information about how many requests are in progress | ||
* @property float LastResponseTimestamp microtime of the last response made by the server | ||
*/ | ||
class TestSessionState extends DataObject | ||
{ | ||
private static $table_name = 'TestSessionState'; | ||
|
||
private static $db = [ | ||
'PendingRequests' => 'Int', | ||
'LastResponseTimestamp' => 'Decimal(14, 0)' | ||
]; | ||
|
||
/** | ||
* 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') | ||
->assign('"LastResponseTimestamp"', microtime(true) * 10000); | ||
|
||
$update->execute(); | ||
} | ||
} |