自定义PHPUnit约束停止工作
我已经创建了一个自定义的
PHPUnit约束并将其连接到一个很好的断言…函数.
我把它放在我的基础TestCase中,它是assertLastError函数: /** * abstract,base test-case class. */ abstract class TestCase extends PHPUnit_Framework_TestCase { /** * assert lint of a php file */ protected function assertLint($filename,$message = '') { self::assertThat($filename,new ConstraintLint,$message); } /** * assert the last error */ protected function assertLastError($error,$file,$message = '') { self::assertThat($file,new ConstraintLastError($error),$message); } /** * assert XML DTD validity */ protected function assertXmlStringValidatesDtdUri($xml,$dtd,$message = '') { self::assertThat($dtd,new ConstraintXmlStringValidatesDtdUri($xml),$message); } ... 到目前为止我调试了约束,我看到evaluate方法被调用并返回FALSE,但是testrunner没有报告我失败. 约束: /** * ConstraintLastError * * For asserting the last error message in a file. * * To test trigger_error(). * * Example: * * $this->assertLastError('Error Message','basenameOfFile.php'); * */ class ConstraintLastError extends PHPUnit_Framework_Constraint { private $file; private $error; public function __construct($error) { $this->error = $error; } /** * Evaluates the constraint for parameter $file. Returns TRUE if the * constraint is met,FALSE otherwise. * * @param string $file Value or object to evaluate. * @return bool */ public function evaluate($file) { $this->file = $file; $error = $this->error; $lastError = error_get_last(); if (NULL === $lastError) return false; $last_message = $lastError['message']; $last_file = $lastError['file']; $result = ($error == $last_message && basename($file) == basename($last_file)); var_dump($result,$error,$last_message,$last_file); return $result; } /** * @param mixed $other * @param string $description * @param boolean $not */ protected function customFailureDescription($other,$description,$not) { return sprintf('Failed asserting that the last error %s',basename($other),$not ? '' : 'no ',implode("n - ",$this->lines)); } /** * Returns a string representation of the constraint. * * @return string */ public function toString() { return sprintf('was %s in file %s.',$this->error,$this->file); } } 我不知道为什么这停止工作,测试用例刚刚完成,我可以在输出中看到错误消息. stacktrace(xdebug打开),var_dump告诉我结果为FALSE.这是测试: public function testGetType() { ... $fragment->setParsed(array()); PHPUnit_Framework_Error_Warning::$enabled = FALSE; $actual = $fragment->getType(); PHPUnit_Framework_Error_Warning::$enabled = TRUE; $this->assertLastError('Invalid State Error FAIL','Fragment.php'); } 这是我刚刚写的一个新测试,我在其他地方也有相同的断言,它们也不再工作了. PHPUnit 3.6.7 解决方法
我现在可以解决这个问题.这与我升级PHPUnit有关,API略有改变.我再次将PHPUnit_Framework_Constraint作为我自己约束的模式,阅读其中的注释有所帮助.
evaluate函数现在有所不同,我现在将评估逻辑移到私有函数中并从evaluate切换到匹配.它与返回值一起使用.默认情况下,evaluate不再使用返回值,但预计会抛出异常.要从中充分受益,您还可以实例化一些比较器对象,但这已经超出了我的想象,您可以在asserEquals约束中找到有关该对象的更多信息. class ConstraintLastError extends PHPUnit_Framework_Constraint { ... /** * Evaluates the constraint for parameter $file. Returns TRUE if the * constraint is met,FALSE otherwise. * * This method can be overridden to implement the evaluation algorithm. * * @param mixed $other Value or object to evaluate. * @return bool */ public function matches($file) { return $this->compareAgainstLast($file,$this->error); } /** * * @param string $file * @param string $error * @return bool */ private function compareAgainstLast($file,$error) { if (!$last = error_get_last()) { $last = array('message' => '(none)','file' => ''); } $this->lastError = $last['message']; $this->lastFile = $last['file']; return $error === $this->lastError && basename($file) === basename($this->lastFile); } /** * @param string $file */ protected function failureDescription($file) { return sprintf('the last error is "%s" in %s,was "%s" in %s',basename($file),$this->lastError,basename($this->lastFile) ); } ... 它现在像一个魅力: 1) FragmentTest::testGetType Failed asserting that the last error is "Suboptimal State Error" in Fragment.php,was "Invalid State Error" in Fragment.php. 其他人有类似的问题,但是一个不同的解决方案切换到失败,你可以调用它,因为它是在基类Phake Fixes issues #43 and #44中实现的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |