|
22 | 22 | namespace ZfcRbacTest\Service; |
23 | 23 |
|
24 | 24 | use PHPUnit\Framework\TestCase; |
| 25 | +use Prophecy\Argument; |
| 26 | +use ZfcRbac\Identity\IdentityInterface; |
| 27 | +use ZfcRbac\Role\HierarchicalRole; |
25 | 28 | use ZfcRbac\Role\InMemoryRoleProvider; |
| 29 | +use ZfcRbac\Role\Role; |
26 | 30 | use ZfcRbac\Role\RoleInterface; |
| 31 | +use ZfcRbac\Role\RoleProviderInterface; |
27 | 32 | use ZfcRbac\Service\RoleService; |
28 | 33 | use ZfcRbacTest\Asset\Identity; |
29 | 34 |
|
@@ -61,4 +66,54 @@ public function testReturnGuestRoleIfGuestIdentityIsGiven(): void |
61 | 66 | $this->assertInstanceOf(RoleInterface::class, $result[0]); |
62 | 67 | $this->assertEquals('guest', $result[0]->getName()); |
63 | 68 | } |
| 69 | + |
| 70 | + public function testReturnTraversableRolesFromIdentityGiven(): void |
| 71 | + { |
| 72 | + $roleService = new RoleService(new InMemoryRoleProvider([])); |
| 73 | + $identity = $this->prophesize(IdentityInterface::class); |
| 74 | + $identity->getRoles()->willReturn($roles = new \ArrayIterator(['first', 'second', 'third'])); |
| 75 | + |
| 76 | + $result = $roleService->getIdentityRoles($identity->reveal()); |
| 77 | + |
| 78 | + $this->assertCount(3, $result); |
| 79 | + $this->assertInstanceOf(RoleInterface::class, $result[0]); |
| 80 | + $this->assertEquals($roles[0], $result[0]->getName()); |
| 81 | + $this->assertEquals($roles[1], $result[1]->getName()); |
| 82 | + $this->assertEquals($roles[2], $result[2]->getName()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testWillNotInvokeRoleProviderIfAllRolesCollected(): void |
| 86 | + { |
| 87 | + $roleProvider = $this->prophesize(RoleProviderInterface::class); |
| 88 | + $roleProvider->getRoles(Argument::any())->shouldNotBeCalled(); |
| 89 | + |
| 90 | + $roleService = new RoleService($roleProvider->reveal()); |
| 91 | + $roles = [new Role('first'), new HierarchicalRole('second'), new Role('third')]; |
| 92 | + $identity = new Identity($roles); |
| 93 | + |
| 94 | + $result = $roleService->getIdentityRoles($identity); |
| 95 | + |
| 96 | + $this->assertCount(3, $result); |
| 97 | + $this->assertInstanceOf(RoleInterface::class, $result[0]); |
| 98 | + $this->assertEquals($roles, $result); |
| 99 | + } |
| 100 | + |
| 101 | + public function testWillCollectRolesOnlyIfRequired(): void |
| 102 | + { |
| 103 | + $roleProvider = $this->prophesize(RoleProviderInterface::class); |
| 104 | + $roles = [new Role('first'), new HierarchicalRole('second'), 'third']; |
| 105 | + $roleProvider->getRoles(['third'])->shouldBeCalled()->willReturn([new Role('third')]); |
| 106 | + |
| 107 | + $roleService = new RoleService($roleProvider->reveal()); |
| 108 | + $identity = new Identity($roles); |
| 109 | + |
| 110 | + $result = $roleService->getIdentityRoles($identity); |
| 111 | + |
| 112 | + $this->assertCount(3, $result); |
| 113 | + $this->assertInstanceOf(RoleInterface::class, $result[0]); |
| 114 | + |
| 115 | + $this->assertEquals($roles[0]->getName(), $result[0]->getName()); |
| 116 | + $this->assertEquals($roles[1]->getName(), $result[1]->getName()); |
| 117 | + $this->assertEquals($roles[2], $result[2]->getName()); |
| 118 | + } |
64 | 119 | } |
0 commit comments