From ca03e9963d7a9d4e9eba0f188110e2c4fd581099 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Mon, 15 Jul 2024 02:51:46 +0200 Subject: [PATCH] [Tests] Aligned PermissionTest with Base changes --- .../Service/Mock/PermissionTest.php | 71 +++++++------------ 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/tests/lib/Repository/Service/Mock/PermissionTest.php b/tests/lib/Repository/Service/Mock/PermissionTest.php index 4f78cd918d..c0ba4920ab 100644 --- a/tests/lib/Repository/Service/Mock/PermissionTest.php +++ b/tests/lib/Repository/Service/Mock/PermissionTest.php @@ -11,6 +11,7 @@ use Ibexa\Contracts\Core\Persistence\User\Policy; use Ibexa\Contracts\Core\Persistence\User\Role; use Ibexa\Contracts\Core\Persistence\User\RoleAssignment; +use Ibexa\Contracts\Core\Repository\PermissionResolver as APIPermissionResolver; use Ibexa\Contracts\Core\Repository\Repository; use Ibexa\Contracts\Core\Repository\Values\User\Limitation; use Ibexa\Contracts\Core\Repository\Values\ValueObject; @@ -18,9 +19,9 @@ use Ibexa\Core\Base\Exceptions\InvalidArgumentValue; use Ibexa\Core\Base\Exceptions\NotFound\LimitationNotFoundException; use Ibexa\Core\Repository\Permission\PermissionResolver; -use Ibexa\Core\Repository\Repository as CoreRepository; use Ibexa\Core\Repository\Values\User\UserReference; use Ibexa\Tests\Core\Repository\Service\Mock\Base as BaseServiceMockTest; +use PHPUnit\Framework\MockObject\MockObject; /** * Mock test case for PermissionResolver. @@ -107,20 +108,20 @@ public function testHasAccessReturnsTrue(array $roles, array $roleAssignments) { /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); - $mockedService = $this->getPermissionResolverMock(null); + $mockedService = $this->getPermissionResolverWithMockedMethods([]); $userHandlerMock ->expects(self::once()) ->method('loadRoleAssignmentsByGroupId') ->with(self::equalTo(10), self::equalTo(true)) - ->will(self::returnValue($roleAssignments)); + ->willReturn($roleAssignments); foreach ($roleAssignments as $at => $roleAssignment) { $userHandlerMock ->expects(self::at($at + 1)) ->method('loadRole') ->with($roleAssignment->roleId) - ->will(self::returnValue($roles[$roleAssignment->roleId])); + ->willReturn($roles[$roleAssignment->roleId]); } $result = $mockedService->hasAccess('dummy-module', 'dummy-function'); @@ -178,7 +179,7 @@ public function testHasAccessReturnsFalse(array $roles, array $roleAssignments) { /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); - $service = $this->getPermissionResolverMock(null); + $service = $this->getPermissionResolverWithMockedMethods([]); $userHandlerMock ->expects(self::once()) @@ -206,7 +207,7 @@ public function testHasAccessReturnsFalseButSudoSoTrue() { /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); - $service = $this->getPermissionResolverMock(null); + $service = $this->getPermissionResolverWithMockedMethods([]); $repositoryMock = $this->getRepositoryMock(); $repositoryMock ->expects(self::any()) @@ -291,7 +292,7 @@ public function testHasAccessReturnsPermissionSets(array $roles, array $roleAssi /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); $roleDomainMapper = $this->getRoleDomainMapperMock(['buildDomainPolicyObject']); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); $permissionResolverMock ->expects(self::once()) @@ -407,7 +408,7 @@ public function testHasAccessReturnsLimitationNotFoundException(array $roles, ar /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); $roleDomainMapper = $this->getRoleDomainMapperMock(); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); $permissionResolverMock ->expects(self::once()) @@ -519,7 +520,7 @@ public function testHasAccessReturnsInvalidArgumentValueException(array $roles, { $this->expectException(InvalidArgumentValue::class); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); /** @var $role \Ibexa\Contracts\Core\Persistence\User\Role */ foreach ($roles as $role) { @@ -587,7 +588,7 @@ public function testHasAccessReturnsPermissionSetsWithRoleLimitation(array $role $limitationTypeMock = $this->createMock(Type::class); $limitationService = $this->getLimitationServiceMock(); $roleDomainMapper = $this->getRoleDomainMapperMock(); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); $permissionResolverMock ->expects(self::once()) @@ -690,13 +691,13 @@ public function providerForTestCanUserSimple() */ public function testCanUserSimple($permissionSets, $result) { - $permissionResolverMock = $this->getPermissionResolverMock(['hasAccess']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['hasAccess']); $permissionResolverMock ->expects(self::once()) ->method('hasAccess') ->with(self::equalTo('test-module'), self::equalTo('test-function')) - ->will(self::returnValue($permissionSets)); + ->willReturn($permissionSets); /** @var $valueObject \Ibexa\Contracts\Core\Repository\Values\ValueObject */ $valueObject = $this->getMockForAbstractClass(ValueObject::class); @@ -714,7 +715,7 @@ public function testCanUserSimple($permissionSets, $result) */ public function testCanUserWithoutLimitations() { - $permissionResolverMock = $this->getPermissionResolverMock( + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods( [ 'hasAccess', 'getCurrentUserReference', @@ -891,7 +892,7 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli /** @var $valueObject \Ibexa\Contracts\Core\Repository\Values\ValueObject */ $valueObject = $this->createMock(ValueObject::class); $limitationServiceMock = $this->getLimitationServiceMock(); - $permissionResolverMock = $this->getPermissionResolverMock( + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods( [ 'hasAccess', 'getCurrentUserReference', @@ -903,7 +904,7 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli ->expects(self::once()) ->method('hasAccess') ->with(self::equalTo('test-module'), self::equalTo('test-function')) - ->will(self::returnValue($permissionSets)); + ->willReturn($permissionSets); $userRef = new UserReference(14); $permissionResolverMock @@ -974,9 +975,9 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli /** * Test for the setCurrentUserReference() and getCurrentUserReference() methods. */ - public function testSetAndGetCurrentUserReference() + public function testSetAndGetCurrentUserReference(): void { - $permissionResolverMock = $this->getPermissionResolverMock(null); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods([]); $userReferenceMock = $this->getUserReferenceMock(); $userReferenceMock @@ -997,19 +998,19 @@ public function testSetAndGetCurrentUserReference() */ public function testGetCurrentUserReferenceReturnsAnonymousUser() { - $permissionResolverMock = $this->getPermissionResolverMock(null); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods([]); self::assertEquals(new UserReference(10), $permissionResolverMock->getCurrentUserReference()); } - protected $permissionResolverMock; + protected MockObject & APIPermissionResolver $permissionResolverMock; /** - * @return \Ibexa\Contracts\Core\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject + * @param string[] $methods */ - protected function getPermissionResolverMock($methods = []) + protected function getPermissionResolverWithMockedMethods(array $methods): MockObject & APIPermissionResolver { - if ($this->permissionResolverMock === null) { + if (!isset($this->permissionResolverMock)) { $configResolverMock = $this->createMock(ConfigResolverInterface::class); $configResolverMock ->method('getParameter') @@ -1018,7 +1019,7 @@ protected function getPermissionResolverMock($methods = []) $this->permissionResolverMock = $this ->getMockBuilder(PermissionResolver::class) - ->setMethods($methods) + ->onlyMethods($methods) ->setConstructorArgs( [ $this->getRoleDomainMapperMock(), @@ -1051,32 +1052,14 @@ protected function getPermissionResolverMock($methods = []) return $this->permissionResolverMock; } - protected $userReferenceMock; + protected MockObject & UserReference $userReferenceMock; - protected function getUserReferenceMock() + protected function getUserReferenceMock(): MockObject & UserReference { - if ($this->userReferenceMock === null) { + if (!isset($this->userReferenceMock)) { $this->userReferenceMock = $this->createMock(UserReference::class); } return $this->userReferenceMock; } - - protected $repositoryMock; - - /** - * @return \Ibexa\Contracts\Core\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getRepositoryMock(): Repository - { - if ($this->repositoryMock === null) { - $this->repositoryMock = $this - ->getMockBuilder(CoreRepository::class) - ->onlyMethods(['getPermissionResolver']) - ->disableOriginalConstructor() - ->getMock(); - } - - return $this->repositoryMock; - } }