I created my own custom PHPUnit constraint and connected it to the nice assert... function.
I insert it into the TestCase database, this is the assertLastError function:
abstract class TestCase extends \PHPUnit_Framework_TestCase { protected function assertLint($filename, $message = '') { self::assertThat($filename, new ConstraintLint, $message); } protected function assertLastError($error, $file, $message = '') { self::assertThat($file, new ConstraintLastError($error), $message); } protected function assertXmlStringValidatesDtdUri($xml, $dtd, $message = '') { self::assertThat($dtd, new ConstraintXmlStringValidatesDtdUri($xml), $message); } ...
I have debugged the restriction so far, and I see that the evaluate method evaluate called and returns FALSE , however testrunner does not tell me about the error.
Limitation:
class ConstraintLastError extends \PHPUnit_Framework_Constraint { private $file; private $error; public function __construct($error) { $this->error = $error; } 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; } protected function customFailureDescription($other, $description, $not) { return sprintf('Failed asserting that the last error %s', basename($other), $not ? '' : 'no ', implode("\n - ", $this->lines)); } public function toString() { return sprintf('was %s in file %s.', $this->error, $this->file); } }
I do not know why this stopped working, the test test just finished cleanly, I see an error message on the output, incl. stacktrace (xdebug enabled) and var_dump tells me that the result is FALSE . Here is the test:
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'); }
This is the new test that I just wrote, I have the same statement in other places, and they no longer work.
PHPUnit 3.6.7