Skip to content

A simple tool for unit testing with PHPUnit and ThinkPHP 6

Notifications You must be signed in to change notification settings

aspirantzhang/thinkphp6-unit-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ThinkPHP6 Unit Test

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

About

A simple tool for unit testing with PHPUnit and ThinkPHP 6

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages