首页 > php教程 > PHP源码 > 正文

[PHPUnit]自动生成PHPUnit测试骨架脚本-提供您的开发效率【2015升级版】

PHP中文网
发布: 2016-05-26 08:19:31
原创
1207 人浏览过


//$ cat ./build_phpunit_test_tpl.php
#!/usr/bin/env php
<?php
/**
 * 单元测试骨架代码自动生成脚本
 * 主要是针对当前项目系列生成相应的单元测试代码,提高开发效率
 *
 * 用法:
 * Usage: php ./build_phpunit_test_tpl.php[bootstrap] [author = dogstar]
 *
 * 1、针对全部public的函数进行单元测试
 * 2、可根据@testcase注释自动生成测试用例
 *
 * 备注:另可使用phpunit-skelgen进行骨架代码生成
 *
 * @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] : &#39;dogstar&#39;;
 
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(&#39;Asia/Shanghai&#39;);
$objName = lcfirst(str_replace(&#39;_&#39;, &#39;&#39;, $className));
 
$code = "$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) == &#39;__&#39;) continue;
 
    $rMethod = new ReflectionMethod($className, $method->name);
    $params = $rMethod->getParameters();
    $isStatic = $rMethod->isStatic();
    $isConstructor = $rMethod->isConstructor();
 
    if($isConstructor) continue;
 
    $initParamStr = &#39;&#39;;
    $callParamStr = &#39;&#39;;
    foreach ($params as $param) {
        $default = &#39;&#39;;
 
        $rp = new ReflectionParameter(array($className, $fun), $param->name);
        if ($rp->isOptional()) {
            $default = $rp->getDefaultValue();
        }
        if (is_string($default)) {
            $default = "&#39;$default&#39;";
        } else if (is_array($default)) {
            $default = var_export($default, true);
        } else if (is_bool($default)) {
            $default = $default ? &#39;true&#39; : &#39;false&#39;;
        } else if ($default === null) {
            $default = &#39;null&#39;;
        } else {
            $default = "&#39;&#39;";
        }
 
        $initParamStr .= "
        \$" . $param->name . " = $default;";
        $callParamStr .= &#39;$&#39; . $param->name . &#39;, &#39;;
    }
    $callParamStr = empty($callParamStr) ? $callParamStr : substr($callParamStr, 0, -2);
 
    /** ------------------- 根据@return对结果类型的简单断言 ------------------ **/
    $returnAssert = &#39;&#39;;
 
    $docComment = $rMethod->getDocComment();
    $docCommentArr = explode("\n", $docComment);
    foreach ($docCommentArr as $comment) {
        if (strpos($comment, &#39;@return&#39;) == false) {
            continue;
        }
        $returnCommentArr = explode(&#39; &#39;, strrchr($comment, &#39;@return&#39;));
        if (count($returnCommentArr) >= 2) {
            switch (strtolower($returnCommentArr[1])) {
            case &#39;bool&#39;:
            case &#39;boolean&#39;:
                $returnAssert = &#39;$this->assertTrue(is_bool($rs));&#39;;
                break;
            case &#39;int&#39;:
                $returnAssert = &#39;$this->assertTrue(is_int($rs));&#39;;
                break;
            case &#39;integer&#39;:
                $returnAssert = &#39;$this->assertTrue(is_integer($rs));&#39;;
                break;
            case &#39;string&#39;:
                $returnAssert = &#39;$this->assertTrue(is_string($rs));&#39;;
                break;
            case &#39;object&#39;:
                $returnAssert = &#39;$this->assertTrue(is_object($rs));&#39;;
                break;
            case &#39;array&#39;:
                $returnAssert = &#39;$this->assertTrue(is_array($rs));&#39;;
                break;
            case &#39;float&#39;:
                $returnAssert = &#39;$this->assertTrue(is_float($rs));&#39;;
                break;
            }
 
            break;
        }
    }
 
    /** ------------------- 基本的单元测试代码生成 ------------------ **/
    $code .= "
    /**
     * @group test$Fun
     */ 
    public function test$Fun()
    {"
    . (empty($initParamStr) ? &#39;&#39; : "$initParamStr\n") 
    . "\n        "
    . ($isStatic ? "\$rs = $className::$fun($callParamStr);" : "\$rs = \$this->$objName->$fun($callParamStr);") 
    . (empty($returnAssert) ? &#39;&#39; : "\n\n        " . $returnAssert . "\n") 
    . "
    }
";
 
    /** ------------------- 根据@testcase 生成测试代码 ------------------ **/
    $caseNum = 0;
    foreach ($docCommentArr as $comment) {
        if (strpos($comment, &#39;@testcase&#39;) == false) {
            continue;
        }
 
        $returnCommentArr = explode(&#39; &#39;, strrchr($comment, &#39;@testcase&#39;));
        if (count($returnCommentArr) > 1) {
            $expRs = $returnCommentArr[1];
            $callParamStrInCase = isset($returnCommentArr[2]) ? $returnCommentArr[2] : &#39;&#39;;
 
            $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";
登录后复制

                   

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板