test: add tests
This commit is contained in:
116
tests/unit/CommandTest.php
Normal file
116
tests/unit/CommandTest.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Nih\CommandBuilder;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ValueError;
|
||||
|
||||
#[CoversClass(Command::class)]
|
||||
#[CoversClass(Stdio::class)]
|
||||
#[CoversClass(StreamException::class)]
|
||||
#[CoversClass(ChildException::class)]
|
||||
final class CommandTest extends TestCase
|
||||
{
|
||||
public function testChildErrorHandling(): void
|
||||
{
|
||||
$this->expectException(ChildException::class);
|
||||
|
||||
try {
|
||||
set_error_handler(ChildException::handleError(...));
|
||||
trigger_error('Oh no!');
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
}
|
||||
|
||||
public function test(): void
|
||||
{
|
||||
$command = new Command('echo');
|
||||
|
||||
$this->assertEquals(
|
||||
'echo',
|
||||
$command->getProgram(),
|
||||
);
|
||||
|
||||
$this->assertNull($command->getCurrentDir());
|
||||
|
||||
$command->currentDir('/tmp');
|
||||
|
||||
$this->assertEquals(
|
||||
'/tmp',
|
||||
$command->getCurrentDir(),
|
||||
);
|
||||
|
||||
$command->arg('foo')->args(['bar', 'baz']);
|
||||
|
||||
$this->assertEquals(
|
||||
['foo', 'bar', 'baz'],
|
||||
$command->getArgs(),
|
||||
);
|
||||
|
||||
$command->env('FOO', 'foo');
|
||||
|
||||
$this->assertEquals(
|
||||
['FOO' => 'foo'],
|
||||
$command->getEnvs(),
|
||||
);
|
||||
|
||||
$command->envs([
|
||||
'BAR' => 'bar',
|
||||
'BAZ' => 'baz',
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'FOO' => 'foo',
|
||||
'BAR' => 'bar',
|
||||
'BAZ' => 'baz',
|
||||
],
|
||||
$command->getEnvs(),
|
||||
);
|
||||
|
||||
$command->envRemove('BAR');
|
||||
|
||||
$this->assertEquals(
|
||||
[
|
||||
'FOO' => 'foo',
|
||||
'BAR' => null,
|
||||
'BAZ' => 'baz',
|
||||
],
|
||||
$command->getEnvs(),
|
||||
);
|
||||
|
||||
$command->envClear();
|
||||
|
||||
$this->assertEquals(
|
||||
[],
|
||||
$command->getEnvs(),
|
||||
);
|
||||
}
|
||||
|
||||
public function testStdioFromStream(): void
|
||||
{
|
||||
// Only verifies its okay to pass valid stream resources.
|
||||
$this->expectNotToPerformAssertions();
|
||||
|
||||
new Command('echo')
|
||||
->stdin(STDIN)
|
||||
->stdout(STDOUT)
|
||||
->stderr(STDERR);
|
||||
}
|
||||
|
||||
public function testThrowsOnEmptyProgram(): void
|
||||
{
|
||||
$this->expectException(ValueError::class);
|
||||
new Command('');
|
||||
}
|
||||
|
||||
public function testToString(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
"'ls' '*' 'foo bar'",
|
||||
(string) new Command('ls')->arg('*')->arg('foo bar'),
|
||||
);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
154
tests/unit/StreamTest.php
Normal file
154
tests/unit/StreamTest.php
Normal 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),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user