added MockRegistry
This commit is contained in:
parent
74df24b028
commit
a901f154c2
@ -11,7 +11,7 @@
|
||||
public function registerInvocation(MockInvocation $invocation) {
|
||||
$this->invocations[] = $invocation;
|
||||
|
||||
foreach (MockObjectCreator::getExpectations($invocation->getClass()) as $expectation) {
|
||||
foreach (MockRegistry::getExpectations($invocation->getClass()) as $expectation) {
|
||||
if ($expectation->matchesInvocation($invocation)) {
|
||||
return $expectation;
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
public function expectsMethod($methodName) {
|
||||
$expectation = new InvocationExpectation($methodName);
|
||||
MockObjectCreator::addExpectation($this->className, $expectation);
|
||||
MockRegistry::addExpectation($this->className, $expectation);
|
||||
return $expectation;
|
||||
}
|
||||
|
||||
|
@ -33,56 +33,6 @@
|
||||
);
|
||||
}
|
||||
|
||||
public static function resetTrackers() {
|
||||
self::$registry = array();
|
||||
self::$expectations = array();
|
||||
}
|
||||
|
||||
public static function addExpectation($className, InvocationExpectation $expectation) {
|
||||
if (!isset(self::$expectations[$className])) {
|
||||
throw new LogicException('Unable to add invocation expectation because the object does not exist in the registry');
|
||||
}
|
||||
|
||||
self::$expectations[$className][] = $expectation;
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
return self::$expectations[$className];
|
||||
}
|
||||
|
||||
public static function registerInvocation($className, $methodName, array $args) {
|
||||
if (!isset(self::$registry[$className])) {
|
||||
throw new LogicException('Unable to register invocation because the object does not exist in the registry');
|
||||
}
|
||||
|
||||
$count = self::getInvocationCount($className, $methodName);
|
||||
$expectation = self::$registry[$className]->registerInvocation(new MockInvocation($className, $methodName, $args, $count));
|
||||
return $expectation;
|
||||
}
|
||||
|
||||
public static function getTracker($name) {
|
||||
if (!isset(self::$registry[$name])) {
|
||||
throw new LogicException('Unable to retrieve invocation tracker because the object does not exist in the registry');
|
||||
}
|
||||
|
||||
return self::$registry[$name];
|
||||
}
|
||||
|
||||
private static function getInvocationCount($className, $methodName) {
|
||||
$count = 1;
|
||||
foreach (self::$registry[$className]->getInvocations() as $invocation) {
|
||||
if ($invocation->getMethod() === $methodName) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
public function addMethod($methodName, $callParent = false, $body = '') {
|
||||
$methodType = 'generic';
|
||||
if ($this->referenceObject->hasMethod($methodName)) {
|
||||
@ -126,10 +76,8 @@
|
||||
|
||||
$code = $this->generateClassDefinition($name);
|
||||
eval($code);
|
||||
//print_r($code); exit;
|
||||
|
||||
self::$registry[$name] = new InvocationTracker();
|
||||
self::$expectations[$name] = array();
|
||||
MockRegistry::addClass($name);
|
||||
|
||||
$obj = new ReflectionClass($name);
|
||||
$obj = $obj->newInstanceArgs($constructorArgs);
|
||||
@ -174,7 +122,7 @@
|
||||
$code = <<<CODE
|
||||
$modifier function $name($params) {
|
||||
$temp1 = func_get_args();
|
||||
$temp2 = MockObjectCreator::registerInvocation(get_class(\$this), __FUNCTION__, $temp1);
|
||||
$temp2 = MockRegistry::registerInvocation(get_class(\$this), __FUNCTION__, $temp1);
|
||||
if ($temp2 instanceof InvocationExpectation) {
|
||||
//this invocation matched an invocation expectation
|
||||
|
67
src/TUnit/framework/mock/MockRegistry.php
Normal file
67
src/TUnit/framework/mock/MockRegistry.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
class MockRegistry {
|
||||
|
||||
private static $trackers = array();
|
||||
private static $expectations = array();
|
||||
|
||||
private function __construct() {}
|
||||
|
||||
public static function reset() {
|
||||
self::$trackers = array();
|
||||
self::$expectations = array();
|
||||
}
|
||||
|
||||
public static function addClass($className) {
|
||||
self::$trackers[$className] = new InvocationTracker();
|
||||
self::$expectations[$className] = array();
|
||||
}
|
||||
|
||||
public static function addExpectation($className, InvocationExpectation $expectation) {
|
||||
if (!isset(self::$expectations[$className])) {
|
||||
throw new LogicException('Unable to add invocation expectation because the object does not exist in the registry');
|
||||
}
|
||||
|
||||
self::$expectations[$className][] = $expectation;
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
return self::$expectations[$className];
|
||||
}
|
||||
|
||||
public static function registerInvocation($className, $methodName, array $args) {
|
||||
if (!isset(self::$trackers[$className])) {
|
||||
throw new LogicException('Unable to register invocation because the object does not exist in the registry');
|
||||
}
|
||||
|
||||
$count = self::getInvocationCount($className, $methodName);
|
||||
$expectation = self::$trackers[$className]->registerInvocation(new MockInvocation($className, $methodName, $args, $count));
|
||||
return $expectation;
|
||||
}
|
||||
|
||||
public static function getTracker($name) {
|
||||
if (!isset(self::$trackers[$name])) {
|
||||
throw new LogicException('Unable to retrieve invocation tracker because the object does not exist in the registry');
|
||||
}
|
||||
|
||||
return self::$trackers[$name];
|
||||
}
|
||||
|
||||
private static function getInvocationCount($className, $methodName) {
|
||||
$count = 1;
|
||||
foreach (self::$trackers[$className]->getInvocations() as $invocation) {
|
||||
if ($invocation->getMethod() === $methodName) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -3,11 +3,11 @@
|
||||
/**
|
||||
* Autoload manifest
|
||||
*
|
||||
* Autogenerated by manifester.php on 2009-06-16 22:58:05
|
||||
* Autogenerated by manifester.php on 2009-06-17 00:17:16
|
||||
*
|
||||
* @package TUnit
|
||||
* @version 0.1.0
|
||||
* @since 0.1.0
|
||||
* @version 0.3.0
|
||||
* @since 0.3.0
|
||||
*/
|
||||
|
||||
return array(
|
||||
@ -35,8 +35,9 @@
|
||||
'IssetConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
|
||||
'MockHandler' => 'TUnit/framework/mock/MockHandler.php',
|
||||
'MockInvocation' => 'TUnit/framework/mock/MockInvocation.php',
|
||||
'MockObject' => 'TUnit/framework/mock/MockObject.php',
|
||||
'MockObjectCreator' => 'TUnit/framework/mock/MockObject.php',
|
||||
'MockObject' => 'TUnit/framework/mock/MockObjectCreator.php',
|
||||
'MockObjectCreator' => 'TUnit/framework/mock/MockObjectCreator.php',
|
||||
'MockRegistry' => 'TUnit/framework/mock/MockRegistry.php',
|
||||
'NotConstraint' => 'TUnit/framework/constraints/NotConstraint.php',
|
||||
'NullConstraint' => 'TUnit/framework/constraints/SimpleConstraints.php',
|
||||
'PassedTestResult' => 'TUnit/framework/result/PassedTestResult.php',
|
||||
|
Loading…
Reference in New Issue
Block a user