<commit_before><?php
namespace PharIo\Phive;

use PHPUnit\Framework\TestCase;

/**
 * @covers \PharIo\Phive\UnixoidEnvironment
 * @covers \PharIo\Phive\Environment
 */
class UnixoidEnvironmentTest extends TestCase {

    /**
     * @dataProvider hasProxyProvider
     *
     * @param array $server
     * @param bool $expected
     */
    public function testHasProxy(array $server, $expected) {
        $env = new UnixoidEnvironment($server, $this->getExecutorMock());
        $this->assertSame($expected, $env->hasProxy());
    }

    public function hasProxyProvider() {
        return [
            [[], false],
            [['https_proxy' => 'foo'], true]
        ];
    }

    /**
     * @dataProvider getProxyProvider
     *
     * @param string $proxy
     */
    public function testGetProxy($proxy) {
        $env = new UnixoidEnvironment(['https_proxy' => $proxy], $this->getExecutorMock());
        $this->assertSame($proxy, $env->getProxy());
    }

    public function getProxyProvider() {
        return [
            ['https://proxy.example.com:8080'],
            ['http://proxy.domain.tld'],
        ];
    }

    /**
     *
     */
    public function testGetHomeDirectory() {
        $env = new UnixoidEnvironment(['HOME' => __DIR__], $this->getExecutorMock());
        $this->assertSame(__DIR__, (string)$env->getHomeDirectory());
    }

    /**
     * @expectedException \BadMethodCallException
     */
    public function testGetProxyThrowsExceptionIfProxyIsNotSet() {
        $env = new UnixoidEnvironment([], $this->getExecutorMock());
        $env->getProxy();
    }

    /**
     * @expectedException \BadMethodCallException
     */
    public function testGetHomeDirectoryThrowsExceptionIfHomeIsNotSet() {
        $env = new UnixoidEnvironment([], $this->getExecutorMock());
        $env->getHomeDirectory();
    }

    /**
     * @dataProvider tputCommandDataProvider
     *
     * @param int $commandExitCode
     * @param string $commandOutput
     * @param bool $expectedResult
     */
    public function testSupportsColoredOutput($commandExitCode, $commandOutput, $expectedResult) {

        if (!function_exists('posix_isatty') || !posix_isatty(STDOUT)) {
            $this->markTestSkipped('requires tty');
        }

        $result = new ExecutorResult('tput', [$commandOutput], $commandExitCode);
        $executor = $this->getExecutorMock();
        $executor->method('execute')->willReturn($result);

        $env = new UnixoidEnvironment([], $executor);
        $this->assertSame($expectedResult, $env->supportsColoredOutput());
    }

    public static function tputCommandDataProvider() {
        return [
            [1, '', false],
            [2, '', false],<commit_msg>Update Test to set fake TERM environment var<commit_after><?php
namespace PharIo\Phive;

use PHPUnit\Framework\TestCase;

/**
 * @covers \PharIo\Phive\UnixoidEnvironment
 * @covers \PharIo\Phive\Environment
 */
class UnixoidEnvironmentTest extends TestCase {

    /**
     * @dataProvider hasProxyProvider
     *
     * @param array $server
     * @param bool $expected
     */
    public function testHasProxy(array $server, $expected) {
        $env = new UnixoidEnvironment($server, $this->getExecutorMock());
        $this->assertSame($expected, $env->hasProxy());
    }

    public function hasProxyProvider() {
        return [
            [[], false],
            [['https_proxy' => 'foo'], true]
        ];
    }

    /**
     * @dataProvider getProxyProvider
     *
     * @param string $proxy
     */
    public function testGetProxy($proxy) {
        $env = new UnixoidEnvironment(['https_proxy' => $proxy], $this->getExecutorMock());
        $this->assertSame($proxy, $env->getProxy());
    }

    public function getProxyProvider() {
        return [
            ['https://proxy.example.com:8080'],
            ['http://proxy.domain.tld'],
        ];
    }

    /**
     *
     */
    public function testGetHomeDirectory() {
        $env = new UnixoidEnvironment(['HOME' => __DIR__], $this->getExecutorMock());
        $this->assertSame(__DIR__, (string)$env->getHomeDirectory());
    }

    /**
     * @expectedException \BadMethodCallException
     */
    public function testGetProxyThrowsExceptionIfProxyIsNotSet() {
        $env = new UnixoidEnvironment([], $this->getExecutorMock());
        $env->getProxy();
    }

    /**
     * @expectedException \BadMethodCallException
     */
    public function testGetHomeDirectoryThrowsExceptionIfHomeIsNotSet() {
        $env = new UnixoidEnvironment([], $this->getExecutorMock());
        $env->getHomeDirectory();
    }

    /**
     * @dataProvider tputCommandDataProvider
     *
     * @param int $commandExitCode
     * @param string $commandOutput
     * @param bool $expectedResult
     */
    public function testSupportsColoredOutput($commandExitCode, $commandOutput, $expectedResult) {

        if (!function_exists('posix_isatty') || !posix_isatty(STDOUT)) {
            $this->markTestSkipped('requires tty');
        }

        $result = new ExecutorResult('tput', [$commandOutput], $commandExitCode);
        $executor = $this->getExecutorMock();
        $executor->method('execute')->willReturn($result);

        $env = new UnixoidEnvironment(['TERM' => 'xterm'], $executor);
        $this->assertSame($expectedResult, $env->supportsColoredOutput());
    }

    public static function tputCommandDataProvider() {
        return [
            [1, '', false],
            [2, '', false],
{'fraction_non_alphanumeric': 0.14556731120029187, 'fraction_numerical': 0.0021889821233126595, 'mean_word_length': 2.7248641304347827, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 2, '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': '4643426', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1531, 'n_words': 277}