Skip to content

Commit

Permalink
Add option to recreate storages in tests (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Jan 24, 2024
1 parent b8cb683 commit a20147a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/Common/AssignmentsStorageTestTrait.php
Expand Up @@ -17,6 +17,8 @@

trait AssignmentsStorageTestTrait
{
protected static bool $reCreateAssignmentsStorageAfterModifications = false;

private ?ItemsStorageInterface $itemsStorage = null;
private ?AssignmentsStorageInterface $assignmentsStorage = null;

Expand Down Expand Up @@ -52,6 +54,7 @@ public function testRenameItem(): void
$storage = $this->getAssignmentsStorage();
$storage->renameItem('Accountant', 'Senior accountant');

$storage = $this->getAssignmentsStorageForModificationAssertions();
$this->assertFalse($storage->hasItem('Accountant'));
$this->assertTrue($storage->hasItem('Senior accountant'));
}
Expand All @@ -75,6 +78,7 @@ public function testRemoveByItemName(): void
$storage = $this->getAssignmentsStorage();
$storage->removeByItemName('Manager');

$storage = $this->getAssignmentsStorageForModificationAssertions();
$this->assertFalse($storage->hasItem('Manager'));
$this->assertCount(2, $storage->getByUserId('jack'));
$this->assertCount(3, $storage->getByUserId('john'));
Expand Down Expand Up @@ -134,6 +138,7 @@ public function testRemoveByUserId(): void
$storage = $this->getAssignmentsStorage();
$storage->removeByUserId('jack');

$storage = $this->getAssignmentsStorageForModificationAssertions();
$this->assertEmpty($storage->getByUserId('jack'));
$this->assertNotEmpty($storage->getByUserId('john'));
}
Expand All @@ -143,6 +148,7 @@ public function testRemove(): void
$storage = $this->getAssignmentsStorage();
$storage->remove('Accountant', 'john');

$storage = $this->getAssignmentsStorageForModificationAssertions();
$this->assertEmpty($storage->get('Accountant', 'john'));
$this->assertNotEmpty($storage->getByUserId('john'));
}
Expand All @@ -152,6 +158,7 @@ public function testClear(): void
$storage = $this->getAssignmentsStorage();
$storage->clear();

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

Expand Down Expand Up @@ -239,6 +246,7 @@ public function testAdd(): void
$storage = $this->getAssignmentsStorage();
$storage->add(new Assignment(userId: 'john', itemName: 'Operator', createdAt: time()));

$storage = $this->getAssignmentsStorageForModificationAssertions();
$this->assertEquals(
new Assignment(userId: 'john', itemName: 'Operator', createdAt: 1_683_707_079),
$storage->get(itemName: 'Operator', userId: 'john'),
Expand All @@ -250,6 +258,7 @@ public function testAddWithCreatedAt(): void
$storage = $this->getAssignmentsStorage();
$storage->add(new Assignment(userId: 'john', itemName: 'Operator', createdAt: 1_694_508_008));

$storage = $this->getAssignmentsStorageForModificationAssertions();
$this->assertEquals(
new Assignment(userId: 'john', itemName: 'Operator', createdAt: 1_694_508_008),
$storage->get(itemName: 'Operator', userId: 'john'),
Expand Down Expand Up @@ -350,4 +359,11 @@ protected function createAssignmentsStorage(): AssignmentsStorageInterface
{
return new FakeAssignmentsStorage();
}

protected function getAssignmentsStorageForModificationAssertions(): AssignmentsStorageInterface
{
return static::$reCreateAssignmentsStorageAfterModifications
? $this->createAssignmentsStorage()
: $this->getAssignmentsStorage();
}
}
20 changes: 20 additions & 0 deletions tests/Common/ItemsStorageTestTrait.php
Expand Up @@ -12,6 +12,8 @@

trait ItemsStorageTestTrait
{
protected static bool $reCreateItemsStorageAfterModifications = false;

private int $initialRolesCount = 0;
private int $initialPermissionsCount = 0;
private int $initialBothRolesChildrenCount = 0;
Expand Down Expand Up @@ -54,6 +56,8 @@ public function testUpdate(string $itemName, string $parentNameForChildrenCheck,
->withRuleName('super admin');
$storage->update($itemName, $item);

$storage = $this->getItemsStorageForModificationAssertions();

$this->assertNull($storage->get($itemName));

$item = $storage->get('Super Admin');
Expand Down Expand Up @@ -134,6 +138,8 @@ public function testAddChild(): void
$storage = $this->getItemsStorage();
$storage->addChild('Parent 2', 'Child 1');

$storage = $this->getItemsStorageForModificationAssertions();

$children = $storage->getAllChildren('Parent 2');
$this->assertCount(3, $children);

Expand All @@ -147,6 +153,7 @@ public function testClear(): void
$storage = $this->getItemsStorage();
$storage->clear();

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

Expand Down Expand Up @@ -340,6 +347,7 @@ public function testRemove(): void
$storage = $this->getItemsStorage();
$storage->remove('Parent 2');

$storage = $this->getItemsStorageForModificationAssertions();
$this->assertNull($storage->get('Parent 2'));
$this->assertNotEmpty($storage->getAll());
$this->assertFalse($storage->hasChildren('Parent 2'));
Expand Down Expand Up @@ -381,6 +389,7 @@ public function testRemoveChildren(): void
$storage = $this->getItemsStorage();
$storage->removeChildren('Parent 2');

$storage = $this->getItemsStorageForModificationAssertions();
$this->assertFalse($storage->hasChildren('Parent 2'));
$this->assertTrue($storage->hasChildren('Parent 1'));
}
Expand All @@ -403,6 +412,7 @@ public function testAdd(): void
$storage = $this->getItemsStorage();
$storage->add($newItem);

$storage = $this->getItemsStorageForModificationAssertions();
$this->assertInstanceOf(Permission::class, $storage->get('Delete post'));
}

Expand All @@ -412,6 +422,8 @@ public function testRemoveChild(): void
$storage->addChild('Parent 2', 'Child 1');
$storage->removeChild('Parent 2', 'Child 1');

$storage = $this->getItemsStorageForModificationAssertions();

$children = $storage->getAllChildren('Parent 2');
$this->assertNotEmpty($children);
$this->assertArrayNotHasKey('Child 1', $children);
Expand Down Expand Up @@ -534,6 +546,7 @@ public function testClearPermissions(): void
$storage = $this->getItemsStorage();
$storage->clearPermissions();

$storage = $this->getItemsStorageForModificationAssertions();
$all = $storage->getAll();
$this->assertNotEmpty($all);
$this->assertContainsOnlyInstancesOf(Role::class, $all);
Expand All @@ -544,6 +557,8 @@ public function testClearRoles(): void
$storage = $this->getItemsStorage();
$storage->clearRoles();

$storage = $this->getItemsStorageForModificationAssertions();

$all = $storage->getAll();
$this->assertNotEmpty($all);
$this->assertContainsOnlyInstancesOf(Permission::class, $storage->getAll());
Expand All @@ -565,6 +580,11 @@ protected function createItemsStorage(): ItemsStorageInterface
return new FakeItemsStorage();
}

protected function getItemsStorageForModificationAssertions(): ItemsStorageInterface
{
return static::$reCreateItemsStorageAfterModifications ? $this->createItemsStorage() : $this->getItemsStorage();
}

protected function getFixtures(): array
{
$time = time();
Expand Down

0 comments on commit a20147a

Please sign in to comment.