added support for setExpectedException()
This commit is contained in:
parent
64a5702a48
commit
2341bf20ea
@ -187,7 +187,7 @@
|
|||||||
$method->getDeclaringClass()->getName() !== __CLASS__ &&
|
$method->getDeclaringClass()->getName() !== __CLASS__ &&
|
||||||
preg_match('/^[\/\*\s]*@test\s*(?:\*\/)?$/m', $method->getDocComment())
|
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);
|
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
|
* Wrapper for interacting with the mock object framework
|
||||||
*
|
*
|
||||||
|
@ -38,6 +38,13 @@
|
|||||||
*/
|
*/
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the expected exception
|
||||||
|
*
|
||||||
|
* @string
|
||||||
|
*/
|
||||||
|
protected $expectedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
@ -50,9 +57,10 @@
|
|||||||
* @param bool $autoVerify
|
* @param bool $autoVerify
|
||||||
*/
|
*/
|
||||||
public function __construct($closure, $name, $autoVerify) {
|
public function __construct($closure, $name, $autoVerify) {
|
||||||
$this->closure = $closure;
|
$this->closure = $closure;
|
||||||
$this->autoVerify = (bool)$autoVerify;
|
$this->autoVerify = (bool)$autoVerify;
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
$this->expectedException = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,6 +76,19 @@
|
|||||||
return $this->name;
|
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
|
* 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) {
|
foreach ($listeners as $listener) {
|
||||||
$listener->onTestMethodPassed($this);
|
$listener->onTestMethodPassed($this);
|
||||||
}
|
}
|
||||||
} catch (TestFailure $failure) {
|
} catch (TestFailure $failure) {
|
||||||
$this->handleTestFailure($failure, $listeners);
|
$this->handleTestFailure($failure, $listeners);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
//test for expected exception
|
if ($this->expectedException !== null) {
|
||||||
foreach ($listeners as $listener) {
|
if ($e instanceof $this->expectedException) {
|
||||||
$listener->onTestMethodErred($this);
|
//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);
|
$result = $this->createTestResult($failure);
|
||||||
|
Loading…
Reference in New Issue
Block a user