Skip to content

Commit

Permalink
Recreate storages in tests (#80)
Browse files Browse the repository at this point in the history
* Recreate storages in tests

* Tests are passing
  • Loading branch information
arogachev authored Jan 26, 2024
1 parent 4da6787 commit ea38291
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"require": {
"php": "^8.1",
"yiisoft/rbac": "dev-master",
"yiisoft/var-dumper": "^1.0"
"yiisoft/var-dumper": "^1.7"
},
"require-dev": {
"ext-uopz": "*",
Expand Down
23 changes: 14 additions & 9 deletions src/AssignmentsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*
* It is suitable for authorization data that is not too big (for example, the authorization data for a personal blog
* system).
*
* @psalm-import-type RawAssignment from SimpleAssignmentsStorage
*/
final class AssignmentsStorage extends SimpleAssignmentsStorage
{
Expand Down Expand Up @@ -86,25 +88,28 @@ public function clear(): void

private function loadAssignments(): void
{
/** @psalm-var array<string|int, string[]> $assignments */
/** @psalm-var list<RawAssignment> $assignments */
$assignments = $this->loadFromFile($this->assignmentFile);
$modifiedTime = @filemtime($this->assignmentFile);
foreach ($assignments as $userId => $roles) {
foreach ($roles as $role) {
/** @psalm-suppress InvalidPropertyAssignmentValue */
$this->assignments[$userId][$role] = new Assignment((string)$userId, $role, $modifiedTime);
}
foreach ($assignments as $assignment) {
/** @psalm-suppress InvalidPropertyAssignmentValue */
$this->assignments[$assignment['user_id']][$assignment['item_name']] = new Assignment(
userId: $assignment['user_id'],
itemName: $assignment['item_name'],
createdAt: $assignment['created_at'] ?? $modifiedTime
);
}
}

private function saveAssignments(): void
{
$assignmentData = [];
foreach ($this->assignments as $userId => $assignments) {
foreach ($assignments as $assignment) {
$assignmentData[$userId][] = $assignment->getItemName();
foreach ($this->assignments as $userAssignments) {
foreach ($userAssignments as $userAssignment) {
$assignmentData[] = $userAssignment->getAttributes();
}
}

$this->saveToFile($assignmentData, $this->assignmentFile);
}
}
27 changes: 14 additions & 13 deletions src/ItemsStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@ private function save(): void
{
$items = [];
foreach ($this->getAll() as $name => $item) {
$items[$name] = array_filter($item->getAttributes());
$data = array_filter($item->getAttributes());
if ($this->hasChildren($name)) {
foreach ($this->getDirectChildren($name) as $child) {
$items[$name]['children'][] = $child->getName();
$data['children'][] = $child->getName();
}
}

$items[] = $data;
}

$this->saveToFile($items, $this->itemFile);
}

Expand All @@ -116,19 +119,17 @@ private function loadItems(): void
/** @psalm-var array<string, RawItem> $items */
$items = $this->loadFromFile($this->itemFile);
$itemsMtime = @filemtime($this->itemFile);
foreach ($items as $name => $item) {
$this->items[$name] = $this
foreach ($items as $item) {
$this->items[$item['name']] = $this
->getInstanceFromAttributes($item)
->withCreatedAt($itemsMtime)
->withUpdatedAt($itemsMtime);
->withCreatedAt($item['created_at'] ?? $itemsMtime)
->withUpdatedAt($item['updated_at'] ?? $itemsMtime);
}

foreach ($items as $name => $item) {
if (isset($item['children'])) {
foreach ($item['children'] as $childName) {
if ($this->hasItem($childName)) {
$this->children[$name][$childName] = $this->items[$childName];
}
foreach ($items as $item) {
foreach ($item['children'] ?? [] as $childName) {
if ($this->hasItem($childName)) {
$this->children[$item['name']][$childName] = $this->items[$childName];
}
}
}
Expand Down Expand Up @@ -161,7 +162,7 @@ private function getInstanceFromAttributes(array $attributes): Permission|Role
$item = $item->withDescription($description);
}

$ruleName = $attributes['ruleName'] ?? null;
$ruleName = $attributes['rule_name'] ?? null;
if ($ruleName !== null) {
$item = $item->withRuleName($ruleName);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/AssignmentsStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ protected function createAssignmentsStorage(): AssignmentsStorageInterface
{
return new AssignmentsStorage($this->getDataPath());
}

protected function getAssignmentsStorageForModificationAssertions(): AssignmentsStorageInterface
{
return $this->createAssignmentsStorage();
}
}
5 changes: 5 additions & 0 deletions tests/ItemsStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ protected function createItemsStorage(): ItemsStorageInterface
return new ItemsStorage($this->getDataPath());
}

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

private function getTempDirectory(): string
{
return __DIR__ . '/temp';
Expand Down

0 comments on commit ea38291

Please sign in to comment.