Skip to content

Commit 67e07bf

Browse files
authored
Merge pull request #655 from cakephp/3.x-merge
merge 2.next -> 3.x
2 parents 59ae576 + 9be055c commit 67e07bf

8 files changed

+81
-3
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: CI
33
on:
44
push:
55
branches:
6-
- 2.x
7-
- 2.next
6+
- 3.x
7+
- 3.next
88
pull_request:
99
branches:
1010
- '*'

src/Middleware/AuthenticationMiddleware.php

+7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
*/
1717
namespace Authentication\Middleware;
1818

19+
use Authentication\AuthenticationService;
1920
use Authentication\AuthenticationServiceInterface;
2021
use Authentication\AuthenticationServiceProviderInterface;
2122
use Authentication\Authenticator\AuthenticationRequiredException;
2223
use Authentication\Authenticator\StatelessInterface;
2324
use Authentication\Authenticator\UnauthenticatedException;
25+
use Cake\Core\ContainerApplicationInterface;
2426
use Laminas\Diactoros\Response;
2527
use Laminas\Diactoros\Response\RedirectResponse;
2628
use Laminas\Diactoros\Stream;
@@ -64,6 +66,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6466
{
6567
$service = $this->getAuthenticationService($request);
6668

69+
if ($this->subject instanceof ContainerApplicationInterface) {
70+
$container = $this->subject->getContainer();
71+
$container->add(AuthenticationService::class, $service);
72+
}
73+
6774
try {
6875
$result = $service->authenticate($request);
6976
} catch (AuthenticationRequiredException $e) {

tests/TestCase/Authenticator/HttpBasicAuthenticatorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ class HttpBasicAuthenticatorTest extends TestCase
3737
'core.Users',
3838
];
3939

40+
/**
41+
* @var \Authentication\IdentifierCollection
42+
*/
43+
protected $identifiers;
44+
45+
/**
46+
* @var \Autnentication\Authenticator\HttpBasicAuthenticator
47+
*/
48+
protected $auth;
49+
4050
/**
4151
* @inheritDoc
4252
*/

tests/TestCase/Authenticator/HttpDigestAuthenticatorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ class HttpDigestAuthenticatorTest extends TestCase
4343
'core.Users',
4444
];
4545

46+
/**
47+
* @var \Authentication\IdentifierCollection
48+
*/
49+
protected $identifiers;
50+
51+
/**
52+
* @var \Autnentication\Authenticator\HttpDigestAuthenticator
53+
*/
54+
protected $auth;
55+
4656
/**
4757
* setup
4858
*

tests/TestCase/Authenticator/JwtAuthenticatorTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class JwtAuthenticatorTest extends TestCase
6060
*/
6161
public $identifiers;
6262

63+
/**
64+
* @var \Cake\Http\ServerRequest
65+
*/
66+
protected $request;
67+
6368
/**
6469
* @inheritDoc
6570
*/

tests/TestCase/Authenticator/SessionAuthenticatorTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ class SessionAuthenticatorTest extends TestCase
4040
'core.Users',
4141
];
4242

43+
/**
44+
* @var \Authentication\IdentifierCollection
45+
*/
46+
protected $identifiers;
47+
48+
protected $sessionMock;
49+
4350
/**
4451
* @inheritDoc
4552
*/

tests/TestCase/Authenticator/TokenAuthenticatorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ class TokenAuthenticatorTest extends TestCase
3434
'core.Users',
3535
];
3636

37+
/**
38+
* @var \Authentication\IdentifierCollection
39+
*/
40+
protected $identifiers;
41+
42+
/**
43+
* @var \Cake\Http\ServerRequest
44+
*/
45+
protected $request;
46+
3747
/**
3848
* @inheritDoc
3949
*/

tests/TestCase/Middleware/AuthenticationMiddlewareTest.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Authentication\IdentityInterface;
2525
use Authentication\Middleware\AuthenticationMiddleware;
2626
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
27+
use Cake\Core\TestSuite\ContainerStubTrait;
2728
use Cake\Http\Response;
2829
use Cake\Http\ServerRequestFactory;
2930
use Firebase\JWT\JWT;
@@ -32,6 +33,18 @@
3233

3334
class AuthenticationMiddlewareTest extends TestCase
3435
{
36+
use ContainerStubTrait;
37+
38+
/**
39+
* @var \Authentication\AuthenticationService
40+
*/
41+
protected $service;
42+
43+
/**
44+
* @var \TestApp\Application
45+
*/
46+
protected $application;
47+
3548
/**
3649
* Fixtures
3750
*/
@@ -116,7 +129,7 @@ public function testApplicationAuthenticationRequestResponse()
116129
$service->method('getIdentityAttribute')->willReturn('identity');
117130

118131
$application = $this->getMockBuilder(Application::class)
119-
->disableOriginalConstructor()
132+
->setConstructorArgs(['config'])
120133
->onlyMethods(['getAuthenticationService', 'middleware'])
121134
->getMock();
122135

@@ -638,4 +651,20 @@ public function testCookieAuthorizationThroughTheMiddlewareStack()
638651

639652
$this->assertStringContainsString('CookieAuth=%5B%22mariano%22', $response->getHeaderLine('Set-Cookie'));
640653
}
654+
655+
public function testMiddlewareInjectsServiceIntoDIC(): void
656+
{
657+
$request = ServerRequestFactory::fromGlobals(
658+
['REQUEST_URI' => '/testpath'],
659+
[],
660+
['username' => 'mariano', 'password' => 'password']
661+
);
662+
$handler = new TestRequestHandler();
663+
664+
$middleware = new AuthenticationMiddleware($this->application);
665+
$middleware->process($request, $handler);
666+
667+
$container = $this->application->getContainer();
668+
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
669+
}
641670
}

0 commit comments

Comments
 (0)