Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Jan 12, 2022
1 parent 58f651e commit b270153
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/CompositeRule.php
Expand Up @@ -77,6 +77,12 @@ public function execute(string $userId, Item $item, array $parameters = []): boo

public function getAttributes(): array
{
$subRulesAttributes = [];

foreach ($this->rules as $rule) {
$subRulesAttributes[] = $rule->getAttributes();
}

return [
'name' => $this->getName(),
'operator' => $this->operator,
Expand Down
12 changes: 12 additions & 0 deletions src/Item.php
Expand Up @@ -57,13 +57,19 @@ public function getName(): string
return $this->name;
}

/**
* @return static
*/
public function withName(string $name): self
{
$new = clone $this;
$new->name = $name;
return $new;
}

/**
* @return static
*/
public function withDescription(string $description): self
{
$new = clone $this;
Expand All @@ -90,6 +96,9 @@ public function getRuleName(): ?string
return $this->ruleName;
}

/**
* @return static
*/
public function withCreatedAt(int $createdAt): self
{
$new = clone $this;
Expand All @@ -102,6 +111,9 @@ public function getCreatedAt(): ?int
return $this->createdAt;
}

/**
* @return static
*/
public function withUpdatedAt(int $updatedAt): self
{
$new = clone $this;
Expand Down
28 changes: 28 additions & 0 deletions tests/CompositeRuleTest.php
Expand Up @@ -47,4 +47,32 @@ public function testInvalidRule(): void
$this->expectExceptionMessage('Each rule should be an instance of \Yiisoft\Rbac\Rule, "string" given.');
new CompositeRule('rule', CompositeRule::OR, ['invalid_rule']);
}

public function testNameOperationAndAttributes(): void
{
$positiveRule = new EasyRule(true);
$negativeRule = new EasyRule(true);

$rule = new CompositeRule(
'composite',
CompositeRule::AND,
[
$positiveRule,
$negativeRule,
]
);

$this->assertSame('composite', $rule->getName());
$this->assertSame(
[
'name' => 'composite',
'operator' => CompositeRule::AND,
'rules' => [
$positiveRule,
$negativeRule,
],
],
$rule->getAttributes()
);
}
}
2 changes: 1 addition & 1 deletion tests/EasyRule.php
Expand Up @@ -19,6 +19,6 @@ public function __construct(bool $expected = true)

public function execute(string $userId, Item $item, array $parameters = []): bool
{
return $this->expected;
return $this->expected && parent::execute($userId, $item, $parameters);
}
}
9 changes: 8 additions & 1 deletion tests/FakeAssignmentsStorage.php
Expand Up @@ -11,6 +11,13 @@
final class FakeAssignmentsStorage implements AssignmentsStorageInterface
{
private array $assignments = [];
private int $now;

public function __construct(int $now)
{
$this->now = $now;
}


public function getAssignments(): array
{
Expand All @@ -32,7 +39,7 @@ public function addAssignment(string $userId, Item $item): void
$this->assignments[$userId][$item->getName()] = new Assignment(
$userId,
$item->getName(),
time()
$this->now
);
}

Expand Down
72 changes: 63 additions & 9 deletions tests/ManagerTest.php
Expand Up @@ -19,6 +19,8 @@
*/
final class ManagerTest extends TestCase
{
private const NOW = 1642027031;

private Manager $manager;

private RolesStorageInterface $rolesStorage;
Expand Down Expand Up @@ -324,11 +326,11 @@ public function testHasChild(): void

public function testAssign(): void
{
$this->manager->assign(
$readerAssignment = $this->manager->assign(
$this->rolesStorage->getRoleByName('reader'),
'readingAuthor'
);
$this->manager->assign(
$authorAssignment = $this->manager->assign(
$this->rolesStorage->getRoleByName('author'),
'readingAuthor'
);
Expand All @@ -341,6 +343,14 @@ public function testAssign(): void
],
array_keys($this->manager->getRolesByUser('readingAuthor'))
);

$this->assertSame('readingAuthor', $readerAssignment->getUserId());
$this->assertSame('reader', $readerAssignment->getItemName());
$this->assertSame(self::NOW, $readerAssignment->getCreatedAt());

$this->assertSame('readingAuthor', $authorAssignment->getUserId());
$this->assertSame('author', $authorAssignment->getItemName());
$this->assertSame(self::NOW, $authorAssignment->getCreatedAt());
}

public function testAssignUnknownItem(): void
Expand Down Expand Up @@ -456,10 +466,29 @@ public function testAddRole(): void

$role = (new Role('new role'))
->withDescription('new role description')
->withRuleName($rule->getName());
->withRuleName($rule->getName())
->withCreatedAt(1642026147)
->withUpdatedAt(1642026148);

$this->manager->addRole($role);
$this->assertNotNull($this->rolesStorage->getRoleByName('new role'));

$storedRole = $this->rolesStorage->getRoleByName('new role');

$this->assertNotNull($storedRole);
$this->assertSame('new role description', $storedRole->getDescription());
$this->assertSame(1642026147, $storedRole->getCreatedAt());
$this->assertSame(1642026148, $storedRole->getUpdatedAt());
$this->assertSame(
[
'name' => 'new role',
'description' => 'new role description',
'ruleName' => EasyRule::class,
'type' => 'role',
'updatedAt' => 1642026148,
'createdAt' => 1642026147
],
$storedRole->getAttributes()
);
}

public function testRemoveRole(): void
Expand All @@ -480,10 +509,29 @@ public function testUpdateRoleNameAndRule(): void
public function testAddPermission(): void
{
$permission = (new Permission('edit post'))
->withDescription('edit a post');
->withDescription('edit a post')
->withCreatedAt(1642026147)
->withUpdatedAt(1642026148);

$this->manager->addPermission($permission);
$this->assertNotNull($this->rolesStorage->getPermissionByName('edit post'));

$storedPermission = $this->rolesStorage->getPermissionByName('edit post');

$this->assertNotNull($storedPermission);
$this->assertSame('edit a post', $storedPermission->getDescription());
$this->assertSame(1642026147, $storedPermission->getCreatedAt());
$this->assertSame(1642026148, $storedPermission->getUpdatedAt());
$this->assertSame(
[
'name' => 'edit post',
'description' => 'edit a post',
'ruleName' => null,
'type' => 'permission',
'updatedAt' => 1642026148,
'createdAt' => 1642026147,
],
$storedPermission->getAttributes()
);
}

public function testRemovePermission(): void
Expand All @@ -495,12 +543,18 @@ public function testRemovePermission(): void
public function testUpdatePermission(): void
{
$permission = $this->rolesStorage->getPermissionByName('updatePost')
->withName('newUpdatePost');
->withName('newUpdatePost')
->withCreatedAt(1642026149)
->withUpdatedAt(1642026150);

$this->manager->updatePermission('updatePost', $permission);

$this->assertNull($this->rolesStorage->getPermissionByName('updatePost'));
$this->assertNotNull($this->rolesStorage->getPermissionByName('newUpdatePost'));
$newPermission = $this->rolesStorage->getPermissionByName('newUpdatePost');
$this->assertNotNull($newPermission);
$this->assertSame(1642026149, $newPermission->getCreatedAt());
$this->assertSame(1642026150, $newPermission->getUpdatedAt());

}

public function testUpdatePermissionNameAlreadyUsed(): void
Expand Down Expand Up @@ -622,7 +676,7 @@ private function createRolesStorage(): RolesStorageInterface

private function createAssignmentsStorage(): AssignmentsStorageInterface
{
$storage = new FakeAssignmentsStorage();
$storage = new FakeAssignmentsStorage(self::NOW);

$storage->addAssignment('reader A', new Permission('Fast Metabolism'));
$storage->addAssignment('reader A', new Role('reader'));
Expand Down

0 comments on commit b270153

Please sign in to comment.