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'),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user