Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check item exists on add role or permission (#129)
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Rbac\Exception; | ||
|
||
use RuntimeException; | ||
use Throwable; | ||
use Yiisoft\Rbac\Item; | ||
|
||
final class ItemAlreadyExistsException extends RuntimeException | ||
{ | ||
public function __construct(Item $item, int $code = 0, ?Throwable $previous = null) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'Role or permission with name "%s" already exists.', | ||
$item->getName() | ||
), | ||
$code, | ||
$previous | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Rbac\Tests\Exception; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Rbac\Exception\ItemAlreadyExistsException; | ||
use Yiisoft\Rbac\Role; | ||
|
||
final class ItemAlreadyExistsExceptionTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$exception = new ItemAlreadyExistsException(new Role('reader')); | ||
|
||
$this->assertSame('Role or permission with name "reader" already exists.', $exception->getMessage()); | ||
$this->assertSame(0, $exception->getCode()); | ||
$this->assertNull($exception->getPrevious()); | ||
} | ||
|
||
public function testCodeAndPreviousException(): void | ||
{ | ||
$code = 212; | ||
$previousException = new Exception(); | ||
|
||
$exception = new ItemAlreadyExistsException(new Role('reader'), $code, $previousException); | ||
|
||
$this->assertSame($code, $exception->getCode()); | ||
$this->assertSame($previousException, $exception->getPrevious()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters