aspirantzhang/thinkphp6-unit-test

A simple tool for unit testing with PHPUnit and ThinkPHP 6

1.1.2 2021-06-28 06:12 UTC

This package is auto-updated.

Last update: 2024-04-28 12:34:38 UTC


README

A simple tool for unit testing with PHPUnit and ThinkPHP 6

Requirements

Install

composer require aspirantzhang/thinkphp6-unit-test --dev

Usage

First, use the UnitTestTrait in your class

use aspirantzhang\thinkphp6UnitTest\UnitTestTrait;

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

License

MIT