Skip to content

Commit

Permalink
prepare release
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 22, 2025
1 parent c53d390 commit 9485a3c
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 77 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
],
"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": {
"Spiral\\Security\\": "src"
}
},
"require-dev": {
"spiral/console": "^3.15",
"spiral/console": "^3.14.10",
"phpunit/phpunit": "^10.1",
"mockery/mockery": "^1.5",
"vimeo/psalm": "^5.9"
Expand Down
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);

self::assertEquals($roles, $actor->getRoles());
$this->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();

self::assertEquals([Guest::ROLE], $actor->getRoles());
$this->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();

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

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

public function testSetRuleException(): void
Expand Down
18 changes: 13 additions & 5 deletions tests/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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
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);

self::assertTrue($rule->allows($actor, static::OPERATION, static::CONTEXT));
$this->assertTrue($rule->allows($actor, static::OPERATION, static::CONTEXT));
}
}
6 changes: 3 additions & 3 deletions tests/Rules/CallableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
}
}
17 changes: 11 additions & 6 deletions tests/Rules/CompositeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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')
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);

self::assertFalse($rule->allows($actor, static::OPERATION, static::CONTEXT));
$this->assertFalse($rule->allows($actor, static::OPERATION, static::CONTEXT));
}
}
Loading

0 comments on commit 9485a3c

Please sign in to comment.