mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-01-17 21:48:59 +01:00
refactored Registry pattern
This commit is contained in:
parent
b3f144c4b2
commit
87f8eb1983
@ -10,9 +10,9 @@ abstract class Registry
|
|||||||
* this introduces global state in your application which can not be mocked up for testing
|
* this introduces global state in your application which can not be mocked up for testing
|
||||||
* and is therefor considered an anti-pattern! Use dependency injection instead!
|
* and is therefor considered an anti-pattern! Use dependency injection instead!
|
||||||
*
|
*
|
||||||
* @var array
|
* @var Service[]
|
||||||
*/
|
*/
|
||||||
private static $storedValues = [];
|
private static $services = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
@ -21,32 +21,21 @@ abstract class Registry
|
|||||||
self::LOGGER,
|
self::LOGGER,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
public static function set(string $key, Service $value)
|
||||||
* @param string $key
|
|
||||||
* @param mixed $value
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function set(string $key, $value)
|
|
||||||
{
|
{
|
||||||
if (!in_array($key, self::$allowedKeys)) {
|
if (!in_array($key, self::$allowedKeys)) {
|
||||||
throw new \InvalidArgumentException('Invalid key given');
|
throw new \InvalidArgumentException('Invalid key given');
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$storedValues[$key] = $value;
|
self::$services[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static function get(string $key): Service
|
||||||
* @param string $key
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function get(string $key)
|
|
||||||
{
|
{
|
||||||
if (!in_array($key, self::$allowedKeys) || !isset(self::$storedValues[$key])) {
|
if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) {
|
||||||
throw new \InvalidArgumentException('Invalid key given');
|
throw new \InvalidArgumentException('Invalid key given');
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$storedValues[$key];
|
return self::$services[$key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
Structural/Registry/Service.php
Normal file
9
Structural/Registry/Service.php
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace DesignPatterns\Structural\Registry;
|
||||||
|
|
||||||
|
|
||||||
|
class Service {
|
||||||
|
|
||||||
|
}
|
@ -3,28 +3,34 @@
|
|||||||
namespace DesignPatterns\Structural\Registry\Tests;
|
namespace DesignPatterns\Structural\Registry\Tests;
|
||||||
|
|
||||||
use DesignPatterns\Structural\Registry\Registry;
|
use DesignPatterns\Structural\Registry\Registry;
|
||||||
use stdClass;
|
use DesignPatterns\Structural\Registry\Service;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class RegistryTest extends TestCase
|
class RegistryTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Service|MockObject
|
||||||
|
*/
|
||||||
|
private $service;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->service = $this->getMockBuilder(Service::class)->getMock();
|
||||||
|
}
|
||||||
|
|
||||||
public function testSetAndGetLogger()
|
public function testSetAndGetLogger()
|
||||||
{
|
{
|
||||||
$key = Registry::LOGGER;
|
Registry::set(Registry::LOGGER, $this->service);
|
||||||
$logger = new stdClass();
|
|
||||||
|
|
||||||
Registry::set($key, $logger);
|
$this->assertSame($this->service, Registry::get(Registry::LOGGER));
|
||||||
$storedLogger = Registry::get($key);
|
|
||||||
|
|
||||||
$this->assertSame($logger, $storedLogger);
|
|
||||||
$this->assertInstanceOf(stdClass::class, $storedLogger);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testThrowsExceptionWhenTryingToSetInvalidKey()
|
public function testThrowsExceptionWhenTryingToSetInvalidKey()
|
||||||
{
|
{
|
||||||
$this->expectException(\InvalidArgumentException::class);
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
|
||||||
Registry::set('foobar', new stdClass());
|
Registry::set('foobar', $this->service);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 20 KiB |
Loading…
x
Reference in New Issue
Block a user