PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/tests/phpunit/PHPUnit/Framework/MockObject/Mock.php

http://github.com/frapi/frapi
PHP | 445 lines | 361 code | 4 blank | 80 comment | 2 complexity | a4792092bbbc9d07b1ed05edfa970820 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category Testing
  38. * @package PHPUnit
  39. * @author Jan Borsodi <jb@ez.no>
  40. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @version SVN: $Id: Mock.php 4404 2008-12-31 09:27:18Z sb $
  44. * @link http://www.phpunit.de/
  45. * @since File available since Release 3.0.0
  46. */
  47. require_once 'PHPUnit/Util/Class.php';
  48. require_once 'PHPUnit/Util/Filter.php';
  49. require_once 'PHPUnit/Framework/MockObject/Matcher.php';
  50. require_once 'PHPUnit/Framework/MockObject/Invocation.php';
  51. require_once 'PHPUnit/Framework/MockObject/MockObject.php';
  52. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  53. /**
  54. * Provides generation of mock classes and objects from existing classes.
  55. *
  56. * The mocked class will contain all the methods of the original class but with
  57. * a different implementation which will call the current
  58. * PHPUnit_Framework_MockObject_InvocationMocker object, this objects takes
  59. * care of checking expectations and stubs.
  60. * It is also possible to define which methods are mocked by passing an array
  61. * of method names.
  62. *
  63. * The simplest way to define a mock object is to do:
  64. *
  65. * <code>
  66. * PHPUnit_Framework_MockObject_Mock::generate('MyClass');
  67. * $o = new Mock_MyClass;
  68. * </code>
  69. *
  70. * The generate() method returns an object which can be queried.
  71. *
  72. * <code>
  73. * $m = PHPUnit_Framework_MockObject::generate('MyClass');
  74. * $o = new $m->mockClassName;
  75. * print "original class was: . $m->className;
  76. * </code>
  77. *
  78. * @category Testing
  79. * @package PHPUnit
  80. * @author Jan Borsodi <jb@ez.no>
  81. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  82. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  83. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  84. * @version Release: @package_version@
  85. * @link http://www.phpunit.de/
  86. * @since Class available since Release 3.0.0
  87. */
  88. class PHPUnit_Framework_MockObject_Mock
  89. {
  90. public $mockClassName;
  91. public $className;
  92. public $fullClassName;
  93. public $namespaceName;
  94. public $methods;
  95. protected $callOriginalConstructor;
  96. protected $callOriginalClone;
  97. protected $callAutoload;
  98. protected static $cache = array();
  99. public function __construct($className, $methods = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
  100. {
  101. $classNameParts = explode('\\', $className);
  102. if (count($classNameParts) > 1) {
  103. $className = array_pop($classNameParts);
  104. $namespaceName = join('\\', $classNameParts);
  105. $this->fullClassName = $namespaceName . '\\' . $className;
  106. } else {
  107. $namespaceName = '';
  108. $this->fullClassName = $className;
  109. }
  110. if ($mockClassName === '') {
  111. do {
  112. $mockClassName = 'Mock_' . $className . '_' . substr(md5(microtime()), 0, 8);
  113. }
  114. while (class_exists($mockClassName, FALSE));
  115. }
  116. else if (class_exists($mockClassName, FALSE)) {
  117. throw new RuntimeException(
  118. sprintf(
  119. 'Class "%s" already exists.',
  120. $mockClassName
  121. )
  122. );
  123. }
  124. $isClass = class_exists($className, $callAutoload);
  125. $isInterface = interface_exists($className, $callAutoload);
  126. if (is_array($methods) && empty($methods) && ($isClass || $isInterface)) {
  127. $methods = get_class_methods($className);
  128. }
  129. if ($isInterface) {
  130. $callOriginalConstructor = FALSE;
  131. }
  132. $this->mockClassName = $mockClassName;
  133. $this->className = $className;
  134. $this->namespaceName = $namespaceName;
  135. $this->methods = $methods;
  136. $this->callOriginalConstructor = $callOriginalConstructor;
  137. $this->callOriginalClone = $callOriginalClone;
  138. $this->callAutoload = $callAutoload;
  139. }
  140. public static function generate($className, $methods = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE)
  141. {
  142. if ($mockClassName == '') {
  143. $key = md5(
  144. $className .
  145. serialize($methods) .
  146. serialize($callOriginalConstructor) .
  147. serialize($callOriginalClone)
  148. );
  149. if (!isset(self::$cache[$key])) {
  150. self::$cache[$key] = self::generateMock(
  151. $className,
  152. $methods,
  153. $mockClassName,
  154. $callOriginalConstructor,
  155. $callOriginalClone,
  156. $callAutoload
  157. );
  158. }
  159. return self::$cache[$key];
  160. }
  161. return self::generateMock(
  162. $className,
  163. $methods,
  164. $mockClassName,
  165. $callOriginalConstructor,
  166. $callOriginalClone,
  167. $callAutoload
  168. );
  169. }
  170. protected static function generateMock($className, $methods, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload)
  171. {
  172. $mock = new PHPUnit_Framework_MockObject_Mock(
  173. $className,
  174. $methods,
  175. $mockClassName,
  176. $callOriginalConstructor,
  177. $callOriginalClone,
  178. $callAutoload
  179. );
  180. $mock->generateClass();
  181. return $mock;
  182. }
  183. protected function generateClass()
  184. {
  185. if (!class_exists($this->fullClassName, $this->callAutoload) && !interface_exists($this->fullClassName, $this->callAutoload)) {
  186. $code = 'class ' . $this->className . ' {}';
  187. if (!empty($this->namespaceName)) {
  188. $code = 'namespace ' . $this->namespaceName . ';' . $code;
  189. }
  190. eval($code);
  191. }
  192. try {
  193. $class = new ReflectionClass($this->fullClassName);
  194. if ($class->isFinal()) {
  195. throw new RuntimeException(
  196. sprintf(
  197. 'Class "%s" is declared "final" and cannot be mocked.',
  198. $this->fullClassName
  199. )
  200. );
  201. }
  202. $code = $this->generateClassDefinition($class);
  203. eval($code);
  204. }
  205. catch (Exception $e) {
  206. throw new RuntimeException(
  207. sprintf(
  208. 'Failed to generate mock class "%s" for class "%s".\n%s',
  209. $this->mockClassName,
  210. $this->fullClassName,
  211. $e->getMessage()
  212. )
  213. );
  214. }
  215. }
  216. protected function generateClassDefinition(ReflectionClass $class)
  217. {
  218. $code = 'class ';
  219. if ($class->isInterface()) {
  220. $code .= sprintf(
  221. "%s implements %s%s {\n",
  222. $this->mockClassName,
  223. !empty($this->namespaceName) ? $this->namespaceName . '\\' : '',
  224. $this->className
  225. );
  226. } else {
  227. $code .= sprintf(
  228. "%s extends %s%s {\n",
  229. $this->mockClassName,
  230. !empty($this->namespaceName) ? $this->namespaceName . '\\' : '',
  231. $this->className
  232. );
  233. }
  234. $code .= $this->generateMockApi($class);
  235. if (is_array($this->methods)) {
  236. foreach ($this->methods as $methodName) {
  237. try {
  238. $method = $class->getMethod($methodName);
  239. if ($this->canMockMethod($method)) {
  240. $code .= $this->generateMethodDefinitionFromExisting($method);
  241. }
  242. }
  243. catch (ReflectionException $e) {
  244. $code .= $this->generateMethodDefinition($class->getName(), $methodName, 'public');
  245. }
  246. }
  247. }
  248. $code .= "}\n";
  249. return $code;
  250. }
  251. protected function canMockMethod(ReflectionMethod $method)
  252. {
  253. $className = $method->getDeclaringClass()->getName();
  254. $methodName = $method->getName();
  255. if ($method->isFinal() || $method->isStatic() ||
  256. $methodName == '__construct' || $methodName == $className ||
  257. $methodName == '__destruct' || $method->getName() == '__clone') {
  258. return FALSE;
  259. }
  260. return TRUE;
  261. }
  262. protected function generateMethodDefinitionFromExisting(ReflectionMethod $method)
  263. {
  264. if ($method->isPrivate()) {
  265. $modifier = 'private';
  266. }
  267. else if ($method->isProtected()) {
  268. $modifier = 'protected';
  269. }
  270. else {
  271. $modifier = 'public';
  272. }
  273. if ($method->returnsReference()) {
  274. $reference = '&';
  275. } else {
  276. $reference = '';
  277. }
  278. return $this->generateMethodDefinition(
  279. $method->getDeclaringClass()->getName(),
  280. $method->getName(),
  281. $modifier,
  282. $reference,
  283. PHPUnit_Util_Class::getMethodParameters($method)
  284. );
  285. }
  286. protected function generateMethodDefinition($className, $methodName, $modifier, $reference = '', $parameters = '')
  287. {
  288. return sprintf(
  289. "\n %s function %s%s(%s) {\n" .
  290. " \$args = func_get_args();\n" .
  291. " \$result = \$this->invocationMocker->invoke(\n" .
  292. " new PHPUnit_Framework_MockObject_Invocation(\$this, \"%s\", \"%s\", \$args)\n" .
  293. " );\n\n" .
  294. " return \$result;\n" .
  295. " }\n",
  296. $modifier,
  297. $reference,
  298. $methodName,
  299. $parameters,
  300. $className,
  301. $methodName
  302. );
  303. }
  304. protected function generateMockApi(ReflectionClass $class)
  305. {
  306. if ($this->callOriginalConstructor) {
  307. $constructorCode = $this->generateConstructorCodeWithParentCall($class);
  308. } else {
  309. $constructorCode = $this->generateConstructorCode($class);
  310. }
  311. if ($this->callOriginalClone && $class->hasMethod('__clone')) {
  312. $cloneCode = $this->generateCloneCodeWithParentCall();
  313. } else {
  314. $cloneCode = $this->generateCloneCode();
  315. }
  316. return sprintf(
  317. " private \$invocationMocker;\n\n" .
  318. "%s" .
  319. "%s" .
  320. " public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation \$matcher) {\n" .
  321. " return \$this->invocationMocker->expects(\$matcher);\n" .
  322. " }\n\n" .
  323. " public function __phpunit_getInvocationMocker() {\n" .
  324. " return \$this->invocationMocker;\n" .
  325. " }\n\n" .
  326. " public function __phpunit_verify() {\n" .
  327. " \$this->invocationMocker->verify();\n" .
  328. " }\n",
  329. $constructorCode,
  330. $cloneCode
  331. );
  332. }
  333. protected function generateConstructorCode(ReflectionClass $class)
  334. {
  335. $arguments = '';
  336. $constructor = $class->getConstructor();
  337. if ($constructor !== NULL) {
  338. $constructorName = $constructor->getName();
  339. foreach (PHPUnit_Util_Class::getHierarchy($class->getName(), TRUE) as $_class) {
  340. foreach ($_class->getInterfaces() as $interface) {
  341. if ($interface->hasMethod($constructorName)) {
  342. $arguments = PHPUnit_Util_Class::getMethodParameters($constructor);
  343. break 2;
  344. }
  345. }
  346. }
  347. }
  348. return sprintf(
  349. " public function __construct(%s) {\n" .
  350. " \$this->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker(\$this);\n" .
  351. " }\n\n",
  352. $arguments
  353. );
  354. }
  355. protected function generateConstructorCodeWithParentCall(ReflectionClass $class)
  356. {
  357. $constructor = $class->getConstructor();
  358. if ($constructor !== NULL) {
  359. return sprintf(
  360. " public function __construct(%s) {\n" .
  361. " \$args = func_get_args();\n" .
  362. " \$this->invocationMocker = new PHPUnit_Framework_MockObject_InvocationMocker;\n" .
  363. " \$class = new ReflectionClass('%s');\n" .
  364. " \$class->getParentClass()->getConstructor()->invokeArgs(\$this, \$args);\n" .
  365. " }\n\n",
  366. PHPUnit_Util_Class::getMethodParameters($constructor),
  367. $this->mockClassName
  368. );
  369. } else {
  370. return $this->generateConstructorCode($class);
  371. }
  372. }
  373. protected function generateCloneCode()
  374. {
  375. return " public function __clone() {\n" .
  376. " \$this->invocationMocker = clone \$this->invocationMocker;\n" .
  377. " }\n\n";
  378. }
  379. protected function generateCloneCodeWithParentCall()
  380. {
  381. return " public function __clone() {\n" .
  382. " \$this->invocationMocker = clone \$this->invocationMocker;\n" .
  383. " parent::__clone();\n" .
  384. " }\n\n";
  385. }
  386. }
  387. ?>