diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 04fe703257c..d8e59ec9c54 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -457,7 +457,8 @@ public function getExpectedException() * @param string $exceptionMessage * @param int $exceptionCode * - * @since Method available since Release 3.2.0 + * @since Method available since Release 3.2.0 + * @deprecated Method deprecated since Release 5.2.0 */ public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null) { @@ -466,12 +467,75 @@ public function setExpectedException($exceptionName, $exceptionMessage = '', $ex $this->expectedExceptionCode = $exceptionCode; } + /** + * @param string $exception + * + * @since Method available since Release 5.2.0 + */ + public function expectException($exception) + { + if (!is_string($exception)) { + throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); + } + + $this->expectedException = $exception; + } + + /** + * @param int|string $code + * + * @throws PHPUnit_Framework_Exception + * + * @since Method available since Release 5.2.0 + */ + public function expectExceptionCode($code) + { + if (!is_int($code) && !is_string($code)) { + throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer or string'); + } + + $this->expectedExceptionCode = $code; + } + + /** + * @param string $message + * + * @throws PHPUnit_Framework_Exception + * + * @since Method available since Release 5.2.0 + */ + public function expectExceptionMessage($message) + { + if (!is_string($message)) { + throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); + } + + $this->expectedExceptionMessage = $message; + } + + /** + * @param string $messageRegExp + * + * @throws PHPUnit_Framework_Exception + * + * @since Method available since Release 5.2.0 + */ + public function expectExceptionMessageRegExp($messageRegExp) + { + if (!is_string($messageRegExp)) { + throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string'); + } + + $this->expectedExceptionMessageRegExp = $messageRegExp; + } + /** * @param mixed $exceptionName * @param string $exceptionMessageRegExp * @param int $exceptionCode * - * @since Method available since Release 4.3.0 + * @since Method available since Release 4.3.0 + * @deprecated Method deprecated since Release 5.6.0 */ public function setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp = '', $exceptionCode = null) { diff --git a/tests/Framework/TestCaseTest.php b/tests/Framework/TestCaseTest.php index 913327865b1..02ba0e5222f 100644 --- a/tests/Framework/TestCaseTest.php +++ b/tests/Framework/TestCaseTest.php @@ -195,6 +195,17 @@ public function testException() $this->assertTrue($result->wasSuccessful()); } + public function testExceptionBackport() + { + $test = new ThrowExceptionTestCase('test'); + $test->expectException('RuntimeException'); + + $result = $test->run(); + + $this->assertEquals(1, count($result)); + $this->assertTrue($result->wasSuccessful()); + } + public function testExceptionWithMessage() { $test = new ThrowExceptionTestCase('test'); @@ -206,6 +217,18 @@ public function testExceptionWithMessage() $this->assertTrue($result->wasSuccessful()); } + public function testExceptionWithMessageBackport() + { + $test = new ThrowExceptionTestCase('test'); + $test->expectException('RuntimeException'); + $test->expectExceptionMessage('A runtime error occurred'); + + $result = $test->run(); + + $this->assertEquals(1, count($result)); + $this->assertTrue($result->wasSuccessful()); + } + public function testExceptionWithWrongMessage() { $test = new ThrowExceptionTestCase('test'); @@ -221,6 +244,22 @@ public function testExceptionWithWrongMessage() ); } + public function testExceptionWithWrongMessageBackport() + { + $test = new ThrowExceptionTestCase('test'); + $test->expectException('RuntimeException'); + $test->expectExceptionMessage('A logic error occurred'); + + $result = $test->run(); + + $this->assertEquals(1, $result->failureCount()); + $this->assertEquals(1, count($result)); + $this->assertEquals( + "Failed asserting that exception message 'A runtime error occurred' contains 'A logic error occurred'.", + $test->getStatusMessage() + ); + } + public function testExceptionWithRegexpMessage() { $test = new ThrowExceptionTestCase('test'); @@ -263,6 +302,39 @@ public function testExceptionWithInvalidRegexpMessage() ); } + public function testExceptionWithWrongRegexpMessageBackport() + { + $test = new ThrowExceptionTestCase('test'); + $test->expectException('RuntimeException'); + $test->expectExceptionMessageRegExp('/logic .*? occurred/'); + + $result = $test->run(); + + $this->assertEquals(1, $result->failureCount()); + $this->assertEquals(1, count($result)); + $this->assertEquals( + "Failed asserting that exception message 'A runtime error occurred' matches '/logic .*? occurred/'.", + $test->getStatusMessage() + ); + } + + /** + * @covers PHPUnit_Framework_Constraint_ExceptionMessageRegExp + */ + public function testExceptionWithInvalidRegexpMessageBackport() + { + $test = new ThrowExceptionTestCase('test'); + $test->expectException('RuntimeException'); + $test->expectExceptionMessageRegExp('#runtime .*? occurred/'); + + $test->run(); + + $this->assertEquals( + "Invalid expected exception message regex given: '#runtime .*? occurred/'", + $test->getStatusMessage() + ); + } + public function testNoException() { $test = new ThrowNoExceptionTestCase('test');