colly_wyx
2018-04-27 74adf3a72663f151dc2c1b87ecb4ea4b0e080a50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
 * 单元测试骨架代码自动生成脚本
 * 主要是针对当前项目系列生成相应的单元测试代码,提高开发效率
 *
 * 用法:
 * Usage: php ./build_test.php <file_path> <class_name> [bootstrap] [author = dogstar]
 *
 * 1、针对全部public的函数进行单元测试
 * 2、可根据@testcase注释自动生成测试用例
 *
 * 备注:另可使用phpunit-skelgen进行骨架代码生成
 *
 * @author: dogstar 20170415
 * @version: 4.1.1
 */
 
if ($argc < 3) {
    echo "\n";
    echo colorfulString("Usage:\n", 'WARNING');
    echo "    php $argv[0] <file_path> <class_name> [bootstrap] [author]\n";
    echo "\n";
 
    echo colorfulString("Options:\n", 'WARNING');
    echo colorfulString('    file_path', 'NOTE'), "         Require. Path to the PHP source code file\n";
    echo colorfulString('    class_name', 'NOTE'), "        Require. The class name need to be tested\n";
    echo colorfulString('    bootstrap', 'NOTE'), "         NOT require. Path to the bootsrap file, usually is test_env.php\n";
    echo colorfulString('    author', 'NOTE'), "            NOT require. Your great name here, default is dogstar\n";
    echo "\n";
 
    echo colorfulString("Demo:\n", 'WARNING');
    echo "    php ./build_test.php ./Demo.php Demo > Demo_Test.php\n";
    echo "\n";
 
    echo colorfulString("Tips:\n", 'WARNING');
    echo "    This will output the code directly, you can save them to test file like with _Test.php suffix.\n";
    echo "\n";
 
    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)) {
    echo colorfulString("Error: cannot find class($className). \n\n", 'FAILURE');
    die();
}
 
$reflector = new ReflectionClass($className);
 
$methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
 
date_default_timezone_set('Asia/Shanghai');
$objName = lcfirst(str_replace(array('_', '\\'), array('', ''), $className));
 
/** ------------------- 生成通用的单元测试代码 ------------------ **/
 
$code = "<?php
/**
 * PhpUnderControl_" . str_replace('_', '', $className) . "_Test
 *
 * 针对 $filePath $className 类的PHPUnit单元测试
 *
 * @author: $author " . date('Ymd') . "
 */
 
";
 
if (file_exists(dirname(__FILE__) . '/test_env.php')) {
    $code .= "require_once dirname(__FILE__) . '/test_env.php';
";
} else {
    $code .= "//require_once dirname(__FILE__) . '/test_env.php';
";
}
 
$initWay = "new $className()";
if (method_exists($className, '__construct')) {
    $constructMethod = new ReflectionMethod($className, '__construct');
    if (!$constructMethod->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('" . (strpos($className, '\\') !== false ? str_replace('\\', '\\\\', $className) : $className) . "')) {
    require dirname(__FILE__) . '/$filePath';
}
 
class PhpUnderControl_" . str_replace(array('_', '\\'), array('', ''), $className) . "_Test extends PHPUnit_Framework_TestCase
{
    public \$$objName;
 
    protected function setUp()
    {
        parent::setUp();
 
        \$this->$objName = $initWay;
    }
 
    protected function tearDown()
    {
        // 输出本次单元测试所执行的SQL语句
        // var_dump(DI()->tracer->getSqls());
 
        // 输出本次单元测试所涉及的追踪埋点
        // var_dump(DI()->tracer->getSqls());
    }
 
";
 
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];
 
            //去掉@testcase和期望的结果
            array_shift($returnCommentArr);
            array_shift($returnCommentArr);
 
            $callParamStrInCase = !empty($returnCommentArr) ? implode(' ', $returnCommentArr) : '';
 
            $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";
 
function colorfulString($text, $type = NULL) {
    $colors = array(
        'WARNING'   => '1;33',
        'NOTE'      => '1;36',
        'SUCCESS'   => '1;32',
        'FAILURE'   => '1;35',
    );
 
    if (empty($type) || !isset($colors[$type])){
        return $text;
    }
 
    return "\033[" . $colors[$type] . "m" . $text . "\033[0m";
}