Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x Added setter/getter for assertions in AuthorizationService. Support for phpunit v11 #73

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"require-dev": {
"malukenho/docheader": "^1.0.0",
"phpunit/phpunit": "^10.0",
"phpunit/phpunit": "^10.0 || ^11.0",
"phpspec/prophecy": "^1.10",
"phpspec/prophecy-phpunit": "^2.0",
"friendsofphp/php-cs-fixer": "^3.43",
Expand Down
11 changes: 4 additions & 7 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
"extends": [
"config:recommended"
],
"baseBranches": ["master", "2.x"],
"packageRules": [
{
"matchPackageNames": ["phpunit/phpunit"],
"allowedVersions": "<11.0"
}
]
"major": {
"dependencyDashboardApproval": true
},
"baseBranches": ["master", "2.x"]
}
54 changes: 54 additions & 0 deletions src/Service/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Lmc\Rbac\Service;

use Lmc\Rbac\Assertion\AssertionInterface;
use Lmc\Rbac\Assertion\AssertionPluginManagerInterface;
use Lmc\Rbac\Assertion\AssertionSet;
use Lmc\Rbac\Identity\IdentityInterface;
Expand Down Expand Up @@ -59,6 +60,59 @@ public function __construct(
$this->assertions = $assertions;
}

/**
* Set assertions, either merging or replacing (default)
* @param array $assertions
* @param bool $merge
* @return void
*/
public function setAssertions(array $assertions, bool $merge = false): void
{
$this->assertions = $merge ?
array_merge($this->assertions, $assertions) :
$assertions;
}

/**
* Set assertion for a given permission
* @param PermissionInterface|string $permission
* @param AssertionInterface|callable|string $assertion
* @return void
*/
public function setAssertion(PermissionInterface|string $permission, AssertionInterface|callable|string $assertion): void
{
$this->assertions[(string) $permission] = $assertion;
}

/**
* Check if there are assertions for the permission
* @param PermissionInterface|string $permission
* @return bool
*/
public function hasAssertion(PermissionInterface|string $permission): bool
{
return isset($this->assertions[(string) $permission]);
}

/**
* Get the assertions
* @return array
*/
public function getAssertions(): array
{
return $this->assertions;
}

/**
* Get the assertions for the given permission
* @param PermissionInterface|string $permission
* @return AssertionInterface|callable|string|null
*/
public function getAssertion(PermissionInterface|string $permission): AssertionInterface|callable|string|null
{
return $this->hasAssertion(($permission)) ? $this->assertions[(string) $permission] : null;
}

public function isGranted(IdentityInterface|null $identity, string|PermissionInterface $permission, mixed $context = null): bool
{
$roles = $this->roleService->getIdentityRoles($identity, $context);
Expand Down
37 changes: 37 additions & 0 deletions src/Service/AuthorizationServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace Lmc\Rbac\Service;

use Lmc\Rbac\Assertion\AssertionInterface;
use Lmc\Rbac\Identity\IdentityInterface;
use Lmc\Rbac\Permission\PermissionInterface;

Expand All @@ -41,4 +42,40 @@ interface AuthorizationServiceInterface
* @return bool
*/
public function isGranted(?IdentityInterface $identity, PermissionInterface|string $permission, mixed $context = null): bool;

/**
* Set assertions, either merging or replacing (default)
* @param array $assertions
* @param bool $merge
* @return void
*/
public function setAssertions(array $assertions, bool $merge = false): void;

/**
* Set assertion for a given permission
* @param PermissionInterface|string $permission
* @param AssertionInterface|callable|string $assertion
* @return void
*/
public function setAssertion(PermissionInterface|string $permission, AssertionInterface|callable|string $assertion): void;

/**
* Check if there are assertions for the permission
* @param PermissionInterface|string $permission
* @return bool
*/
public function hasAssertion(PermissionInterface|string $permission): bool;

/**
* Get the assertions
* @return array
*/
public function getAssertions(): array;

/**
* Get the assertions for the given permission
* @param PermissionInterface|string $permission
* @return AssertionInterface|callable|string|null
*/
public function getAssertion(PermissionInterface|string $permission): AssertionInterface|callable|string|null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@

declare(strict_types=1);

namespace LmcRbacTest\Container;
namespace LmcRbacTest\Assertion;

use Laminas\ServiceManager\ServiceManager;
use Lmc\Rbac\Assertion\AssertionContainer;
use Lmc\Rbac\Assertion\AssertionContainerFactory;
use Lmc\Rbac\Assertion\AssertionPluginManager;
use Lmc\Rbac\Assertion\AssertionPluginManagerFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Assertion\AssertionPluginManagerFactory
*/
#[CoversClass('\Lmc\Rbac\Assertion\AssertionPluginManagerFactory')]
class AssertionPluginManagerFactoryTest extends TestCase
{
public function testFactory(): void
Expand Down
5 changes: 2 additions & 3 deletions test/Assertion/AssertionPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
use Lmc\Rbac\Assertion\AssertionInterface;
use Lmc\Rbac\Assertion\AssertionPluginManager;
use LmcRbacTest\Asset\SimpleAssertion;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Assertion\AssertionPluginManager
*/
#[CoversClass('\Lmc\Rbac\Assertion\AssertionPluginManager')]
class AssertionPluginManagerTest extends TestCase
{
public function testValidationOfPluginSucceedsIfAssertionInterfaceIsImplemented()
Expand Down
10 changes: 4 additions & 6 deletions test/Assertion/AssertionSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
use Lmc\Rbac\Exception\InvalidArgumentException;
use Lmc\Rbac\Identity\IdentityInterface;
use LmcRbacTest\Asset\SimpleAssertion;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Assertion\AssertionSet
*/
#[CoversClass('\Lmc\Rbac\Assertion\AssertionSet')]
class AssertionSetTest extends TestCase
{
public function testImplementsAssertionInterface()
Expand Down Expand Up @@ -222,9 +222,7 @@ public function testThrowExceptionForInvalidAssertion()
$this->assertTrue($assertionSet->assert('permission'));
}

/**
* @dataProvider dpMatrix
*/
#[DataProvider('dpMatrix')]
public function testMatrix(array $assertions, bool $expectedResult, array $assertionCalledCount)
{
$assertionContainer = $this->getMockBuilder(AssertionPluginManagerInterface::class)->getMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* and is licensed under the MIT license.
*/

namespace LmcRbacTest\Service;
namespace LmcRbacTest\Asset;

/**
* @author Eric Richer <[email protected]>
Expand Down
5 changes: 2 additions & 3 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
namespace LmcRbacTest;

use Lmc\Rbac\ConfigProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\ConfigProvider
*/
#[CoversClass('Lmc\Rbac\ConfigProvider')]
class ConfigProviderTest extends TestCase
{
public function testProvidesExpectedConfiguration()
Expand Down
5 changes: 2 additions & 3 deletions test/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

use Lmc\Rbac\ConfigProvider;
use Lmc\Rbac\Module;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Module
*/
#[CoversClass('\Lmc\Rbac\Module')]
class ModuleTest extends TestCase
{
public function testProvidesExpectedConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@

declare(strict_types=1);

namespace LmcRbacTest\Container;
namespace LmcRbacTest\Options;

use Laminas\ServiceManager\ServiceManager;
use Lmc\Rbac\Options\ModuleOptionsFactory;
use Lmc\Rbac\Options\ModuleOptions;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Options\ModuleOptionsFactory
*/
#[CoversClass('\Lmc\Rbac\Options\ModuleOptionsFactory')]
class ModuleOptionsFactoryTest extends TestCase
{
public function testFactory(): void
Expand Down
5 changes: 2 additions & 3 deletions test/Options/ModuleOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
namespace LmcRbacTest\Options;

use Lmc\Rbac\Options\ModuleOptions;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Options\ModuleOptions
*/
#[CoversClass('\Lmc\Rbac\Options\ModuleOptions')]
class ModuleOptionsTest extends TestCase
{
public function testAssertModuleDefaultOptions(): void
Expand Down
27 changes: 2 additions & 25 deletions test/Rbac/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@

use Lmc\Rbac\Rbac;
use Lmc\Rbac\Role\Role;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Rbac
* @group Coverage
*/
#[CoversClass('\Lmc\Rbac\Rbac')]
class RbacTest extends TestCase
{
/**
* @covers \Lmc\Rbac\Rbac::isGranted
*/
public function testCanConvertSingleRole(): void
{
$role = new Role('Foo');
Expand All @@ -44,18 +39,12 @@ public function testCanConvertSingleRole(): void
$this->assertTrue($rbac->isGranted($role, 'permission'));
}

/**
* @covers \Lmc\Rbac\Rbac::isGranted
*/
public function testCanUseEmptyArray(): void
{
$rbac = new Rbac();
$this->assertFalse($rbac->isGranted([], 'permission'));
}

/**
* @covers \Lmc\Rbac\Rbac::isGranted
*/
public function testCanCheckMultipleRolesWithMatchingPermission(): void
{
$role1 = new Role('Foo');
Expand All @@ -69,9 +58,6 @@ public function testCanCheckMultipleRolesWithMatchingPermission(): void
$this->assertTrue($rbac->isGranted($roles, 'permission'));
}

/**
* @covers \Lmc\Rbac\Rbac::isGranted
*/
public function testReturnFalseIfNoRoleHasPermission(): void
{
$role1 = new Role('Foo');
Expand All @@ -83,9 +69,6 @@ public function testReturnFalseIfNoRoleHasPermission(): void
$this->assertFalse($rbac->isGranted($roles, 'permission'));
}

/**
* @covers \Lmc\Rbac\Rbac::isGranted
*/
public function testCanCheckHierarchicalRole(): void
{
$childRole = new Role('Bar');
Expand All @@ -99,9 +82,6 @@ public function testCanCheckHierarchicalRole(): void
$this->assertTrue($rbac->isGranted($parentRole, 'permission'));
}

/**
* @covers \Lmc\Rbac\Rbac::isGranted
*/
public function testReturnFalseIfNoHierarchicalRoleHasPermission(): void
{
$childRole = new Role('Bar');
Expand All @@ -114,9 +94,6 @@ public function testReturnFalseIfNoHierarchicalRoleHasPermission(): void
$this->assertFalse($rbac->isGranted($parentRole, 'permission'));
}

/**
* @covers \Lmc\Rbac\Rbac::isGranted
*/
public function testCanCheckTraversableAsRolesList(): void
{
$role1 = new Role('Foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@

declare(strict_types=1);

namespace LmcRbacTest\Container;
namespace LmcRbacTest\Role;

use Laminas\ServiceManager\ServiceManager;
use Lmc\Rbac\Role\InMemoryRoleProviderFactory;
use Lmc\Rbac\Options\ModuleOptions;
use Lmc\Rbac\Role\InMemoryRoleProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Role\InMemoryRoleProviderFactory
*/
#[CoversClass('\Lmc\Rbac\Role\InMemoryRoleProviderFactory')]
class InMemoryRoleProviderFactoryTest extends TestCase
{
public function testFactoryUsingObjectRepository(): void
Expand Down
5 changes: 2 additions & 3 deletions test/Role/InMemoryRoleProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

use Lmc\Rbac\Role\InMemoryRoleProvider;
use Lmc\Rbac\Role\RoleInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Rbac\Role\InMemoryRoleProvider
*/
#[CoversClass('\Lmc\Rbac\Role\InMemoryRoleProvider')]
class InMemoryRoleProviderTest extends TestCase
{
public function testInMemoryProvider(): void
Expand Down
Loading