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

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

PHP中文网
PHP中文网원래의
2016-05-26 08:19:311244검색


//$ 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";

                   

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.