TRR · v0.0.2 · 生成phpunit文件篇
嗝嗝 9/5/2019  
      command
    
      TRR
    
生成phpunit文件篇
简介
thinkphp从创建到现在,在单元测试上面,下的功夫很浅,说实话如果你不喜欢看源码,那有可能连单元测试文档都看不懂。
所以经过作者反复摸索,终于在TRR基础上,加上了生成单元测试文件,虽然目前还不成熟,格式也不太好看,但也能快乐使用。
优点:
- 自动生成
单元测试文件 - 自动填装默认参数,作为请求参数
 - 按照项目目录生成,贴合用户习惯
 - 减少工作量
 - 快捷
 - 简单
 
缺点:
- 格式不太好看(后期会优化)
 - 不支持
header参数默认值设置 
目前TRR使用的扩展wangyu/tp-anntation:dev-dev
需要更换为:
composer require wangyu/reflex-core=0.1.0-alpha1
composer require wangyu/tp-anntation=dev-feature/testCase
 1
2
2
注意⚠ 目前都为测试版本,版本后续会发布正式版本。
提示💡单元测试不会启动thinkphp的中间件以及路由服务。
前期准备
请提前安装好 topthink/think-testing=2.0.*
composer安装命令
composer require topthink/think-testing=2.0.*
 1
 注册TP think 命令
 注意事项
thinkphp5.1 think 命令配置文件在application/command.php
注册命令
<?php
return [
    'trr:doc' => \WangYu\annotation\DocCommand::class,
    'trr:test' => \WangYu\annotation\TestCaseCommand::class,
];
 1
2
3
4
5
2
3
4
5
写接口注解参数默认值
 @param注解函数
 @param 反射标识说明:
| 名称 | 注释 | 使用说明 | 
|---|---|---|
| param | 路由参数验证器 | @param('参数名','参数简介','参数规则','参数默认值') | 
例如:
/**
 .... 其他注解
 * @param('name','名称','require','12e1')
 * @param('password','密码','require','cas')
 */
public function create()
{
    #......
}
 1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
 @validate注解函数
 @validate 反射标识说明:
| 名称 | 注释 | 使用说明 | 
|---|---|---|
| validate | 路由参数验证器类,需要继承 \WangYu\validate\BaseValidate |  @validate('验证器名称') | 
/**
 ....
 * @validate('CreateGroup')
 */
public function create(){#......}
 1
2
3
4
5
2
3
4
5
需要在application/api/validate/manage/CreateGroup.php验证器类里,新增默认protected类型属性变量$default
如下:
protected $default = [
    'name' => "231",
    'age' => '21'
];
 1
2
3
4
2
3
4
 生成phpunit文件
 命令行模式
- 运行 
trr:test命令 
php think trr:test --module=api1- 输出
 
wy@aokodeiMac TRR (master) $ php think trr:test --module=api Success. Please view it under the Unit Test Folder1
2- 查看 
trr:test命令帮助 
php think trr:test -h1- 输出
 
wy@aokodeiMac TRR (master) $ php think trr:test -h Usage: annotation:test [options] Options: --module=MODULE your API Folder,Examples: api = /application/api [default: "api"] -h, --help Display this help message -V, --version Display this console version -q, --quiet Do not output any message --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug1
2
3
4
5
6
7
8
9
10
11
12
13- 运行 
 
查看生成的单元测试文件
请在TRR根目录下tests文件夹下,根据--module=api指定模块下生成单元测试文件目录
如下:
wy@aokodeiMac TRR (master) $ tree tests/
tests/
├── ExampleTest.php
├── TestCase.php
└── api
    └── controller
        ├── AuthTest.php
        └── v1
            └── BookTest.php
3 directories, 4 files
 1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11