<commit_before><?php

namespace Matthimatiker\CommandLockingBundle\Tests\EventListener;

use Matthimatiker\CommandLockingBundle\EventListener\CommandLockingListener;
use Webfactory\Constraint\IsEventSubscriberConstraint;

class CommandLockingListenerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * System under test.
     *
     * @var CommandLockingListener
     */
    private $listener = null;

    /**
     * Initializes the test environment.
     */
    protected function setUp()
    {
        parent::setUp();
        $this->listener = new CommandLockingListener('mock');
    }

    /**
     * Cleans up the test environment.
     */
    protected function tearDown()
    {
        $this->listener = null;
        parent::tearDown();
    }

    public function testIsValidEventSubscriber()
    {
        $this->assertThat($this->listener, new IsEventSubscriberConstraint());
    }

    public function testAddsLockOption()
    {

    }

    public function testDoesNotLockIfNotRequested()
    {

    }

    public function testLocksIfRequested()
    {

    }

    public function testUsesExplicitlyRequestedLockManager()
    {

    }

    public function testThrowsExceptionIfRequestedLockManagerIsNotAvailable()
    {

    }

    public function testRunsCommandIfNoLockIsRequested()
    {

    }

    public function testDoesNotRunCommandIfLockingFails()
    {

    }

    public function testRunsCommandIfLockingSucceeds()
    {

    }

    public function testReleasesLockWhenLockedCommandTerminates()
    {

    }

    public function testDoesNotReleaseWhenCommandWasNotLocked()
    {

    }

    public function testDoesNotInterfereWithOptionsOfCommand()
    {

    }
}<commit_msg>implemented some tests<commit_after><?php

namespace Matthimatiker\CommandLockingBundle\Tests\EventListener;

use Matthimatiker\CommandLockingBundle\EventListener\CommandLockingListener;
use Matthimatiker\CommandLockingBundle\Locking\LockManagerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Webfactory\Constraint\IsEventSubscriberConstraint;

class CommandLockingListenerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * System under test.
     *
     * @var CommandLockingListener
     */
    private $listener = null;

    /**
     * @var LockManagerInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $lockManager = null;

    /**
     * @var Application
     */
    private $application = null;

    /**
     * Initializes the test environment.
     */
    protected function setUp()
    {
        parent::setUp();
        $this->lockManager = $this->getMock(LockManagerInterface::class);
        $this->listener = new CommandLockingListener('mock');
        $this->listener->registerLockManager('mock', $this->lockManager);
        $this->application = $this->createApplicationWith($this->listener);
    }

    /**
     * Cleans up the test environment.
     */
    protected function tearDown()
    {
        $this->application = null;
        $this->listener = null;
        $this->lockManager = null;
        parent::tearDown();
    }

    public function testIsValidEventSubscriber()
    {
        $this->assertThat($this->listener, new IsEventSubscriberConstraint());
    }

    public function testAddsLockOption()
    {

    }

    public function testDoesNotLockIfNotRequested()
    {
        $this->lockManager->expects($this->never())->method('lock');

        $this->runApplication('test:cmd');
    }

    public function testLocksIfRequested()
    {
        $this->lockManager->expects($this->once())->method('lock');

        $this->runApplication('test:cmd --lock');
    }

    public function testUsesExplicitlyRequestedLockManager()
    {
        $this->lockManager->expects($this->never())->method('lock');
        $customLockManager = $this->getMock(LockManagerInterface::class);
        $customLockManager->expects($this->once())->method('lock');
        $this->listener->registerLockManager('custom', $customLockManager);

        $this->runApplication('test:cmd --lock=custom');
    }

    public function testThrowsExceptionIfRequestedLockManagerIsNotAvailable()
    {
        $this->setExpectedException(\RuntimeException::class);
        $this->runApplication('test:cmd --lock=custom');
    }

    public function testRunsCommandIfNoLockIsRequested()
    {
        $this->assertCommandWillRun($this->once());

        $this->runApplication('test:cmd');
    }

    public function testDoesNotRunCommandIfLockingFails()
    {
        $this->lockManager->expects($this->once())->method('lock')->willReturn(false);
        $this->assertCommandWillRun($this->never());

        $this->runApplication('test:cmd --lock');
    }

    public function testRunsCommandIfLockingSucceeds()
    {
        $this->lockManager->expects($this->once())->method('lock')->willReturn(true);
        $this->assertCommandWillRun($this->once());

        $this->runApplication('test:cmd --lock');
    }

    public function testReleasesLockWhenLockedCommandTerminates()
    {
        $this->lockManager->expects($this->any())->method('lock')->willReturn(true);
        $this->lockManager->expects($this->once())->method('release');

        $this->runApplication('test:cmd --lock');
    }

    public function testDoesNotReleaseWhenNoLockWasRequested()
    {
        $this->lockManager->expects($this->never())->method('release');

        $this->runApplication('test:cmd');
    }

    public function testDoesNotReleaseWhenCommandLockingFails()
    {
        $this->lockManager->expects($this->any())->method('lock')->willReturn(false);
        $this->lockManager->expects($this->never())->method('release');

        $this->runApplication('test:cmd --lock');
    }

    public function testDoesNotInterfereWithOptionsOfCommand()
    {

    }

    /**
     * Asserts that the test command will run the given number of times.
     *
     * Example:
     *
     *     $this->assertCommandWillRun($this->once());
     *
     * @param \PHPUnit_Framework_MockObject_Matcher_Invocation $invocations
     */
    private function assertCommandWillRun(\PHPUnit_Framework_MockObject_Matcher_Invocation $invocations)
    {
        $codeMock = $this->getMock(\stdClass::class, array('__invoke'));
        $codeMock->expects($invocations)
            ->method('__invoke');
        $command = $this->application->find('test:cmd');
        $command->setCode($codeMock);
    }

    /**
     * Runs the console application with the given argument line.
     *
     * Example:
     *
     *     $this->runApplication('test:cmd --lock');
     *
     * @param string $commandLineArguments
     */
    private function runApplication($commandLineArguments)
    {
        $this->application->run(new StringInput($commandLineArguments), new NullOutput());
    }

    /**
     * @param CommandLockingListener $listener
     * @return Application
     */
    private function createApplicationWith(CommandLockingListener $listener)
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addSubscriber($listener);
        $application = new Application();
        $application->setDispatcher($dispatcher);
        $application->setAutoExit(false);
        $command = new Command('test:cmd');
        $command->setCode(function() {});
        $application->add($command);
        return $application;
    }
}
{'fraction_non_alphanumeric': 0.12882703777335985, 'fraction_numerical': 0.0, 'mean_word_length': 3.1806094182825486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 104, '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': '3625609', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2055, 'n_words': 309}