<commit_before><?php

namespace League\CommonMark\Tests\Functional;

use mikehaertl\shellcommand\Command;

class BinTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Tests the behavior of not providing any Markdown input
     */
    public function testNoArgsOrStdin()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->execute();

        $this->assertEquals(1, $cmd->getExitCode());
        $this->assertEmpty($cmd->getOutput());
        $this->assertContains('Usage:', $cmd->getError());
    }

    /**
     * Tests the -h flag
     */
    public function testHelpShortFlag()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg('-h');
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $this->assertContains('Usage:', $cmd->getOutput());
    }

    /**
     * Tests the --help option
     */
    public function testHelpOption()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg('--help');
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $this->assertContains('Usage:', $cmd->getOutput());
    }

    /**
     * Tests the behavior of using unknown options
     */
    public function testUnknownOption()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg('--foo');
        $cmd->execute();

        $this->assertEquals(1, $cmd->getExitCode());
        $this->assertContains('Unknown option', $cmd->getError());
    }

    /**
     * Tests converting a file by filename
     */
    public function testFileArgument()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg($this->getPathToData('atx_header.md'));
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $expectedContents = trim(file_get_contents($this->getPathToData('atx_header.html')));
        $this->assertEquals($expectedContents, trim($cmd->getOutput()));
    }

    /**
     * Tests converting Markdown from STDIN
     */
    public function testStdin()
    {
        $cmd = new Command(sprintf('cat %s | %s ', $this->getPathToData('atx_header.md'), $this->getPathToCommonmark()));
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $expectedContents = trim(file_get_contents($this->getPathToData('atx_header.html')));
        $this->assertEquals($expectedContents, trim($cmd->getOutput()));
    }

    /**
     * Returns the full path the commonmark "binary"
     *
     * @return string
     */
    protected function getPathToCommonmark()
    {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            return realpath(__DIR__ . '/../../bin/commonmark.bat');
        }

        return realpath(__DIR__ . '/../../bin/commonmark');
    }

    /**
     * Returns the full path to the test data file
     *
     * @param string $file
     *
     * @return string
     */<commit_msg>Skip the STDIN test on Windows<commit_after><?php

namespace League\CommonMark\Tests\Functional;

use mikehaertl\shellcommand\Command;

class BinTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Tests the behavior of not providing any Markdown input
     */
    public function testNoArgsOrStdin()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->execute();

        $this->assertEquals(1, $cmd->getExitCode());
        $this->assertEmpty($cmd->getOutput());
        $this->assertContains('Usage:', $cmd->getError());
    }

    /**
     * Tests the -h flag
     */
    public function testHelpShortFlag()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg('-h');
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $this->assertContains('Usage:', $cmd->getOutput());
    }

    /**
     * Tests the --help option
     */
    public function testHelpOption()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg('--help');
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $this->assertContains('Usage:', $cmd->getOutput());
    }

    /**
     * Tests the behavior of using unknown options
     */
    public function testUnknownOption()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg('--foo');
        $cmd->execute();

        $this->assertEquals(1, $cmd->getExitCode());
        $this->assertContains('Unknown option', $cmd->getError());
    }

    /**
     * Tests converting a file by filename
     */
    public function testFileArgument()
    {
        $cmd = new Command($this->getPathToCommonmark());
        $cmd->addArg($this->getPathToData('atx_header.md'));
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $expectedContents = trim(file_get_contents($this->getPathToData('atx_header.html')));
        $this->assertEquals($expectedContents, trim($cmd->getOutput()));
    }

    /**
     * Tests converting Markdown from STDIN
     */
    public function testStdin()
    {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $this->markTestSkipped('Test skipped: STDIN is not supported on Windows');
        }

        $cmd = new Command(sprintf('cat %s | %s ', $this->getPathToData('atx_header.md'), $this->getPathToCommonmark()));
        $cmd->execute();

        $this->assertEquals(0, $cmd->getExitCode());
        $expectedContents = trim(file_get_contents($this->getPathToData('atx_header.html')));
        $this->assertEquals($expectedContents, trim($cmd->getOutput()));
    }

    /**
     * Returns the full path the commonmark "binary"
     *
     * @return string
     */
    protected function getPathToCommonmark()
    {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            return realpath(__DIR__ . '/../../bin/commonmark.bat');
        }

        return realpath(__DIR__ . '/../../bin/commonmark');
    }

    /**
     * Returns the full path to the test data file
     *
     * @param string $file
     *
     * @return string
     */
{'fraction_non_alphanumeric': 0.16946938775510204, 'fraction_numerical': 0.0029387755102040815, 'mean_word_length': 2.5126146788990824, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 96, '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': '96272', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1789, 'n_words': 340}