PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/script/lib/PHPUnit/Util/Skeleton/Test.php

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