From 6727744434bf19016364aef1a49f80b32d045b8b Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Fri, 13 Oct 2017 17:40:53 +0200 Subject: [PATCH] Improve upon e20825098d10596ed69ee39007e4c8579d0e6a47 and provide better failure messages for expectExceptionMessage() etc. when expectException() is not used --- src/Framework/TestCase.php | 57 ++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 732b1313c25..1d157f144fb 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -184,14 +184,14 @@ abstract class TestCase extends Assert implements Test, SelfDescribing * * @var string */ - private $expectedExceptionMessage = ''; + private $expectedExceptionMessage; /** * The regex pattern to validate the expected Exception message. * * @var string */ - private $expectedExceptionMessageRegExp = ''; + private $expectedExceptionMessageRegExp; /** * The code of the expected Exception. @@ -589,10 +589,6 @@ public function expectException($exception) */ public function expectExceptionCode($code) { - if (!$this->expectedException) { - $this->expectedException = \Exception::class; - } - if (!\is_int($code) && !\is_string($code)) { throw InvalidArgumentHelper::factory(1, 'integer or string'); } @@ -607,10 +603,6 @@ public function expectExceptionCode($code) */ public function expectExceptionMessage($message) { - if (!$this->expectedException) { - $this->expectedException = \Exception::class; - } - if (!\is_string($message)) { throw InvalidArgumentHelper::factory(1, 'string'); } @@ -1101,14 +1093,16 @@ protected function runTest() } if ($checkException) { - $this->assertThat( - $e, - new ExceptionConstraint( - $this->expectedException - ) - ); + if ($this->expectedException !== null) { + $this->assertThat( + $e, + new ExceptionConstraint( + $this->expectedException + ) + ); + } - if (!empty($this->expectedExceptionMessage)) { + if ($this->expectedExceptionMessage !== null) { $this->assertThat( $e, new ExceptionMessage( @@ -1117,7 +1111,7 @@ protected function runTest() ); } - if (!empty($this->expectedExceptionMessageRegExp)) { + if ($this->expectedExceptionMessageRegExp !== null) { $this->assertThat( $e, new ExceptionMessageRegularExpression( @@ -1148,6 +1142,33 @@ protected function runTest() $this->expectedException ) ); + } elseif ($this->expectedExceptionMessage !== null) { + $this->numAssertions++; + + throw new AssertionFailedError( + sprintf( + 'Failed asserting that exception with message "%s" is thrown', + $this->expectedExceptionMessage + ) + ); + } elseif ($this->expectedExceptionMessageRegExp !== null) { + $this->numAssertions++; + + throw new AssertionFailedError( + sprintf( + 'Failed asserting that exception with message matching "%s" is thrown', + $this->expectedExceptionMessageRegExp + ) + ); + } elseif ($this->expectedExceptionCode !== null) { + $this->numAssertions++; + + throw new AssertionFailedError( + sprintf( + 'Failed asserting that exception with code "%s" is thrown', + $this->expectedExceptionCode + ) + ); } return $testResult;