Skip to content

Commit

Permalink
Initialise the middleware with a LoggerInterface if available
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
settermjd committed Jan 9, 2025
1 parent 4fd0bc1 commit 0cf8a6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Middleware/RedirectToNewDomainMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Settermjd\Middleware;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Settermjd\Middleware\Exception\InvalidConfigurationException;

/**
Expand Down Expand Up @@ -37,6 +38,7 @@ public function __invoke(ContainerInterface $container): RedirectToNewDomainMidd
oldDomain: $oldDomain,
newDomain: $newDomain,
redirectStatus: $redirectStatus,
logger: $container->get(LoggerInterface::class) ?? null
);
}
}
19 changes: 11 additions & 8 deletions test/Middleware/RedirectToNewDomainMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 0cf8a6e

Please sign in to comment.