test: add tests
This commit is contained in:
81
tests/unit/StdioTest.php
Normal file
81
tests/unit/StdioTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user