Skip to content

Commit

Permalink
Return $this instead of throwing "already assigned" exception in `M…
Browse files Browse the repository at this point in the history
…anager::assign()` (#253)
  • Loading branch information
arogachev committed Feb 14, 2024
1 parent d99efda commit 98cf8d7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -56,6 +56,7 @@
- Bug #237: Handle not found base item in access tree (@arogachev)
- Enh #245: Handle same names during renaming item in `AssignmentsStorage` (@arogachev)
- Chg #208: Rename `getAccessTree()` to `getHierarchy()` in `ItemsStorageInterface` (@arogachev)
- Enh #252: Return `$this` instead of throwing "already assigned" exception in `Manager::assign()` (@arogachev)

## 1.0.2 April 20, 2023

Expand Down
4 changes: 1 addition & 3 deletions src/Manager.php
Expand Up @@ -153,9 +153,7 @@ public function assign(string $itemName, int|Stringable|string $userId, ?int $cr
}

if ($this->assignmentsStorage->exists($itemName, $userId)) {
throw new InvalidArgumentException(
"\"$itemName\" {$item->getType()} has already been assigned to user $userId.",
);
return $this;
}

$assignment = new Assignment($userId, $itemName, $createdAt ?? time());
Expand Down
20 changes: 5 additions & 15 deletions tests/Common/ManagerLogicTestTrait.php
Expand Up @@ -485,23 +485,13 @@ public function testAssignUnknownItem(): void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('There is no item named "nonExistRole".');

$manager->assign(
'nonExistRole',
'reader'
);
$manager->assign(itemName: 'nonExistRole', userId: 'reader');
}

public function testAssignAlreadyAssignedItem(): void
{
$manager = $this->createFilledManager();

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('"reader" role has already been assigned to user reader A.');

$manager->assign(
'reader',
'reader A'
);
$this->assertSame($manager, $manager->assign(itemName: 'reader', userId: 'reader A'));
}

public function testAssignPermissionDirectlyWhenItIsDisabled(): void
Expand All @@ -511,7 +501,7 @@ public function testAssignPermissionDirectlyWhenItIsDisabled(): void

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Assigning permissions directly is disabled. Prefer assigning roles only.');
$manager->assign('readPost', 'id7');
$manager->assign(itemName: 'readPost', userId: 'id7');
}

public function testAssignPermissionDirectlyWhenEnabled(): void
Expand Down Expand Up @@ -1021,8 +1011,8 @@ public function testDataPersistency(): void
->addRole((new Role('role1'))->withCreatedAt(1_694_502_936)->withUpdatedAt(1_694_502_936))
->addRole((new Role('role2'))->withCreatedAt(1_694_502_976)->withUpdatedAt(1_694_502_976))
->addChild('role1', 'role2');
$manager->assign('role1', 1);
$manager->assign('role2', 2);
$manager->assign(itemName: 'role1', userId: 1);
$manager->assign(itemName: 'role2', userId: 2);

$this->assertEquals(
[
Expand Down

0 comments on commit 98cf8d7

Please sign in to comment.