thinkphp6 unit test
1.1.2
A simple tool for unit testing with PHPUnit and ThinkPHP 6
Thinkphp v6.0+
PHPUnit v9.x+
Mockery v1.x+
composer require aspirantzhang/thinkphp6-unit-test --dev
First, use the UnitTestTrait
in your class
use aspirantzhangthinkphp6UnitTestUnitTestTrait;
When testing a class (such as a controller), use it before your statement, and set $this->app
as the parameter of the controller, like this
$this->startRequest();
$yourController = new YourController($this->app);
or simply a function test
$this->startApp();
A full method test might be
public function testAdminHome()
{
$this->startRequest();
$adminController = new AdminController($this->app);
$response = $adminController->home();
$this->assertEquals(200, $response->getCode());
}
More supported usage
// 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();
Finally, close the request.
$this->endRequest();
You can refer to the project using this package https://github.com/aspirantzhang/octopus
MIT