PageRenderTime 73ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/MyVenture-BlackHawk/Library/UnitTest/Include/Util/Skeleton/Test.php

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