51 lines
1.4 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2014-05-05 12:00:18 -03:00
namespace DesignPatterns\Structural\Registry\Tests;
2019-12-14 12:50:05 +01:00
use InvalidArgumentException;
use DesignPatterns\Structural\Registry\Registry;
2019-08-26 06:18:51 +02:00
use DesignPatterns\Structural\Registry\Service;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-09 00:35:08 +01:00
use PHPUnit\Framework\TestCase;
2017-03-09 00:35:08 +01:00
class RegistryTest extends TestCase
{
2019-12-14 12:50:05 +01:00
/**
* @var Service
*/
private MockObject $service;
2019-08-26 06:18:51 +02:00
protected function setUp(): void
{
2019-08-26 06:18:51 +02:00
$this->service = $this->getMockBuilder(Service::class)->getMock();
}
2016-02-14 18:57:02 +01:00
2019-08-26 06:18:51 +02:00
public function testSetAndGetLogger()
{
Registry::set(Registry::LOGGER, $this->service);
2016-02-14 18:57:02 +01:00
2019-08-26 06:18:51 +02:00
$this->assertSame($this->service, Registry::get(Registry::LOGGER));
2016-09-23 10:47:24 +02:00
}
public function testThrowsExceptionWhenTryingToSetInvalidKey()
{
2019-12-14 12:50:05 +01:00
$this->expectException(InvalidArgumentException::class);
2019-08-17 23:05:15 +02:00
2019-08-26 06:18:51 +02:00
Registry::set('foobar', $this->service);
2016-09-23 10:47:24 +02:00
}
/**
* 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()
{
2019-12-14 12:50:05 +01:00
$this->expectException(InvalidArgumentException::class);
2019-08-17 23:05:15 +02:00
2016-09-23 10:47:24 +02:00
Registry::get(Registry::LOGGER);
}
}