Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 1859bc1

Browse files
authored
Merge pull request #384 from svycka/improove-roleservice-tests
Improve RoleService tests
2 parents 6c23fd4 + 46be072 commit 1859bc1

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/Service/RoleService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getIdentityRoles(IdentityInterface $identity = null, $context =
8686
* @param array|Traversable $roles
8787
* @return RoleInterface[]
8888
*/
89-
protected function convertRoles($roles): array
89+
private function convertRoles($roles): array
9090
{
9191
if ($roles instanceof Traversable) {
9292
$roles = iterator_to_array($roles);

test/Service/RoleServiceTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
namespace ZfcRbacTest\Service;
2323

2424
use PHPUnit\Framework\TestCase;
25+
use Prophecy\Argument;
26+
use ZfcRbac\Identity\IdentityInterface;
27+
use ZfcRbac\Role\HierarchicalRole;
2528
use ZfcRbac\Role\InMemoryRoleProvider;
29+
use ZfcRbac\Role\Role;
2630
use ZfcRbac\Role\RoleInterface;
31+
use ZfcRbac\Role\RoleProviderInterface;
2732
use ZfcRbac\Service\RoleService;
2833
use ZfcRbacTest\Asset\Identity;
2934

@@ -61,4 +66,54 @@ public function testReturnGuestRoleIfGuestIdentityIsGiven(): void
6166
$this->assertInstanceOf(RoleInterface::class, $result[0]);
6267
$this->assertEquals('guest', $result[0]->getName());
6368
}
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+
}
64119
}

0 commit comments

Comments
 (0)