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

/vendor/agavi/vendor/PHPUnit/Util/Skeleton/Test.php

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