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()); } } }
public function testConstraintIsInstanceOf2() { $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf('Exception'); try { $constraint->fail(new stdClass(), 'custom message'); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals("custom message\nFailed asserting that <stdClass> is an instance of class \"Exception\".", $e->getDescription()); return; } $this->fail(); }
public function testFailureIsInstanceOf2() { $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf('Exception'); try { $constraint->fail(new stdClass(), 'custom message'); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals("custom message\nFailed asserting that <stdClass> is an instance of class \"Exception\".\n", PHPUnit_Framework_TestFailure::exceptionToString($e)); return; } $this->fail(); }
public function testConstraintIsInstanceOf() { $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf('Exception'); $this->assertFalse($constraint->evaluate(new stdClass())); $this->assertTrue($constraint->evaluate(new Exception())); $this->assertEquals('is instance of class "Exception"', $constraint->toString()); try { $constraint->fail(new stdClass(), ''); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals('Failed asserting that <stdClass> is an instance of class "Exception".', $e->getDescription()); return; } $this->fail(); }
/** * Make it public */ public function matches($other) { return parent::matches($other); }
/** * Asserts that $actual is of instance $expected, use instead of the one defined * in PHPUnit_Framework_Assert for compatibility reasons (doesn't exist prior to v3.5) * Fails with optional $message if instance doesn't match * If phpUnit provides an assertInstanceOf class (>= v.3.5) this one will be used, * otherwise it will be checked directly here. * @param $expected The expected Instance * @param $actual The object to test * @param $message The message to return if assertion fails (optional) */ public static function assertInstanceOf($expected, $actual, $message = '') { if (method_exists('PHPUnit_Framework_Assert', 'assertInstanceOf')) { PHPUnit_Framework_Assert::assertInstanceOf($expected, $actual, $message); } else { $constraint; if (is_string($expected)) { if (class_exists($expected) || interface_exists($expected)) { $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf($expected); } else { throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'class or interface name'); } } else { throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); } if (!$constraint->evaluate($actual)) { $constraint->fail($actual, $message); } } }