Я создал собственное ограничение 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);
}
}
Понятия не имею, почему это перестало работать, тестовый случай просто завершается чисто, я вижу сообщение об ошибке в выводе, вкл. 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