mock verification

This commit is contained in:
tmont 2009-06-19 07:17:52 +00:00
parent a1ac29341b
commit e989cdbb53
8 changed files with 69 additions and 18 deletions

View File

@ -8,6 +8,7 @@
protected $args;
protected $returnValue;
protected $echoString;
protected $verified;
public function __construct($methodName) {
$this->method = $methodName;
@ -15,6 +16,7 @@
$this->args = array();
$this->returnValue = null;
$this->echoString = null;
$this->verified = false;
}
public final function getMethod() {
@ -96,6 +98,14 @@
}
}
public final function isVerified() {
return $this->verified;
}
public final function setVerified($verified) {
$this->verified = (bool)$verified;
}
}
?>

View File

@ -13,6 +13,7 @@
foreach (MockRegistry::getExpectations($invocation->getClass()) as $expectation) {
if ($expectation->matchesInvocation($invocation)) {
$expectation->setVerified(true);
return $expectation;
}
}
@ -24,6 +25,16 @@
return $this->invocations;
}
public function verify() {
foreach (MockRegistry::getAllExpectations() as $expectation) {
if (!$expectation->isVerified()) {
return false;
}
}
return true;
}
}
?>

View File

@ -25,6 +25,10 @@
self::$expectations[$className][] = $expectation;
}
public static function getAllExpectations() {
return Util::arrayFlatten(self::$expectations);
}
public static function getExpectations($className) {
if (!isset(self::$expectations[$className])) {
throw new LogicException('Unable to add invocation expectation because the object does not exist in the registry');
@ -51,6 +55,10 @@
return self::$trackers[$name];
}
public static function getTrackers() {
return self::$trackers;
}
private static function getInvocationCount($className, $methodName) {
$count = 0;
foreach (self::$trackers[$className]->getInvocations() as $invocation) {

View File

@ -1,9 +0,0 @@
<?php
interface Verifiable {
public function verify();
}
?>

View File

@ -3,20 +3,29 @@
class TestCase implements Testable {
protected $name;
private $autoVerify;
const ANY = -1;
const AT_LEAST_ONCE = -2;
protected static $mockStorage = array();
public function __construct($name) {
$this->name = $name;
$this->name = $name;
$this->autoVerify = true;
}
public function getName() {
public final function getName() {
return $this->name;
}
public final function getAutoVerify() {
return $this->autoVerify;
}
public final function setAutoVerify($autoVerify) {
$this->autoVerify = (bool)$autoVerify;
}
public function setUp() {
}

View File

@ -15,9 +15,6 @@
}
public function run(array $listeners) {
//reset mock object registry
MockRegistry::reset();
foreach ($listeners as $listener) {
$listener->beforeTestMethod($this);
}
@ -28,6 +25,16 @@
$this->testCase->setUp();
try {
$this->method->invoke($this->testCase);
//verify if necessary
if ($this->testCase->getAutoVerify()) {
foreach (MockRegistry::getTrackers() as $tracker) {
if (!$tracker->verify()) {
throw new FailedTest('Verification of InvocationTracker failed');
}
}
}
foreach ($listeners as $listener) {
$listener->onTestMethodPassed($this);
}
@ -49,6 +56,9 @@
$listener->afterTestMethod($this);
}
//reset mock object registry
MockRegistry::reset();
return $result;
}

View File

@ -3,7 +3,7 @@
/**
* Autoload manifest
*
* Autogenerated by manifester.php on 2009-06-17 23:43:51
* Autogenerated by manifester.php on 2009-06-19 00:17:29
*
* @package TUnit
* @version 0.3.0
@ -58,8 +58,7 @@
'Testable' => 'TUnit/framework/test/Testable.php',
'TrueConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
'Usage' => 'TUnit/util/cli.php',
'Util' => 'TUnit/util/Util.php',
'Verifiable' => 'TUnit/framework/mock/Verifiable.php'
'Util' => 'TUnit/util/Util.php'
);
?>

View File

@ -73,6 +73,19 @@
return rtrim($list, ', ');
}
public static function arrayFlatten($arr) {
$flattened = array();
if (is_array($arr)) {
foreach ($arr as $value) {
$flattened = array_merge($flattened, self::arrayFlatten($value));
}
} else {
$flattened[] = $arr;
}
return $flattened;
}
}
?>