From 9485a3c6881f2704096430608d1da63b5e52e1f4 Mon Sep 17 00:00:00 2001 From: spiralbot Date: Wed, 22 Jan 2025 15:14:18 +0000 Subject: [PATCH] prepare release --- composer.json | 6 ++--- tests/Actors/ActorTest.php | 2 +- tests/Actors/GuestTest.php | 2 +- tests/Actors/NullActorTest.php | 2 +- tests/GuardTest.php | 30 ++++++++++++++--------- tests/PermissionManagerTest.php | 39 +++++++++++++++++------------- tests/RuleManagerTest.php | 20 ++++++++-------- tests/RuleTest.php | 18 ++++++++++---- tests/Rules/AllowRuleTest.php | 2 +- tests/Rules/CallableRuleTest.php | 6 ++--- tests/Rules/CompositeRuleTest.php | 17 ++++++++----- tests/Rules/ForbidRuleTest.php | 2 +- tests/Traits/GuardedTraitTest.php | 40 ++++++++++++++++++------------- 13 files changed, 109 insertions(+), 77 deletions(-) diff --git a/composer.json b/composer.json index 736a1b45..1bb1333a 100644 --- a/composer.json +++ b/composer.json @@ -34,8 +34,8 @@ ], "require": { "php": ">=8.1", - "spiral/core": "^3.15", - "spiral/hmvc": "^3.15" + "spiral/core": "^3.14.10", + "spiral/hmvc": "^3.14.10" }, "autoload": { "psr-4": { @@ -43,7 +43,7 @@ } }, "require-dev": { - "spiral/console": "^3.15", + "spiral/console": "^3.14.10", "phpunit/phpunit": "^10.1", "mockery/mockery": "^1.5", "vimeo/psalm": "^5.9" diff --git a/tests/Actors/ActorTest.php b/tests/Actors/ActorTest.php index 38ede910..660a7109 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); - self::assertEquals($roles, $actor->getRoles()); + $this->assertEquals($roles, $actor->getRoles()); } } diff --git a/tests/Actors/GuestTest.php b/tests/Actors/GuestTest.php index 450a488d..1d88942f 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(); - self::assertEquals([Guest::ROLE], $actor->getRoles()); + $this->assertEquals([Guest::ROLE], $actor->getRoles()); } } diff --git a/tests/Actors/NullActorTest.php b/tests/Actors/NullActorTest.php index 48cd27a8..20e2f269 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(); - self::assertEquals([], $actor->getRoles()); + $this->assertEquals([], $actor->getRoles()); } } diff --git a/tests/GuardTest.php b/tests/GuardTest.php index 6d08ff88..f5add45e 100644 --- a/tests/GuardTest.php +++ b/tests/GuardTest.php @@ -4,7 +4,6 @@ namespace Spiral\Tests\Security; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Spiral\Security\ActorInterface; use Spiral\Security\Exception\GuardException; @@ -17,13 +16,22 @@ class GuardTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - private MockObject&PermissionsInterface $permission; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|PermissionsInterface + */ + private $permission; - private MockObject&ActorInterface $actor; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface + */ + private $actor; - private array $roles = ['user', 'admin']; + /** + * @var array + */ + private $roles = ['user', 'admin']; - protected function setUp(): void + public function setUp(): void { $this->permission = $this->createMock(PermissionsInterface::class); $this->actor = $this->createMock(ActorInterface::class); @@ -53,7 +61,7 @@ public function testAllows(): void ->willReturn($rule); $guard = new Guard($this->permission, $this->actor, $this->roles); - self::assertTrue($guard->allows(static::OPERATION, static::CONTEXT)); + $this->assertTrue($guard->allows(static::OPERATION, static::CONTEXT)); } public function testAllowsPermissionsHasNoRole(): void @@ -61,7 +69,7 @@ public function testAllowsPermissionsHasNoRole(): void $this->permission->method('hasRole')->with($this->anything())->willReturn(false); $guard = new Guard($this->permission, $this->actor, $this->roles); - self::assertFalse($guard->allows(static::OPERATION, static::CONTEXT)); + $this->assertFalse($guard->allows(static::OPERATION, static::CONTEXT)); } public function testAllowsNoActor(): void @@ -77,8 +85,8 @@ public function testWithActor(): void $guard = new Guard($this->permission); $guardWithActor = $guard->withActor($this->actor); - self::assertEquals($this->actor, $guardWithActor->getActor()); - self::assertNotEquals($guard, $guardWithActor); + $this->assertEquals($this->actor, $guardWithActor->getActor()); + $this->assertNotEquals($guard, $guardWithActor); } public function testWithRoles(): void @@ -86,7 +94,7 @@ public function testWithRoles(): void $guard = new Guard($this->permission, $this->actor); $guardWithRoles = $guard->withRoles($this->roles); - self::assertEquals($this->roles, $guardWithRoles->getRoles()); - self::assertNotEquals($guard, $guardWithRoles); + $this->assertEquals($this->roles, $guardWithRoles->getRoles()); + $this->assertNotEquals($guard, $guardWithRoles); } } diff --git a/tests/PermissionManagerTest.php b/tests/PermissionManagerTest.php index c4e0faf1..920d0254 100644 --- a/tests/PermissionManagerTest.php +++ b/tests/PermissionManagerTest.php @@ -4,7 +4,6 @@ namespace Spiral\Tests\Security; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Spiral\Security\Exception\PermissionException; use Spiral\Security\Exception\RoleException; @@ -24,9 +23,12 @@ class PermissionManagerTest extends TestCase public const ROLE = 'test'; public const PERMISSION = 'permission'; - private MockObject&RulesInterface $rules; + /** + * @var RulesInterface + */ + private $rules; - protected function setUp(): void + public function setUp(): void { $this->rules = $this->createMock(RulesInterface::class); } @@ -35,15 +37,15 @@ public function testRoles(): void { $manager = new PermissionManager($this->rules); - 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)); + $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)); $manager->addRole('one'); $manager->addRole('two'); - self::assertSame(['one', 'two'], $manager->getRoles()); + $this->assertEquals(['one', 'two'], $manager->getRoles()); } public function testAddRoleException(): void @@ -87,15 +89,15 @@ public function testAssociation(): void $manager->addRole(static::ROLE); // test simple permission - self::assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION, AllowRule::class)); - self::assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION)); + $this->assertEquals($manager, $manager->associate(static::ROLE, static::PERMISSION, AllowRule::class)); + $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION)); // test pattern 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->associate(static::ROLE, static::PERMISSION . '.*', AllowRule::class)); + $this->assertEquals($allowRule, $manager->getRule(static::ROLE, static::PERMISSION . '.' . static::PERMISSION)); - self::assertEquals($manager, $manager->deassociate(static::ROLE, static::PERMISSION)); - self::assertEquals($forbidRule, $manager->getRule(static::ROLE, static::PERMISSION)); + $this->assertEquals($manager, $manager->deassociate(static::ROLE, static::PERMISSION)); + $this->assertEquals($forbidRule, $manager->getRule(static::ROLE, static::PERMISSION)); } public function testGetRuleRoleException(): void @@ -124,7 +126,7 @@ public function testRulesForRole(): void $manager->addRole('admin'); $manager->associate('admin', 'post.edit', AllowRule::class); - self::assertSame([ + $this->assertSame([ 'post.edit' => AllowRule::class ], $manager->getPermissions('admin')); } @@ -138,7 +140,10 @@ public function testGetFallbackRule(): void ->with(ForbidRule::class) ->willReturn(new ForbidRule()); - self::assertInstanceOf(ForbidRule::class, $manager->getRule(static::ROLE, static::PERMISSION)); + $this->assertInstanceOf( + ForbidRule::class, + $manager->getRule(static::ROLE, static::PERMISSION) + ); } public function testAssociateRoleException(): void diff --git a/tests/RuleManagerTest.php b/tests/RuleManagerTest.php index da1365f5..6dbf3687 100644 --- a/tests/RuleManagerTest.php +++ b/tests/RuleManagerTest.php @@ -27,7 +27,7 @@ class RuleManagerTest extends TestCase /** @var RuleInterface */ private $rule; - protected function setUp(): void + public function setUp(): void { $this->container = m::mock(ContainerInterface::class); $this->rule = m::mock(RuleInterface::class); @@ -44,18 +44,18 @@ public function testFlow(): void $manager = new RuleManager($this->container); - 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)); + $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)); // other rule types $manager->set('RuleInterface', $this->rule); - self::assertEquals($this->rule, $manager->get('RuleInterface')); - $manager->set('Closure', static fn(): bool => true); - self::assertInstanceOf(\Spiral\Security\Rule\CallableRule::class, $manager->get('Closure')); + $this->assertEquals($this->rule, $manager->get('RuleInterface')); + $manager->set('Closure', fn() => true); + $this->assertTrue($manager->get('Closure') instanceof CallableRule); $manager->set('Array', $this->testFlow(...)); - self::assertInstanceOf(\Spiral\Security\Rule\CallableRule::class, $manager->get('Array')); + $this->assertTrue($manager->get('Array') instanceof CallableRule); } public function testHasWithNotRegisteredClass(): void @@ -63,7 +63,7 @@ public function testHasWithNotRegisteredClass(): void $ruleClass = $this->rule::class; $manager = new RuleManager($this->container); - self::assertTrue($manager->has($ruleClass)); + $this->assertTrue($manager->has($ruleClass)); } public function testSetRuleException(): void diff --git a/tests/RuleTest.php b/tests/RuleTest.php index c2bc0d19..b3aabe43 100644 --- a/tests/RuleTest.php +++ b/tests/RuleTest.php @@ -5,7 +5,6 @@ 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; @@ -22,11 +21,20 @@ class RuleTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - private MockObject&ActorInterface $actor; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface + */ + private $actor; - private MockObject&ResolverInterface $resolver; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|ResolverInterface + */ + private $resolver; - private MockObject&Rule $rule; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|Rule + */ + private $rule; protected function setUp(): void { @@ -60,7 +68,7 @@ public function testAllows(string $permission, array $context, bool $allowed): v ->with($parameters) ->willReturn($allowed); - self::assertSame($allowed, $this->rule->allows($this->actor, $permission, $context)); + $this->assertEquals($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 00c046b3..bba98a02 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); - self::assertTrue($rule->allows($actor, static::OPERATION, static::CONTEXT)); + $this->assertTrue($rule->allows($actor, static::OPERATION, static::CONTEXT)); } } diff --git a/tests/Rules/CallableRuleTest.php b/tests/Rules/CallableRuleTest.php index 70d59d0a..1af470bb 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); - self::assertTrue($rule->allows($actor, static::OPERATION, $context)); - self::assertFalse($rule->allows($actor, static::OPERATION, $context)); + $this->assertTrue($rule->allows($actor, static::OPERATION, $context)); + $this->assertFalse($rule->allows($actor, static::OPERATION, $context)); } } diff --git a/tests/Rules/CompositeRuleTest.php b/tests/Rules/CompositeRuleTest.php index a714aa93..29eb8426 100644 --- a/tests/Rules/CompositeRuleTest.php +++ b/tests/Rules/CompositeRuleTest.php @@ -5,7 +5,6 @@ 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; @@ -19,9 +18,12 @@ class CompositeRuleTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - private MockObject&ActorInterface $actor; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface $callable + */ + private $actor; - protected function setUp(): void + public function setUp(): void { $this->actor = $this->createMock(ActorInterface::class); } @@ -33,7 +35,10 @@ public function testAllow(bool $expected, string $compositeRuleClass, array $rul /** @var RuleInterface $rule */ $rule = new $compositeRuleClass($repository); - self::assertEquals($expected, $rule->allows($this->actor, static::OPERATION, static::CONTEXT)); + $this->assertEquals( + $expected, + $rule->allows($this->actor, static::OPERATION, static::CONTEXT) + ); } public static function allowsProvider(): \Traversable @@ -48,10 +53,10 @@ public static function allowsProvider(): \Traversable yield [false, OneCompositeRule::class, [$forbidRule, $forbidRule, $forbidRule]]; } - + private function createRepository(array $rules): RulesInterface { - /** @var MockObject|RulesInterface $repository */ + /** @var \PHPUnit_Framework_MockObject_MockObject|RulesInterface $repository */ $repository = $this->createMock(RulesInterface::class); $repository->method('get') diff --git a/tests/Rules/ForbidRuleTest.php b/tests/Rules/ForbidRuleTest.php index 5d4a0638..a6465cc9 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); - self::assertFalse($rule->allows($actor, static::OPERATION, static::CONTEXT)); + $this->assertFalse($rule->allows($actor, static::OPERATION, static::CONTEXT)); } } diff --git a/tests/Traits/GuardedTraitTest.php b/tests/Traits/GuardedTraitTest.php index ec1c2f5a..f55f4689 100644 --- a/tests/Traits/GuardedTraitTest.php +++ b/tests/Traits/GuardedTraitTest.php @@ -4,7 +4,6 @@ namespace Spiral\Tests\Security\Traits; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use Spiral\Core\Container; @@ -20,17 +19,24 @@ class GuardedTraitTest extends TestCase public const OPERATION = 'test'; public const CONTEXT = []; - private object $trait; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|GuardedTrait + */ + private $trait; - private MockObject&GuardInterface $guard; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|GuardInterface + */ + private $guard; - private MockObject&ContainerInterface $container; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface + */ + private $container; - protected function setUp(): void + public function setUp(): void { - $this->trait = new class { - use GuardedTrait; - }; + $this->trait = $this->getMockForTrait(GuardedTrait::class); $this->guard = $this->createMock(GuardInterface::class); $this->container = $this->createMock(ContainerInterface::class); } @@ -38,10 +44,10 @@ protected function setUp(): void public function testGetGuardFromContainer(): void { $this->container->method('has')->willReturn(true); - $this->container->method('get')->willReturn($this->guard); + $this->container->method('get')->will($this->returnValue($this->guard)); ContainerScope::runScope($this->container, function (): void { - self::assertEquals($this->guard, $this->trait->getGuard()); + $this->assertEquals($this->guard, $this->trait->getGuard()); }); } @@ -52,7 +58,7 @@ public function testGuardScopeException(): void $this->container->method('has')->willReturn(false); ContainerScope::runScope($this->container, function (): void { - self::assertEquals($this->guard, $this->trait->getGuard()); + $this->assertEquals($this->guard, $this->trait->getGuard()); }); } @@ -60,14 +66,14 @@ public function testGuardScopeException2(): void { $this->expectException(ScopeException::class); - self::assertEquals($this->guard, $this->trait->getGuard()); + $this->assertEquals($this->guard, $this->trait->getGuard()); } public function testAllows(): void { $this->guard->method('allows') ->with(static::OPERATION, static::CONTEXT) - ->willReturn(true) + ->will($this->returnValue(true)) ; $guarded = new Guarded(); @@ -76,18 +82,18 @@ public function testAllows(): void $container->bind(GuardInterface::class, $this->guard); ContainerScope::runScope($container, function () use ($guarded): void { - self::assertTrue($guarded->allows(static::OPERATION, static::CONTEXT)); - self::assertFalse($guarded->denies(static::OPERATION, static::CONTEXT)); + $this->assertTrue($guarded->allows(static::OPERATION, static::CONTEXT)); + $this->assertFalse($guarded->denies(static::OPERATION, static::CONTEXT)); }); } public function testResolvePermission(): void { $guarded = new Guarded(); - self::assertSame(static::OPERATION, $guarded->resolvePermission(static::OPERATION)); + $this->assertEquals(static::OPERATION, $guarded->resolvePermission(static::OPERATION)); $guarded = new GuardedWithNamespace(); $resolvedPermission = GuardedWithNamespace::GUARD_NAMESPACE . '.' . static::OPERATION; - self::assertSame($resolvedPermission, $guarded->resolvePermission(static::OPERATION)); + $this->assertEquals($resolvedPermission, $guarded->resolvePermission(static::OPERATION)); } }