cvs: pear /PHPUnit2/Extensions/MockObject Mock.php
| From: | Sebastian Bergmann | Date: | Fri, 17 Feb 2006 16:02:37 +0000 |
| Subject: | cvs: pear /PHPUnit2/Extensions/MockObject Mock.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-38329@lists.php.net to get a copy of this message | ||
sebastian Fri Feb 17 16:02:37 2006 UTC
Modified files:
/pear/PHPUnit2/Extensions/MockObject Mock.php
Log:
Handle __construct(), __destruct(), and __clone() methods uniformly. Call verify() in
__destruct(). Add type hints.
http://cvs.php.net/viewcvs.cgi/pear/PHPUnit2/Extensions/MockObject/Mock.php?r1=1.12&r2=1.13&diff_format=u
Index: pear/PHPUnit2/Extensions/MockObject/Mock.php
diff -u pear/PHPUnit2/Extensions/MockObject/Mock.php:1.12
pear/PHPUnit2/Extensions/MockObject/Mock.php:1.13
--- pear/PHPUnit2/Extensions/MockObject/Mock.php:1.12 Mon Jan 23 18:10:02 2006
+++ pear/PHPUnit2/Extensions/MockObject/Mock.php Fri Feb 17 16:02:37 2006
@@ -42,7 +42,7 @@
* Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright 2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.opensource.org/licenses/bsd-license.php
BSD License
- * @version CVS: $Id: Mock.php,v 1.12 2006/01/23 18:10:02 sebastian Exp $
+ * @version CVS: $Id: Mock.php,v 1.13 2006/02/17 16:02:37 sebastian Exp $
* @link http://pear.php.net/package/PHPUnit2
* @since File available since Release 3.0.0
*/
@@ -146,7 +146,7 @@
}
}
- protected function generateClassDefinition($class)
+ protected function generateClassDefinition(ReflectionClass $class)
{
if ($class->isInterface()) {
$code = 'interface ';
@@ -178,7 +178,7 @@
return $code;
}
- protected function canMockMethod($method)
+ protected function canMockMethod(ReflectionMethod $method)
{
if ($method->isConstructor() || $method->isDestructor()) {
return FALSE;
@@ -191,12 +191,12 @@
return TRUE;
}
- protected function shouldMockMethod($method)
+ protected function shouldMockMethod(ReflectionMethod $method)
{
return in_array($method->getName(), $this->methods);
}
- protected function generateMethodDefinition($class, $method)
+ protected function generateMethodDefinition(ReflectionMethod $method)
{
$code = "\n ";
@@ -234,19 +234,42 @@
$method->getName(),
$this->generateMethodParameters($method),
- var_export($class->getName(), TRUE),
+ var_export($method->getClass()->getName(), TRUE),
var_export($method->getName(), TRUE)
);
return $code;
}
- protected function generateMockApi($class)
+ protected function generateMockApi(ReflectionClass $class)
+ {
+ return sprintf(
+ " private \$invocationMocker;\n\n" .
+ "%s" .
+ "%s" .
+ "%s" .
+ " public function getInvocationMocker() {\n" .
+ " return \$this->invocationMocker;\n" .
+ " }\n\n" .
+ " public function expects(PHPUnit2_Extensions_MockObject_Matcher_Invocation
\$matcher) {\n" .
+ " return \$this->invocationMocker->expects(\$matcher);\n" .
+ " }\n\n" .
+ " public function verify() {\n" .
+ " return \$this->invocationMocker->verify();\n" .
+ " }\n",
+
+ $this->generateConstructorCode($class),
+ $this->generateDestructorCode($class),
+ $this->generateCloneCode($class)
+ );
+ }
+
+ protected function generateConstructorCode(ReflectionClass $class)
{
$constructor = $class->getConstructor();
-
+
if ($constructor) {
- $constructorCode = sprintf(
+ return sprintf(
" public function __construct(%s) {\n" .
" \$this->invocationMocker = new
PHPUnit2_Extensions_MockObject_InvocationMocker(\$this);\n" .
" parent::%s(%s);\n" .
@@ -257,34 +280,38 @@
$this->generateMethodParameters($constructor, TRUE)
);
} else {
- $constructorCode =
- " public function __construct() {\n" .
- " \$this->invocationMocker = new
PHPUnit2_Extensions_MockObject_InvocationMocker(\$this);\n" .
- " }\n\n";
+ return " public function __construct() {\n" .
+ " \$this->invocationMocker = new
PHPUnit2_Extensions_MockObject_InvocationMocker(\$this);\n" .
+ " }\n\n";
}
+ }
- return sprintf(
- " private \$invocationMocker;\n\n" .
- "%s" .
- " public function __destruct() {\n" .
- " //\$this->invocationMocker->verify();\n" .
- " }\n\n" .
- " public function __clone() {\n" .
- " \$this->invocationMocker = clone \$this->invocationMocker;\n" .
- " parent::__clone();\n" .
- " }\n\n" .
- " public function getInvocationMocker() {\n" .
- " return \$this->invocationMocker;\n" .
- " }\n\n" .
- " public function expects(PHPUnit2_Extensions_MockObject_Matcher_Invocation
\$matcher) {\n" .
- " return \$this->invocationMocker->expects(\$matcher);\n" .
- " }\n\n" .
- " public function verify() {\n" .
- " return \$this->invocationMocker->verify();\n" .
- " }\n",
+ protected function generateDestructorCode(ReflectionClass $class)
+ {
+ if ($class->hasMethod('__destruct')) {
+ return " public function __destruct() {\n" .
+ " parent::__destruct();" .
+ " \$this->invocationMocker->verify();\n" .
+ " }\n\n";
+ } else {
+ return " public function __destruct() {\n" .
+ " \$this->invocationMocker->verify();\n" .
+ " }\n\n";
+ }
+ }
- $constructorCode
- );
+ protected function generateCloneCode(ReflectionClass $class)
+ {
+ if ($class->hasMethod('__clone')) {
+ return " public function __clone() {\n" .
+ " \$this->invocationMocker = clone
\$this->invocationMocker;\n" .
+ " parent::__clone();\n" .
+ " }\n\n";
+ } else {
+ return " public function __clone() {\n" .
+ " \$this->invocationMocker = clone
\$this->invocationMocker;\n" .
+ " }\n\n";
+ }
}
protected function generateMethodParameters($method, $asCall = FALSE)