PHPUnit_Framework_Constraint_IsInstanceOf::fail PHP Method

fail() public method

Creates the appropriate exception for the constraint which can be caught by the unit test system. This can be called if a call to evaluate() fails.
public fail ( mixed $other, string $description, boolean $not = FALSE )
$other mixed The value passed to evaluate() which failed the constraint check.
$description string A string with extra description of what was going on while the evaluation failed.
$not boolean Flag to indicate negation.
    public function fail($other, $description, $not = FALSE)
    {
        throw new PHPUnit_Framework_ExpectationFailedException(sprintf('%sFailed asserting that %s is %san instance of class "%s".', !empty($description) ? $description . "\n" : '', PHPUnit_Util_Type::toString($other, TRUE), $not ? 'not ' : '', $this->className), NULL);
    }

Usage Example

Esempio n. 1
0
 public static function assertException($runnable, $class, $message)
 {
     try {
         $runnable();
         self::fail('No exception thrown');
     } catch (Exception $e) {
         if ($e instanceof $class) {
             $messageConstraint = new PHPUnit_Framework_Constraint_StringContains($message, true);
             if (!$messageConstraint->evaluate($e->getMessage())) {
                 throw $messageConstraint->fail($e->getMessage(), PHP_EOL . $e->getTraceAsString());
             }
             self::assertTrue(true);
             // update assertion counter
             self::assertTrue(true);
             // update assertion counter
         } else {
             if ($e instanceof PHPUnit_Framework_AssertionFailedError && $e->getMessage() === 'No exception thrown') {
                 throw $e;
             }
             $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf($class);
             throw $constraint->fail($e, 'Expected ' . $class . ' but ' . get_class($e) . ' with message "' . $e->getMessage() . '"' . PHP_EOL . $e->getTraceAsString());
         }
     }
 }
All Usage Examples Of PHPUnit_Framework_Constraint_IsInstanceOf::fail
PHPUnit_Framework_Constraint_IsInstanceOf