test: add tests

This commit is contained in:
2026-02-15 15:46:27 +01:00
parent bd065eab32
commit 6f2cb7e69d
10 changed files with 5673 additions and 5 deletions

81
tests/unit/StdioTest.php Normal file
View File

@@ -0,0 +1,81 @@
<?php
namespace Nih\CommandBuilder;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(Stdio::class)]
final class StdioTest extends TestCase
{
public function testNull(): void
{
$null = match (PHP_OS_FAMILY) {
'Windows' => 'nul',
default => '/dev/null',
};
$this->assertEquals(
['file', $null, 'r'],
Stdio::null()->getDescriptiorSpec(0),
);
$this->assertEquals(
['file', $null, 'w'],
Stdio::null()->getDescriptiorSpec(1),
);
$this->assertEquals(
['file', $null, 'w'],
Stdio::null()->getDescriptiorSpec(2),
);
}
public function testInherit(): void
{
$this->assertEquals(
STDIN,
Stdio::inherit()->getDescriptiorSpec(0),
);
$this->assertEquals(
STDOUT,
Stdio::inherit()->getDescriptiorSpec(1),
);
$this->assertEquals(
STDERR,
Stdio::inherit()->getDescriptiorSpec(2),
);
}
public function testFile(): void
{
$this->assertEquals(
['file', '/foo/bar/baz', 'r'],
Stdio::file('/foo/bar/baz', 'r')->getDescriptiorSpec(0),
);
}
public function testStream(): void
{
$this->assertEquals(
STDOUT,
Stdio::stream(STDOUT)->getDescriptiorSpec(0),
);
}
public function testThrowsOnInvalidStream(): void
{
$handle = fopen('php://memory', 'w');
$this->assertNotFalse($handle);
fclose($handle);
$this->expectException(InvalidArgumentException::class);
/** @psalm-suppress InvalidArgument */
Stdio::stream($handle);
}
}