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

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

PHP中文网
Release: 2016-05-26 08:19:31
Original
1242 people have browsed it


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

                   

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template