VoteEvent example

$token = $this->getMockBuilder(TokenInterface::class)->getMockForAbstractClass();

        $voter
            ->expects($this->once())
            ->method('vote')
            ->with($token, 'anysubject', ['attr1'])
            ->willReturn(VoterInterface::ACCESS_DENIED);

        $eventDispatcher
            ->expects($this->once())
            ->method('dispatch')
            ->with(new VoteEvent($voter, 'anysubject', ['attr1'], VoterInterface::ACCESS_DENIED), 'debug.security.authorization.vote');

        $sut = new TraceableVoter($voter$eventDispatcher);
        $result = $sut->vote($token, 'anysubject', ['attr1']);

        $this->assertSame(VoterInterface::ACCESS_DENIED, $result);
    }

    public function testSupportsAttributeOnCacheable()
    {
        $voter = $this->getMockBuilder(CacheableVoterInterface::class)->getMockForAbstractClass();
        $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMockForAbstractClass();

        
->getMockBuilder(TraceableAccessDecisionManager::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['addVoterVote'])
            ->getMock();

        $traceableAccessDecisionManager
            ->expects($this->once())
            ->method('addVoterVote')
            ->with($voter['myattr1', 'myattr2'], VoterInterface::ACCESS_GRANTED);

        $sut = new VoteListener($traceableAccessDecisionManager);
        $sut->onVoterVote(new VoteEvent($voter, 'mysubject', ['myattr1', 'myattr2'], VoterInterface::ACCESS_GRANTED));
    }
}
public function __construct(VoterInterface $voter, EventDispatcherInterface $eventDispatcher)
    {
        $this->voter = $voter;
        $this->eventDispatcher = $eventDispatcher;
    }

    public function vote(TokenInterface $token, mixed $subject, array $attributes): int
    {
        $result = $this->voter->vote($token$subject$attributes);

        $this->eventDispatcher->dispatch(new VoteEvent($this->voter, $subject$attributes$result), 'debug.security.authorization.vote');

        return $result;
    }

    public function getDecoratedVoter(): VoterInterface
    {
        return $this->voter;
    }

    public function supportsAttribute(string $attribute): bool
    {
        
Home | Imprint | This part of the site doesn't use cookies.