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 testConstraintStringContains2() { $constraint = new PHPUnit_Framework_Constraint_StringContains('foo'); try { $constraint->fail('barbazbar', 'custom message'); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals("custom message\nFailed asserting that <string:barbazbar> contains \"foo\".", $e->getDescription()); return; } $this->fail(); }
public function testFailureStringContains2() { $constraint = new PHPUnit_Framework_Constraint_StringContains('foo'); try { $constraint->fail('barbazbar', 'custom message'); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals("custom message\nFailed asserting that <string:barbazbar> contains \"foo\".\n", PHPUnit_Framework_TestFailure::exceptionToString($e)); return; } $this->fail(); }
public function testConstraintStringContains() { $constraint = new PHPUnit_Framework_Constraint_StringContains('foo'); $this->assertFalse($constraint->evaluate('barbazbar')); $this->assertTrue($constraint->evaluate('barfoobar')); $this->assertEquals('contains "foo"', $constraint->toString()); try { $constraint->fail('barbazbar', ''); } catch (PHPUnit_Framework_ExpectationFailedException $e) { $this->assertEquals('Failed asserting that <string:barbazbar> contains "foo".', $e->getDescription()); return; } $this->fail(); }