PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/iic_db/INC/Vendor/PHPUnit/Util/Skeleton/Test.php

http://iiccms.googlecode.com/
PHP | 348 lines | 225 code | 51 blank | 72 comment | 52 complexity | 6a3968a4de87eb7c9686b7610305b470 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2008, 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 Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: Test.php 3532 2008-08-08 09:15:01Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 3.3.0
  45. */
  46. require_once 'PHPUnit/Util/Filter.php';
  47. require_once 'PHPUnit/Util/Template.php';
  48. require_once 'PHPUnit/Util/Skeleton.php';
  49. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  50. /**
  51. * Generator for test class skeletons from classes.
  52. *
  53. * @category Testing
  54. * @package PHPUnit
  55. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  56. * @copyright 2002-2008 Sebastian Bergmann <sb@sebastian-bergmann.de>
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: 3.3.9
  59. * @link http://www.phpunit.de/
  60. * @since Class available since Release 3.3.0
  61. */
  62. class PHPUnit_Util_Skeleton_Test extends PHPUnit_Util_Skeleton
  63. {
  64. /**
  65. * @var array
  66. */
  67. protected $methodNameCounter = array();
  68. /**
  69. * Constructor.
  70. *
  71. * @param string $inClassName
  72. * @param string $inSourceFile
  73. * @throws RuntimeException
  74. */
  75. public function __construct($inClassName, $inSourceFile = '')
  76. {
  77. $this->inClassName = $inClassName;
  78. $this->outClassName = $inClassName . 'Test';
  79. if (class_exists($inClassName)) {
  80. $this->inSourceFile = '<internal>';
  81. }
  82. else if (empty($inSourceFile) && is_file($inClassName . '.php')) {
  83. $this->inSourceFile = $inClassName . '.php';
  84. }
  85. else if (empty($inSourceFile) ||
  86. is_file(str_replace('_', '/', $inClassName) . '.php')) {
  87. $this->inSourceFile = str_replace('_', '/', $inClassName) . '.php';
  88. $this->outSourceFile = str_replace('_', '/', $inClassName) . 'Test.php';
  89. }
  90. else if (empty($inSourceFile)) {
  91. throw new RuntimeException(
  92. sprintf(
  93. 'Neither "%s.php" nor "%s.php" could be opened.',
  94. $inClassName,
  95. str_replace('_', '/', $inClassName)
  96. )
  97. );
  98. }
  99. else if (!is_file($inSourceFile)) {
  100. throw new RuntimeException(
  101. sprintf(
  102. '"%s" could not be opened.',
  103. $inSourceFile
  104. )
  105. );
  106. } else {
  107. $this->inSourceFile = $inSourceFile;
  108. }
  109. if ($this->inSourceFile != '<internal>') {
  110. include_once $this->inSourceFile;
  111. }
  112. if (!class_exists($inClassName)) {
  113. throw new RuntimeException(
  114. sprintf(
  115. 'Could not find class "%s" in "%s".',
  116. $inClassName,
  117. realpath($this->inSourceFile)
  118. )
  119. );
  120. }
  121. $this->outSourceFile = dirname($this->inSourceFile) . DIRECTORY_SEPARATOR . $this->inClassName . 'Test.php';
  122. }
  123. /**
  124. * Generates the test class' source.
  125. *
  126. * @param boolean $verbose
  127. * @return mixed
  128. */
  129. public function generate($verbose = FALSE)
  130. {
  131. $class = new ReflectionClass($this->inClassName);
  132. $methods = '';
  133. $incompleteMethods = '';
  134. foreach ($class->getMethods() as $method) {
  135. if (!$method->isConstructor() &&
  136. !$method->isAbstract() &&
  137. $method->isPublic() &&
  138. $method->getDeclaringClass()->getName() == $this->inClassName) {
  139. $assertAnnotationFound = FALSE;
  140. if (preg_match_all('/@assert(.*)$/Um', $method->getDocComment(), $annotations)) {
  141. foreach ($annotations[1] as $annotation) {
  142. if (preg_match('/\((.*)\)\s+([^\s]*)\s+(.*)/', $annotation, $matches)) {
  143. switch ($matches[2]) {
  144. case '==': {
  145. $assertion = 'Equals';
  146. }
  147. break;
  148. case '!=': {
  149. $assertion = 'NotEquals';
  150. }
  151. break;
  152. case '===': {
  153. $assertion = 'Same';
  154. }
  155. break;
  156. case '!==': {
  157. $assertion = 'NotSame';
  158. }
  159. break;
  160. case '>': {
  161. $assertion = 'GreaterThan';
  162. }
  163. break;
  164. case '>=': {
  165. $assertion = 'GreaterThanOrEqual';
  166. }
  167. break;
  168. case '<': {
  169. $assertion = 'LessThan';
  170. }
  171. break;
  172. case '<=': {
  173. $assertion = 'LessThanOrEqual';
  174. }
  175. break;
  176. case 'throws': {
  177. $assertion = 'exception';
  178. }
  179. break;
  180. default: {
  181. throw new RuntimeException;
  182. }
  183. }
  184. if ($assertion == 'exception') {
  185. $template = 'TestMethodException';
  186. }
  187. else if ($assertion == 'Equals' && strtolower($matches[3]) == 'true') {
  188. $assertion = 'True';
  189. $template = 'TestMethodBool';
  190. }
  191. else if ($assertion == 'NotEquals' && strtolower($matches[3]) == 'true') {
  192. $assertion = 'False';
  193. $template = 'TestMethodBool';
  194. }
  195. else if ($assertion == 'Equals' && strtolower($matches[3]) == 'false') {
  196. $assertion = 'False';
  197. $template = 'TestMethodBool';
  198. }
  199. else if ($assertion == 'NotEquals' && strtolower($matches[3]) == 'false') {
  200. $assertion = 'True';
  201. $template = 'TestMethodBool';
  202. }
  203. else {
  204. $template = 'TestMethod';
  205. }
  206. if ($method->isStatic()) {
  207. $template .= 'Static';
  208. }
  209. $methodTemplate = new PHPUnit_Util_Template(
  210. sprintf(
  211. '%s%sTemplate%s%s.tpl',
  212. dirname(__FILE__),
  213. DIRECTORY_SEPARATOR,
  214. DIRECTORY_SEPARATOR,
  215. $template
  216. )
  217. );
  218. $origMethodName = $method->getName();
  219. $methodName = ucfirst($origMethodName);
  220. if (isset($this->methodNameCounter[$methodName])) {
  221. $this->methodNameCounter[$methodName]++;
  222. } else {
  223. $this->methodNameCounter[$methodName] = 1;
  224. }
  225. if ($this->methodNameCounter[$methodName] > 1) {
  226. $methodName .= $this->methodNameCounter[$methodName];
  227. }
  228. $methodTemplate->setVar(
  229. array(
  230. 'annotation' => trim($annotation),
  231. 'arguments' => $matches[1],
  232. 'assertion' => isset($assertion) ? $assertion : '',
  233. 'expected' => $matches[3],
  234. 'origMethodName' => $origMethodName,
  235. 'className' => $this->inClassName,
  236. 'methodName' => $methodName
  237. )
  238. );
  239. $methods .= $methodTemplate->render();
  240. $assertAnnotationFound = TRUE;
  241. }
  242. }
  243. }
  244. if (!$assertAnnotationFound) {
  245. $methodTemplate = new PHPUnit_Util_Template(
  246. sprintf(
  247. '%s%sTemplate%sIncompleteTestMethod.tpl',
  248. dirname(__FILE__),
  249. DIRECTORY_SEPARATOR,
  250. DIRECTORY_SEPARATOR
  251. )
  252. );
  253. $methodTemplate->setVar(
  254. array(
  255. 'methodName' => ucfirst($method->getName())
  256. )
  257. );
  258. $incompleteMethods .= $methodTemplate->render();
  259. }
  260. }
  261. }
  262. $classTemplate = new PHPUnit_Util_Template(
  263. sprintf(
  264. '%s%sTemplate%sTestClass.tpl',
  265. dirname(__FILE__),
  266. DIRECTORY_SEPARATOR,
  267. DIRECTORY_SEPARATOR
  268. )
  269. );
  270. if ($this->inSourceFile != '<internal>') {
  271. $requireClassFile = sprintf(
  272. "\n\nrequire_once '%s';",
  273. $this->inSourceFile
  274. );
  275. } else {
  276. $requireClassFile = '';
  277. }
  278. $classTemplate->setVar(
  279. array(
  280. 'className' => $this->inClassName,
  281. 'requireClassFile' => $requireClassFile,
  282. 'methods' => $methods . $incompleteMethods,
  283. 'date' => date('Y-m-d'),
  284. 'time' => date('H:i:s')
  285. )
  286. );
  287. if (!$verbose) {
  288. return $classTemplate->render();
  289. } else {
  290. return array(
  291. 'code' => $classTemplate->render(),
  292. 'incomplete' => empty($methods)
  293. );
  294. }
  295. }
  296. }
  297. ?>