Skip to content

Commit

Permalink
Improve upon e208250 and provide better failure messages for expectEx…
Browse files Browse the repository at this point in the history
…ceptionMessage() etc. when expectException() is not used
  • Loading branch information
sebastianbergmann committed Oct 13, 2017
1 parent e0bc901 commit 6727744
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/Framework/TestCase.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand Down Expand Up @@ -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(
Expand All @@ -1117,7 +1111,7 @@ protected function runTest()
);
}

if (!empty($this->expectedExceptionMessageRegExp)) {
if ($this->expectedExceptionMessageRegExp !== null) {
$this->assertThat(
$e,
new ExceptionMessageRegularExpression(
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6727744

Please sign in to comment.