thinkphp6 unit test
1.1.2
使用 PHPUnit 和 ThinkPHP 6 进行单元测试的简单工具
Thinkphp v6.0+
PHPUnit v9.x+
嘲笑 v1.x+
composer require aspirantzhang/thinkphp6-unit-test --dev
首先,在你的类中使用UnitTestTrait
use aspirantzhangthinkphp6UnitTestUnitTestTrait;
当测试一个类(例如控制器)时,在语句之前使用它,并将$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
麻省理工学院