研究thinkphp6的phpunit整合血泪史
说血泪,是因为项目关键时刻,花了很多时间解决!
解决过程不说了,看代码把,代码记录了战乱的始终。。。
完整的解决方案,已经记录了部分还处于草稿状态,等我赶赶项目先,过两天抽时间,到时整理一个笔记分享。
我就特么的想不明白,难道大家使用tp6都不用phpunit单元测试了?网络一点资料都没有,大神难道都是不写笔记的?。。。
<?php
declare (strict_types=1);
namespace tests;
require_once __DIR__ . '/../vendor/autoload.php';
use app\exusr\model\User;
use app\index\controller\DebugController;
final class DemoTest extends \PHPUnit\Framework\TestCase
{
/**
* 单元测试初始化
*/
/*protected function setUp():void
{
// 执行HTTP应用并响应
$http = (new \think\App())->http;
//$_GET["s"] = isset($_GET["s"])?strtolower($_GET["s"]):'/';
//$_SERVER["HTTP_ACCEPT"] = "application/json";
$response = $http->run();
$response->send();
$http->end($response);
}*/
/**
* 单元测试收尾执行
*/
/*public function tearDown():void
{
}*/
/**
* @test
*/
public function testDemo1(): void
{
//断言相等
$this->assertEquals('value1', 'value1');
//断言为真
$this->assertTrue(true);
//断言数组中存在索引
$this->assertArrayHasKey('aa', ['aa' => 1, 'bb' => 2, 'dd' => 4]);
}
public function testDemo2(): void
{
//断言相等
$this->assertEquals('value1', 'value2');
//断言为真
$this->assertTrue(false);
//断言数组中存在索引
$this->assertArrayHasKey('cc', ['aa' => 1, 'bb' => 2, 'dd' => 4]);
}
/**
* @test
*/
public function testControllerx(): void
{
$request = new \app\Request();
$request->setMethod('POST');
$request->setUrl('/index/debug/testing26');
$request->withGet(['xid' => 99,]);
$request->withPost(['aaa' => 111, 'bbb' => 222,]);
$request->withHeader(['HTTP_ACCEPT' => 'application/json',]);
$app = (new \think\App());
$app->http->run($request);
$debug = new DebugController($app);
$data = $debug->testing(1, 2);
//强制截断
//$data = preg_replace('/<[^>]+>/', '', $data);
if (strlen(strval($data)) > 500) {
$data = substr($data, 0, 500);
}
$this->assertEquals(1111111111, $data);
}
/**
* @test
*/
public function testControllerxtwo(): void
{
$request = new \app\Request();
$request->setMethod('POST');
$request->setUrl('/index/debug/testing26');
$request->withGet(['xid' => 99,]);
$request->withPost(['aaa' => 111, 'bbb' => 222,]);
$request->withHeader(['HTTP_ACCEPT' => 'application/json',]);
$app = (new \think\App());
$http = $app->http;
$response = $http->run($request);
$data = $response->getData();
//$debug = new DebugController($app);
//$data = $debug->testing(1, 2);
//强制截断
//$data = preg_replace('/<[^>]+>/', '', $data);
if (strlen(strval($data)) > 500) {
$data = substr($data, 0, 500);
}
$this->assertEquals(1111111111, $data);
}
/**
* @test
*/
public function testControllerxzero(): void
{
$request = new \app\Request();
$request->setUrl('/index/debug/testing26');
$request->withPost(['aaa' => 111, 'bbb' => 222,]);
$request->withHeader(['HTTP_ACCEPT'=>'application/json']);
$request->setMethod('POST');
$app = (new \think\App());
//$app->setAppPath('mobile');
$http = $app->http;
//$http->path('admin');
$response = $http->run($request);
$debug = new DebugController($app);
$data = $debug->testing(1,2);
//强制截断
//$data = preg_replace('/<[^>]+>/', '', $data);
if (strlen($data) > 500) {
$data = substr($data, 0, 500);
}
//app\ExceptionHandle.php的render需要配置方便调试:
//return \response($e->getTraceAsString());
//return \response($e->getMessage());
#$debug = new DebugController(new \think\App());
$this->assertEquals(1111111111, $data);
}
/**
* @test
*/
public function testModelx(): void
{
//(new \think\App())->http->run();
//((new \think\App())->http)->run();
//断言相等
$this->assertEquals('value14', User::find(1)->nickname);
}
}
一行路由,曲线救国:
<?php
declare (strict_types=1);
#这里可以引入,也可由phpunit.xml初始化引入
require_once __DIR__ . '/../vendor/autoload.php';
//require_once __DIR__ . '/../vendor/topthink/framework/src/helper.php';
#需要这样子run一下,run完返回$response,测试模型必须执行这行玩意儿
((new \think\App())->http)->run();
/**
* 构造一个请求应用
* @param array $data 入参数组
* @param string $method 请求方法
* @return \think\App
*/
function visitor($controller, $action, $data, $uid = null, $method = 'POST')
{
if (is_numeric($uid)) {
$data['xid'] = $uid;//非生产环境可以强制注入登录的测试用户
}
$app = new \think\App();
#$app->request->setMethod($method);
//$app->request->setController('Xlogin');
//$app->request->setUrl('/index/debug/testing26');
//$app->request->setUrl('/index/oauth/login');
//$app->request->setBaseUrl('/index/oauth/login');
//$app->request->withGet(['xid' => 99,'xx' => 'get-xx','yy' => 'get-yy',]);
//$app->request->withGet($data);
//$app->request->withPost(['aaa' => 111, 'bbb' => 222,'xx' => 'post-xx','yy' => 'post-yy',]);
#$app->request->withPost($data);
$request = new \app\Request();
$request->withPost($data);
$request->withRoute($data);//tp6的bug啊,一行代码曲线救国
$response = $app->http->run($request);
//重置请求对象
$app->request->setMethod($method);//非必须
$app->request->setAction($action);
//$app->request->withGet($data);
//$app->request->withPost($data);
//$response->send();
$app->http->end($response);
$test = new $controller($app);
$result = $test->$action();
$content = $result->getContent();
//强制截断
//$data = preg_replace('/<[^>]+>/', '', $data);
/*if (strlen(strval($content)) > 800) {
return substr($content, 0, 500);
}*/
//如果不是json怎么办?写个日志
return json_decode($content);
}
function hiapp2($data = [], $uid = null, $method = 'POST')
{
if (is_numeric($uid)) {
$data['xid'] = $uid;//非生产环境可以强制注入登录的测试用户
}
$request = new \app\Request();
$request->setMethod($method);
$request->setController('Xlogin');
$request->setAction('login');
//$request->setUrl('/index/debug/testing26');
$request->setUrl('/index/oauth/login');
$request->setBaseUrl('/index/oauth/login');
//$request->withGet(['xid' => 99,'xx' => 'get-xx','yy' => 'get-yy',]);
$request->withGet($data);
//$request->withPost(['aaa' => 111, 'bbb' => 222,'xx' => 'post-xx','yy' => 'post-yy',]);
//$request->withInput(json_encode(['aaa' => 111, 'bbb' => 222,'yy' => 'ayy',]));
$request->withHeader(['HTTP_ACCEPT' => 'application/json',]);
//$request->withHeader(['Content-Type' => 'application/x-www-form-urlencoded',]);
//$request->withHeader(['Content-Type' => 'application/form-data',]);
$app = new \think\App();
$response = $app->http->run($request);
//$response->send();
$app->http->end($response);
return $app;
}