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..708e01e --- /dev/null +++ b/test/AppTest/InMemoryContainer.php @@ -0,0 +1,47 @@ + */ + public array $services = []; + + public function setService(string $name, mixed $service): void + { + $this->services[$name] = $service; + } + + /** + * @param string $id + * @return mixed + */ + public function get($id) + { + if (! $this->has($id)) { + throw new RuntimeException(sprintf('Service not found "%s"', $id)); + } + + return $this->services[$id]; + } + + /** + * @param string $id + * @return bool + */ + public function has($id) + { + return array_key_exists($id, $this->services); + } +}