From 0cf8a6e8b09d4fb67761830cef49b063b5ce98b3 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Thu, 9 Jan 2025 21:14:01 +1000 Subject: [PATCH] Initialise the middleware with a LoggerInterface if available This was overlooked previously, somehow. So, this change ensures that, if a LoggerInterface service is available in the DI container, then it is used to initialise the middleware. --- .../RedirectToNewDomainMiddlewareFactory.php | 2 ++ ...directToNewDomainMiddlewareFactoryTest.php | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Middleware/RedirectToNewDomainMiddlewareFactory.php b/src/Middleware/RedirectToNewDomainMiddlewareFactory.php index 10847f3..9759437 100644 --- a/src/Middleware/RedirectToNewDomainMiddlewareFactory.php +++ b/src/Middleware/RedirectToNewDomainMiddlewareFactory.php @@ -5,6 +5,7 @@ namespace Settermjd\Middleware; use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; use Settermjd\Middleware\Exception\InvalidConfigurationException; /** @@ -37,6 +38,7 @@ public function __invoke(ContainerInterface $container): RedirectToNewDomainMidd oldDomain: $oldDomain, newDomain: $newDomain, redirectStatus: $redirectStatus, + logger: $container->get(LoggerInterface::class) ?? null ); } } diff --git a/test/Middleware/RedirectToNewDomainMiddlewareFactoryTest.php b/test/Middleware/RedirectToNewDomainMiddlewareFactoryTest.php index d04795b..6fef891 100644 --- a/test/Middleware/RedirectToNewDomainMiddlewareFactoryTest.php +++ b/test/Middleware/RedirectToNewDomainMiddlewareFactoryTest.php @@ -7,6 +7,7 @@ use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; use Settermjd\Middleware\Exception\InvalidConfigurationException; use Settermjd\Middleware\RedirectToNewDomainMiddleware; use Settermjd\Middleware\RedirectToNewDomainMiddlewareFactory; @@ -23,16 +24,18 @@ public function testCanInstantiateMiddlewareWithConfigDataWhenSet(): void ->with("config") ->willReturn(true); $container - ->expects($this->once()) + ->expects($this->atMost(2)) ->method("get") - ->with("config") - ->willReturn([ - "redirect-to-new-domain-middleware" => [ - "old" => "deploywithdockercompose.com", - "new" => "https://deploywithdockercompose.webdevwithmatt.com", - "status" => 301, + ->willReturnOnConsecutiveCalls( + [ + "redirect-to-new-domain-middleware" => [ + "old" => "deploywithdockercompose.com", + "new" => "https://deploywithdockercompose.webdevwithmatt.com", + "status" => 301, + ], ], - ]); + $this->createMock(LoggerInterface::class), + ); $middleware = $factory($container); self::assertInstanceOf(RedirectToNewDomainMiddleware::class, $middleware);