2
我想测试一个调用exec()
的php函数,那么最好的方法是什么?我用它来获得的git describe
结果:为对象执行exec创建测试()
class Version
{
public function getVersionString()
{
$result = exec('git describe --always');
if (false !== strpos($result, 'fatal')) {
throw new RuntimeException(sprintf(
'Git describe returns error: %s',
$result
));
}
return $result;
}
}
所以我想如果执行命令来测试,并在发生错误时,抛出异常(即“预期”的行为和“例外”的行为)。
class VersionTest extends PHPUnit_Framework_TestCase
{
public function testVersionResultsString()
{
$version = new Version();
$result = $version->getVersionString();
$this->assertEquals('...', $result);
}
public function testVersionResultHasFatalErrorThrowsException()
{
// trigger something that will cause the fatal
$this->setExpectedException('RuntimeException');
$version = new Version();
$result = $version->getVersionString();
}
}
过程中的类和测试的是真正比较复杂一点,但本质是捕捉exec()
地方。任何想法如何?