Implement test on AbstractStoredObjectVoter

To avoid having to duplicate tests, a test is written\
for the abstract voter.
This commit is contained in:
Julie Lenaerts 2024-07-01 12:21:25 +02:00
parent 254122d125
commit d1653a074b

View File

@ -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
}
}