CanUseSessionTest
extends TestCase
in package
The base of every unit test that needs more than the class under test.
A test asks for what it needs and gets it without a web server, a database or a config file:
$this->bootFramework(["rootDirectory" => "sub"]);
$request = $this->request($this->input()->withUrl("http://localhost/sub/a/b"));
Everything handed out here is taken back after the test, so no test sees the settings, the request or the cached values of another one.
Table of Contents
- DECADES = 2000000000
- LONG_AGO = "2000-01-01 12:00:00"
- $temporaryDirectories : array<string|int, mixed>
- testAddsTheExtensionToTheLifetime() : void
- testGivesANewSessionTheConfiguredLifetime() : void
- testGivesANewSessionTheSevenDayDefaultLifetimeWithoutLoginTimeoutSeconds() : void
- testIgnoresTheTimeoutAndTheExtensionOnceAnExpiryIsFixed() : void
- testKeepsASessionExemptFromExpiringDespiteAFixedExpiry() : void
- testLetsAFixedExpiryOutrankTheComputedLifetime() : void
- testReportsAnOldSessionAsNotExpiredWhenTheExtensionCoversTheGap() : void
- testReportsAnOldSessionWithoutExtensionAsExpired() : void
- testReportsARecentlyCreatedSessionAsNotExpired() : void
- assertThrows() : Throwable
- Asserts that the action throws exactly this class, no subclass of it, with exactly this message. expectException() accepts subclasses and a message that only contains the expected one.
- bootFramework() : ZubZet
- Provides the framework instance the global helpers (zubzet(), config(), request(), ...) work on. It holds exactly the given settings: nothing is read from a config file, nothing connects, and there is no request, response, database or user until the test sets one.
- callHidden() : mixed
- Calls a method that is not public. Only for logic that has no public way in; a test goes through the public method wherever there is one.
- currentUser() : User
- Makes a user without database access the current one, the one user() returns. Its constructor would identify it through the database, so it is left out: the user is anonymous unless the test says otherwise.
- inDirectory() : mixed
- Runs the action with the given directory as the working directory, for code that reads relative paths. The previous directory is restored afterwards, also when the action throws.
- input() : State
- An input without any data. The with...() methods of the state fill it:
- removeSetting() : void
- Removes a setting, as if its line was missing in the config file.
- request() : Request
- Makes the given input the current request, the one request() returns.
- requireArgon2id() : void
- Skips the test on a PHP built without Argon2. The framework hashes with PASSWORD_ARGON2ID unconditionally and composer.json does not require the support, so the tests that reach it have to tolerate its absence.
- response() : Response
- Makes a new response the current one, the one response() returns.
- setHiddenStatic() : void
- Sets a static property that is not public. Only for state a class keeps for the whole process and offers no way to reset.
- setSettings() : void
- Changes single settings of the booted framework and keeps the others.
- setUp() : void
- tearDown() : void
- temporaryDirectory() : string
- An empty directory with a trailing slash, removed after the test.
- clearEndpoint() : void
- clearRuntimeCaches() : void
- secondsAgo() : string
- session() : Session
Constants
DECADES
private
mixed
DECADES
= 2000000000
LONG_AGO
private
mixed
LONG_AGO
= "2000-01-01 12:00:00"
Properties
$temporaryDirectories
private
array<string|int, mixed>
$temporaryDirectories
= []
Methods
testAddsTheExtensionToTheLifetime()
public
testAddsTheExtensionToTheLifetime() : void
Return values
void —testGivesANewSessionTheConfiguredLifetime()
public
testGivesANewSessionTheConfiguredLifetime() : void
Return values
void —testGivesANewSessionTheSevenDayDefaultLifetimeWithoutLoginTimeoutSeconds()
public
testGivesANewSessionTheSevenDayDefaultLifetimeWithoutLoginTimeoutSeconds() : void
Return values
void —testIgnoresTheTimeoutAndTheExtensionOnceAnExpiryIsFixed()
public
testIgnoresTheTimeoutAndTheExtensionOnceAnExpiryIsFixed() : void
Return values
void —testKeepsASessionExemptFromExpiringDespiteAFixedExpiry()
public
testKeepsASessionExemptFromExpiringDespiteAFixedExpiry() : void
Return values
void —testLetsAFixedExpiryOutrankTheComputedLifetime()
public
testLetsAFixedExpiryOutrankTheComputedLifetime() : void
Return values
void —testReportsAnOldSessionAsNotExpiredWhenTheExtensionCoversTheGap()
public
testReportsAnOldSessionAsNotExpiredWhenTheExtensionCoversTheGap() : void
Return values
void —testReportsAnOldSessionWithoutExtensionAsExpired()
public
testReportsAnOldSessionWithoutExtensionAsExpired() : void
Return values
void —testReportsARecentlyCreatedSessionAsNotExpired()
public
testReportsARecentlyCreatedSessionAsNotExpired() : void
Return values
void —assertThrows()
Asserts that the action throws exactly this class, no subclass of it, with exactly this message. expectException() accepts subclasses and a message that only contains the expected one.
protected
assertThrows(class-string<\Throwable> $class, string $message, callable $action) : Throwable
Parameters
- $class : class-string<\Throwable>
- $message : string
- $action : callable
Return values
Throwable —bootFramework()
Provides the framework instance the global helpers (zubzet(), config(), request(), ...) work on. It holds exactly the given settings: nothing is read from a config file, nothing connects, and there is no request, response, database or user until the test sets one.
protected
bootFramework([array<string, mixed> $settings = [] ]) : ZubZet
Parameters
- $settings : array<string, mixed> = []
-
What config() returns
Return values
ZubZet —callHidden()
Calls a method that is not public. Only for logic that has no public way in; a test goes through the public method wherever there is one.
protected
callHidden(object|class-string $target, string $method, mixed ...$arguments) : mixed
Parameters
- $target : object|class-string
-
An object, or a class name for a static method
- $method : string
- $arguments : mixed
Return values
mixed —currentUser()
Makes a user without database access the current one, the one user() returns. Its constructor would identify it through the database, so it is left out: the user is anonymous unless the test says otherwise.
protected
currentUser([bool $loggedIn = false ]) : User
Parameters
- $loggedIn : bool = false
Return values
User —inDirectory()
Runs the action with the given directory as the working directory, for code that reads relative paths. The previous directory is restored afterwards, also when the action throws.
protected
inDirectory(string $directory, callable $action) : mixed
Parameters
- $directory : string
- $action : callable
Return values
mixed —input()
An input without any data. The with...() methods of the state fill it:
protected
input([string $requestOrder = "GPC" ]) : State
$this->input()->withMethod("POST")->withPost(["name" => "value"])
REQUEST is put together from GET, POST and COOKIE in the given order, whatever the php.ini of the machine says, so a test sees the same REQUEST everywhere.
Parameters
- $requestOrder : string = "GPC"
Return values
State —removeSetting()
Removes a setting, as if its line was missing in the config file.
protected
removeSetting(string $key) : void
Parameters
- $key : string
Return values
void —request()
Makes the given input the current request, the one request() returns.
protected
request([State|null $input = null ]) : Request
Boots the framework without settings when the test has not done so.
Parameters
- $input : State|null = null
Return values
Request —requireArgon2id()
Skips the test on a PHP built without Argon2. The framework hashes with PASSWORD_ARGON2ID unconditionally and composer.json does not require the support, so the tests that reach it have to tolerate its absence.
protected
requireArgon2id() : void
Return values
void —response()
Makes a new response the current one, the one response() returns.
protected
response() : Response
Boots the framework without settings when the test has not done so.
Return values
Response —setHiddenStatic()
Sets a static property that is not public. Only for state a class keeps for the whole process and offers no way to reset.
protected
setHiddenStatic(class-string $class, string $property, mixed $value) : void
Parameters
- $class : class-string
- $property : string
- $value : mixed
Return values
void —setSettings()
Changes single settings of the booted framework and keeps the others.
protected
setSettings(array<string, mixed> $settings) : void
Parameters
- $settings : array<string, mixed>
Return values
void —setUp()
protected
setUp() : void
Return values
void —tearDown()
protected
tearDown() : void
Return values
void —temporaryDirectory()
An empty directory with a trailing slash, removed after the test.
protected
temporaryDirectory() : string
Return values
string —clearEndpoint()
private
clearEndpoint() : void
Return values
void —clearRuntimeCaches()
private
clearRuntimeCaches() : void
Return values
void —secondsAgo()
private
secondsAgo(int $seconds) : string
Parameters
- $seconds : int
Return values
string —session()
private
session(array<string|int, mixed> $row) : Session
Parameters
- $row : array<string|int, mixed>