From 85a4c44cb101a019a867c26d0a77bceea7b70f2c Mon Sep 17 00:00:00 2001 From: spiralbot Date: Sat, 4 Jan 2025 12:32:38 +0000 Subject: [PATCH] refactor: enable phpunit code quality set for rector (#1186) --- tests/Actors/ActorTest.php | 2 +- tests/Actors/GuestTest.php | 2 +- tests/Actors/NullActorTest.php | 2 +- tests/GuardTest.php | 12 ++++++------ tests/PermissionManagerTest.php | 31 ++++++++++++++----------------- tests/RuleManagerTest.php | 16 ++++++++-------- tests/RuleTest.php | 2 +- tests/Rules/AllowRuleTest.php | 2 +- tests/Rules/CallableRuleTest.php | 4 ++-- tests/Rules/CompositeRuleTest.php | 5 +---- tests/Rules/ForbidRuleTest.php | 2 +- tests/Traits/GuardedTraitTest.php | 18 +++++++++--------- 12 files changed, 46 insertions(+), 52 deletions(-) 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 bb9b5f11..d34f245e 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -53,7 +53,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 @@ -61,7 +61,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 @@ -77,8 +77,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 @@ -86,7 +86,7 @@ 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); } } diff --git a/tests/PermissionManagerTest.php b/tests/PermissionManagerTest.php index 23621129..4416ce69 100644 --- a/tests/PermissionManagerTest.php +++ b/tests/PermissionManagerTest.php @@ -35,15 +35,15 @@ 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 @@ -87,15 +87,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 @@ -124,7 +124,7 @@ public function testRulesForRole(): void $manager->addRole('admin'); $manager->associate('admin', 'post.edit', AllowRule::class); - $this->assertSame([ + self::assertSame([ 'post.edit' => AllowRule::class ], $manager->getPermissions('admin')); } @@ -138,10 +138,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 diff --git a/tests/RuleManagerTest.php b/tests/RuleManagerTest.php index 60e040cf..6fdd1532 100644 --- a/tests/RuleManagerTest.php +++ b/tests/RuleManagerTest.php @@ -44,18 +44,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')); + self::assertEquals($this->rule, $manager->get('RuleInterface')); $manager->set('Closure', static fn(): bool => true); - $this->assertTrue($manager->get('Closure') instanceof CallableRule); + 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 +63,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 diff --git a/tests/RuleTest.php b/tests/RuleTest.php index f373a648..c2bc0d19 100644 --- a/tests/RuleTest.php +++ b/tests/RuleTest.php @@ -60,7 +60,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 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 881101d7..70d59d0a 100644 --- a/tests/Rules/CallableRuleTest.php +++ b/tests/Rules/CallableRuleTest.php @@ -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 28d7ec44..af2dd82c 100644 --- a/tests/Rules/CompositeRuleTest.php +++ b/tests/Rules/CompositeRuleTest.php @@ -33,10 +33,7 @@ public function testAllow(bool $expected, string $compositeRuleClass, array $rul /** @var RuleInterface $rule */ $rule = new $compositeRuleClass($repository); - $this->assertEquals( - $expected, - $rule->allows($this->actor, static::OPERATION, static::CONTEXT) - ); + self::assertEquals($expected, $rule->allows($this->actor, static::OPERATION, static::CONTEXT)); } public static function allowsProvider(): \Traversable 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 dc2daf4a..1d924519 100644 --- a/tests/Traits/GuardedTraitTest.php +++ b/tests/Traits/GuardedTraitTest.php @@ -38,10 +38,10 @@ public function setUp(): void 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()); }); } @@ -52,7 +52,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()); }); } @@ -60,14 +60,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(); @@ -76,18 +76,18 @@ 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)); } }