File: ConsoleTest.php

package info (click to toggle)
phpunit-environment 9.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 33,644 kB
  • sloc: php: 11,034; xml: 61; makefile: 21
file content (97 lines) | stat: -rw-r--r-- 2,851 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php declare(strict_types=1);
/*
 * This file is part of sebastian/environment.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace SebastianBergmann\Environment;

use const PHP_BINARY;
use function assert;
use function fclose;
use function fopen;
use function is_resource;
use function proc_close;
use function proc_open;
use function stream_get_contents;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily;
use PHPUnit\Framework\Attributes\Ticket;
use PHPUnit\Framework\TestCase;

#[CoversClass(Console::class)]
final class ConsoleTest extends TestCase
{
    #[Ticket('https://github.com/sebastianbergmann/environment/issues/79')]
    public function testIsInteractiveReturnsFalseForPipeResource(): void
    {
        $process = proc_open(
            'echo hello',
            [1 => ['pipe', 'w']],
            $pipes,
        );

        $this->assertIsResource($process);

        assert(isset($pipes[1]) && is_resource($pipes[1]));

        try {
            $this->assertFalse((new Console)->isInteractive($pipes[1]));
        } finally {
            fclose($pipes[1]);
            proc_close($process);
        }
    }

    #[Ticket('https://github.com/sebastianbergmann/environment/issues/79')]
    public function testIsInteractiveReturnsFalseForRegularFileResource(): void
    {
        $resource = fopen(__FILE__, 'r');

        $this->assertNotFalse($resource);

        try {
            $this->assertFalse((new Console)->isInteractive($resource));
        } finally {
            fclose($resource);
        }
    }

    #[Ticket('https://github.com/sebastianbergmann/environment/issues/79')]
    #[RequiresOperatingSystemFamily('Linux')]
    public function testGetNumberOfColumnsDoesNotProduceStderrWarningsInNonTtyEnvironment(): void
    {
        $process = proc_open(
            [
                PHP_BINARY,
                '-r',
                'require __DIR__ . "/src/autoload.php"; echo (new SebastianBergmann\Environment\Console)->getNumberOfColumns();',
            ],
            [
                0 => ['file', '/dev/null', 'r'],
                1 => ['pipe', 'w'],
                2 => ['pipe', 'w'],
            ],
            $pipes,
            __DIR__ . '/..',
        );

        $this->assertIsResource($process);

        assert(isset($pipes[1]) && is_resource($pipes[1]));
        assert(isset($pipes[2]) && is_resource($pipes[2]));

        $stdout = stream_get_contents($pipes[1]);
        $stderr = stream_get_contents($pipes[2]);

        fclose($pipes[1]);
        fclose($pipes[2]);
        proc_close($process);

        $this->assertSame('80', $stdout);
        $this->assertSame('', $stderr);
    }
}