mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-11 01:14:01 +02:00
update deps & install rector
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace DesignPatterns\Structural\Registry;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
abstract class Registry
|
||||
{
|
||||
const LOGGER = 'logger';
|
||||
@@ -12,19 +14,16 @@ abstract class Registry
|
||||
*
|
||||
* @var Service[]
|
||||
*/
|
||||
private static $services = [];
|
||||
private static array $services = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $allowedKeys = [
|
||||
private static array $allowedKeys = [
|
||||
self::LOGGER,
|
||||
];
|
||||
|
||||
public static function set(string $key, Service $value)
|
||||
{
|
||||
if (!in_array($key, self::$allowedKeys)) {
|
||||
throw new \InvalidArgumentException('Invalid key given');
|
||||
throw new InvalidArgumentException('Invalid key given');
|
||||
}
|
||||
|
||||
self::$services[$key] = $value;
|
||||
@@ -33,7 +32,7 @@ abstract class Registry
|
||||
public static function get(string $key): Service
|
||||
{
|
||||
if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) {
|
||||
throw new \InvalidArgumentException('Invalid key given');
|
||||
throw new InvalidArgumentException('Invalid key given');
|
||||
}
|
||||
|
||||
return self::$services[$key];
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace DesignPatterns\Structural\Registry\Tests;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use DesignPatterns\Structural\Registry\Registry;
|
||||
use DesignPatterns\Structural\Registry\Service;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
@@ -9,7 +10,10 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RegistryTest extends TestCase
|
||||
{
|
||||
private $service;
|
||||
/**
|
||||
* @var Service
|
||||
*/
|
||||
private MockObject $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -18,7 +22,6 @@ class RegistryTest extends TestCase
|
||||
|
||||
public function testSetAndGetLogger()
|
||||
{
|
||||
/** @noinspection PhpParamsInspection */
|
||||
Registry::set(Registry::LOGGER, $this->service);
|
||||
|
||||
$this->assertSame($this->service, Registry::get(Registry::LOGGER));
|
||||
@@ -26,7 +29,7 @@ class RegistryTest extends TestCase
|
||||
|
||||
public function testThrowsExceptionWhenTryingToSetInvalidKey()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
Registry::set('foobar', $this->service);
|
||||
}
|
||||
@@ -40,7 +43,7 @@ class RegistryTest extends TestCase
|
||||
*/
|
||||
public function testThrowsExceptionWhenTryingToGetNotSetKey()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
Registry::get(Registry::LOGGER);
|
||||
}
|
||||
|
Reference in New Issue
Block a user