mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Refactor Registry into a trait, so it can be tested separately
This commit is contained in:
parent
333a9daf5a
commit
679651b242
12
phpunit.xml
Normal file
12
phpunit.xml
Normal file
@ -0,0 +1,12 @@
|
||||
<phpunit bootstrap="./vendor/autoload.php" colors="true">
|
||||
<testsuites>
|
||||
<testsuite name="Tests">
|
||||
<directory>./test</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist addUncoveredFilesFromWhitelist="true">
|
||||
<directory>./src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
50
src/Loop.php
50
src/Loop.php
@ -4,16 +4,13 @@ namespace Interop\Async;
|
||||
|
||||
final class Loop
|
||||
{
|
||||
use Registry;
|
||||
|
||||
/**
|
||||
* @var LoopDriver
|
||||
*/
|
||||
private static $driver = null;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $registry = [];
|
||||
|
||||
/**
|
||||
* Execute a callback within the scope of an event loop driver.
|
||||
*
|
||||
@ -64,49 +61,6 @@ final class Loop
|
||||
self::get()->stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores information in the loop bound registry. This can be used to store
|
||||
* loop bound information. Stored information is package private.
|
||||
* Packages MUST NOT retrieve the stored state of other packages.
|
||||
*
|
||||
* Therefore packages SHOULD use the following prefix to keys:
|
||||
* `vendor.package.`
|
||||
*
|
||||
* @param string $key namespaced storage key
|
||||
* @param mixed $value the value to be stored
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function storeState($key, $value)
|
||||
{
|
||||
if (self::$driver === null) {
|
||||
throw new \RuntimeException('Not within the scope of an event loop driver');
|
||||
}
|
||||
|
||||
self::$registry[$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches information stored bound to the loop. Stored information is
|
||||
* package private. Packages MUST NOT retrieve the stored state of
|
||||
* other packages.
|
||||
*
|
||||
* Therefore packages SHOULD use the following prefix to keys:
|
||||
* `vendor.package.`
|
||||
*
|
||||
* @param string $key namespaced storage key
|
||||
*
|
||||
* @return mixed previously stored value or null if it doesn't exist
|
||||
*/
|
||||
public static function fetchState($key)
|
||||
{
|
||||
if (self::$driver === null) {
|
||||
throw new \RuntimeException('Not within the scope of an event loop driver');
|
||||
}
|
||||
|
||||
return isset(self::$registry[$key]) ? self::$registry[$key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defer the execution of a callback.
|
||||
*
|
||||
|
57
src/Registry.php
Normal file
57
src/Registry.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Interop\Async\EventLoop;
|
||||
|
||||
trait Registry {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $registry = null;
|
||||
|
||||
/**
|
||||
* Stores information in the loop bound registry. This can be used to store
|
||||
* loop bound information. Stored information is package private.
|
||||
* Packages MUST NOT retrieve the stored state of other packages.
|
||||
*
|
||||
* Therefore packages SHOULD use the following prefix to keys:
|
||||
* `vendor.package.`
|
||||
*
|
||||
* @param string $key namespaced storage key
|
||||
* @param mixed $value the value to be stored
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function storeState($key, $value)
|
||||
{
|
||||
if (self::$registry === null) {
|
||||
throw new \RuntimeException('Not within the scope of an event loop driver');
|
||||
}
|
||||
|
||||
if ($value === null) {
|
||||
unset(self::$registry[$key]);
|
||||
} else {
|
||||
self::$registry[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches information stored bound to the loop. Stored information is
|
||||
* package private. Packages MUST NOT retrieve the stored state of
|
||||
* other packages.
|
||||
*
|
||||
* Therefore packages SHOULD use the following prefix to keys:
|
||||
* `vendor.package.`
|
||||
*
|
||||
* @param string $key namespaced storage key
|
||||
*
|
||||
* @return mixed previously stored value or null if it doesn't exist
|
||||
*/
|
||||
public static function fetchState($key)
|
||||
{
|
||||
if (self::$registry === null) {
|
||||
throw new \RuntimeException('Not within the scope of an event loop driver');
|
||||
}
|
||||
|
||||
return isset(self::$registry[$key]) ? self::$registry[$key] : null;
|
||||
}
|
||||
}
|
60
test/RegistryTest.php
Normal file
60
test/RegistryTest.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Interop\Async\EventLoop;
|
||||
|
||||
class RegistryTest extends \PHPUnit_Framework_TestCase {
|
||||
use Registry;
|
||||
|
||||
protected function setUp() {
|
||||
self::$registry = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function fetchfailsOutsideOfLoop() {
|
||||
self::fetchState("foobar");
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function storefailsOutsideOfLoop() {
|
||||
self::fetchState("store");
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function defaultsToNull() {
|
||||
// emulate we're in an event loop…
|
||||
self::$registry = [];
|
||||
$this->assertNull(self::fetchState("foobar"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideValues
|
||||
*/
|
||||
public function fetchesStoredValue($value) {
|
||||
// emulate we're in an event loop…
|
||||
self::$registry = [];
|
||||
|
||||
$this->assertNull(self::fetchState("foobar"));
|
||||
self::storeState("foobar", $value);
|
||||
|
||||
$this->assertSame($value, self::fetchState("foobar"));
|
||||
}
|
||||
|
||||
public function provideValues() {
|
||||
return [
|
||||
["string"],
|
||||
[42],
|
||||
[1.001],
|
||||
[true],
|
||||
[false],
|
||||
[null],
|
||||
[new \StdClass],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user