-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import InMemoryStore from Cirrus Identity
- Loading branch information
Showing
7 changed files
with
322 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\TestUtils; | ||
|
||
use SimpleSAML\Store\StoreInterface; | ||
use SimpleSAML\Utils\ClearableState; | ||
|
||
/** | ||
* Class InMemoryStore. Used for testing SSP code. | ||
* | ||
* Use it by setting `'store.type'` in config.php to this clas | ||
*/ | ||
class InMemoryStore implements ClearableState, StoreInterface | ||
{ | ||
private static array $store = []; | ||
|
||
|
||
/** | ||
* Retrieve a value from the data store. | ||
* | ||
* @param string $type The data type. | ||
* @param string $key The key. | ||
* | ||
* @return mixed|null The value. | ||
*/ | ||
public function get(string $type, string $key): mixed | ||
{ | ||
if (array_key_exists($key, self::$store)) { | ||
$item = self::$store[$key]; | ||
if (isset($item['expire']) && $item['expire'] < time()) { | ||
$this->delete($type, $key); | ||
return null; | ||
} | ||
return $item['value']; | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* Save a value to the data store. | ||
* | ||
* @param string $type The data type. | ||
* @param string $key The key. | ||
* @param mixed $value The value. | ||
* @param int|null $expire The expiration time (unix timestamp), or null if it never expires. | ||
*/ | ||
public function set(string $type, string $key, mixed $value, ?int $expire = null): void | ||
{ | ||
self::$store[$key] = [ | ||
'type' => $type, | ||
'value' => $value, | ||
'expire' => $expire | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* Delete a value from the data store. | ||
* | ||
* @param string $type The data type. | ||
* @param string $key The key. | ||
*/ | ||
public function delete(string $type, string $key): void | ||
{ | ||
unset(self::$store[$key]); | ||
} | ||
|
||
|
||
/** | ||
* Clear any cached internal state. | ||
*/ | ||
public static function clearInternalState(): void | ||
{ | ||
self::$store = []; | ||
} | ||
} |
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,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache"> | ||
<coverage> | ||
<report> | ||
<clover outputFile="build/logs/clover.xml"/> | ||
<html outputDirectory="build/coverage" lowUpperBound="35" highLowerBound="70"/> | ||
<text outputFile="php://stdout" showUncoveredFiles="true"/> | ||
</report> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="The project's test suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<logging/> | ||
<source> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
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,6 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
$projectRoot = dirname(__DIR__); | ||
require_once($projectRoot . '/vendor/autoload.php'); |
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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SimpleSAML\Test\TestUtils; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use SimpleSAML\Configuration; | ||
use SimpleSAML\Store\StoreFactory; | ||
use SimpleSAML\TestUtils\InMemoryStore; | ||
|
||
use function sleep; | ||
use function time; | ||
|
||
#[CoversClass(InMemoryStore::class)] | ||
class InMemoryStoreTest extends TestCase | ||
{ | ||
protected function tearDown(): void | ||
{ | ||
InMemoryStore::clearInternalState(); | ||
Configuration::clearInternalState(); | ||
} | ||
|
||
|
||
public function testState(): void | ||
{ | ||
// given: an SSP config that overrides the store type | ||
$config = [ | ||
'store.type' => 'SimpleSAML\TestUtils\InMemoryStore', | ||
]; | ||
Configuration::loadFromArray($config, '[ARRAY]', 'simplesaml'); | ||
|
||
// when: getting the store | ||
$store = Store::getInstance('SimpleSAML\TestUtils\InMemoryStore'); | ||
|
||
// then: will give us the right type of store | ||
$this->assertInstanceOf(InMemoryStore::class, $store); | ||
|
||
// and: we can store stuff | ||
$this->assertNull($store->get('string', 'key'), 'Key does not exist yet'); | ||
$store->set('string', 'key', 'value'); | ||
$this->assertEquals('value', $store->get('string', 'key')); | ||
$store->delete('string', 'key'); | ||
$this->assertNull($store->get('string', 'key'), 'Key was removed'); | ||
} | ||
|
||
|
||
public function testExpiration(): void | ||
{ | ||
$store = new InMemoryStore(); | ||
$store->set('string', 'key', 'value', time() + 1); | ||
$this->assertEquals('value', $store->get('string', 'key')); | ||
$this->assertEquals('value', $store->get('string', 'key')); | ||
sleep(2); | ||
$this->assertNull($store->get('string', 'key')); | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"symbol-whitelist": [ | ||
] | ||
} |