diff --git a/composer.json b/composer.json index 1bb1333a..736a1b45 100644 --- a/composer.json +++ b/composer.json @@ -34,8 +34,8 @@ ], "require": { "php": ">=8.1", - "spiral/core": "^3.14.10", - "spiral/hmvc": "^3.14.10" + "spiral/core": "^3.15", + "spiral/hmvc": "^3.15" }, "autoload": { "psr-4": { @@ -43,7 +43,7 @@ } }, "require-dev": { - "spiral/console": "^3.14.10", + "spiral/console": "^3.15", "phpunit/phpunit": "^10.1", "mockery/mockery": "^1.5", "vimeo/psalm": "^5.9" diff --git a/src/Actor/Actor.php b/src/Actor/Actor.php index fc20bd84..13481717 100644 --- a/src/Actor/Actor.php +++ b/src/Actor/Actor.php @@ -12,9 +12,8 @@ class Actor implements ActorInterface { public function __construct( - private readonly array $roles - ) { - } + private readonly array $roles, + ) {} public function getRoles(): array { diff --git a/src/Actor/Guest.php b/src/Actor/Guest.php index cd9b2c61..23868658 100644 --- a/src/Actor/Guest.php +++ b/src/Actor/Guest.php @@ -15,6 +15,6 @@ final class Guest implements ActorInterface public function getRoles(): array { - return [static::ROLE]; + return [self::ROLE]; } } diff --git a/src/Command/RolePermissionsCommand.php b/src/Command/RolePermissionsCommand.php index 9ecbc60e..78ab9fe0 100644 --- a/src/Command/RolePermissionsCommand.php +++ b/src/Command/RolePermissionsCommand.php @@ -14,11 +14,9 @@ class RolePermissionsCommand extends Command { protected const NAME = 'security:role:permissions'; protected const DESCRIPTION = 'Get Role(s) Permissions'; - protected const ARGUMENTS = [ ['role', InputArgument::OPTIONAL, 'Role to get permissions'], ]; - private const TABLE_HEADERS = ['role', 'permission', 'rule', 'allowed']; /** @@ -41,7 +39,7 @@ protected function perform(PermissionsInterface $rbac): int /** @noinspection SlowArrayOperationsInLoopInspection */ $rows = \array_merge( $this->getRolePermissions($role, $rbac), - $rows + $rows, ); } } diff --git a/src/Exception/GuardException.php b/src/Exception/GuardException.php index 05a7ae7d..27aad647 100644 --- a/src/Exception/GuardException.php +++ b/src/Exception/GuardException.php @@ -7,6 +7,4 @@ /** * Generic RBAC exception. */ -class GuardException extends SecurityException -{ -} +class GuardException extends SecurityException {} diff --git a/src/Exception/PermissionException.php b/src/Exception/PermissionException.php index da09bcc7..0875efc8 100644 --- a/src/Exception/PermissionException.php +++ b/src/Exception/PermissionException.php @@ -7,6 +7,4 @@ /** * When permission format is invalid or permission does not exists at moment of removal. */ -class PermissionException extends GuardException -{ -} +class PermissionException extends GuardException {} diff --git a/src/Exception/RoleException.php b/src/Exception/RoleException.php index 28144c5b..8d4c1c10 100644 --- a/src/Exception/RoleException.php +++ b/src/Exception/RoleException.php @@ -7,6 +7,4 @@ /** * Exceptions relation to role management. */ -class RoleException extends GuardException -{ -} +class RoleException extends GuardException {} diff --git a/src/Exception/RuleException.php b/src/Exception/RuleException.php index 40b34ce3..e0145460 100644 --- a/src/Exception/RuleException.php +++ b/src/Exception/RuleException.php @@ -7,6 +7,4 @@ /** * Rule specific exception. */ -class RuleException extends GuardException -{ -} +class RuleException extends GuardException {} diff --git a/src/Exception/SecurityException.php b/src/Exception/SecurityException.php index 6845a7cf..d34b5c8b 100644 --- a/src/Exception/SecurityException.php +++ b/src/Exception/SecurityException.php @@ -4,6 +4,4 @@ namespace Spiral\Security\Exception; -class SecurityException extends \RuntimeException -{ -} +class SecurityException extends \RuntimeException {} diff --git a/src/Guard.php b/src/Guard.php index 184ab78e..383463fa 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -17,9 +17,8 @@ final class Guard implements GuardInterface public function __construct( private readonly PermissionsInterface $permissions, private ?ActorInterface $actor = null, - private array $roles = [] - ) { - } + private array $roles = [], + ) {} public function allows(string $permission, array $context = []): bool { diff --git a/src/Matcher.php b/src/Matcher.php index 6202ef29..eec670f4 100644 --- a/src/Matcher.php +++ b/src/Matcher.php @@ -26,7 +26,7 @@ public function matches(string $string, string $pattern): bool return match (true) { $string === $pattern => true, !$this->isPattern($pattern) => false, - default => (bool) \preg_match($this->getRegex($pattern), $string) + default => (bool) \preg_match($this->getRegex($pattern), $string), }; } diff --git a/src/PermissionManager.php b/src/PermissionManager.php index 849ef852..765c9012 100644 --- a/src/PermissionManager.php +++ b/src/PermissionManager.php @@ -28,11 +28,12 @@ final class PermissionManager implements PermissionsInterface * Roles associated with their permissions. */ private array $permissions = []; + private readonly Matcher $matcher; public function __construct( private readonly RulesInterface $rules, - private readonly string $defaultRule = ForbidRule::class + private readonly string $defaultRule = ForbidRule::class, ) { $this->matcher = new Matcher(); } @@ -117,7 +118,6 @@ public function deassociate(string $role, string $permission): PermissionManager } /** - * * @throws PermissionException */ private function findRule(string $role, string $permission): string diff --git a/src/PermissionsInterface.php b/src/PermissionsInterface.php index 799ef516..c967ba79 100644 --- a/src/PermissionsInterface.php +++ b/src/PermissionsInterface.php @@ -70,6 +70,6 @@ public function getRule(string $role, string $permission): RuleInterface; public function associate( string $role, string $permission, - string $rule = 'Spiral\Security\Rules\AllowRule' + string $rule = 'Spiral\Security\Rules\AllowRule', ); } diff --git a/src/Rule.php b/src/Rule.php index fe15e9be..a6cd8467 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -36,9 +36,8 @@ abstract class Rule implements RuleInterface ]; public function __construct( - protected ResolverInterface $resolver - ) { - } + protected ResolverInterface $resolver, + ) {} /** * @throws RuleException diff --git a/src/Rule/CompositeRule.php b/src/Rule/CompositeRule.php index 6a416b0c..d22df4f4 100644 --- a/src/Rule/CompositeRule.php +++ b/src/Rule/CompositeRule.php @@ -31,9 +31,8 @@ abstract class CompositeRule implements RuleInterface protected const RULES = []; public function __construct( - private readonly RulesInterface $repository - ) { - } + private readonly RulesInterface $repository, + ) {} public function allows(ActorInterface $actor, string $permission, array $context): bool { diff --git a/src/RuleManager.php b/src/RuleManager.php index fe3fd5a7..3846700c 100644 --- a/src/RuleManager.php +++ b/src/RuleManager.php @@ -18,9 +18,8 @@ final class RuleManager implements RulesInterface private array $rules = []; public function __construct( - private readonly ContainerInterface $container - ) { - } + private readonly ContainerInterface $container, + ) {} public function set(string $name, mixed $rule = null): RuleManager { @@ -55,7 +54,7 @@ public function has(string $name): bool // We are allowing to use class names without direct registration \class_exists($name) => true, // Relying on container binding - default => $this->container->has($name) + default => $this->container->has($name), }; } @@ -84,7 +83,7 @@ public function get(string $name): RuleInterface throw new RuleException(\sprintf( "Rule '%s' must point to RuleInterface, '%s' given", $name, - !empty($rule) ? $rule::class : 'null' + !empty($rule) ? $rule::class : 'null', )); } diff --git a/src/Traits/GuardedTrait.php b/src/Traits/GuardedTrait.php index c9a3ec8f..4a7d3fbf 100644 --- a/src/Traits/GuardedTrait.php +++ b/src/Traits/GuardedTrait.php @@ -22,7 +22,7 @@ public function getGuard(): GuardInterface $container = ContainerScope::getContainer(); if (empty($container) || !$container->has(GuardInterface::class)) { throw new ScopeException( - 'Unable to get `GuardInterface`, binding is missing or container scope is not set' + 'Unable to get `GuardInterface`, binding is missing or container scope is not set', ); } diff --git a/tests/Actors/ActorTest.php b/tests/Actors/ActorTest.php index 660a7109..38ede910 100644 --- a/tests/Actors/ActorTest.php +++ b/tests/Actors/ActorTest.php @@ -17,6 +17,6 @@ public function testGetRoles(): void /** @var ActorInterface $actor */ $actor = new Actor($roles); - $this->assertEquals($roles, $actor->getRoles()); + self::assertEquals($roles, $actor->getRoles()); } } diff --git a/tests/Actors/GuestTest.php b/tests/Actors/GuestTest.php index 1d88942f..450a488d 100644 --- a/tests/Actors/GuestTest.php +++ b/tests/Actors/GuestTest.php @@ -15,6 +15,6 @@ public function testGetRoles(): void /** @var ActorInterface $actor */ $actor = new Guest(); - $this->assertEquals([Guest::ROLE], $actor->getRoles()); + self::assertEquals([Guest::ROLE], $actor->getRoles()); } } diff --git a/tests/Actors/NullActorTest.php b/tests/Actors/NullActorTest.php index 20e2f269..48cd27a8 100644 --- a/tests/Actors/NullActorTest.php +++ b/tests/Actors/NullActorTest.php @@ -15,6 +15,6 @@ public function testGetRoles(): void /** @var ActorInterface $actor */ $actor = new NullActor(); - $this->assertEquals([], $actor->getRoles()); + self::assertEquals([], $actor->getRoles()); } } diff --git a/tests/GuardTest.php b/tests/GuardTest.php index f5add45e..eb6cb96f 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -4,6 +4,7 @@ namespace Spiral\Tests\Security; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Spiral\Security\ActorInterface; use Spiral\Security\Exception\GuardException; @@ -16,26 +17,9 @@ class GuardTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - /** - * @var \PHPUnit_Framework_MockObject_MockObject|PermissionsInterface - */ - private $permission; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface - */ - private $actor; - - /** - * @var array - */ - private $roles = ['user', 'admin']; - - public function setUp(): void - { - $this->permission = $this->createMock(PermissionsInterface::class); - $this->actor = $this->createMock(ActorInterface::class); - } + private MockObject&PermissionsInterface $permission; + private MockObject&ActorInterface $actor; + private array $roles = ['user', 'admin']; public function testAllows(): void { @@ -61,7 +45,7 @@ public function testAllows(): void ->willReturn($rule); $guard = new Guard($this->permission, $this->actor, $this->roles); - $this->assertTrue($guard->allows(static::OPERATION, static::CONTEXT)); + self::assertTrue($guard->allows(static::OPERATION, static::CONTEXT)); } public function testAllowsPermissionsHasNoRole(): void @@ -69,7 +53,7 @@ public function testAllowsPermissionsHasNoRole(): void $this->permission->method('hasRole')->with($this->anything())->willReturn(false); $guard = new Guard($this->permission, $this->actor, $this->roles); - $this->assertFalse($guard->allows(static::OPERATION, static::CONTEXT)); + self::assertFalse($guard->allows(static::OPERATION, static::CONTEXT)); } public function testAllowsNoActor(): void @@ -85,8 +69,8 @@ public function testWithActor(): void $guard = new Guard($this->permission); $guardWithActor = $guard->withActor($this->actor); - $this->assertEquals($this->actor, $guardWithActor->getActor()); - $this->assertNotEquals($guard, $guardWithActor); + self::assertEquals($this->actor, $guardWithActor->getActor()); + self::assertNotEquals($guard, $guardWithActor); } public function testWithRoles(): void @@ -94,7 +78,13 @@ public function testWithRoles(): void $guard = new Guard($this->permission, $this->actor); $guardWithRoles = $guard->withRoles($this->roles); - $this->assertEquals($this->roles, $guardWithRoles->getRoles()); - $this->assertNotEquals($guard, $guardWithRoles); + self::assertEquals($this->roles, $guardWithRoles->getRoles()); + self::assertNotEquals($guard, $guardWithRoles); + } + + protected function setUp(): void + { + $this->permission = $this->createMock(PermissionsInterface::class); + $this->actor = $this->createMock(ActorInterface::class); } } diff --git a/tests/PermissionManagerTest.php b/tests/PermissionManagerTest.php index 920d0254..9ad8611c 100644 --- a/tests/PermissionManagerTest.php +++ b/tests/PermissionManagerTest.php @@ -4,13 +4,13 @@ namespace Spiral\Tests\Security; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Spiral\Security\Exception\PermissionException; use Spiral\Security\Exception\RoleException; use Spiral\Security\PermissionManager; use Spiral\Security\Rule\AllowRule; use Spiral\Security\Rule\ForbidRule; -use Spiral\Security\RuleInterface; use Spiral\Security\RulesInterface; /** @@ -23,29 +23,21 @@ class PermissionManagerTest extends TestCase public const ROLE = 'test'; public const PERMISSION = 'permission'; - /** - * @var RulesInterface - */ - private $rules; - - public function setUp(): void - { - $this->rules = $this->createMock(RulesInterface::class); - } + private MockObject&RulesInterface $rules; public function testRoles(): void { $manager = new PermissionManager($this->rules); - $this->assertFalse($manager->hasRole(static::ROLE)); - $this->assertEquals($manager, $manager->addRole(static::ROLE)); - $this->assertTrue($manager->hasRole(static::ROLE)); - $this->assertEquals($manager, $manager->removeRole(static::ROLE)); - $this->assertFalse($manager->hasRole(static::ROLE)); + self::assertFalse($manager->hasRole(static::ROLE)); + self::assertEquals($manager, $manager->addRole(static::ROLE)); + self::assertTrue($manager->hasRole(static::ROLE)); + self::assertEquals($manager, $manager->removeRole(static::ROLE)); + self::assertFalse($manager->hasRole(static::ROLE)); $manager->addRole('one'); $manager->addRole('two'); - $this->assertEquals(['one', 'two'], $manager->getRoles()); + self::assertSame(['one', 'two'], $manager->getRoles()); } public function testAddRoleException(): void @@ -89,15 +81,15 @@ public function testAssociation(): void $manager->addRole(static::ROLE); // test simple permission - $this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION, AllowRule::class)); - $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION)); + self::assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION, AllowRule::class)); + self::assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION)); // test pattern permission - $this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION . '.*', AllowRule::class)); - $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION . '.' . static::PERMISSION)); + self::assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION . '.*', AllowRule::class)); + self::assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION . '.' . static::PERMISSION)); - $this->assertEquals($manager, $manager->deassociate(static::ROLE, static::PERMISSION)); - $this->assertEquals($forbidRule, $manager->getRule(static::ROLE, static::PERMISSION)); + self::assertEquals($manager, $manager->deassociate(static::ROLE, static::PERMISSION)); + self::assertEquals($forbidRule, $manager->getRule(static::ROLE, static::PERMISSION)); } public function testGetRuleRoleException(): void @@ -126,8 +118,8 @@ public function testRulesForRole(): void $manager->addRole('admin'); $manager->associate('admin', 'post.edit', AllowRule::class); - $this->assertSame([ - 'post.edit' => AllowRule::class + self::assertSame([ + 'post.edit' => AllowRule::class, ], $manager->getPermissions('admin')); } @@ -140,10 +132,7 @@ public function testGetFallbackRule(): void ->with(ForbidRule::class) ->willReturn(new ForbidRule()); - $this->assertInstanceOf( - ForbidRule::class, - $manager->getRule(static::ROLE, static::PERMISSION) - ); + self::assertInstanceOf(ForbidRule::class, $manager->getRule(static::ROLE, static::PERMISSION)); } public function testAssociateRoleException(): void @@ -162,4 +151,9 @@ public function testAssociatePermissionException(): void $manager->addRole(static::ROLE); $manager->associate(static::ROLE, static::PERMISSION); } + + protected function setUp(): void + { + $this->rules = $this->createMock(RulesInterface::class); + } } diff --git a/tests/RuleManagerTest.php b/tests/RuleManagerTest.php index 6dbf3687..8e627dd6 100644 --- a/tests/RuleManagerTest.php +++ b/tests/RuleManagerTest.php @@ -10,7 +10,6 @@ use Spiral\Security\Exception\RuleException; use Spiral\Security\RuleInterface; use Spiral\Security\RuleManager; -use Spiral\Security\Rule\CallableRule; /** * Class RuleManagerTest @@ -27,12 +26,6 @@ class RuleManagerTest extends TestCase /** @var RuleInterface */ private $rule; - public function setUp(): void - { - $this->container = m::mock(ContainerInterface::class); - $this->rule = m::mock(RuleInterface::class); - } - public function testFlow(): void { $ruleClass = $this->rule::class; @@ -44,18 +37,18 @@ public function testFlow(): void $manager = new RuleManager($this->container); - $this->assertEquals($manager, $manager->set(self::RULE_NAME, $ruleClass)); - $this->assertTrue($manager->has(self::RULE_NAME)); - $this->assertEquals($this->rule, $manager->get(self::RULE_NAME)); - $this->assertEquals($manager, $manager->remove(self::RULE_NAME)); + self::assertEquals($manager, $manager->set(self::RULE_NAME, $ruleClass)); + self::assertTrue($manager->has(self::RULE_NAME)); + self::assertEquals($this->rule, $manager->get(self::RULE_NAME)); + self::assertEquals($manager, $manager->remove(self::RULE_NAME)); // other rule types $manager->set('RuleInterface', $this->rule); - $this->assertEquals($this->rule, $manager->get('RuleInterface')); - $manager->set('Closure', fn() => true); - $this->assertTrue($manager->get('Closure') instanceof CallableRule); + self::assertEquals($this->rule, $manager->get('RuleInterface')); + $manager->set('Closure', static fn(): bool => true); + self::assertInstanceOf(\Spiral\Security\Rule\CallableRule::class, $manager->get('Closure')); $manager->set('Array', $this->testFlow(...)); - $this->assertTrue($manager->get('Array') instanceof CallableRule); + self::assertInstanceOf(\Spiral\Security\Rule\CallableRule::class, $manager->get('Array')); } public function testHasWithNotRegisteredClass(): void @@ -63,7 +56,7 @@ public function testHasWithNotRegisteredClass(): void $ruleClass = $this->rule::class; $manager = new RuleManager($this->container); - $this->assertTrue($manager->has($ruleClass)); + self::assertTrue($manager->has($ruleClass)); } public function testSetRuleException(): void @@ -107,11 +100,17 @@ public function testGetWithSomethingOtherThanRule(): void $this->container->shouldReceive('get') ->with($ruleClass) - ->andReturn(new $ruleClass); + ->andReturn(new $ruleClass()); $manager = new RuleManager($this->container); $this->expectException(RuleException::class); $manager->get($ruleClass); } + + protected function setUp(): void + { + $this->container = m::mock(ContainerInterface::class); + $this->rule = m::mock(RuleInterface::class); + } } diff --git a/tests/RuleTest.php b/tests/RuleTest.php index b3aabe43..f73c4574 100644 --- a/tests/RuleTest.php +++ b/tests/RuleTest.php @@ -5,6 +5,7 @@ namespace Spiral\Tests\Security; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Spiral\Core\ResolverInterface; use Spiral\Security\ActorInterface; @@ -21,39 +22,27 @@ class RuleTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - /** - * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface - */ - private $actor; + private MockObject&ActorInterface $actor; + private MockObject&ResolverInterface $resolver; + private MockObject&Rule $rule; - /** - * @var \PHPUnit_Framework_MockObject_MockObject|ResolverInterface - */ - private $resolver; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|Rule - */ - private $rule; - - protected function setUp(): void + public static function allowsProvider(): \Traversable { - $this->actor = $this->createMock(ActorInterface::class); - $this->resolver = $this->createMock(ResolverInterface::class); - $this->rule = $this->getMockBuilder(Rule::class) - ->setConstructorArgs([$this->resolver]) - ->addMethods(['check'])->getMock(); + yield ['test.create', [], false]; + yield ['test.create', [], true]; + yield ['test.create', ['a' => 'b'], false]; + yield ['test.create', ['a' => 'b'], true]; } #[DataProvider('allowsProvider')] public function testAllows(string $permission, array $context, bool $allowed): void { $parameters = [ - 'actor' => $this->actor, - 'user' => $this->actor, - 'permission' => $permission, - 'context' => $context, - ] + $context; + 'actor' => $this->actor, + 'user' => $this->actor, + 'permission' => $permission, + 'context' => $context, + ] + $context; $method = new \ReflectionMethod($this->rule, 'check'); $this->resolver @@ -68,7 +57,7 @@ public function testAllows(string $permission, array $context, bool $allowed): v ->with($parameters) ->willReturn($allowed); - $this->assertEquals($allowed, $this->rule->allows($this->actor, $permission, $context)); + self::assertSame($allowed, $this->rule->allows($this->actor, $permission, $context)); } public function testAllowsException(): void @@ -77,11 +66,12 @@ public function testAllowsException(): void $this->rule->allows($this->actor, static::OPERATION, static::CONTEXT); } - public static function allowsProvider(): \Traversable + protected function setUp(): void { - yield ['test.create', [], false]; - yield ['test.create', [], true]; - yield ['test.create', ['a' => 'b'], false]; - yield ['test.create', ['a' => 'b'], true]; + $this->actor = $this->createMock(ActorInterface::class); + $this->resolver = $this->createMock(ResolverInterface::class); + $this->rule = $this->getMockBuilder(Rule::class) + ->setConstructorArgs([$this->resolver]) + ->addMethods(['check'])->getMock(); } } diff --git a/tests/Rules/AllowRuleTest.php b/tests/Rules/AllowRuleTest.php index bba98a02..00c046b3 100644 --- a/tests/Rules/AllowRuleTest.php +++ b/tests/Rules/AllowRuleTest.php @@ -26,6 +26,6 @@ public function testAllow(): void /** @var ActorInterface $actor */ $actor = $this->createMock(ActorInterface::class); - $this->assertTrue($rule->allows($actor, static::OPERATION, static::CONTEXT)); + self::assertTrue($rule->allows($actor, static::OPERATION, static::CONTEXT)); } } diff --git a/tests/Rules/CallableRuleTest.php b/tests/Rules/CallableRuleTest.php index 1af470bb..70d59d0a 100644 --- a/tests/Rules/CallableRuleTest.php +++ b/tests/Rules/CallableRuleTest.php @@ -20,7 +20,7 @@ public function testAllow(): void $actor = $this->createMock(ActorInterface::class); $context = []; - /** @var \PHPUnit_Framework_MockObject_MockObject|callable $callable */ + /** @var \PHPUnit\Framework\MockObject\MockObject|callable $callable */ $callable = $this->getMockBuilder(\stdClass::class) ->addMethods(['__invoke']) ->getMock(); @@ -32,7 +32,7 @@ public function testAllow(): void /** @var RuleInterface $rule */ $rule = new CallableRule($callable); - $this->assertTrue($rule->allows($actor, static::OPERATION, $context)); - $this->assertFalse($rule->allows($actor, static::OPERATION, $context)); + self::assertTrue($rule->allows($actor, static::OPERATION, $context)); + self::assertFalse($rule->allows($actor, static::OPERATION, $context)); } } diff --git a/tests/Rules/CompositeRuleTest.php b/tests/Rules/CompositeRuleTest.php index 29eb8426..9f07f763 100644 --- a/tests/Rules/CompositeRuleTest.php +++ b/tests/Rules/CompositeRuleTest.php @@ -5,6 +5,7 @@ namespace Spiral\Tests\Security\Rules; use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls; use PHPUnit\Framework\TestCase; use Spiral\Security\ActorInterface; @@ -18,28 +19,7 @@ class CompositeRuleTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - /** - * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface $callable - */ - private $actor; - - public function setUp(): void - { - $this->actor = $this->createMock(ActorInterface::class); - } - - #[DataProvider('allowsProvider')] - public function testAllow(bool $expected, string $compositeRuleClass, array $rules): void - { - $repository = $this->createRepository($rules); - - /** @var RuleInterface $rule */ - $rule = new $compositeRuleClass($repository); - $this->assertEquals( - $expected, - $rule->allows($this->actor, static::OPERATION, static::CONTEXT) - ); - } + private MockObject&ActorInterface $actor; public static function allowsProvider(): \Traversable { @@ -53,16 +33,19 @@ public static function allowsProvider(): \Traversable yield [false, OneCompositeRule::class, [$forbidRule, $forbidRule, $forbidRule]]; } - - private function createRepository(array $rules): RulesInterface + #[DataProvider('allowsProvider')] + public function testAllow(bool $expected, string $compositeRuleClass, array $rules): void { - /** @var \PHPUnit_Framework_MockObject_MockObject|RulesInterface $repository */ - $repository = $this->createMock(RulesInterface::class); + $repository = $this->createRepository($rules); - $repository->method('get') - ->will(new ConsecutiveCalls($rules)); + /** @var RuleInterface $rule */ + $rule = new $compositeRuleClass($repository); + self::assertEquals($expected, $rule->allows($this->actor, static::OPERATION, static::CONTEXT)); + } - return $repository; + protected function setUp(): void + { + $this->actor = $this->createMock(ActorInterface::class); } private static function allowRule(): RuleInterface @@ -80,4 +63,15 @@ private static function forbidRule(): RuleInterface return $rule; } + + private function createRepository(array $rules): RulesInterface + { + /** @var MockObject|RulesInterface $repository */ + $repository = $this->createMock(RulesInterface::class); + + $repository->method('get') + ->will(new ConsecutiveCalls($rules)); + + return $repository; + } } diff --git a/tests/Rules/ForbidRuleTest.php b/tests/Rules/ForbidRuleTest.php index a6465cc9..5d4a0638 100644 --- a/tests/Rules/ForbidRuleTest.php +++ b/tests/Rules/ForbidRuleTest.php @@ -26,6 +26,6 @@ public function testAllow(): void /** @var ActorInterface $actor */ $actor = $this->createMock(ActorInterface::class); - $this->assertFalse($rule->allows($actor, static::OPERATION, static::CONTEXT)); + self::assertFalse($rule->allows($actor, static::OPERATION, static::CONTEXT)); } } diff --git a/tests/Traits/GuardedTraitTest.php b/tests/Traits/GuardedTraitTest.php index f55f4689..817a9c63 100644 --- a/tests/Traits/GuardedTraitTest.php +++ b/tests/Traits/GuardedTraitTest.php @@ -4,6 +4,7 @@ namespace Spiral\Tests\Security\Traits; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use Spiral\Core\Container; @@ -19,35 +20,17 @@ class GuardedTraitTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - /** - * @var \PHPUnit_Framework_MockObject_MockObject|GuardedTrait - */ - private $trait; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|GuardInterface - */ - private $guard; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface - */ - private $container; - - public function setUp(): void - { - $this->trait = $this->getMockForTrait(GuardedTrait::class); - $this->guard = $this->createMock(GuardInterface::class); - $this->container = $this->createMock(ContainerInterface::class); - } + private object $trait; + private MockObject&GuardInterface $guard; + private MockObject&ContainerInterface $container; public function testGetGuardFromContainer(): void { $this->container->method('has')->willReturn(true); - $this->container->method('get')->will($this->returnValue($this->guard)); + $this->container->method('get')->willReturn($this->guard); ContainerScope::runScope($this->container, function (): void { - $this->assertEquals($this->guard, $this->trait->getGuard()); + self::assertEquals($this->guard, $this->trait->getGuard()); }); } @@ -58,7 +41,7 @@ public function testGuardScopeException(): void $this->container->method('has')->willReturn(false); ContainerScope::runScope($this->container, function (): void { - $this->assertEquals($this->guard, $this->trait->getGuard()); + self::assertEquals($this->guard, $this->trait->getGuard()); }); } @@ -66,14 +49,14 @@ public function testGuardScopeException2(): void { $this->expectException(ScopeException::class); - $this->assertEquals($this->guard, $this->trait->getGuard()); + self::assertEquals($this->guard, $this->trait->getGuard()); } public function testAllows(): void { $this->guard->method('allows') ->with(static::OPERATION, static::CONTEXT) - ->will($this->returnValue(true)) + ->willReturn(true) ; $guarded = new Guarded(); @@ -82,18 +65,27 @@ public function testAllows(): void $container->bind(GuardInterface::class, $this->guard); ContainerScope::runScope($container, function () use ($guarded): void { - $this->assertTrue($guarded->allows(static::OPERATION, static::CONTEXT)); - $this->assertFalse($guarded->denies(static::OPERATION, static::CONTEXT)); + self::assertTrue($guarded->allows(static::OPERATION, static::CONTEXT)); + self::assertFalse($guarded->denies(static::OPERATION, static::CONTEXT)); }); } public function testResolvePermission(): void { $guarded = new Guarded(); - $this->assertEquals(static::OPERATION, $guarded->resolvePermission(static::OPERATION)); + self::assertSame(static::OPERATION, $guarded->resolvePermission(static::OPERATION)); $guarded = new GuardedWithNamespace(); $resolvedPermission = GuardedWithNamespace::GUARD_NAMESPACE . '.' . static::OPERATION; - $this->assertEquals($resolvedPermission, $guarded->resolvePermission(static::OPERATION)); + self::assertSame($resolvedPermission, $guarded->resolvePermission(static::OPERATION)); + } + + protected function setUp(): void + { + $this->trait = new class { + use GuardedTrait; + }; + $this->guard = $this->createMock(GuardInterface::class); + $this->container = $this->createMock(ContainerInterface::class); } }