From a258721b265132b9794f569b61931489f000b122 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Mon, 22 Jan 2024 21:14:07 +0100 Subject: [PATCH] add ability to set custom container instance --- src/Middleware/AuthenticationMiddleware.php | 19 ++++++++++++++-- .../AuthenticationMiddlewareTest.php | 22 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Middleware/AuthenticationMiddleware.php b/src/Middleware/AuthenticationMiddleware.php index 15d860d0..c52dca8b 100644 --- a/src/Middleware/AuthenticationMiddleware.php +++ b/src/Middleware/AuthenticationMiddleware.php @@ -23,6 +23,7 @@ use Authentication\Authenticator\StatelessInterface; use Authentication\Authenticator\UnauthenticatedException; use Cake\Core\ContainerApplicationInterface; +use Cake\Core\ContainerInterface; use Cake\Core\InstanceConfigTrait; use InvalidArgumentException; use Laminas\Diactoros\Response; @@ -63,15 +64,26 @@ class AuthenticationMiddleware implements MiddlewareInterface */ protected $subject; + /** + * The container instance from the application + * + * @var \Cake\Core\ContainerInterface|null + */ + protected $container; + /** * Constructor * * @param \Authentication\AuthenticationServiceInterface|\Authentication\AuthenticationServiceProviderInterface $subject Authentication service or application instance. * @param array $config Array of configuration settings. + * @param \Cake\Core\ContainerInterface|null $container The container instance from the application * @throws \InvalidArgumentException When invalid subject has been passed. */ - public function __construct($subject, $config = []) - { + public function __construct( + $subject, + $config = [], + ?ContainerInterface $container = null + ) { $this->setConfig($config); if ( @@ -89,6 +101,7 @@ public function __construct($subject, $config = []) } $this->subject = $subject; + $this->container = $container; } /** @@ -105,6 +118,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if ($this->subject instanceof ContainerApplicationInterface) { $container = $this->subject->getContainer(); $container->add(AuthenticationService::class, $service); + } elseif ($this->container) { + $this->container->add(AuthenticationService::class, $service); } try { diff --git a/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php b/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php index d1145c2d..ac6a855a 100644 --- a/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php +++ b/tests/TestCase/Middleware/AuthenticationMiddlewareTest.php @@ -22,6 +22,7 @@ use Authentication\IdentityInterface; use Authentication\Middleware\AuthenticationMiddleware; use Authentication\Test\TestCase\AuthenticationTestCase as TestCase; +use Cake\Core\Container; use Cake\Core\TestSuite\ContainerStubTrait; use Cake\Http\Response; use Cake\Http\ServerRequestFactory; @@ -772,4 +773,25 @@ public function testMiddlewareInjectsServiceIntoDIC(): void $container = $this->application->getContainer(); $this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class)); } + + public function testMiddlewareInjectsServiceIntoDICCustomContainerInstance(): void + { + $request = ServerRequestFactory::fromGlobals( + ['REQUEST_URI' => '/testpath'], + [], + ['username' => 'mariano', 'password' => 'password'] + ); + $handler = new TestRequestHandler(); + + $provider = $this->createMock(AuthenticationServiceProviderInterface::class); + $provider + ->method('getAuthenticationService') + ->willReturn($this->service); + $container = new Container(); + + $middleware = new AuthenticationMiddleware($provider, [], $container); + $middleware->process($request, $handler); + + $this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class)); + } }