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

116
tests/unit/CommandTest.php Normal file
View 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'),
);
}
}