-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test execution failure (missing command)
- Loading branch information
Showing
2 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |