php laravel测试用例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php laravel测试用例相关的知识,希望对你有一定的参考价值。
# laravel测试用例
## 基类
TestCase.php
<?php
namespace Tests;
use App\Repositories\Common\ResponseJson\SysResponseJsonFormat;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Http\JsonResponse;
/**
* TestCase
*
* @method assertTrue($bool)
* @since 2018/9/20
* @abstract
* @package Tests
*/
class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
/**
* get获取系统内的指定控制器的方法
*
* @param callable $actionArr
* @param array $parameters
* @param array $cookies
* @param array $files
* @param array $server
* @param null $content
* @return \Illuminate\Http\Response|JsonResponse
*/
public function getAction($actionArr, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
return $this->call('GET', getActionUrl($actionArr), $parameters, $cookies, $files, $server, $content);
}
/**
* get获取系统内的指定控制器的方法
*
* @param callable $actionArr
* @param array $parameters
* @param array $cookies
* @param array $files
* @param array $server
* @param null $content
* @return \Illuminate\Http\Response|JsonResponse
*/
public function postAction($actionArr, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
return $this->call('POST', getActionUrl($actionArr), $parameters, $cookies, $files, $server, $content);
}
/**
* 判断getAction和postAction的api的值
*
* @param JsonResponse $data
*/
public function assertApiActionSuccess($data)
{
$this->assertResponseOk();
$this->assertTrue($this->ifSuccessByApiAction($data));
}
/**
* 获取getAction和postAction的api的data
*
* @param JsonResponse $data
* @param array $default
* @return array
*/
protected function handleApiActionData($data, $default = [])
{
$jsonString = '';
if ($data instanceof JsonResponse){
$jsonString = $data->content();
}
return SysResponseJsonFormat::instance()->getDataByDecode($jsonString, $default);
}
/**
* 判断getAction和postAction的api的code是否为success
*
* @param JsonResponse $data
* @return bool
*/
protected function ifSuccessByApiAction($data)
{
$jsonString = '';
if ($data instanceof JsonResponse){
$jsonString = $data->content();
}
return SysResponseJsonFormat::instance()->ifSuccessByDecode($jsonString);
}
}
以上是关于php laravel测试用例的主要内容,如果未能解决你的问题,请参考以下文章
在 Laravel 测试用例中模拟一个 http 请求并解析路由参数