Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
72 changes: 72 additions & 0 deletions tests/Framework/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down