Files
DesignPatternsPHP/Structural/Registry/Tests/RegistryTest.php
Mario Simão 56970cc221 style: Adopt PSR12
Dev dependency flyeralarm/php-code-validator has been removed.

Closes #444
2021-10-01 10:26:04 -03:00

49 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace DesignPatterns\Structural\Registry\Tests;
use InvalidArgumentException;
use DesignPatterns\Structural\Registry\Registry;
use DesignPatterns\Structural\Registry\Service;
use PHPUnit\Framework\TestCase;
class RegistryTest extends TestCase
{
private Service $service;
protected function setUp(): void
{
$this->service = $this->getMockBuilder(Service::class)->getMock();
}
public function testSetAndGetLogger()
{
Registry::set(Registry::LOGGER, $this->service);
$this->assertSame($this->service, Registry::get(Registry::LOGGER));
}
public function testThrowsExceptionWhenTryingToSetInvalidKey()
{
$this->expectException(InvalidArgumentException::class);
Registry::set('foobar', $this->service);
}
/**
* notice @runInSeparateProcess here: without it, a previous test might have set it already and
* testing would not be possible. That's why you should implement Dependency Injection where an
* injected class may easily be replaced by a mockup
*
* @runInSeparateProcess
*/
public function testThrowsExceptionWhenTryingToGetNotSetKey()
{
$this->expectException(InvalidArgumentException::class);
Registry::get(Registry::LOGGER);
}
}