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
麻省理工學院