|
16 | 16 | */
|
17 | 17 | namespace Authorization\Test\TestCase\Middleware;
|
18 | 18 |
|
| 19 | +use Authorization\AuthorizationService; |
19 | 20 | use Authorization\AuthorizationServiceInterface;
|
20 | 21 | use Authorization\AuthorizationServiceProviderInterface;
|
21 | 22 | use Authorization\Exception\AuthorizationRequiredException;
|
22 | 23 | use Authorization\Exception\Exception;
|
23 | 24 | use Authorization\IdentityDecorator;
|
24 | 25 | use Authorization\IdentityInterface;
|
25 | 26 | use Authorization\Middleware\AuthorizationMiddleware;
|
| 27 | +use Cake\Core\Container; |
26 | 28 | use Cake\Http\Response;
|
27 | 29 | use Cake\Http\ServerRequest;
|
| 30 | +use Cake\Http\ServerRequestFactory; |
28 | 31 | use Cake\TestSuite\TestCase;
|
29 | 32 | use Psr\Http\Message\RequestInterface;
|
30 | 33 | use Psr\Http\Message\ResponseInterface;
|
31 | 34 | use Psr\Http\Message\ServerRequestInterface;
|
32 | 35 | use RuntimeException;
|
33 | 36 | use stdClass;
|
| 37 | +use TestApp\Application; |
34 | 38 | use TestApp\Http\TestRequestHandler;
|
35 | 39 | use TestApp\Identity;
|
36 | 40 |
|
@@ -270,4 +274,46 @@ public function testUnauthorizedHandlerRequireAuthz()
|
270 | 274 | $result = $middleware->process($request, $handler);
|
271 | 275 | $this->assertSame(200, $result->getStatusCode());
|
272 | 276 | }
|
| 277 | + |
| 278 | + public function testMiddlewareInjectsServiceIntoDIC() |
| 279 | + { |
| 280 | + $request = ServerRequestFactory::fromGlobals( |
| 281 | + ['REQUEST_URI' => '/testpath'], |
| 282 | + [], |
| 283 | + ['username' => 'mariano', 'password' => 'password'] |
| 284 | + ); |
| 285 | + $handler = new TestRequestHandler(); |
| 286 | + $application = new Application('config'); |
| 287 | + |
| 288 | + $middleware = new AuthorizationMiddleware($application, ['requireAuthorizationCheck' => false]); |
| 289 | + $middleware->process($request, $handler); |
| 290 | + |
| 291 | + $container = $application->getContainer(); |
| 292 | + $this->assertInstanceOf(AuthorizationService::class, $container->get(AuthorizationService::class)); |
| 293 | + } |
| 294 | + |
| 295 | + public function testMiddlewareInjectsServiceIntoDICViaCustomContainerInstance() |
| 296 | + { |
| 297 | + $request = ServerRequestFactory::fromGlobals( |
| 298 | + ['REQUEST_URI' => '/testpath'], |
| 299 | + [], |
| 300 | + ['username' => 'mariano', 'password' => 'password'] |
| 301 | + ); |
| 302 | + $handler = new TestRequestHandler(); |
| 303 | + |
| 304 | + $service = $this->createMock(AuthorizationServiceInterface::class); |
| 305 | + $provider = $this->createMock(AuthorizationServiceProviderInterface::class); |
| 306 | + $provider |
| 307 | + ->expects($this->once()) |
| 308 | + ->method('getAuthorizationService') |
| 309 | + ->with($this->isInstanceOf(ServerRequestInterface::class)) |
| 310 | + ->willReturn($service); |
| 311 | + |
| 312 | + $container = new Container(); |
| 313 | + |
| 314 | + $middleware = new AuthorizationMiddleware($provider, ['requireAuthorizationCheck' => false], $container); |
| 315 | + $middleware->process($request, $handler); |
| 316 | + |
| 317 | + $this->assertEquals($service, $container->get(AuthorizationService::class)); |
| 318 | + } |
273 | 319 | }
|
0 commit comments