|
19 | 19 | use Authorization\AuthorizationService; |
20 | 20 | use Authorization\IdentityDecorator; |
21 | 21 | use Authorization\Policy\BeforePolicyInterface; |
| 22 | +use Authorization\Policy\BeforeScopeInterface; |
22 | 23 | use Authorization\Policy\Exception\MissingMethodException; |
23 | 24 | use Authorization\Policy\MapResolver; |
24 | 25 | use Authorization\Policy\OrmResolver; |
|
30 | 31 | use TestApp\Model\Table\ArticlesTable; |
31 | 32 | use TestApp\Policy\ArticlePolicy; |
32 | 33 | use TestApp\Policy\MagicCallPolicy; |
| 34 | +use TypeError; |
33 | 35 |
|
34 | 36 | class AuthorizationServiceTest extends TestCase |
35 | 37 | { |
@@ -470,6 +472,70 @@ public function testBeforeResultFalse() |
470 | 472 | $this->assertFalse($result); |
471 | 473 | } |
472 | 474 |
|
| 475 | + public function testBeforeScopeNonNull() |
| 476 | + { |
| 477 | + $entity = new Article(); |
| 478 | + |
| 479 | + $policy = $this->getMockBuilder(BeforeScopeInterface::class) |
| 480 | + ->onlyMethods(['beforeScope']) |
| 481 | + ->addMethods(['scopeIndex']) |
| 482 | + ->getMock(); |
| 483 | + |
| 484 | + $policy->expects($this->once()) |
| 485 | + ->method('beforeScope') |
| 486 | + ->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'index') |
| 487 | + ->willReturn('foo'); |
| 488 | + |
| 489 | + $policy->expects($this->never()) |
| 490 | + ->method('scopeIndex'); |
| 491 | + |
| 492 | + $resolver = new MapResolver([ |
| 493 | + Article::class => $policy, |
| 494 | + ]); |
| 495 | + |
| 496 | + $service = new AuthorizationService($resolver); |
| 497 | + |
| 498 | + $user = new IdentityDecorator($service, [ |
| 499 | + 'role' => 'admin', |
| 500 | + ]); |
| 501 | + |
| 502 | + $result = $service->applyScope($user, 'index', $entity); |
| 503 | + $this->assertEquals('foo', $result); |
| 504 | + } |
| 505 | + |
| 506 | + public function testBeforeScopeNull() |
| 507 | + { |
| 508 | + $entity = new Article(); |
| 509 | + |
| 510 | + $policy = $this->getMockBuilder(BeforeScopeInterface::class) |
| 511 | + ->onlyMethods(['beforeScope']) |
| 512 | + ->addMethods(['scopeIndex']) |
| 513 | + ->getMock(); |
| 514 | + |
| 515 | + $policy->expects($this->once()) |
| 516 | + ->method('beforeScope') |
| 517 | + ->with($this->isInstanceOf(IdentityDecorator::class), $entity, 'index') |
| 518 | + ->willReturn(null); |
| 519 | + |
| 520 | + $policy->expects($this->once()) |
| 521 | + ->method('scopeIndex') |
| 522 | + ->with($this->isInstanceOf(IdentityDecorator::class), $entity) |
| 523 | + ->willReturn('bar'); |
| 524 | + |
| 525 | + $resolver = new MapResolver([ |
| 526 | + Article::class => $policy, |
| 527 | + ]); |
| 528 | + |
| 529 | + $service = new AuthorizationService($resolver); |
| 530 | + |
| 531 | + $user = new IdentityDecorator($service, [ |
| 532 | + 'role' => 'admin', |
| 533 | + ]); |
| 534 | + |
| 535 | + $result = $service->applyScope($user, 'index', $entity); |
| 536 | + $this->assertEquals('bar', $result); |
| 537 | + } |
| 538 | + |
473 | 539 | public function testMissingMethod() |
474 | 540 | { |
475 | 541 | $entity = new Article(); |
|
0 commit comments