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

154
tests/unit/StreamTest.php Normal file
View File

@@ -0,0 +1,154 @@
<?php
namespace Nih\CommandBuilder;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversTrait;
use PHPUnit\Framework\TestCase;
#[CoversClass(ChildStdin::class)]
#[CoversClass(ChildStdout::class)]
#[CoversClass(ChildStderr::class)]
#[CoversClass(ChildException::class)]
#[CoversClass(StreamException::class)]
#[CoversTrait(StreamTrait::class)]
#[CoversTrait(StreamReadTrait::class)]
#[CoversTrait(StreamWriteTrait::class)]
final class StreamTest extends TestCase
{
public function testErrorHandler(): void
{
$this->expectException(StreamException::class);
try {
set_error_handler(StreamException::handleError(...));
trigger_error('Oh no!');
} finally {
restore_error_handler();
}
}
public function testOnlyAcceptsStreamResource(): void
{
$this->expectException(InvalidArgumentException::class);
$handle = fopen('php://memory', 'w+');
$this->assertNotFalse($handle);
fwrite($handle, 'test');
fclose($handle);
/** @psalm-suppress InvalidArgument */
new class($handle) {
use StreamTrait;
};
}
public function testReadRead(): void
{
$handle = fopen('php://memory', 'w+');
$this->assertNotFalse($handle);
fwrite($handle, 'test');
fseek($handle, 0);
$r = new class($handle) {
use StreamReadTrait;
};
$this->assertEquals('test', $r->read(4));
$this->assertEmpty($r->read(4));
$r->close();
$this->expectException(StreamException::class);
$r->read(4);
}
public function testReadGetContents(): void
{
$handle = fopen('php://memory', 'w+');
$this->assertNotFalse($handle);
fwrite($handle, 'test');
fseek($handle, 0);
$r = new class($handle) {
use StreamReadTrait;
};
$this->assertEquals('test', $r->getContents());
$this->assertEmpty($r->getContents());
$r->close();
$this->expectException(StreamException::class);
$r->getContents();
}
public function testWriteWrite(): void
{
$handle = fopen('php://memory', 'w');
$this->assertNotFalse($handle);
$w = new class($handle) {
use StreamWriteTrait;
};
$this->assertEquals(4, $w->write('test'));
$w->close();
$this->expectException(StreamException::class);
$w->write('test');
}
public function testWriteFlush(): void
{
$handle = fopen('php://memory', 'w');
$this->assertNotFalse($handle);
$w = new class($handle) {
use StreamWriteTrait;
};
$w->flush();
$w->close();
$this->expectException(StreamException::class);
$w->flush();
}
public function testChildStdin(): void
{
$handle = fopen('php://memory', 'w');
$this->assertNotFalse($handle);
$stdin = new ChildStdin($handle);
$this->assertEquals(
$handle,
$stdin->getDescriptiorSpec(0),
);
}
public function testChildStdout(): void
{
$handle = fopen('php://memory', 'r');
$this->assertNotFalse($handle);
$stdout = new ChildStdout($handle);
$this->assertEquals(
$handle,
$stdout->getDescriptiorSpec(1),
);
}
public function testChildStderr(): void
{
$handle = fopen('php://memory', 'r');
$this->assertNotFalse($handle);
$stderr = new ChildStderr($handle);
$this->assertEquals(
$handle,
$stderr->getDescriptiorSpec(2),
);
}
}