From d1653a074bb3595bed9e828f16b8f90325e3272f Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 1 Jul 2024 12:21:25 +0200 Subject: [PATCH] Implement test on AbstractStoredObjectVoter To avoid having to duplicate tests, a test is written\ for the abstract voter. --- ....php => AbstractStoredObjectVoterTest.php} | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) rename src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/{AccompanyingCourseStoredObjectVoterTest.php => AbstractStoredObjectVoterTest.php} (73%) diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AccompanyingCourseStoredObjectVoterTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AbstractStoredObjectVoterTest.php similarity index 73% rename from src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AccompanyingCourseStoredObjectVoterTest.php rename to src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AbstractStoredObjectVoterTest.php index f7ad25987..d5ab471ab 100644 --- a/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AccompanyingCourseStoredObjectVoterTest.php +++ b/src/Bundle/ChillDocStoreBundle/Tests/Security/Authorization/AbstractStoredObjectVoterTest.php @@ -5,15 +5,17 @@ namespace Chill\DocStoreBundle\Tests\Security\Authorization; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\AccompanyingCourseDocumentRepository; +use Chill\DocStoreBundle\Repository\AssociatedEntityToStoredObjectInterface; use Chill\DocStoreBundle\Security\Authorization\AccompanyingCourseDocumentVoter; use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum; +use Chill\DocStoreBundle\Security\Authorization\StoredObjectVoters\AbstractStoredObjectVoter; use Chill\DocStoreBundle\Service\WorkflowDocumentService; use Chill\MainBundle\Entity\User; use ChillDocStoreBundle\Security\Authorization\StoredObjectVoters\AccompanyingCourseStoredObjectVoter; use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; -class AccompanyingCourseStoredObjectVoterTest extends PHPUnit\Framework\TestCase +class AbstractStoredObjectVoterTest extends PHPUnit\Framework\TestCase { private $repository; private $security; @@ -26,11 +28,28 @@ class AccompanyingCourseStoredObjectVoterTest extends PHPUnit\Framework\TestCase $this->security = $this->createMock(Security::class); $this->workflowDocumentService = $this->createMock(WorkflowDocumentService::class); - $this->voter = new AccompanyingCourseStoredObjectVoter( - $this->repository, - $this->security, - $this->workflowDocumentService - ); + // Anonymous class extending the abstract class + $this->voter = new class($this->repository, $this->security, $this->workflowDocumentService) extends AbstractStoredObjectVoter { + protected function attributeToRole($attribute): string + { + return AccompanyingCourseDocumentVoter::SEE_DETAILS; + } + + protected function getRepository(): AssociatedEntityToStoredObjectInterface + { + // TODO: Implement getRepository() method. + } + + protected function getClass(): string + { + // TODO: Implement getClass() method. + } + + protected function canBeAssociatedWithWorkflow(): bool + { + // TODO: Implement canBeAssociatedWithWorkflow() method. + } + }; } private function setupMockObjects(): array @@ -94,7 +113,7 @@ class AccompanyingCourseStoredObjectVoterTest extends PHPUnit\Framework\TestCase list($user, $token, $subject, $entity) = $this->setupMockObjects(); // Setup mocks for voteOnAttribute method - $this->setupMocksForVoteOnAttribute($user, $token, $subject, $entity, false); + $this->setupMocksForVoteOnAttribute($user, $token, true, $entity, false); // Test voteOnAttribute method $attribute = StoredObjectRoleEnum::SEE; @@ -103,4 +122,16 @@ class AccompanyingCourseStoredObjectVoterTest extends PHPUnit\Framework\TestCase // Assert that access is denied when workflow is not allowed $this->assertFalse($result); } + + public function testAbstractStoredObjectVoter(): void + { + $voter = new class extends AbstractStoredObjectVoter { + // Implement abstract methods here + public function someMethod() { + // method implementation + } + }; + // Run tests on $voter + } + }