From f85756382cf7824a4b98015705d90acc2bc052fd Mon Sep 17 00:00:00 2001 From: George Steel Date: Mon, 8 Jan 2024 15:02:47 +0000 Subject: [PATCH] Simplify Factory test by utilising a PSR container stub Signed-off-by: George Steel --- .../Handler/HomePageHandlerFactoryTest.php | 57 +++---------------- test/AppTest/InMemoryContainer.php | 41 +++++++++++++ 2 files changed, 49 insertions(+), 49 deletions(-) create mode 100644 test/AppTest/InMemoryContainer.php diff --git a/test/AppTest/Handler/HomePageHandlerFactoryTest.php b/test/AppTest/Handler/HomePageHandlerFactoryTest.php index fa1f3a6..a671ec6 100644 --- a/test/AppTest/Handler/HomePageHandlerFactoryTest.php +++ b/test/AppTest/Handler/HomePageHandlerFactoryTest.php @@ -6,73 +6,32 @@ use App\Handler\HomePageHandler; use App\Handler\HomePageHandlerFactory; +use AppTest\InMemoryContainer; use Mezzio\Router\RouterInterface; use Mezzio\Template\TemplateRendererInterface; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Psr\Container\ContainerInterface; - -use function in_array; class HomePageHandlerFactoryTest extends TestCase { - /** @var ContainerInterface&MockObject */ - protected $container; - - /** @var RouterInterface&MockObject */ - protected $router; - - protected function setUp(): void - { - $this->container = $this->createMock(ContainerInterface::class); - $this->router = $this->createMock(RouterInterface::class); - } - public function testFactoryWithoutTemplate(): void { - $this->container - ->expects($this->once()) - ->method('has') - ->with(TemplateRendererInterface::class) - ->willReturn(false); - $this->container - ->expects($this->once()) - ->method('get') - ->with(RouterInterface::class) - ->willReturn($this->router); + $container = new InMemoryContainer(); + $container->setService(RouterInterface::class, $this->createMock(RouterInterface::class)); $factory = new HomePageHandlerFactory(); - $homePage = $factory($this->container); + $homePage = $factory($container); self::assertInstanceOf(HomePageHandler::class, $homePage); } public function testFactoryWithTemplate(): void { - $renderer = $this->createMock(TemplateRendererInterface::class); - $this->container - ->expects($this->once()) - ->method('has') - ->with(TemplateRendererInterface::class) - ->willReturn(true); - $this->container - ->expects($this->exactly(2)) - ->method('get') - ->with(self::callback(static function (string $name): bool { - self::assertTrue(in_array($name, [ - RouterInterface::class, - TemplateRendererInterface::class, - ])); - - return true; - })) - ->willReturnOnConsecutiveCalls( - $this->router, - $renderer - ); + $container = new InMemoryContainer(); + $container->setService(RouterInterface::class, $this->createMock(RouterInterface::class)); + $container->setService(TemplateRendererInterface::class, $this->createMock(TemplateRendererInterface::class)); $factory = new HomePageHandlerFactory(); - $homePage = $factory($this->container); + $homePage = $factory($container); self::assertInstanceOf(HomePageHandler::class, $homePage); } diff --git a/test/AppTest/InMemoryContainer.php b/test/AppTest/InMemoryContainer.php new file mode 100644 index 0000000..60c6d98 --- /dev/null +++ b/test/AppTest/InMemoryContainer.php @@ -0,0 +1,41 @@ + */ + public array $services = []; + + public function setService(string $name, mixed $service): void + { + $this->services[$name] = $service; + } + + /** @return mixed */ + public function get(string $id) + { + if (! $this->has($id)) { + throw new RuntimeException(sprintf('Service not found "%s"', $id)); + } + + return $this->services[$id]; + } + + /** @return bool */ + public function has(string $id) + { + return array_key_exists($id, $this->services); + } +}