Skip to content

Commit

Permalink
Test execution failure (missing command)
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Mar 26, 2019
1 parent f9a8bd1 commit 54870c5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ public function __construct(string $command) {
* Runs the command in a concurrent thread.
* Sets the input, output and errors streams.
*/
public function exec() {
public function exec(bool $blocking = false) {
$descriptor = [
0 => ["pipe", "r"],
1 => ["pipe", "w"],
2 => ["pipe", "w"],
];

$this->process = proc_open(
escapeshellcmd($this->command),
$this->command,
$descriptor,
$this->pipes
);

if(!is_resource($this->process)) {
throw new DaemonException("An unexpected error occurred while trying to run $this->command.");
}
$this->status = proc_get_status($this->process);

stream_set_blocking($this->pipes[1], 0);
stream_set_blocking($this->pipes[2], 0);
stream_set_blocking($this->pipes[0], 0);

$this->status = proc_get_status($this->process);
if($blocking) {
while($this->isRunning()) {
usleep(10000);
}
}
}

public function isRunning():bool {
Expand Down
80 changes: 80 additions & 0 deletions test/unit/ProcessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace Gt\Daemon\Test;

use Gt\Daemon\Process;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;

class ProcessTest extends TestCase {
protected $tmpBase;

protected function setUp():void {
$this->tmpBase = implode(DIRECTORY_SEPARATOR, [
sys_get_temp_dir(),
"phpgt",
"test",
"daemon",
]);
}

public function tearDown():void {
if(!is_dir($this->tmpBase)) {
return;
}

$directory = new RecursiveDirectoryIterator(
$this->tmpBase,
RecursiveDirectoryIterator::CURRENT_AS_FILEINFO
| RecursiveDirectoryIterator::KEY_AS_PATHNAME
);
$iterator = new RecursiveIteratorIterator(
$directory,
RecursiveIteratorIterator::CHILD_FIRST
);

foreach($iterator as $file) {
/** @var SplFileInfo $file */
if($file->getFilename() === "."
|| $file->getFilename() === "..") {
continue;
}

if($file->isFile()) {
unlink($file->getPathname());
}
else {
rmdir($file->getPathname());
}
}

rmdir($this->tmpBase);
}

public function testExec() {
$tmpFile = implode(DIRECTORY_SEPARATOR, [
$this->tmpBase,
uniqid(),
]);
if(!is_dir(dirname($tmpFile))) {
mkdir(dirname($tmpFile), 0775, true);
}
$command = PHP_BINARY . " -r 'touch(\"$tmpFile\");'";
$sut = new Process($command);

self::assertFileNotExists($tmpFile);
$sut->exec();
while($sut->isRunning()) {
usleep(100000);
}

self::assertFileExists($tmpFile);
}

public function testExecFailure() {
$sut = new Process("/this/does/not/exist/" . uniqid());
$sut->exec(true);
self::assertEquals(127, $sut->getExitCode());
}
}

0 comments on commit 54870c5

Please sign in to comment.