From 2341bf20ea6b13ba9bab9f2ed2b75e2a086bf6b0 Mon Sep 17 00:00:00 2001 From: tmont Date: Thu, 2 Jul 2009 16:58:17 +0000 Subject: [PATCH] added support for setExpectedException() --- src/Testify/framework/test/TestCase.php | 26 +++++++++++- src/Testify/framework/test/TestMethod.php | 50 +++++++++++++++++++---- 2 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/Testify/framework/test/TestCase.php b/src/Testify/framework/test/TestCase.php index 8ea47fb..0ed4605 100644 --- a/src/Testify/framework/test/TestCase.php +++ b/src/Testify/framework/test/TestCase.php @@ -187,7 +187,7 @@ $method->getDeclaringClass()->getName() !== __CLASS__ && preg_match('/^[\/\*\s]*@test\s*(?:\*\/)?$/m', $method->getDocComment()) ) { - $methods[] = new TestMethod(Util::getClosure($this, $method->getName()), get_class($this) . '::' . $method->getName(), $this->getAutoVerify()); + $methods[$method->getName()] = new TestMethod(Util::getClosure($this, $method->getName()), get_class($this) . '::' . $method->getName(), $this->getAutoVerify()); } } @@ -222,6 +222,30 @@ return $creator->generate($args, $name); } + /** + * Sets the exception that is expected to be thrown in this method + * + * @author Tommy Montgomery + * @version 1.0 + * @since 1.0 + * + * @param string $exceptionName The name of the exception that is expected to be thrown + * @throws InvalidArgumentException + */ + protected function setExpectedException($exceptionName) { + if (!is_string($exceptionName)) { + throw new InvalidArgumentException('1st argument must be a string'); + } + + //get name of calling method + $trace = debug_backtrace(); + $method = $trace[1]['function']; + unset($trace); + + $methods = $this->getTestableMethods(); + $methods[$method]->setExpectedException($exceptionName); + } + /** * Wrapper for interacting with the mock object framework * diff --git a/src/Testify/framework/test/TestMethod.php b/src/Testify/framework/test/TestMethod.php index 6a7a9de..6a934d9 100644 --- a/src/Testify/framework/test/TestMethod.php +++ b/src/Testify/framework/test/TestMethod.php @@ -38,6 +38,13 @@ */ protected $name; + /** + * Name of the expected exception + * + * @string + */ + protected $expectedException; + /** * Constructor * @@ -50,9 +57,10 @@ * @param bool $autoVerify */ public function __construct($closure, $name, $autoVerify) { - $this->closure = $closure; - $this->autoVerify = (bool)$autoVerify; - $this->name = $name; + $this->closure = $closure; + $this->autoVerify = (bool)$autoVerify; + $this->name = $name; + $this->expectedException = null; } /** @@ -68,6 +76,19 @@ return $this->name; } + /** + * Sets the expected exception + * + * @author Tommy Montgomery + * @version 1.0 + * @since 1.0 + * + * @param string $exceptionName Name of the exception that is expected to be thrown + */ + public final function setExpectedException($exceptionName) { + $this->expectedException = $exceptionName; + } + /** * Runs the test * @@ -107,18 +128,31 @@ } } + if ($this->expectedException !== null) { + //expected exception was never thrown + throw new FailedTest('Expected exception "' . $this->expectedException . '" to be thrown'); + } + foreach ($listeners as $listener) { $listener->onTestMethodPassed($this); } } catch (TestFailure $failure) { $this->handleTestFailure($failure, $listeners); } catch (Exception $e) { - //test for expected exception - foreach ($listeners as $listener) { - $listener->onTestMethodErred($this); + if ($this->expectedException !== null) { + if ($e instanceof $this->expectedException) { + //we expected this exception + foreach ($listeners as $listener) { + $listener->onTestMethodPassed($this); + } + } + } else { + foreach ($listeners as $listener) { + $listener->onTestMethodErred($this); + } + + $failure = new ErredTest($e->getMessage(), $e); } - - $failure = new ErredTest($e->getMessage(), $e); } $result = $this->createTestResult($failure);