Skip to content

Commit

Permalink
Test storages (#192)
Browse files Browse the repository at this point in the history
* Test fake storages

* Naming (review)
  • Loading branch information
arogachev authored Sep 15, 2023
1 parent a106af5 commit 1ebcf04
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 45 deletions.
13 changes: 13 additions & 0 deletions tests/AssignmentsStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Rbac\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Rbac\Tests\Common\AssignmentsStorageTestTrait;

final class AssignmentsStorageTest extends TestCase
{
use AssignmentsStorageTestTrait;
}
96 changes: 82 additions & 14 deletions tests/Common/AssignmentsStorageTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,41 @@
namespace Yiisoft\Rbac\Tests\Common;

use Yiisoft\Rbac\Assignment;
use Yiisoft\Rbac\AssignmentsStorageInterface;
use Yiisoft\Rbac\Item;
use Yiisoft\Rbac\ItemsStorageInterface;
use Yiisoft\Rbac\Permission;
use Yiisoft\Rbac\Role;
use Yiisoft\Rbac\Tests\Support\FakeAssignmentsStorage;
use Yiisoft\Rbac\Tests\Support\FakeItemsStorage;

trait AssignmentsStorageTestTrait
{
private ?ItemsStorageInterface $itemsStorage = null;
private ?AssignmentsStorageInterface $storage = null;

protected function setUp(): void
{
$this->populateItemsStorage();
$this->populateAssignmentsStorage();
}

protected function tearDown(): void
{
$this->getItemsStorage()->clear();
$this->getAssignmentsStorage()->clear();
}

public function testHasItem(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();

$this->assertTrue($storage->hasItem('Accountant'));
}

public function testRenameItem(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$storage->renameItem('Accountant', 'Senior accountant');

$this->assertFalse($storage->hasItem('Accountant'));
Expand All @@ -27,7 +48,7 @@ public function testRenameItem(): void

public function testGetAll(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$all = $storage->getAll();

$this->assertCount(3, $all);
Expand All @@ -41,7 +62,7 @@ public function testGetAll(): void

public function testRemoveByItemName(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$storage->removeByItemName('Manager');

$this->assertFalse($storage->hasItem('Manager'));
Expand All @@ -51,7 +72,7 @@ public function testRemoveByItemName(): void

public function testGetByUserId(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$assignments = $storage->getByUserId('john');

$this->assertCount(3, $assignments);
Expand All @@ -78,7 +99,7 @@ public function dataGetByItemNames(): array
*/
public function testGetByItemNames(array $itemNames, array $expectedAssignments): void
{
$assignments = $this->getStorage()->getByItemNames($itemNames);
$assignments = $this->getAssignmentsStorage()->getByItemNames($itemNames);
$this->assertCount(count($expectedAssignments), $assignments);

$assignmentFound = false;
Expand All @@ -100,7 +121,7 @@ public function testGetByItemNames(array $itemNames, array $expectedAssignments)

public function testRemoveByUserId(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$storage->removeByUserId('jack');

$this->assertEmpty($storage->getByUserId('jack'));
Expand All @@ -109,7 +130,7 @@ public function testRemoveByUserId(): void

public function testRemove(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$storage->remove('Accountant', 'john');

$this->assertEmpty($storage->get('Accountant', 'john'));
Expand All @@ -118,15 +139,15 @@ public function testRemove(): void

public function testClear(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$storage->clear();

$this->assertEmpty($storage->getAll());
}

public function testGet(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$assignment = $storage->get('Manager', 'jack');

$this->assertSame('Manager', $assignment->getItemName());
Expand All @@ -136,7 +157,7 @@ public function testGet(): void

public function testGetNonExisting(): void
{
$this->assertNull($this->getStorage()->get('Researcher', 'jeff'));
$this->assertNull($this->getAssignmentsStorage()->get('Researcher', 'jeff'));
}

public function dataExists(): array
Expand All @@ -155,7 +176,7 @@ public function dataExists(): array
*/
public function testExists(string $itemName, string $userId, bool $expectedExists): void
{
$this->assertSame($expectedExists, $this->getStorage()->exists($itemName, $userId));
$this->assertSame($expectedExists, $this->getAssignmentsStorage()->exists($itemName, $userId));
}

public function dataUserHasItem(): array
Expand All @@ -175,12 +196,12 @@ public function dataUserHasItem(): array
*/
public function testUserHasItem(string $userId, array $itemNames, bool $expectedUserHasItem): void
{
$this->assertSame($expectedUserHasItem, $this->getStorage()->userHasItem($userId, $itemNames));
$this->assertSame($expectedUserHasItem, $this->getAssignmentsStorage()->userHasItem($userId, $itemNames));
}

public function testAdd(): void
{
$storage = $this->getStorage();
$storage = $this->getAssignmentsStorage();
$storage->add('Operator', 'john');

$this->assertInstanceOf(Assignment::class, $storage->get('Operator', 'john'));
Expand Down Expand Up @@ -227,4 +248,51 @@ static function (array $item) use ($time): array {

return ['items' => $items, 'assignments' => $assignments];
}

protected function populateItemsStorage(): void
{
foreach ($this->getFixtures()['items'] as $itemData) {
$name = $itemData['name'];
$item = $itemData['type'] === Item::TYPE_PERMISSION ? new Permission($name) : new Role($name);
$item = $item
->withCreatedAt($itemData['createdAt'])
->withUpdatedAt($itemData['updatedAt']);
$this->getItemsStorage()->add($item);
}
}

protected function populateAssignmentsStorage(): void
{
foreach ($this->getFixtures()['assignments'] as $assignmentData) {
$this->getAssignmentsStorage()->add($assignmentData['itemName'], $assignmentData['userId']);
}
}

protected function getItemsStorage(): ItemsStorageInterface
{
if ($this->itemsStorage === null) {
$this->itemsStorage = $this->createItemsStorage();
}

return $this->itemsStorage;
}

protected function getAssignmentsStorage(): AssignmentsStorageInterface
{
if ($this->storage === null) {
$this->storage = $this->createAssignmentsStorage();
}

return $this->storage;
}

protected function createItemsStorage(): ItemsStorageInterface
{
return new FakeItemsStorage();
}

protected function createAssignmentsStorage(): AssignmentsStorageInterface
{
return new FakeAssignmentsStorage();
}
}
Loading

0 comments on commit 1ebcf04

Please sign in to comment.