This script can quickly generate PHP unit test code for existing code for auxiliary testing to reduce development workload. You can also use PHPUnit-skelgen officially provided by phpunit to achieve the same effect. It's just a custom generation script, which pays more attention to user needs and facilitates modification and expansion of user needs. In addition, the new version can be
This script can quickly generate PHP unit test code for existing code for auxiliary testing to reduce development workload. You can also use PHPUnit-skelgen officially provided by phpunit to achieve the same effect. It's just a custom generation script, which pays more attention to user needs and facilitates modification and expansion of user needs. In addition, the new version can be
This script can quickly generate PHP unit test code for existing code for auxiliary testing to reduce development workload. You can also use PHPUnit-skelgen officially provided by phpunit to achieve the same effect. It's just a custom generation script, which pays more attention to user needs and facilitates modification and expansion of user needs. In addition, simple test cases can be automatically generated based on the annotation @ testcase in the new version! Please try it out. Thank you!
$./Build_phpunit_test_tpl.php
Usage:
Php./build_phpunit_test_tpl.php [Bootstrap] [author = dogstar]
Demo:
Php./build_phpunit_test_tpl.php./Demo. php Demo> Demo_Test.php
For more information, see: http://my.oschina.net/u/256338/blog/285579
<无>
// $ Cat./build_phpunit_test_tpl.php #! /Usr/bin/env php
[Bootstrap] [author = dogstar] ** 1. Perform unit tests on all public functions. * 2. automatically generate test cases based on @ testcase annotations. ** note: you can also use phpunit-skelgen to generate skeleton code ** @ author: dogstar 20150108 * @ version: 4.0.0 */if ($ argc <3) {echo "Usage: php $ argv [0]
[Bootstrap] [author = dogstar] Demo: php. /build_phpunit_test_tpl.php. /Demo. php Demo> Demo_Test.php "; die () ;}$ filePath = $ argv [1]; $ className = $ argv [2]; $ bootstrap = isset ($ argv [3])? $ Argv [3]: null; $ author = isset ($ argv [4])? $ Argv [4]: 'domainstar'; if (! Empty ($ bootstrap) {require $ bootstrap;} require $ filePath; if (! Class_exists ($ className) {die ("Error: cannot find class ($ className ). \ n ") ;}$ reflector = new ReflectionClass ($ className); $ methods = $ reflector ctor-> getMethods (ReflectionMethod: IS_PUBLIC); date_default_timezone_set ('Asia/Shanghai '); $ objName = lcfirst (str_replace ('_', '', $ className); $ code ="
IsPublic () {if (is_callable (array ($ className, 'getinstance') {$ initWay = "$ className: getInstance ()";} else if (is_callable (array ($ className, 'newinstance ') {$ initWay = "$ className: newInstance ()";} else {$ initWay = 'null' ;}}$ code. = "if (! Class_exists ('$ classname') {require dirname (_ FILE __). '/$ filepath';} class PhpUnderControl _". str_replace ('_', '', $ className ). "_ Test extends PHPUnit_Framework_TestCase {public \ $ objName; protected function setUp () {parent: setUp (); \ $ this-> $ objName = $ initWay ;} protected function tearDown () {}"; foreach ($ methods as $ method) {if ($ method-> class! = $ ClassName) continue; $ fun = $ method-> name; $ Fun = ucfirst ($ fun); if (strlen ($ Fun)> 2 & substr ($ Fun, 0, 2) = '_') continue; $ rMethod = new ReflectionMethod ($ className, $ method-> name); $ params = $ rMethod-> getParameters (); $ isStatic = $ rMethod-> isStatic (); $ isConstructor = $ rMethod-> isConstructor (); if ($ isConstructor) continue; $ initParamStr = ''; $ callParamStr = ''; foreach ($ params as $ param) {$ Default = ''; $ rp = new ReflectionParameter (array ($ className, $ fun), $ param-> name); if ($ rp-> isOptional ()) {$ default = $ rp-> getDefaultValue ();} if (is_string ($ default) {$ default = "'$ default '";} else if (is_array ($ default) {$ default = var_export ($ default, true);} else if (is_bool ($ default) {$ default = $ default? 'True': 'false';} else if ($ default = null) {$ default = 'null';} else {$ default = "''";} $ initParamStr. = "\ $ ". $ param-> name. "= $ default;"; $ callParamStr. = '$ '. $ param-> name. ',';} $ callParamStr = empty ($ callParamStr )? $ CallParamStr: substr ($ callParamStr, 0,-2);/** --------------------- Simple assertion of the result type based on @ return ---------------- **/$ returnAssert = ''; $ docComment = $ rMethod-> getDocComment (); $ docCommentArr = explode ("\ n", $ docComment); foreach ($ docCommentArr as $ comment) {if (strpos ($ comment, '@ return') = false) {continue;} $ returnCommentArr = explode ('', strrchr ($ comment, '@ return'); if (count ($ returnComme NtArr)> = 2) {switch (strtolower ($ returnCommentArr [1]) {case 'bool ': case 'boolean ': $ returnAssert = '$ this-> assertTrue (is_bool ($ rs);'; break; case 'int ': $ returnAssert = '$ this-> assertTrue (is_int ($ rs);'; break; case 'integer ': $ returnAssert = '$ this-> assertTrue (is_integer ($ rs);'; break; case 'string ': $ returnAssert = '$ this-> assertTrue (is_string ($ rs);'; break; case 'object': $ returnAssert = '$ This-> assertTrue (is_object ($ rs);'; break; case 'array ': $ returnAssert = '$ this-> assertTrue (is_array ($ rs);'; break; case 'float ': $ returnAssert = '$ this-> assertTrue (is_float ($ rs);'; break ;}} /** ------------------- basic unit test code generation ------------------ **/$ code. = "/*** @ group test $ Fun */public function test $ Fun (){". (empty ($ initParamStr )? '':" $ InitParamStr \ n ")." \ n ". ($ isStatic? "\ $ Rs = $ className: $ fun ($ callParamStr);": "\ $ rs =\$ this-> $ objName-> $ fun ($ callParamStr ); "). (empty ($ returnAssert )? '':" \ N ". $ returnAssert. "\ n "). "}";/** ------------------- generate test code according to @ testcase ---------------- **/$ caseNum = 0; foreach ($ docCommentArr as $ comment) {if (strpos ($ comment, '@ testcase') = false) {continue;} $ returnCommentArr = explode ('', strrchr ($ comment, '@ testcase ')); if (count ($ returnCommentArr)> 1) {$ expRs = $ returnCommentArr [1]; $ callParamStrInCase = isset ($ returnCommentArr [2])? $ ReturnCommentArr [2]: ''; $ code. = "/*** @ group test $ Fun */public function test {$ Fun} Case {$ caseNum }(){". "\ n ". ($ isStatic? "\ $ Rs = $ className: $ fun ($ callParamStrInCase);": "\ $ rs =\$ this-> $ objName-> $ fun ($ callParamStrInCase ); "). "\ n \ $ this-> assertEquals ({$ expRs}, \ $ rs );". "}"; $ caseNum ++ ;}}$ code. = "}"; echo $ code; echo "\ n ";