PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Testing » PHPUnit2 » Bug #6669

Unintuitive behavior in ExceptionTestCase

Details

Request #6669Unintuitive behavior in ExceptionTestCase
Submitted2006-02-02 14:04 UTC
Fromjohan dot forsberg at ongame dot com
StatusClosed
PackagePHPUnit2
PHP VersionIrrelevant
Roadmaps(Not assigned)

Comments

[2006-02-02 14:04 UTC] johan dot forsberg at ongame dot com

Description:
------------
Extensions/ExceptionTestCase.php:

The default expected exception is simply "Exception". This leads to the test below passing when it should fail.

Why? Because setExpectedException throws an exception, which is caught in runTest and matches the default expected exception "Exception".

Related RFE: Please allow setExpectedException(false) to stop expecting exceptions for a part of a test!

Test script:
---------------
public function testSomething()
{
$this->setExpectedException('MipselledException');
return;
}
--
Suggested patch:
class PHPUnit2_Extensions_InvalidExceptionException extends Exception {}
public function setExpectedException($exceptionName) {
...
throw new PHPUnit2_Extensions_InvalidExceptionException ('Undefined exception '.$exceptionName);

}
protected function runTest() {
...
} catch (PHPUnit2_Extensions_InvalidExceptionException $e) {
throw $e;
}

Expected result:
----------------
Test should fail.

Actual result:
--------------
Test passes

[2006-02-02 15:12 UTC] sebastian at php dot net

Please test the patch below:

Index: Extensions/ExceptionTestCase.php
===================================================================
RCS file: /repository/pear/PHPUnit2/Extensions/ExceptionTestCase.php,v
retrieving revision 1.22
diff -u -b -B -r1.22 ExceptionTestCase.php
--- Extensions/ExceptionTestCase.php 4 Jan 2006 07:04:51 -0000 1.22
+++ Extensions/ExceptionTestCase.php 2 Feb 2006 15:11:22 -0000
@@ -74,10 +74,10 @@
/**
* The name of the expected Exception.
*
- * @var string
+ * @var mixed
* @access private
*/
- private $expectedException = 'Exception';
+ private $expectedException = NULL;

/**
* @return string
@@ -90,17 +90,15 @@
}

/**
- * @param string $exceptionName
+ * @param mixed $exceptionName
* @throws InvalidArgumentException
* @access public
* @since Method available since Release 2.2.0
*/
public function setExpectedException($exceptionName)
{
- if (is_string($exceptionName) && class_exists($exceptionName)) {
+ if ((is_string($exceptionName) && class_exists($exceptionName)) || $exceptionName === NULL) {
$this->expectedException = $exceptionName;
- } else {
- throw new InvalidArgumentException;
}
}

@@ -109,6 +107,7 @@
*/
protected function runTest()
{
+ if ($this->expectedException !== NULL) {
try {
parent::runTest();
}
@@ -122,6 +121,9 @@
}

$this->fail('Expected exception ' . $this->expectedException);
+ } else {
+ parent::runTest();
+ }
}
}

[2006-02-08 08:21 UTC] sebastian at php dot net

This bug has been fixed in CVS.

If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET).

If this was a problem with the pear.php.net website, the change should be live shortly.

Otherwise, the fix will appear in the package's next release.

Thank you for the report and for helping us make PEAR better.