aspirantzhang / thinkphp6-unit-test
一个使用PHPUnit和ThinkPHP 6进行单元测试的简单工具
1.1.2
2021-06-28 06:12 UTC
Requires
- topthink/framework: ^6.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.5
README
一个使用PHPUnit和ThinkPHP 6进行单元测试的简单工具
需求
安装
composer require aspirantzhang/thinkphp6-unit-test --dev
使用方法
首先,在您的类中使用UnitTestTrait
use aspirantzhang\thinkphp6UnitTest\UnitTestTrait;
当测试一个类(如控制器)时,在使用语句之前使用它,并将$this->app作为控制器的参数设置,例如
$this->startRequest();
$yourController = new YourController($this->app);
或者简单地一个函数测试
$this->startApp();
完整的方法测试可能如下
public function testAdminHome()
{
$this->startRequest();
$adminController = new AdminController($this->app);
$response = $adminController->home();
$this->assertEquals(200, $response->getCode());
}
更多支持的使用方法
// get with no param
$this->startRequest();
// get with param
$this->startRequest('GET', ['trash' => 'onlyTrashed']);
// post with data
$this->startRequest('POST', ['type' => 'delete', 'ids' => [1]]);
// put with data
$this->startRequest('PUT', ['display_name' => 'Admin']);
// mock localization
$this->mockLang('zh-cn');
// close mock localization
$this->endMockLang();
最后,关闭请求。
$this->endRequest();
您可以通过这个链接查看使用此包的项目 https://github.com/aspirantzhang/octopus
许可证
MIT