<commit_before><?php

namespace League\Tactician\Bundle\Tests\Security\Voter;

use League\Tactician\Bundle\Security\Voter\HandleCommandVoter;
use League\Tactician\Bundle\Tests\Fake\FakeCommand;
use Mockery;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Unit test for the handle command voter
 *
 * @author <NAME>
 */
class HandleCommandVoterTest extends TestCase
{
    /**
     * The decision manager mock.
     */
    private $decisionManager;

    /**
     * Set up.
     */
    public function setUp()
    {
        $this->decisionManager = Mockery::mock(AccessDecisionManager::class);
    }

    /**
     * @dataProvider provideTestVoteData
     */
    public function testVote($attribute, $subject, $decision, $mapping, $expected)
    {
        $this->decisionManager->shouldReceive('decide')->andReturn($decision);
        $voter = new HandleCommandVoter($this->decisionManager, $mapping);
        $tokenMock = Mockery::mock(TokenInterface::class);

        $this->assertEquals($expected, $voter->vote($tokenMock, $subject, [$attribute]));
    }

    /**
     * Gets the testdata for the vote test.
     *
     * @return array
     */
    public function provideTestVoteData()
    {
        return [
            // Testcase: default access is false
            ['handle', new FakeCommand, true, [], VoterInterface::ACCESS_DENIED],
            // Testcase: abstain when not handling a command, but using the handle attribute
            ['handle', null, true, [], VoterInterface::ACCESS_ABSTAIN],
            // Testcase: abstain when not handling a command and not using the handle attribute
            ['create', null, true, [], VoterInterface::ACCESS_ABSTAIN],
            // Testcase: abstain when not handling a command
            ['create', new FakeCommand, true, [], VoterInterface::ACCESS_ABSTAIN],
            // Testcase: default is unrelated to decision manager
            ['handle', new FakeCommand, false, [], VoterInterface::ACCESS_DENIED],
            // Testcase: deny access if decision manager returns false
            ['handle', new FakeCommand, false, [FakeCommand::class => ['ROLE_USER']], VoterInterface::ACCESS_DENIED],
            // Testcase: grant access if decision manager returns true and the command is in the mapping
            ['handle', new FakeCommand, true, [FakeCommand::class => ['ROLE_USER']], VoterInterface::ACCESS_GRANTED],
            // Testcase: deny access if the command is not in the mapping (i.e. a default deny access case)
            ['handle', new FakeCommand, false, ['someOtherCommand' => ['ROLE_USER']], VoterInterface::ACCESS_DENIED],
        ];
    }
}
<commit_msg>Move testcase description to dataProvider array key<commit_after><?php

namespace League\Tactician\Bundle\Tests\Security\Voter;

use League\Tactician\Bundle\Security\Voter\HandleCommandVoter;
use League\Tactician\Bundle\Tests\Fake\FakeCommand;
use Mockery;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Unit test for the handle command voter
 *
 * @author <NAME>
 */
class HandleCommandVoterTest extends TestCase
{
    /**
     * The decision manager mock.
     */
    private $decisionManager;

    /**
     * Set up.
     */
    public function setUp()
    {
        $this->decisionManager = Mockery::mock(AccessDecisionManager::class);
    }

    /**
     * @dataProvider provideTestVoteData
     */
    public function testVote($attribute, $subject, $decision, $mapping, $expected)
    {
        $this->decisionManager->shouldReceive('decide')->andReturn($decision);
        $voter = new HandleCommandVoter($this->decisionManager, $mapping);
        $tokenMock = Mockery::mock(TokenInterface::class);

        $this->assertEquals($expected, $voter->vote($tokenMock, $subject, [$attribute]));
    }

    /**
     * Gets the testdata for the vote test.
     *
     * @return array
     */
    public function provideTestVoteData()
    {
        return [
            'default access is false' => [
                'handle',
                new FakeCommand,
                true,
                [],
                VoterInterface::ACCESS_DENIED
            ],
            'default is unrelated to decision manager' => [
                'handle',
                new FakeCommand,
                false,
                [],
                VoterInterface::ACCESS_DENIED
            ],
            'abstain when not handling a command, but using the handle attribute' => [
                'handle',
                null,
                true,
                [],
                VoterInterface::ACCESS_ABSTAIN
            ],
            'abstain when not handling a command and not using the handle attribute' => [
                'create',
                null,
                true,
                [],
                VoterInterface::ACCESS_ABSTAIN
            ],
            'abstain when handling a command and not using the handle attribute' => [
                'create',
                new FakeCommand,
                true,
                [],
                VoterInterface::ACCESS_ABSTAIN
            ],
            'deny access if decision manager returns false, even if the command is in the mapping' => [
                'handle',
                new FakeCommand,
                false,
                [FakeCommand::class => ['ROLE_USER']],
                VoterInterface::ACCESS_DENIED
            ],
            'grant access if decision manager returns true and the command is in the mapping' => [
                'handle',
                new FakeCommand,
                true,
                [FakeCommand::class => ['ROLE_USER']],
                VoterInterface::ACCESS_GRANTED
            ],
            'deny access if the command is not in the mapping (i.e. a default deny access case)' => [
                'handle',
                new FakeCommand,
                false,
                ['someOtherCommand' => ['ROLE_USER']],
                VoterInterface::ACCESS_DENIED
            ],
        ];
    }
}

{'fraction_non_alphanumeric': 0.10754539762053851, 'fraction_numerical': 0.0, 'mean_word_length': 2.147290640394089, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5899548', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1631, 'n_words': 419}