2017-01-11 19:24:02 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test\Process;
|
|
|
|
|
2017-03-16 16:04:52 +01:00
|
|
|
use Amp\Loop;
|
2017-01-11 19:24:02 +01:00
|
|
|
use Amp\Process\Process;
|
2017-06-16 05:53:33 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-01-11 19:24:02 +01:00
|
|
|
|
2017-06-16 05:53:33 +02:00
|
|
|
class ProcessTest extends TestCase {
|
2017-01-11 19:24:02 +01:00
|
|
|
const CMD_PROCESS = 'echo foo';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Process\StatusError
|
|
|
|
*/
|
|
|
|
public function testMultipleExecution() {
|
2017-06-15 19:11:38 +02:00
|
|
|
Loop::run(function () {
|
2017-01-11 19:24:02 +01:00
|
|
|
$process = new Process(self::CMD_PROCESS);
|
2017-03-17 04:19:15 +01:00
|
|
|
$process->start();
|
|
|
|
$process->start();
|
2017-01-11 19:24:02 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-16 05:49:41 +01:00
|
|
|
public function testIsRunning() {
|
2017-06-15 19:11:38 +02:00
|
|
|
Loop::run(function () {
|
2017-01-16 05:49:41 +01:00
|
|
|
$process = new Process("exit 42");
|
2017-03-17 04:19:15 +01:00
|
|
|
$process->start();
|
|
|
|
$promise = $process->join();
|
2017-01-16 05:49:41 +01:00
|
|
|
|
|
|
|
$this->assertTrue($process->isRunning());
|
|
|
|
|
|
|
|
yield $promise;
|
|
|
|
|
|
|
|
$this->assertFalse($process->isRunning());
|
2017-03-16 16:04:52 +01:00
|
|
|
});
|
2017-01-16 05:49:41 +01:00
|
|
|
}
|
|
|
|
|
2017-01-11 21:27:40 +01:00
|
|
|
public function testExecuteResolvesToExitCode() {
|
2017-06-15 19:11:38 +02:00
|
|
|
Loop::run(function () {
|
2017-01-11 21:27:40 +01:00
|
|
|
$process = new Process("exit 42");
|
2017-03-17 04:19:15 +01:00
|
|
|
$process->start();
|
|
|
|
$code = yield $process->join();
|
2017-01-11 19:24:02 +01:00
|
|
|
|
2017-01-11 21:27:40 +01:00
|
|
|
$this->assertSame(42, $code);
|
2017-01-16 05:49:41 +01:00
|
|
|
$this->assertFalse($process->isRunning());
|
2017-03-16 16:04:52 +01:00
|
|
|
});
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCommandCanRun() {
|
2017-06-15 19:11:38 +02:00
|
|
|
Loop::run(function () {
|
2017-01-11 19:24:02 +01:00
|
|
|
$process = new Process(self::CMD_PROCESS);
|
2017-03-17 04:19:15 +01:00
|
|
|
$process->start();
|
|
|
|
$promise = $process->join();
|
2017-01-11 19:24:02 +01:00
|
|
|
|
|
|
|
$completed = false;
|
2017-06-15 19:11:38 +02:00
|
|
|
$promise->onResolve(function () use (&$completed) { $completed = true; });
|
2017-01-11 19:24:02 +01:00
|
|
|
$this->assertFalse($completed);
|
|
|
|
$this->assertInternalType('int', $process->getPid());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Process\ProcessException
|
|
|
|
* @expectedExceptionMessage The process was killed
|
|
|
|
*/
|
|
|
|
public function testKillSignals() {
|
2017-06-15 19:11:38 +02:00
|
|
|
Loop::run(function () {
|
2017-01-11 19:24:02 +01:00
|
|
|
$process = new Process(self::CMD_PROCESS);
|
2017-03-17 04:19:15 +01:00
|
|
|
$process->start();
|
|
|
|
$promise = $process->join();
|
2017-01-11 19:24:02 +01:00
|
|
|
|
|
|
|
$process->kill();
|
|
|
|
|
|
|
|
$code = yield $promise;
|
2017-03-16 16:04:52 +01:00
|
|
|
});
|
2017-01-11 19:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCommand() {
|
|
|
|
$process = new Process(self::CMD_PROCESS);
|
|
|
|
$this->assertSame(self::CMD_PROCESS, $process->getCommand());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOptions() {
|
|
|
|
$process = new Process(self::CMD_PROCESS);
|
|
|
|
$this->assertSame([], $process->getOptions());
|
|
|
|
}
|
2017-06-15 19:11:38 +02:00
|
|
|
}
|