php自动生成phpunit,[PHPUnit]自动生成PHPUnit测试骨架脚本-提供您的开发效率【2015...

[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] : 'dogstar';

if (!empty($bootstrap)) {

require $bootstrap;

}

require $filePath;

if (!class_exists($className)) {

die("Error: cannot find class($className). \n");

}

$reflector = new ReflectionClass($className);

$methods = $reflector->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);

/** ------------------- 根据@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($returnCommentArr) >= 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;

}

break;

}

}

/** ------------------- 基本的单元测试代码生成 ------------------ **/

$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\n " . $returnAssert . "\n")

. "

}

";

/** ------------------- 根据@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\n \$this->assertEquals({$expRs}, \$rs);"

. "

}

";

$caseNum ++;

}

}

}

$code .= "

}";

echo $code;

echo "\n";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值