Skip to content

Commit 17d941e

Browse files
authored
Merge pull request #647 from cakephp/2.next-service-di
2.next: add authentication service to DIC by default
2 parents 3dfad95 + bdbe04b commit 17d941e

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/Middleware/AuthenticationMiddleware.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Authentication\Authenticator\AuthenticationRequiredException;
2323
use Authentication\Authenticator\StatelessInterface;
2424
use Authentication\Authenticator\UnauthenticatedException;
25+
use Cake\Core\ContainerApplicationInterface;
2526
use Cake\Core\InstanceConfigTrait;
2627
use InvalidArgumentException;
2728
use Laminas\Diactoros\Response;
@@ -101,6 +102,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
101102
{
102103
$service = $this->getAuthenticationService($request);
103104

105+
if ($this->subject instanceof ContainerApplicationInterface) {
106+
$container = $this->subject->getContainer();
107+
$container->add(AuthenticationService::class, $service);
108+
}
109+
104110
try {
105111
$result = $service->authenticate($request);
106112
} catch (AuthenticationRequiredException $e) {

tests/TestCase/Middleware/AuthenticationMiddlewareTest.php

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
namespace Authentication\Test\TestCase\Middleware;
1818

1919
use Authentication\AuthenticationService;
20-
use Authentication\AuthenticationServiceInterface;
2120
use Authentication\AuthenticationServiceProviderInterface;
22-
use Authentication\Authenticator\ResultInterface;
2321
use Authentication\Authenticator\UnauthenticatedException;
2422
use Authentication\IdentityInterface;
2523
use Authentication\Middleware\AuthenticationMiddleware;
2624
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
25+
use Cake\Core\TestSuite\ContainerStubTrait;
2726
use Cake\Http\Response;
2827
use Cake\Http\ServerRequestFactory;
2928
use Cake\Http\Uri;
@@ -33,6 +32,8 @@
3332

3433
class AuthenticationMiddlewareTest extends TestCase
3534
{
35+
use ContainerStubTrait;
36+
3637
/**
3738
* Fixtures
3839
*/
@@ -105,31 +106,6 @@ public function testProviderAuthentication()
105106
$this->assertTrue($service->authenticators()->has('Form'));
106107
}
107108

108-
public function testApplicationAuthenticationRequestResponse()
109-
{
110-
$request = ServerRequestFactory::fromGlobals();
111-
$handler = new TestRequestHandler();
112-
113-
$service = $this->createMock(AuthenticationServiceInterface::class);
114-
115-
$service->method('authenticate')
116-
->willReturn($this->createMock(ResultInterface::class));
117-
$service->method('getIdentityAttribute')->willReturn('identity');
118-
119-
$application = $this->getMockBuilder(Application::class)
120-
->disableOriginalConstructor()
121-
->setMethods(['getAuthenticationService', 'middleware'])
122-
->getMock();
123-
124-
$application->expects($this->once())
125-
->method('getAuthenticationService')
126-
->with($request)
127-
->willReturn($service);
128-
129-
$middleware = new AuthenticationMiddleware($application);
130-
$middleware->process($request, $handler);
131-
}
132-
133109
public function testInvalidSubject()
134110
{
135111
$this->expectException('InvalidArgumentException');
@@ -770,4 +746,20 @@ public function testServiceConfigurationFallback()
770746
$this->assertSame('redirect', $service->getConfig('queryParam'));
771747
$this->assertSame('/login', $service->getConfig('unauthenticatedRedirect'));
772748
}
749+
750+
public function testMiddlewareInjectsServiceIntoDIC(): void
751+
{
752+
$request = ServerRequestFactory::fromGlobals(
753+
['REQUEST_URI' => '/testpath'],
754+
[],
755+
['username' => 'mariano', 'password' => 'password']
756+
);
757+
$handler = new TestRequestHandler();
758+
759+
$middleware = new AuthenticationMiddleware($this->application);
760+
$middleware->process($request, $handler);
761+
762+
$container = $this->application->getContainer();
763+
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
764+
}
773765
}

0 commit comments

Comments
 (0)