<commit_before><?php

namespace Skillberto\CommandHandler\Tests;

use Skillberto\CommandHandler\Command;
use Skillberto\CommandHandler\CommandHandler;
use Symfony\Component\Process\Process;

class CommandHandlerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var CommandHandler
     */
    protected $commandHandler;

    protected $correctCommand_1 = "php ./Tests/Resources/test_correct_1.php";

    protected $correctCommand_2 = "php ./Tests/Resources/test_correct_2.php";

    protected $wrongCommand = "php ./Tests/Resources/test_wrong.php";

    protected $correctOutput = "Executing: php ./Tests/Resources/test_correct_1.php\nfoo";

    protected $wrongOutput = "Executing: php ./Tests/Resources/test_wrong.php\n";

    protected $skipOutput = "Skipped: php ./Tests/Resources/test_wrong.php\n";

    protected $errorOutput = "Error: php ./Tests/Resources/test_wrong.php\n";

    protected function setUp()
    {
        $output = new BufferedOutput();

        $this->commandHandler = new CommandHandler($output);
        $this->commandHandler
            ->add($this->correctCommand_1)
            ->addCollection(array($this->correctCommand_1));
    }

    public function testCorrectCommandWithoutSkip()
    {
        $this->commandHandler->execute();

        $this->assertOutputEquals(
            $this->correctOutput. $this->correctOutput
        );
    }

    public function testCorrectCommandWithSkip()
    {
        $this->commandHandler
            ->addSkippable($this->correctCommand_1)
            ->addSkippableCollection(array($this->correctCommand_1))
            ->execute();

        $this->commandHandler->getSkippedMessages();
        $this->commandHandler->getErrorMessage();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput . $this->correctOutput. $this->correctOutput
        );
    }

    public function testCorrectCommandWithTimeout()
    {
        $that = $this;

        $this->commandHandler
            ->addCommand(new Command($this->correctCommand_2, false, 0.3))
            ->setTimeout(0.2)
            ->execute(function(Process $process, Command $command) use ($that) {
                if ($that->correctCommand_2 == $command->get()) {
                    $that->assertEquals($process->getTimeout(), $command->getTimeout());
                } else {
                    $that->assertEquals($process->getTimeout(), $that->commandHandler->getTimeout());
                }
            });
    }

    public function testWrongCommandWithSkip()
    {
        $this->commandHandler
            ->addSkippable($this->wrongCommand)
            ->execute();

        $this->commandHandler->getSkippedMessages();
        $this->commandHandler->getErrorMessage();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput . $this->wrongOutput. $this->skipOutput
        );

    }

    public function testWrongCommandWithoutSkipAsError()
    {
        $this->commandHandler
            ->add($this->wrongCommand)
            ->execute();

        $this->commandHandler->getSkippedMessages();
        $this->commandHandler->getErrorMessage();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput . $this->wrongOutput. $this->errorOutput
        );

    }

    public function testPrefix()
    {
        $prefix = 'php ';
        $correctCommand_1 = $this->correctCommand_1;

        if (substr($correctCommand_1, 0, strlen($prefix)) == $prefix) {
            $correctCommand_1 = substr($correctCommand_1, strlen($prefix));
        } else {
            throw new \InvalidArgumentException(sprintf('%s command is incorrect, %s prefix is not found at the beginning.', $correctCommand_1, $prefix));
        }

        $output = new BufferedOutput();

        $this->commandHandler = new CommandHandler($output, $prefix);
        $this->commandHandler
            ->add($correctCommand_1)
            ->addCollection(array($correctCommand_1))
            ->execute();

        $this->assertOutputEquals(
            $this->correctOutput. $this->correctOutput
        );
    }

    protected function formatOutput($output)
    {
        return preg_replace( "/\r|\n/", "",$output);
    }

    /**
     * Format expected parameter(as expected) and output of commandHandler (as actual) with formatOutput, and compare them with assertEquals (the expected and actual).
     *
     * @param $expected
     */
    protected function assertOutputEquals($expected)
    {
        $this->assertEquals(
            $this->formatOutput($expected),
            $this->formatOutput($this->commandHandler->getOutput()->fetch())
        );
    }
}<commit_msg>test refactored<commit_after><?php

namespace Skillberto\CommandHandler\Tests;

use Skillberto\CommandHandler\Command;
use Skillberto\CommandHandler\CommandHandler;
use Symfony\Component\Process\Process;

class CommandHandlerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var CommandHandler
     */
    protected $commandHandler;

    protected $correctCommand_1 = "php ./Tests/Resources/test_correct_1.php";

    protected $correctCommand_2 = "php ./Tests/Resources/test_correct_2.php";

    protected $wrongCommand = "php ./Tests/Resources/test_wrong.php";

    protected $correctOutput = "Executing: php ./Tests/Resources/test_correct_1.php\nfoo";

    protected $wrongOutput = "Executing: php ./Tests/Resources/test_wrong.php\n";

    protected $skipOutput = "Skipped: php ./Tests/Resources/test_wrong.php\n";

    protected $errorOutput = "Error: php ./Tests/Resources/test_wrong.php\n";

    protected function setUp()
    {
        $this->commandHandler = $this->createHandler($this->correctCommand_1);
    }

    public function testCorrectCommandWithoutSkip()
    {
        $this->commandHandler->execute();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput
        );
    }

    public function testCorrectCommandWithSkip()
    {
        $this->commandHandler
            ->addSkippable($this->correctCommand_1)
            ->addSkippableCollection(array($this->correctCommand_1))
            ->execute();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput . $this->correctOutput . $this->correctOutput
        );
    }

    public function testCorrectCommandWithTimeout()
    {
        $that = $this;

        $this->commandHandler
            ->addCommand(new Command($this->correctCommand_2, false, 0.3))
            ->setTimeout(0.2)
            ->execute(function(Process $process, Command $command) use ($that) {
                if ($that->correctCommand_2 == $command->get()) {
                    $that->assertEquals($process->getTimeout(), $command->getTimeout());
                } else {
                    $that->assertEquals($process->getTimeout(), $that->commandHandler->getTimeout());
                }
            });
    }

    public function testWrongCommandWithSkip()
    {
        $this->commandHandler
            ->addSkippable($this->wrongCommand)
            ->execute();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput . $this->wrongOutput . $this->skipOutput
        );
    }

    public function testWrongCommandWithoutSkipAsError()
    {
        $this->commandHandler
            ->add($this->wrongCommand)
            ->execute();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput . $this->wrongOutput . $this->errorOutput
        );

    }

    public function testPrefix()
    {
        list($prefix, $command) = $this->createPrefixAndCommand("php ");

        $this->commandHandler = $this->createHandler($command, $prefix);

        $this->commandHandler->execute();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput
        );
    }

    public function testAddHandler()
    {
        $handler = $this->createHandler($this->correctCommand_1);

        $this->commandHandler
            ->addHandler($handler)
            ->execute();

        $this->assertOutputEquals(
            $this->correctOutput . $this->correctOutput . $this->correctOutput .$this->correctOutput
        );
    }

    public function testAddHandlerWithPrefix()
    {

    }

    /**
     * @param  $output
     * @return string
     */
    protected function formatOutput($output)
    {
        return preg_replace( "/\r|\n/", "",$output);
    }

    /**
     * Format expected parameter(as expected) and output of commandHandler (as actual) with formatOutput, and compare them with assertEquals (the expected and actual).
     *
     * @param $expected
     */
    protected function assertOutputEquals($expected)
    {
        $this->commandHandler->getSkippedMessages();
        $this->commandHandler->getErrorMessage();

        $this->assertEquals(
            $this->formatOutput($expected),
            $this->formatOutput($this->commandHandler->getOutput()->fetch())
        );
    }

    /**
     * @param  string $prefix
     * @return array
     */
    protected function createPrefixAndCommand($prefix)
    {
        $correctCommand_1 = $this->correctCommand_1;

        if (substr($correctCommand_1, 0, strlen($prefix)) == $prefix) {
            $correctCommand_1 = substr($correctCommand_1, strlen($prefix));
        } else {
            throw new \InvalidArgumentException(sprintf('%s command is incorrect, %s prefix is not found at the beginning of command.', $correctCommand_1, $prefix));
        }

        return array($prefix, $correctCommand_1);
    }

    /**
     * @param  string $command
     * @param  string $prefix
     * @return CommandHandler With two command ($command)
     */
    protected function createHandler($command, $prefix = "")
    {
        $output = new BufferedOutput();

        $commandHandler = new CommandHandler($output, $prefix);
        $commandHandler
            ->add($command)
            ->addCollection(array($command));

        return $commandHandler;
    }
}
{'fraction_non_alphanumeric': 0.13141153081510934, 'fraction_numerical': 0.00467196819085487, 'mean_word_length': 2.503133704735376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 166, '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': '941247', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2621, 'n_words': 475}