build_test.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * 单元测试骨架代码自动生成脚本
  4. * 主要是针对当前项目系列生成相应的单元测试代码,提高开发效率
  5. *
  6. * 用法:
  7. * Usage: php ./build_test.php <file_path> <class_name> [bootstrap] [author = dogstar]
  8. *
  9. * 1、针对全部public的函数进行单元测试
  10. * 2、可根据@testcase注释自动生成测试用例
  11. *
  12. * 备注:另可使用phpunit-skelgen进行骨架代码生成
  13. *
  14. * @author: dogstar 20170708
  15. * @version: 5.1.1
  16. */
  17. if ($argc < 3) {
  18. echo "\n";
  19. echo colorfulString("Usage:\n", 'WARNING');
  20. echo " php $argv[0] <file_path> <class_name> [bootstrap] [author]\n";
  21. echo "\n";
  22. echo colorfulString("Options:\n", 'WARNING');
  23. echo colorfulString(' file_path', 'NOTE'), " Require. Path to the PHP source code file\n";
  24. echo colorfulString(' class_name', 'NOTE'), " Require. The class name need to be tested\n";
  25. echo colorfulString(' bootstrap', 'NOTE'), " NOT require. Path to the bootsrap file, usually is test_env.php\n";
  26. echo colorfulString(' author', 'NOTE'), " NOT require. Your great name here, default is dogstar\n";
  27. echo "\n";
  28. echo colorfulString("Demo:\n", 'WARNING');
  29. echo " $argv[0] ./Demo.php Demo > Demo_Test.php\n";
  30. echo " $argv[0] ./Demo.php Demo > Demo_Test.php\n";
  31. echo " $argv[0] ./src/Request.php PhalApi\\\\Reqeust > Request_Test.php\n";
  32. echo "\n";
  33. echo colorfulString("Tips:\n", 'WARNING');
  34. echo " This will output the code directly, you can save them to test file like with _Test.php suffix.\n";
  35. echo "\n";
  36. die();
  37. }
  38. $filePath = $argv[1];
  39. $className = $argv[2];
  40. $bootstrap = isset($argv[3]) ? $argv[3] : null;
  41. $author = isset($argv[4]) ? $argv[4] : 'dogstar';
  42. // 尝试加载composer autoload
  43. $autoloadFiles = array(
  44. dirname(__FILE__) . '/../vendor/autoload.php',
  45. dirname(__FILE__) . '/../../../vendor/autoload.php',
  46. );
  47. foreach ($autoloadFiles as $file) {
  48. if (file_exists($file)) {
  49. require_once $file;
  50. }
  51. }
  52. // 引入启动文件
  53. if (!empty($bootstrap)) {
  54. require_once $bootstrap;
  55. }
  56. // 引入源代码
  57. require_once $filePath;
  58. if (!class_exists($className)) {
  59. echo colorfulString("Error: cannot find class($className). \n\n", 'FAILURE');
  60. die();
  61. }
  62. $reflector = new ReflectionClass($className);
  63. $methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
  64. date_default_timezone_set('Asia/Shanghai');
  65. $objName = lcfirst(str_replace(array('_', '\\'), array('', ''), $className));
  66. /** ------------------- 生成通用的单元测试代码 ------------------ **/
  67. $code = "<?php
  68. ";
  69. if (file_exists(dirname(__FILE__) . '/test_env.php')) {
  70. $code .= "require_once dirname(__FILE__) . '/bootstrap.php';
  71. ";
  72. } else {
  73. $code .= "//require_once dirname(__FILE__) . '/bootstrap.php';
  74. ";
  75. }
  76. $initWay = "new $className()";
  77. if (method_exists($className, '__construct')) {
  78. $constructMethod = new ReflectionMethod($className, '__construct');
  79. if (!$constructMethod->isPublic()) {
  80. if (is_callable(array($className, 'getInstance'))) {
  81. $initWay = "$className::getInstance()";
  82. } else if(is_callable(array($className, 'newInstance'))) {
  83. $initWay = "$className::newInstance()";
  84. } else {
  85. $initWay = 'NULL';
  86. }
  87. }
  88. }
  89. $code .= "
  90. if (!class_exists('" . (strpos($className, '\\') !== false ? str_replace('\\', '\\\\', $className) : $className) . "')) {
  91. require dirname(__FILE__) . '/$filePath';
  92. }
  93. /**
  94. * PhpUnderControl_" . str_replace('_', '', $className) . "_Test
  95. *
  96. * 针对 $filePath $className 类的PHPUnit单元测试
  97. *
  98. * @author: $author " . date('Ymd') . "
  99. */
  100. class PhpUnderControl_" . str_replace(array('_', '\\'), array('', ''), $className) . "_Test extends \PHPUnit_Framework_TestCase
  101. {
  102. public \$$objName;
  103. protected function setUp()
  104. {
  105. parent::setUp();
  106. \$this->$objName = $initWay;
  107. }
  108. protected function tearDown()
  109. {
  110. // 输出本次单元测试所执行的SQL语句
  111. // var_dump(\PhalApi\DI()->tracer->getSqls());
  112. // 输出本次单元测试所涉及的追踪埋点
  113. // var_dump(\PhalApi\DI()->tracer->getSqls());
  114. }
  115. ";
  116. foreach ($methods as $method) {
  117. if($method->class != $className) continue;
  118. $fun = $method->name;
  119. $Fun = ucfirst($fun);
  120. if (strlen($Fun) > 2 && substr($Fun, 0, 2) == '__') continue;
  121. $rMethod = new ReflectionMethod($className, $method->name);
  122. $params = $rMethod->getParameters();
  123. $isStatic = $rMethod->isStatic();
  124. $isConstructor = $rMethod->isConstructor();
  125. if($isConstructor) continue;
  126. $initParamStr = '';
  127. $callParamStr = '';
  128. foreach ($params as $param) {
  129. $default = '';
  130. $rp = new ReflectionParameter(array($className, $fun), $param->name);
  131. if ($rp->isOptional()) {
  132. $default = $rp->getDefaultValue();
  133. }
  134. if (is_string($default)) {
  135. $default = "'$default'";
  136. } else if (is_array($default)) {
  137. $default = var_export($default, true);
  138. } else if (is_bool($default)) {
  139. $default = $default ? 'true' : 'false';
  140. } else if ($default === null) {
  141. $default = 'null';
  142. } else {
  143. $default = "''";
  144. }
  145. $initParamStr .= "
  146. \$" . $param->name . " = $default;";
  147. $callParamStr .= '$' . $param->name . ', ';
  148. }
  149. $callParamStr = empty($callParamStr) ? $callParamStr : substr($callParamStr, 0, -2);
  150. /** ------------------- 根据@return对结果类型的简单断言 ------------------ **/
  151. $returnAssert = '';
  152. $docComment = $rMethod->getDocComment();
  153. $docCommentArr = explode("\n", $docComment);
  154. foreach ($docCommentArr as $comment) {
  155. if (strpos($comment, '@return') == false) {
  156. continue;
  157. }
  158. $returnCommentArr = explode(' ', strrchr($comment, '@return'));
  159. if (count($returnCommentArr) >= 2) {
  160. switch (strtolower($returnCommentArr[1])) {
  161. case 'bool':
  162. case 'boolean':
  163. $returnAssert = '$this->assertTrue(is_bool($rs));';
  164. break;
  165. case 'int':
  166. $returnAssert = '$this->assertTrue(is_int($rs));';
  167. break;
  168. case 'integer':
  169. $returnAssert = '$this->assertTrue(is_integer($rs));';
  170. break;
  171. case 'string':
  172. $returnAssert = '$this->assertTrue(is_string($rs));';
  173. break;
  174. case 'object':
  175. $returnAssert = '$this->assertTrue(is_object($rs));';
  176. break;
  177. case 'array':
  178. $returnAssert = '$this->assertTrue(is_array($rs));';
  179. break;
  180. case 'float':
  181. $returnAssert = '$this->assertTrue(is_float($rs));';
  182. break;
  183. }
  184. break;
  185. }
  186. }
  187. /** ------------------- 基本的单元测试代码生成 ------------------ **/
  188. $code .= "
  189. /**
  190. * @group test$Fun
  191. */
  192. public function test$Fun()
  193. {"
  194. . (empty($initParamStr) ? '' : "$initParamStr\n")
  195. . "\n "
  196. . ($isStatic ? "\$rs = $className::$fun($callParamStr);" : "\$rs = \$this->$objName->$fun($callParamStr);")
  197. . (empty($returnAssert) ? '' : "\n\n " . $returnAssert . "\n")
  198. . "
  199. }
  200. ";
  201. /** ------------------- 根据@testcase 生成测试代码 ------------------ **/
  202. $caseNum = 0;
  203. foreach ($docCommentArr as $comment) {
  204. if (strpos($comment, '@testcase') == false) {
  205. continue;
  206. }
  207. $returnCommentArr = explode(' ', strrchr($comment, '@testcase'));
  208. if (count($returnCommentArr) > 1) {
  209. $expRs = $returnCommentArr[1];
  210. //去掉@testcase和期望的结果
  211. array_shift($returnCommentArr);
  212. array_shift($returnCommentArr);
  213. $callParamStrInCase = !empty($returnCommentArr) ? implode(' ', $returnCommentArr) : '';
  214. $code .= "
  215. /**
  216. * @group test$Fun
  217. */
  218. public function test{$Fun}Case{$caseNum}()
  219. {"
  220. . "\n "
  221. . ($isStatic ? "\$rs = $className::$fun($callParamStrInCase);" : "\$rs = \$this->$objName->$fun($callParamStrInCase);")
  222. . "\n\n \$this->assertEquals({$expRs}, \$rs);"
  223. . "
  224. }
  225. ";
  226. $caseNum ++;
  227. }
  228. }
  229. }
  230. $code .= "
  231. }";
  232. echo $code;
  233. echo "\n";
  234. function colorfulString($text, $type = NULL) {
  235. $colors = array(
  236. 'WARNING' => '1;33',
  237. 'NOTE' => '1;36',
  238. 'SUCCESS' => '1;32',
  239. 'FAILURE' => '1;35',
  240. );
  241. if (empty($type) || !isset($colors[$type])){
  242. return $text;
  243. }
  244. return "\033[" . $colors[$type] . "m" . $text . "\033[0m";
  245. }