Skip to content

Commit 7752bd4

Browse files
authored
Merge pull request #5 from php5friends/development
Backport PHPUnit_Framework_TestCase::expectException()
2 parents bde07dc + d8c8364 commit 7752bd4

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

src/Framework/TestCase.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ public function getExpectedException()
457457
* @param string $exceptionMessage
458458
* @param int $exceptionCode
459459
*
460-
* @since Method available since Release 3.2.0
460+
* @since Method available since Release 3.2.0
461+
* @deprecated Method deprecated since Release 5.2.0
461462
*/
462463
public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null)
463464
{
@@ -466,12 +467,75 @@ public function setExpectedException($exceptionName, $exceptionMessage = '', $ex
466467
$this->expectedExceptionCode = $exceptionCode;
467468
}
468469

470+
/**
471+
* @param string $exception
472+
*
473+
* @since Method available since Release 5.2.0
474+
*/
475+
public function expectException($exception)
476+
{
477+
if (!is_string($exception)) {
478+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
479+
}
480+
481+
$this->expectedException = $exception;
482+
}
483+
484+
/**
485+
* @param int|string $code
486+
*
487+
* @throws PHPUnit_Framework_Exception
488+
*
489+
* @since Method available since Release 5.2.0
490+
*/
491+
public function expectExceptionCode($code)
492+
{
493+
if (!is_int($code) && !is_string($code)) {
494+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'integer or string');
495+
}
496+
497+
$this->expectedExceptionCode = $code;
498+
}
499+
500+
/**
501+
* @param string $message
502+
*
503+
* @throws PHPUnit_Framework_Exception
504+
*
505+
* @since Method available since Release 5.2.0
506+
*/
507+
public function expectExceptionMessage($message)
508+
{
509+
if (!is_string($message)) {
510+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
511+
}
512+
513+
$this->expectedExceptionMessage = $message;
514+
}
515+
516+
/**
517+
* @param string $messageRegExp
518+
*
519+
* @throws PHPUnit_Framework_Exception
520+
*
521+
* @since Method available since Release 5.2.0
522+
*/
523+
public function expectExceptionMessageRegExp($messageRegExp)
524+
{
525+
if (!is_string($messageRegExp)) {
526+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
527+
}
528+
529+
$this->expectedExceptionMessageRegExp = $messageRegExp;
530+
}
531+
469532
/**
470533
* @param mixed $exceptionName
471534
* @param string $exceptionMessageRegExp
472535
* @param int $exceptionCode
473536
*
474-
* @since Method available since Release 4.3.0
537+
* @since Method available since Release 4.3.0
538+
* @deprecated Method deprecated since Release 5.6.0
475539
*/
476540
public function setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp = '', $exceptionCode = null)
477541
{

tests/Framework/TestCaseTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ public function testException()
195195
$this->assertTrue($result->wasSuccessful());
196196
}
197197

198+
public function testExceptionBackport()
199+
{
200+
$test = new ThrowExceptionTestCase('test');
201+
$test->expectException('RuntimeException');
202+
203+
$result = $test->run();
204+
205+
$this->assertEquals(1, count($result));
206+
$this->assertTrue($result->wasSuccessful());
207+
}
208+
198209
public function testExceptionWithMessage()
199210
{
200211
$test = new ThrowExceptionTestCase('test');
@@ -206,6 +217,18 @@ public function testExceptionWithMessage()
206217
$this->assertTrue($result->wasSuccessful());
207218
}
208219

220+
public function testExceptionWithMessageBackport()
221+
{
222+
$test = new ThrowExceptionTestCase('test');
223+
$test->expectException('RuntimeException');
224+
$test->expectExceptionMessage('A runtime error occurred');
225+
226+
$result = $test->run();
227+
228+
$this->assertEquals(1, count($result));
229+
$this->assertTrue($result->wasSuccessful());
230+
}
231+
209232
public function testExceptionWithWrongMessage()
210233
{
211234
$test = new ThrowExceptionTestCase('test');
@@ -221,6 +244,22 @@ public function testExceptionWithWrongMessage()
221244
);
222245
}
223246

247+
public function testExceptionWithWrongMessageBackport()
248+
{
249+
$test = new ThrowExceptionTestCase('test');
250+
$test->expectException('RuntimeException');
251+
$test->expectExceptionMessage('A logic error occurred');
252+
253+
$result = $test->run();
254+
255+
$this->assertEquals(1, $result->failureCount());
256+
$this->assertEquals(1, count($result));
257+
$this->assertEquals(
258+
"Failed asserting that exception message 'A runtime error occurred' contains 'A logic error occurred'.",
259+
$test->getStatusMessage()
260+
);
261+
}
262+
224263
public function testExceptionWithRegexpMessage()
225264
{
226265
$test = new ThrowExceptionTestCase('test');
@@ -263,6 +302,39 @@ public function testExceptionWithInvalidRegexpMessage()
263302
);
264303
}
265304

305+
public function testExceptionWithWrongRegexpMessageBackport()
306+
{
307+
$test = new ThrowExceptionTestCase('test');
308+
$test->expectException('RuntimeException');
309+
$test->expectExceptionMessageRegExp('/logic .*? occurred/');
310+
311+
$result = $test->run();
312+
313+
$this->assertEquals(1, $result->failureCount());
314+
$this->assertEquals(1, count($result));
315+
$this->assertEquals(
316+
"Failed asserting that exception message 'A runtime error occurred' matches '/logic .*? occurred/'.",
317+
$test->getStatusMessage()
318+
);
319+
}
320+
321+
/**
322+
* @covers PHPUnit_Framework_Constraint_ExceptionMessageRegExp
323+
*/
324+
public function testExceptionWithInvalidRegexpMessageBackport()
325+
{
326+
$test = new ThrowExceptionTestCase('test');
327+
$test->expectException('RuntimeException');
328+
$test->expectExceptionMessageRegExp('#runtime .*? occurred/');
329+
330+
$test->run();
331+
332+
$this->assertEquals(
333+
"Invalid expected exception message regex given: '#runtime .*? occurred/'",
334+
$test->getStatusMessage()
335+
);
336+
}
337+
266338
public function testNoException()
267339
{
268340
$test = new ThrowNoExceptionTestCase('test');

0 commit comments

Comments
 (0)