PHPUnit user restriction stopped working

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, 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); } ... 

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:

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

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

+4
source share
1 answer

I could solve it now. This is due to the fact that I updated PHPUnit, the API has changed a bit. I took PHPUnit_Framework_Constraint as a template for my own restriction again, there read the comments.

The evaluate function evaluate now different, now I moved the evaluation logic to a private function and switched from evaluate to matches . It works with return value. evaluate does not work with the default return value longer, but expects an exception to be thrown. To take full advantage of this, you can also create an instance of some comparator object, but it was above my head, you can find more information about this in 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) ); } ... 

Now it works like a charm:

 1) FragmentTest::testGetType Failed asserting that the last error is "Suboptimal State Error" in Fragment.php, was "Invalid State Error" in Fragment.php. 

Others had similar problems, but another solution switched to fail , which you can also name because it is implemented in the base class of Phake Fixes Problem No. 43 and # 44 .

+3
source

Source: https://habr.com/ru/post/1398494/


All Articles