diff --git a/.env b/.env
new file mode 100644
index 0000000..b08f450
--- /dev/null
+++ b/.env
@@ -0,0 +1 @@
+DATABASE_URL=mysql://bang:password@rds-bang:3306/test
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 9bc30cc..53b0cae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,3 +8,4 @@ composer.lock
/behat.yml
/phpspec.yml
/.phpunit.result.cache
+/var
diff --git a/composer.json b/composer.json
index efaaba5..7cf6b98 100644
--- a/composer.json
+++ b/composer.json
@@ -14,21 +14,25 @@
],
"require": {
"php": "^8.1",
- "symfony/framework-bundle": "~6.0|~7.0",
+ "symfony/framework-bundle": "^7.0",
"webmozart/assert": "^1.10",
- "doctrine/annotations": "^2.0",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^3.2",
- "symfony/security-bundle": "~6.0|~7.0",
+ "symfony/security-bundle": "^7.0",
"symfony/orm-pack": "^2.4",
"symfony/twig-pack": "^1.0",
- "symfony/dotenv": "~6.0|~7.0"
+ "symfony/dotenv": "^7.0"
},
"autoload": {
"psr-4": { "PhpRbacBundle\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Tests\\PhpRbacBundle\\": "tests/" }
+ },
+ "require-dev": {
+ "phpunit/phpunit": "^11.4",
+ "zenstruck/foundry": "^2.1",
+ "symfony/yaml": "^7.0"
}
}
diff --git a/phpunit.xml b/phpunit.xml
index b7d61ce..ecd4dbb 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -6,16 +6,13 @@
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
- convertDeprecationsToExceptions="false"
>
-
-
-
-
+
+
@@ -23,28 +20,4 @@
tests
-
-
-
- src
-
-
-
-
-
-
-
diff --git a/src/Command/RbacAddPermissionCommand.php b/src/Command/RbacAddPermissionCommand.php
index 59bcfe4..4becf95 100755
--- a/src/Command/RbacAddPermissionCommand.php
+++ b/src/Command/RbacAddPermissionCommand.php
@@ -49,6 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
$permissions = [];
+
foreach ($permissionsTmp as $permission) {
$pathNodes = $this->permissionRepository->getPath($permission->getId());
$path = '/'.implode('/', $pathNodes);
@@ -56,6 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$path = str_replace('//', '/', $path);
$permissions[$path] = $permission;
}
+
ksort($permissions);
$question = new Question('Enter the code of the permission : ');
@@ -64,7 +66,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$description = $helper->ask($input, $output, $question);
$question = new ChoiceQuestion('Enter the parent of the permission : ', array_keys($permissions), 0);
$parentPath = $helper->ask($input, $output, $question);
- $permission = $this->permissionManager->add($code, $description, $permissions[$parentPath]->getId());
+
+ $this->permissionManager->add($code, $description, $permissions[$parentPath]->getId());
return Command::SUCCESS;
}
diff --git a/src/Command/RbacAddRoleCommand.php b/src/Command/RbacAddRoleCommand.php
index 78103d7..0f74b75 100755
--- a/src/Command/RbacAddRoleCommand.php
+++ b/src/Command/RbacAddRoleCommand.php
@@ -64,7 +64,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$description = $helper->ask($input, $output, $question);
$question = new ChoiceQuestion('Enter the parent of the role : ', array_keys($roles), 0);
$parentPath = $helper->ask($input, $output, $question);
- $role = $this->roleManager->add($code, $description, $roles[$parentPath]->getId());
+
+ $this->roleManager->add($code, $description, $roles[$parentPath]->getId());
return Command::SUCCESS;
}
diff --git a/src/Core/Manager/NodeManager.php b/src/Core/Manager/NodeManager.php
index 9a689d8..1132058 100644
--- a/src/Core/Manager/NodeManager.php
+++ b/src/Core/Manager/NodeManager.php
@@ -6,7 +6,11 @@
use PhpRbacBundle\Exception\RbacException;
use PhpRbacBundle\Repository\RoleRepository;
use PhpRbacBundle\Repository\PermissionRepository;
+use Symfony\Component\TypeInfo\Type;
+/**
+ * @template T of NodeInterface
+ */
abstract class NodeManager implements NodeManagerInterface
{
public function __construct(protected RoleRepository|PermissionRepository $repository)
@@ -29,6 +33,9 @@ public function add(string $code, string $description, int $parentId = self::ROO
}
}
+ /**
+ * @return NodeInterface
+ */
public function addPath(string $path, array $descriptions): NodeInterface
{
if ($path[0] !== '/') {
@@ -64,17 +71,24 @@ public function addPath(string $path, array $descriptions): NodeInterface
public function getPathId(string $path): int
{
- if (substr($path, -1) == "/") {
+ if (str_ends_with($path, '/')) {
$path = substr($path, 0, strlen($path) - 1);
}
+
return $this->repository->getPathId($path);
}
+ /**
+ * @return T
+ */
public function getNode(int $nodeId): NodeInterface
{
return $this->repository->getById($nodeId);
}
+ /**
+ * @return T
+ */
public function updateNode(int $nodeId, string $code, string $description): NodeInterface
{
$node = $this->getNode($nodeId);
@@ -94,8 +108,8 @@ public function getPath(int $nodeId): string
return "/";
}
foreach ($nodes as $node) {
- if ($node->getId() != self::ROOT_ID) {
- $path .= "/" . $node->getCode();
+ if ($node->getId() !== self::ROOT_ID) {
+ $path .= '/'.$node->getCode();
}
}
@@ -107,6 +121,9 @@ public function getChildren(int $nodeId): array
return $this->repository->getChildren($nodeId);
}
+ /**
+ * @return list
+ */
public function getParents(int $nodeId): array
{
return $this->repository->getPath($nodeId);
@@ -117,6 +134,9 @@ public function getDepth(int $nodeId): int
return count($this->repository->getPath($nodeId));
}
+ /**
+ * @return T
+ */
public function getParent(int $nodeId): NodeInterface
{
$nodes = $this->repository->getPath($nodeId);
@@ -127,8 +147,8 @@ public function getParent(int $nodeId): NodeInterface
return $nodes[count($nodes) - 2];
}
- public function reset()
+ public function reset(): void
{
- return $this->repository->reset();
+ $this->repository->reset();
}
}
diff --git a/src/Core/Manager/PermissionManager.php b/src/Core/Manager/PermissionManager.php
index 581822d..79f58c9 100644
--- a/src/Core/Manager/PermissionManager.php
+++ b/src/Core/Manager/PermissionManager.php
@@ -7,6 +7,8 @@
/**
* @property PermissionRepository $repository
+ *
+ * @extends NodeManager
*/
class PermissionManager extends NodeManager implements PermissionManagerInterface
{
diff --git a/src/Core/Manager/PermissionManagerInterface.php b/src/Core/Manager/PermissionManagerInterface.php
index dc0123b..197f687 100644
--- a/src/Core/Manager/PermissionManagerInterface.php
+++ b/src/Core/Manager/PermissionManagerInterface.php
@@ -10,20 +10,14 @@ interface PermissionManagerInterface extends NodeManagerInterface
/**
* Remove permission and attach all the sub-permission to the parent
*
- * @param PermissionInterface $permission
- *
* @throws RbacPermissionNotFoundException
- * @return boolean
*/
public function remove(PermissionInterface $permission): bool;
/**
* Remove Permission and all sub-permissions from system
*
- * @param PermissionInterface $permission
- *
* @throws RbacPermissionNotFoundException
- * @return boolean
*/
public function removeRecursively(PermissionInterface $permission): bool;
diff --git a/src/Core/Manager/RoleManager.php b/src/Core/Manager/RoleManager.php
index 8636f4a..4f43fc5 100644
--- a/src/Core/Manager/RoleManager.php
+++ b/src/Core/Manager/RoleManager.php
@@ -7,6 +7,8 @@
/**
* @property RoleRepository $repository
+ *
+ * @extends NodeManager
*/
class RoleManager extends NodeManager implements RoleManagerInterface
{
diff --git a/src/Core/RbacVoter.php b/src/Core/RbacVoter.php
index 666aa38..50430d4 100644
--- a/src/Core/RbacVoter.php
+++ b/src/Core/RbacVoter.php
@@ -24,7 +24,7 @@ protected function voteOnAttribute(string $attribute, mixed $subject, TokenInter
}
try {
- return $this->rbacManager->hasPermission($attribute, $user->getId());
+ return $this->rbacManager->hasPermission($attribute, $user->getUserIdentifier());
} catch (Exception) {
return false;
}
diff --git a/src/EventSubscriber/AccessControlDriver.php b/src/EventSubscriber/AccessControlDriver.php
index 1394246..8baccea 100755
--- a/src/EventSubscriber/AccessControlDriver.php
+++ b/src/EventSubscriber/AccessControlDriver.php
@@ -30,14 +30,14 @@ public function __construct(
]
];
- public function load(array $config)
+ public function load(array $config): void
{
$this->config = $config;
}
- private function checkAttributes(array $attributes, string $controller, string $method = "")
+ private function checkAttributes(array $attributes, string $controller, string $method = ""): void
{
- if (empty($attributes)) {
+ if ($attributes === []) {
return;
}
@@ -59,12 +59,14 @@ private function checkAttributes(array $attributes, string $controller, string $
}
- public function onKernelController(ControllerEvent $event)
+ public function onKernelController(ControllerEvent $event): void
{
$controllers = $event->getController();
+
if (!is_array($controllers)) {
return;
}
+
$controller = $controllers[0];
$method = $controllers[1];
diff --git a/src/PhpRbacBundle.php b/src/PhpRbacBundle.php
index ba7b3ac..f472fba 100644
--- a/src/PhpRbacBundle.php
+++ b/src/PhpRbacBundle.php
@@ -12,6 +12,7 @@ final class PhpRbacBundle extends Bundle
public function build(ContainerBuilder $container): void
{
parent::build($container);
+
$container->addCompilerPass(new DoctrineResolveTargetEntityPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
}
}
diff --git a/src/Repository/NodeEntityTrait.php b/src/Repository/NodeEntityTrait.php
index ec54a93..35557b9 100644
--- a/src/Repository/NodeEntityTrait.php
+++ b/src/Repository/NodeEntityTrait.php
@@ -2,6 +2,7 @@
namespace PhpRbacBundle\Repository;
+use Doctrine\DBAL\ParameterType;
use PhpRbacBundle\Entity\Node;
use PhpRbacBundle\Exception\RbacException;
use PhpRbacBundle\Core\Manager\NodeManagerInterface;
@@ -20,23 +21,23 @@ public function deleteNode(int $nodeId): bool
$dql = "DELETE {$this->getClassName()} node WHERE node.left = :left";
$query = $entityManager->createQuery($dql);
- $query->setParameter(":left", $info->getLeft());
+ $query->setParameter(":left", $info->getLeft(), ParameterType::INTEGER);
$query->execute();
$dql = "UPDATE {$this->getClassName()} node SET node.right = node.right - 1, node.left = node.left - 1 WHERE node.left BETWEEN :left AND :right";
$query = $entityManager->createQuery($dql);
- $query->setParameter(":left", $info->getLeft());
- $query->setParameter(":right", $info->getRight());
+ $query->setParameter(":left", $info->getLeft(), ParameterType::INTEGER);
+ $query->setParameter(":right", $info->getRight(), ParameterType::INTEGER);
$query->execute();
$dql = "UPDATE {$this->getClassName()} node SET node.right = node.right - 2 WHERE node.right > :right";
$query = $entityManager->createQuery($dql);
- $query->setParameter(":right", $info->getRight());
+ $query->setParameter(":right", $info->getRight(), ParameterType::INTEGER);
$query->execute();
$dql = "UPDATE {$this->getClassName()} node SET node.left = node.left - 2 WHERE node.left > :right";
$query = $entityManager->createQuery($dql);
- $query->setParameter(":right", $info->getRight());
+ $query->setParameter(":right", $info->getRight(), ParameterType::INTEGER);
$query->execute();
return true;
@@ -55,20 +56,20 @@ public function deleteSubtree(int $nodeId): bool
$dql = "DELETE {$this->getClassName()} node WHERE node.left BETWEEN :left AND :right";
$query = $entityManager->createQuery($dql);
- $query->setParameter(":left", $info->getLeft());
- $query->setParameter(":right", $info->getRight());
+ $query->setParameter(":left", $info->getLeft(), ParameterType::INTEGER);
+ $query->setParameter(":right", $info->getRight(), ParameterType::INTEGER);
$query->execute();
$dql = "UPDATE {$this->getClassName()} node SET node.right = node.right - :widht WHERE node.right > :right";
$query = $entityManager->createQuery($dql);
- $query->setParameter(":width", $width);
- $query->setParameter(":right", $info->getRight());
+ $query->setParameter(":width", $width, ParameterType::INTEGER);
+ $query->setParameter(":right", $info->getRight(), ParameterType::INTEGER);
$query->execute();
$dql = "UPDATE {$this->getClassName()} node SET node.left = node.left - :widht WHERE node.left > :right";
$query = $entityManager->createQuery($dql);
- $query->setParameter(":width", $width);
- $query->setParameter(":right", $info->getRight());
+ $query->setParameter(":width", $width, ParameterType::INTEGER);
+ $query->setParameter(":right", $info->getRight(), ParameterType::INTEGER);
$query->execute();
return true;
@@ -78,8 +79,8 @@ public function pathId(string $path, string $classException): mixed
{
$pathCmpl = "root" . strtolower($path);
- $tableName = $this->getClassMetadata()
- ->getTableName();
+ $tableName = $this->getClassMetadata()->getTableName();
+
$parts = explode("/", $pathCmpl);
$sql = "
SELECT
@@ -91,14 +92,14 @@ public function pathId(string $path, string $classException): mixed
{$tableName} as node ON node.tree_left BETWEEN parent.tree_left AND parent.tree_right
WHERE
node.code = :code
+ AND
+ path = :path
GROUP BY
node.id
- HAVING
- path = :path
";
- $pdo = $this->getEntityManager()
- ->getConnection();
+ $pdo = $this->getEntityManager()->getConnection();
+
$query = $pdo->prepare($sql);
$finalPart = end($parts);
$query->bindValue(":code", strtolower($finalPart));
@@ -110,6 +111,7 @@ public function pathId(string $path, string $classException): mixed
}
$row = $result->fetchAssociative();
+
return $row['id'];
}
@@ -172,6 +174,7 @@ private function getPathFunc(int $nodeId, string $rbacExceptionClass): array
$query = $this->getEntityManager()
->createQuery($dql);
+
$query->setParameter(':nodeId', $nodeId);
$result = $query->getResult();
diff --git a/src/Repository/PermissionRepository.php b/src/Repository/PermissionRepository.php
index 88bbaff..39949dc 100644
--- a/src/Repository/PermissionRepository.php
+++ b/src/Repository/PermissionRepository.php
@@ -2,6 +2,7 @@
namespace PhpRbacBundle\Repository;
+use Doctrine\DBAL\ParameterType;
use Doctrine\ORM\Exception\ORMException;
use PhpRbacBundle\Entity\Permission;
use PhpRbacBundle\Entity\RoleInterface;
@@ -38,12 +39,13 @@ public function __construct(ManagerRegistry $registry, string $entityClass)
->getTableName();
}
- public function initTable()
+ public function initTable(): void
{
$sql = "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE role_permission; TRUNCATE {$this->tableName}; SET FOREIGN_KEY_CHECKS = 1";
$this->getEntityManager()
->getConnection()
->executeQuery($sql);
+
$sql = "INSERT INTO {$this->tableName} (id, code, description, tree_left, tree_right) VALUES (1, 'root', 'root', 0, 1)";
$this->getEntityManager()
->getConnection()
@@ -57,11 +59,10 @@ public function initTable()
*/
public function add(Permission $entity, bool $flush = true): void
{
- $this->getEntityManager()
- ->persist($entity);
+ $this->getEntityManager()->persist($entity);
+
if ($flush) {
- $this->getEntityManager()
- ->flush();
+ $this->getEntityManager()->flush();
}
}
@@ -71,11 +72,10 @@ public function add(Permission $entity, bool $flush = true): void
*/
public function remove(Permission $entity, bool $flush = true): void
{
- $this->getEntityManager()
- ->remove($entity);
+ $this->getEntityManager()->remove($entity);
+
if ($flush) {
- $this->getEntityManager()
- ->flush();
+ $this->getEntityManager()->flush();
}
}
@@ -87,6 +87,7 @@ public function getPathId(string $path): int
public function getById(int $nodeId): Permission
{
$node = $this->find($nodeId);
+
if (empty($node)) {
throw new RbacPermissionNotFoundException("Permission {$nodeId} not found");
}
@@ -127,20 +128,17 @@ public function getChildren(int $nodeId): array
WHERE
node.tree_left BETWEEN parent.tree_left AND parent.tree_right
AND node.tree_left BETWEEN sub_parent.tree_left AND sub_parent.tree_right
- AND sub_parent.id = sub_tree.id
+ AND sub_parent.id = sub_tree.id AND depth = 1
GROUP BY
node.id
- HAVING
- depth = 1
ORDER BY
node.tree_left
";
$rsm = new ResultSetMapping();
$rsm->addEntityResult($this->getClassName(), 'node');
- $query = $this->getEntityManager()
- ->createNativeQuery($sql, $rsm);
- $query->setParameter(':nodeId', $nodeId);
+ $query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
+ $query->setParameter(':nodeId', $nodeId, ParameterType::INTEGER);
$result = $query->getResult();
@@ -153,8 +151,7 @@ public function getChildren(int $nodeId): array
public function hasPermission(int $permissionId, mixed $userId): bool
{
- $pdo = $this->getEntityManager()
- ->getConnection();
+ $pdo = $this->getEntityManager()->getConnection();
$sql = "
SELECT
@@ -178,14 +175,15 @@ public function hasPermission(int $permissionId, mixed $userId): bool
";
$query = $pdo->prepare($sql);
$query->bindValue(":userId", $userId);
- $query->bindValue(":permissionId", $permissionId);
+ $query->bindValue(":permissionId", $permissionId, ParameterType::INTEGER);
$stmt = $query->executeQuery();
- if ($stmt->rowCount() == 0) {
+ if ($stmt->rowCount() === 0) {
return false;
}
$row = $stmt->fetchAssociative();
+
return $row['result'] >= 1;
}
diff --git a/src/Repository/RoleRepository.php b/src/Repository/RoleRepository.php
index e8ab358..7851e7a 100644
--- a/src/Repository/RoleRepository.php
+++ b/src/Repository/RoleRepository.php
@@ -2,6 +2,7 @@
namespace PhpRbacBundle\Repository;
+use Doctrine\DBAL\ParameterType;
use Doctrine\ORM\Exception\ORMException;
use PhpRbacBundle\Entity\Role;
use PhpRbacBundle\Entity\RoleInterface;
@@ -37,7 +38,7 @@ public function __construct(ManagerRegistry $registry, string $entityClass)
->getTableName();
}
- public function initTable()
+ public function initTable(): void
{
$sql = "SET FOREIGN_KEY_CHECKS = 0; TRUNCATE user_role; TRUNCATE role_permission; TRUNCATE {$this->tableName};SET FOREIGN_KEY_CHECKS = 1;";
$this->getEntityManager()
@@ -58,6 +59,7 @@ public function initTable()
public function add(Role $entity, bool $flush = true): void
{
$this->getEntityManager()->persist($entity);
+
if ($flush) {
$this->getEntityManager()->flush();
}
@@ -70,6 +72,7 @@ public function add(Role $entity, bool $flush = true): void
public function remove(Role $entity, bool $flush = true): void
{
$this->getEntityManager()->remove($entity);
+
if ($flush) {
$this->getEntityManager()->flush();
}
@@ -83,6 +86,7 @@ public function getPathId(string $path,): int
public function getById(int $nodeId): Role
{
$node = $this->find($nodeId);
+
if (empty($node)) {
throw new RbacRoleNotFoundException();
}
@@ -123,11 +127,9 @@ public function getChildren(int $nodeId): array
WHERE
node.tree_left BETWEEN parent.tree_left AND parent.tree_right
AND node.tree_left BETWEEN sub_parent.tree_left AND sub_parent.tree_right
- AND sub_parent.id = sub_tree.id
+ AND sub_parent.id = sub_tree.id AND depth = 1
GROUP BY
node.id
- HAVING
- depth = 1
ORDER BY
node.tree_left
";
@@ -136,7 +138,7 @@ public function getChildren(int $nodeId): array
$rsm->addEntityResult($this->getClassName(), 'node');
$query = $this->getEntityManager()
->createNativeQuery($sql, $rsm);
- $query->setParameter(':nodeId', $nodeId);
+ $query->setParameter(':nodeId', $nodeId, ParameterType::INTEGER);
$result = $query->getResult();
@@ -185,15 +187,16 @@ public function hasPermission(int $roleId, int $permissionId): bool
)
";
$query = $pdo->prepare($sql);
- $query->bindValue(":roleId", $roleId);
- $query->bindValue(":permissionId", $permissionId);
+ $query->bindValue(":roleId", $roleId, ParameterType::INTEGER);
+ $query->bindValue(":permissionId", $permissionId, ParameterType::INTEGER);
$stmt = $query->executeQuery();
- if ($stmt->rowCount() == 0) {
+ if ($stmt->rowCount() === 0) {
return false;
}
$row = $stmt->fetchAssociative();
+
return $row['result'] >= 1;
}
@@ -215,10 +218,11 @@ public function hasRole(int $roleId, mixed $userId): bool
user_role.user_id = :userId AND TR.ID = :roleId
";
$query = $pdo->prepare($sql);
- $query->bindValue(":roleId", $roleId);
+ $query->bindValue(":roleId", $roleId, ParameterType::INTEGER);
$query->bindValue(":userId", $userId);
$stmt = $query->executeQuery();
- if ($stmt->rowCount() == 0) {
+
+ if ($stmt->rowCount() === 0) {
return false;
}
diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php
new file mode 100644
index 0000000..b0dbb4b
--- /dev/null
+++ b/tests/AbstractTestCase.php
@@ -0,0 +1,11 @@
+ faker()->email(),
+ ];
+ }
+
+ public static function class(): string
+ {
+ return FixtureUser::class;
+ }
+}
\ No newline at end of file
diff --git a/tests/Fixture/Entity/FixturePermission.php b/tests/Fixture/Entity/FixturePermission.php
new file mode 100644
index 0000000..0396d9e
--- /dev/null
+++ b/tests/Fixture/Entity/FixturePermission.php
@@ -0,0 +1,14 @@
+ true])]
+ public ?int $userId = null;
+
+ #[ORM\Column(type: Types::STRING, length: 64)]
+ public string $email = '';
+}
\ No newline at end of file
diff --git a/tests/Fixture/TestKernel.php b/tests/Fixture/TestKernel.php
new file mode 100644
index 0000000..9010dc6
--- /dev/null
+++ b/tests/Fixture/TestKernel.php
@@ -0,0 +1,84 @@
+loadFromExtension('php_rbac', [
+ 'no_authentication_section' => [
+ 'default' => 'deny',
+ ],
+ 'resolve_target_entities' => [
+ 'user' => FixtureUser::class,
+ 'role' => UserRole::class,
+ 'permission' => Permission::class,
+ ],
+ ]);
+
+ $container->loadFromExtension('framework', [
+ 'http_method_override' => false,
+ 'secret' => 'S3CRET',
+ 'router' => ['utf8' => true],
+ 'test' => true,
+ ]);
+
+ $container->loadFromExtension('security', [
+ 'firewalls' => [
+ 'dev' => [
+ 'pattern' => '^/',
+ 'lazy' => true,
+ ],
+ ],
+ 'access_control' => [
+ ['path' => '/', 'role' => 'ROLE_USER'],
+ ],
+ ]);
+
+ $container->loadFromExtension('doctrine', [
+ 'dbal' => ['url' => '%env(resolve:DATABASE_URL)%', 'use_savepoints' => true],
+ 'orm' => [
+ 'auto_generate_proxy_classes' => true,
+ 'auto_mapping' => true,
+ 'mappings' => [
+ 'Entity' => [
+ 'is_bundle' => false,
+ 'type' => 'attribute',
+ 'dir' => '%kernel.project_dir%/Tests/Fixture/Entity',
+ 'prefix' => 'Tests\PhpRbacBundle\Fixture\Entity',
+ 'alias' => 'Entity',
+ ],
+ ],
+ 'controller_resolver' => ['auto_mapping' => true],
+ ],
+ ]);
+
+ $container->register('logger', NullLogger::class);
+ }
+}
\ No newline at end of file
diff --git a/tests/KernelHelper.php b/tests/KernelHelper.php
deleted file mode 100644
index b9dc236..0000000
--- a/tests/KernelHelper.php
+++ /dev/null
@@ -1,25 +0,0 @@
-boot();
- $this->container = self::$kernel->getContainer();
- }
-}
diff --git a/tests/Permission/TestPermission.php b/tests/Permission/TestPermission.php
index eef12cb..687a3bd 100644
--- a/tests/Permission/TestPermission.php
+++ b/tests/Permission/TestPermission.php
@@ -1,32 +1,29 @@
boot();
- $this->container = self::$kernel->getContainer();
+ parent::setUp();
- $this->container->get(PermissionRepository::class)->initTable();
- $this->container->get(RoleRepository::class)->initTable();
+ $this->getContainer()->get(PermissionRepository::class)->initTable();
+ $this->getContainer()->get(RoleRepository::class)->initTable();
}
public function testSearchPermission()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
+
try {
$permissionId = $manager->getPathId("/");
$this->assertEquals(PermissionManager::ROOT_ID, $permissionId);
@@ -38,7 +35,7 @@ public function testSearchPermission()
public function testAddPermission()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$permission = $manager->add("Notepad", "Notepad", PermissionManager::ROOT_ID);
@@ -60,7 +57,7 @@ public function testAddPermission()
public function testAddDoublePermission()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$permission1 = $manager->add("Notepad", "Notepad", PermissionManager::ROOT_ID);
$permission2 = $manager->add("Notepad", "Notepad", PermissionManager::ROOT_ID);
@@ -74,7 +71,7 @@ public function testAddDoublePermission()
public function testAddSubPermission()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$permission = $manager->add("notepad", "Notepad", PermissionManager::ROOT_ID);
$subPermission = $manager->add("todolist", "Todo list", $permission->getId());
@@ -90,7 +87,7 @@ public function testAddSubPermission()
public function testAddDoubleSubPermission()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$permission = $manager->add("notepad", "Notepad", PermissionManager::ROOT_ID);
$subPermission1 = $manager->add("todolist", "Todo list", $permission->getId());
@@ -101,7 +98,7 @@ public function testAddDoubleSubPermission()
public function testAddPath()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$permission = $manager->addPath("/notepad/todolist/read", ['notepad' => 'Notepad', 'todolist' => "Todo list", "read" => "Read Access"]);
$this->assertGreaterThan(0, $permission->getId());
@@ -119,7 +116,7 @@ public function testAddPath()
public function testPath()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$manager->addPath("/notepad/todolist/read", ['notepad' => 'Notepad', 'todolist' => "Todo list", "read" => "Read Access"]);
$id = $manager->getPathId("/notepad/todolist/read");
@@ -129,7 +126,7 @@ public function testPath()
public function testGetById()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$this->expectException(RbacPermissionNotFoundException::class);
$id = $manager->getNode(2);
}
@@ -137,14 +134,14 @@ public function testGetById()
public function testGetPathId()
{
/** @var PermissionManager $manager */
- $manager = $this->container->get(PermissionManager::class);
+ $manager = $this->getContainer()->get(PermissionManager::class);
$this->expectException(RbacPermissionNotFoundException::class);
$id = $manager->getPathId("/notepad/todolist/read");
}
public function tearDown(): void
{
- $this->container->get(PermissionRepository::class)->initTable();
- $this->container->get(RoleRepository::class)->initTable();
+ $this->getContainer()->get(PermissionRepository::class)->initTable();
+ $this->getContainer()->get(RoleRepository::class)->initTable();
}
}
diff --git a/tests/Role/TestRole.php b/tests/Role/TestRole.php
index c9df9c9..e141c62 100644
--- a/tests/Role/TestRole.php
+++ b/tests/Role/TestRole.php
@@ -1,6 +1,6 @@
boot();
- $this->container = self::$kernel->getContainer();
-
- $this->container->get(PermissionRepository::class)->initTable();
- $this->container->get(RoleRepository::class)->initTable();
+ $this->getContainer()->get(PermissionRepository::class)->initTable();
+ $this->getContainer()->get(RoleRepository::class)->initTable();
}
- public function testSearchRole()
+ public function testSearchRole(): void
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
+
try {
$roleId = $manager->getPathId("/");
$this->assertEquals(RoleManager::ROOT_ID, $roleId);
@@ -37,10 +32,10 @@ public function testSearchRole()
}
}
- public function testAddRole()
+ public function testAddRole(): void
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
$role = $manager->add("Editor", "Editor", RoleManager::ROOT_ID);
$this->assertGreaterThan(0, $role->getId());
@@ -58,10 +53,10 @@ public function testAddRole()
/**
* @depends testAddRole
*/
- public function testAddDoubleRole()
+ public function testAddDoubleRole(): void
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
$role1 = $manager->add("Editor", "Editor", RoleManager::ROOT_ID);
$role2 = $manager->add("Editor", "Editor", RoleManager::ROOT_ID);
@@ -69,13 +64,11 @@ public function testAddDoubleRole()
$this->assertSame($role1->getId(), $role2->getId());
}
- /**
- * @depends testAddRole
- */
- public function testAddSubRole()
+ #[Depends('testAddRole')]
+ public function testAddSubRole(): void
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
$role = $manager->add("Editor", "Editor", RoleManager::ROOT_ID);
$subRole = $manager->add("reviewer", "Reviewer", $role->getId());
@@ -85,13 +78,11 @@ public function testAddSubRole()
$this->assertLessThan($role->getRight(), $subRole->getRight(), "Error Right {$role->getRight()} {$subRole->getRight()}");
}
- /**
- * @depends testAddSubRole
- */
- public function testAddDoubleSubRole()
+ #[Depends('testAddSubRole')]
+ public function testAddDoubleSubRole(): void
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
$role = $manager->add("Editor", "Editor", RoleManager::ROOT_ID);
$subRole1 = $manager->add("reviewer", "Reviewer", $role->getId());
@@ -99,13 +90,11 @@ public function testAddDoubleSubRole()
$this->assertSame($subRole1->getId(), $subRole2->getId());
}
- /**
- * @depends testAddRole
- */
+ #[Depends('testAddRole')]
public function testAddPath()
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
$role = $manager->addPath("/editor/reviewer", ['editor' => 'Editor', 'reviewer' => "Reviewer"]);
$this->assertGreaterThan(0, $role->getId());
@@ -120,10 +109,10 @@ public function testAddPath()
$this->assertLessThan($role->getRight(), $subRole->getRight(), "Error Right {$role->getRight()} {$subRole->getRight()}");
}
- public function testPath()
+ public function testPath(): void
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
$manager->addPath("/editor/reviewer", ['editor' => 'Editor', 'reviewer' => "Reviewer"]);
$id = $manager->getPathId("/editor/reviewer");
@@ -133,7 +122,7 @@ public function testPath()
public function testGetById()
{
/** @var RoleManager $manager */
- $manager = $this->container->get(RoleManager::class);
+ $manager = $this->getContainer()->get(RoleManager::class);
$this->expectException(RbacRoleNotFoundException::class);
$id = $manager->getNode(2);
$this->expectException(RbacRoleNotFoundException::class);
@@ -151,13 +140,13 @@ public function testGetPathId()
public function testAssign()
{
/** @var PermissionManager $pManager */
- $pManager = $this->container->get(PermissionManager::class);
+ $pManager = $this->getContainer()->get(PermissionManager::class);
$pManager->addPath("/notepad/todolist/read", ['notepad' => 'Notepad', 'todolist' => "Todo list", "read" => "Read Access"]);
$pManager->addPath("/notepad/todolist/write", ['notepad' => 'Notepad', 'todolist' => "Todo list", "write" => "Write Access"]);
/** @var RoleManager $rManager */
- $rManager = $this->container->get(RoleManager::class);
+ $rManager = $this->getContainer()->get(RoleManager::class);
$rManager->addPath("/editor/reviewer", ['editor' => 'Editor', 'reviewer' => "Reviewer"]);
$editorId = $rManager->getPathId("/editor");
@@ -185,16 +174,16 @@ public function testAssign()
$this->assertFalse($rManager->hasPermission($reviewer->getId(), $perm2));
}
- public function testUnassign()
+ public function testUnassign(): void
{
/** @var PermissionManager $pManager */
- $pManager = $this->container->get(PermissionManager::class);
+ $pManager = $this->getContainer()->get(PermissionManager::class);
$pManager->addPath("/notepad/todolist/read", ['notepad' => 'Notepad', 'todolist' => "Todo list", "read" => "Read Access"]);
$pManager->addPath("/notepad/todolist/write", ['notepad' => 'Notepad', 'todolist' => "Todo list", "write" => "Write Access"]);
/** @var RoleManager $rManager */
- $rManager = $this->container->get(RoleManager::class);
+ $rManager = $this->getContainer()->get(RoleManager::class);
$rManager->addPath("/editor/reviewer", ['editor' => 'Editor', 'reviewer' => "Reviewer"]);
$editorId = $rManager->getPathId("/editor");
@@ -216,7 +205,7 @@ public function testUnassign()
public function tearDown(): void
{
- $this->container->get(PermissionRepository::class)->initTable();
- $this->container->get(RoleRepository::class)->initTable();
+ $this->getContainer()->get(PermissionRepository::class)->initTable();
+ $this->getContainer()->get(RoleRepository::class)->initTable();
}
}
diff --git a/tests/Role/TestUser.php b/tests/Role/TestUser.php
index 7322b94..0145f69 100644
--- a/tests/Role/TestUser.php
+++ b/tests/Role/TestUser.php
@@ -1,22 +1,18 @@
container->get('doctrine');
- /** @var UserRepository $uRepo */
- $uRepo = $doctrine->getManager()->getRepository(User::class);
+ $uRepo = $doctrine->getManager()->getRepository(User::class);
+
/** @var RoleManager $manager */
$manager = $this->container->get(RoleManager::class);
/** @var RbacInterface $rbac */
$rbac = $this->container->get(Rbac::class);
- $role = $manager->add("editor", "Editor", RoleManager::ROOT_ID);
+ $role = $manager->add("editor", "Editor");
$user = $uRepo->findOneBy(['email' => 'test@test.com']);
$user->addRbacRole($role);
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index f816037..1da4a37 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -2,16 +2,15 @@
use Symfony\Component\Dotenv\Dotenv;
-if (file_exists(__DIR__ . '/vendor/autoload.php')) {
- require __DIR__ . '/vendor/autoload.php';
-} elseif (file_exists(__DIR__ . '/../../autoload.php')) {
- require __DIR__ . '/../../autoload.php';
-} else {
- require __DIR__ . '/../../../autoload.php';
-}
+require dirname(__DIR__).'/vendor/autoload.php';
-if (file_exists(dirname(__DIR__) . '/config/bootstrap.php')) {
- require dirname(__DIR__) . '/config/bootstrap.php';
-} elseif (method_exists(Dotenv::class, 'bootEnv')) {
- (new Dotenv())->bootEnv(dirname(__DIR__) . '/../../../.env');
-}
+// Load cached env vars if the .env.local.php file exists
+// Run "composer dump-env prod" to create it (requires symfony/flex >=1.2)
+if (is_array($env = @include dirname(__DIR__).'/.env.local.php')) {
+ $_ENV += $env;
+} elseif (!class_exists(Dotenv::class)) {
+ throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
+} else {
+ // load all the .env files
+ (new Dotenv())->usePutenv(false)->loadEnv(dirname(__DIR__).'/.env');
+}
\ No newline at end of file