Skip to content

Commit

Permalink
refactor: enable phpunit code quality set for rector (#1186)
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 4, 2025
1 parent dbfac48 commit 85a4c44
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 52 deletions.
2 changes: 1 addition & 1 deletion tests/Actors/ActorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/Actors/GuestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/Actors/NullActorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public function testGetRoles(): void
/** @var ActorInterface $actor */
$actor = new NullActor();

$this->assertEquals([], $actor->getRoles());
self::assertEquals([], $actor->getRoles());
}
}
12 changes: 6 additions & 6 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ 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
{
$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
Expand All @@ -77,16 +77,16 @@ 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
{
$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);
}
}
31 changes: 14 additions & 17 deletions tests/PermissionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'));
}
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions tests/RuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ 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
{
$ruleClass = $this->rule::class;
$manager = new RuleManager($this->container);

$this->assertTrue($manager->has($ruleClass));
self::assertTrue($manager->has($ruleClass));
}

public function testSetRuleException(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/AllowRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
4 changes: 2 additions & 2 deletions tests/Rules/CallableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
5 changes: 1 addition & 4 deletions tests/Rules/CompositeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/ForbidRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
18 changes: 9 additions & 9 deletions tests/Traits/GuardedTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}

Expand All @@ -52,22 +52,22 @@ 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());
});
}

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();
Expand All @@ -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));
}
}

0 comments on commit 85a4c44

Please sign in to comment.