7a8ed48e3d
This name is available since phpunit 4.8.36 and 5.4.3.
127 lines
2.7 KiB
PHP
127 lines
2.7 KiB
PHP
<?php
|
|
namespace Tests\PHPCensor\ProcessControl;
|
|
|
|
/**
|
|
* Some helpers to
|
|
*/
|
|
abstract class ProcessControlTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
/**
|
|
* @var resource
|
|
*/
|
|
protected $process;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $pipes;
|
|
|
|
/**
|
|
* @var \PHPCensor\ProcessControl\ProcessControlInterface
|
|
*/
|
|
protected $object;
|
|
|
|
/** Starts a process.
|
|
*
|
|
* @return int The PID of the process.
|
|
*/
|
|
protected function startProcess()
|
|
{
|
|
$desc = [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]];
|
|
$this->pipes = [];
|
|
|
|
$this->process = proc_open($this->getTestCommand(), $desc, $this->pipes);
|
|
sleep(1);
|
|
|
|
$this->assertTrue(is_resource($this->process));
|
|
$this->assertTrue($this->isRunning());
|
|
|
|
$status = proc_get_status($this->process);
|
|
return (integer)$status['pid'];
|
|
}
|
|
|
|
/** End the running process.
|
|
*
|
|
* @return int
|
|
*/
|
|
protected function endProcess()
|
|
{
|
|
if (!is_resource($this->process)) {
|
|
return;
|
|
}
|
|
array_map('fclose', $this->pipes);
|
|
$exitCode = proc_close($this->process);
|
|
$this->assertFalse($this->isRunning());
|
|
$this->process = null;
|
|
return $exitCode;
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
protected function isRunning()
|
|
{
|
|
if (!is_resource($this->process)) {
|
|
return false;
|
|
}
|
|
$status = proc_get_status($this->process);
|
|
return (boolean)$status['running'];
|
|
}
|
|
|
|
public function testIsRunning()
|
|
{
|
|
if (!$this->object->isAvailable()) {
|
|
$this->markTestSkipped();
|
|
}
|
|
|
|
$pid = $this->startProcess();
|
|
|
|
$this->assertTrue($this->object->isRunning($pid));
|
|
|
|
fwrite($this->pipes[0], PHP_EOL);
|
|
|
|
$exitCode = $this->endProcess();
|
|
|
|
$this->assertEquals(0, $exitCode);
|
|
$this->assertFalse($this->object->isRunning($pid));
|
|
}
|
|
|
|
public function testSoftKill()
|
|
{
|
|
if (!$this->object->isAvailable()) {
|
|
$this->markTestSkipped();
|
|
}
|
|
|
|
$pid = $this->startProcess();
|
|
|
|
self::assertTrue($this->object->kill($pid));
|
|
sleep(1);
|
|
|
|
self::assertFalse($this->isRunning());
|
|
}
|
|
|
|
public function testForcefullyKill()
|
|
{
|
|
if (!$this->object->isAvailable()) {
|
|
$this->markTestSkipped();
|
|
}
|
|
|
|
$pid = $this->startProcess();
|
|
|
|
$this->object->kill($pid, true);
|
|
sleep(1);
|
|
|
|
self::assertFalse($this->isRunning());
|
|
}
|
|
|
|
abstract public function testIsAvailable();
|
|
|
|
abstract public function getTestCommand();
|
|
|
|
protected function tearDown()
|
|
{
|
|
parent::tearDown();
|
|
$this->endProcess();
|
|
}
|
|
}
|