自定义 PHPUnit 约束停止工作


Custom PHPUnit Constraint stopped working

我为自己创建了一个自定义的PHPUnit约束,并将其连接到一个不错的assert...函数。

把它塞进我的基本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,但是测试运行器没有向我报告失败。

约束:

/**
 * 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, $file, $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);
    }
}

我不知道为什么这停止工作,测试用例刚刚干净地完成,我可以在输出中看到错误消息,包括堆栈跟踪(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切换到matches。它适用于返回值。 默认情况下,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'
                    , $this->error, 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.

其他人也有类似的问题,但切换到fail的解决方案不同,您也可以调用它,因为它是在基类 Phake 修复问题 #43 和 #44 中实现的。