Skip to content

Commit

Permalink
Test fake storages
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Sep 13, 2023
1 parent e36569a commit e2cedc0
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 3 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;
}
68 changes: 68 additions & 0 deletions tests/Common/AssignmentsStorageTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,31 @@
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->populateStorage();
}

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

public function testHasItem(): void
{
$storage = $this->getStorage();
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 populateStorage(): void
{
foreach ($this->getFixtures()['assignments'] as $assignmentData) {
$this->getStorage()->add($assignmentData['itemName'], $assignmentData['userId']);
}
}

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

return $this->itemsStorage;
}

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

return $this->storage;
}

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

protected function createStorage(): AssignmentsStorageInterface
{
return new FakeAssignmentsStorage();
}
}
46 changes: 46 additions & 0 deletions tests/Common/ItemsStorageTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Yiisoft\Rbac\Tests\Common;

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

trait ItemsStorageTestTrait
{
Expand All @@ -16,6 +18,18 @@ trait ItemsStorageTestTrait
private int $initialBothPermissionsChildrenCount = 0;
private int $initialItemsChildrenCount = 0;

private ?ItemsStorageInterface $storage = null;

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

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

public function dataUpdate(): array
{
return [
Expand Down Expand Up @@ -497,6 +511,20 @@ public function testClearRoles(): void
$this->assertTrue($storage->hasChildren('Parent 5'));
}

protected function getStorage(): ItemsStorageInterface
{
if ($this->storage === null) {
$this->storage = $this->createStorage();
}

return $this->storage;
}

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

protected function getFixtures(): array
{
$time = time();
Expand Down Expand Up @@ -576,6 +604,24 @@ protected function getFixtures(): array
return ['items' => $items, 'itemsChildren' => $itemsChildren];
}

protected function populateStorage(): void
{
$storage = $this->getStorage();
$fixtures = $this->getFixtures();
foreach ($fixtures['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']);
$storage->add($item);
}

foreach ($fixtures['itemsChildren'] as $itemChildData) {
$storage->addChild($itemChildData['parent'], $itemChildData['child']);
}
}

private function getItemsCount(): int
{
return $this->initialRolesCount + $this->initialPermissionsCount;
Expand Down
13 changes: 13 additions & 0 deletions tests/ItemsStorageTest.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\ItemsStorageTestTrait;

final class ItemsStorageTest extends TestCase
{
use ItemsStorageTestTrait;
}
4 changes: 1 addition & 3 deletions tests/Support/FakeItemsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ private function removeItemsByType(string $type): void

private function clearChildrenFromItem(string $itemName): void
{
foreach ($this->children as &$children) {
unset($children[$itemName]);
}
unset($this->children[$itemName]);
}

private function updateChildrenForItemName(string $name, Item $item): void
Expand Down

0 comments on commit e2cedc0

Please sign in to comment.