如何为后端控制器执行单元测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何为后端控制器执行单元测试相关的知识,希望对你有一定的参考价值。
我想测试我的后端控制器是否通过单元测试正常运行,所以我不需要在更换时手动检查它们。
不幸的是,我尝试的一切都找不到404。
我的phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="../../../tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="ExitControl.Administration test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
我试图访问后端网址的测试
public function setUp()
{
parent::setup();
$user = User::create([
'email' => 'tester@admin.com',
'login' => 'tester',
'password' => 'test',
'password_confirmation' => 'test',
'send_invite' => false,
'is_activated' => true,
]);
$user->is_superuser = true;
$user->save();
$user = BackendAuth::authenticate([
'login' => 'tester',
'password' => 'test'
], true);
}
public function testCreateIndexView()
{
$controller = $this->getController();
$configUrl = config('app.url') . '/' .
config('cms.backendUri') . '/';
$url = preg_replace('/([^:])(/{2,})/', '$1/',
$configUrl .
str_replace(['controllers\', '\'], ['', '/'],
strtolower(get_class($controller))));
$result = $this->get($url);
dd($result);// shows a 404
}
dd()的输出;
PHPUnit 5.7.27 by Sebastian Bergmann and contributors. CreateExitcontrolAdministrationCompaniesTable > dropping table exitcontrol_administration_companies CreateExitcontrolAdministrationCompaniesTable > creating table exitcontrol_administration_companies IlluminateFoundationTestingTestResponse {#55 +baseResponse: IlluminateHttpResponse {#1696 +headers: SymfonyComponentHttpFoundationResponseHeaderBag {#1695 #computedCacheControl: array:2 [ "no-cache" => true "private" => true ] #cookies: array:1 [ "" => array:1 [ "/" => array:2 [ "october_session" => SymfonyComponentHttpFoundationCookie {#1073 #name: "october_session" #value: "eyJpdiI6Imh1SHlCUkhcL29rMlk2dFBkZTRNWlpnPT0iLCJ2YWx1ZSI6IjFJamxxRW9aQkM5bk9kM2VnMExLUTdVNER0MzlcL2xacFdkMCs0dTdXR1N0NVwva01KaTVwbnhVUVI2S0Q2MmRJaldWdEtyRldBcmNkc2dWMnhrVTJoZ3c9PSIsIm1hYyI6IjUyYTJiYWRlNDUzYTFjZjRhMDVhNzliMzJjNGVhZTBjNWViMDlmNTJkMWM3NjhlZGE4NTRjYjhiY2U3M2EzNmUifQ==" #domain: null #expire: 1537286593 #path: "/" #secure: false #httpOnly: true -raw: false -sameSite: null } "admin_auth" => SymfonyComponentHttpFoundationCookie {#1072 #name: "admin_auth" #value: "eyJpdiI6InVWM1pFNExzMytCMzZQWEJISTJoOFE9PSIsInZhbHVlIjoiU1l2NitIUDNFd3JVeHdhenh2alV5YWxtMW1vYTFsbUhweDlJd3RcL2xDQnQyY3cwMVAzOFNDQjdrQXNZS1JFUmV4U2FVWG5XV0g3d0VOYStIR2hxV25nMDNlc1wvdStma2hkYlZTV01vemg0R1FpXC80ZWQ5YjMwcjdPSW9aRnJzS1MiLCJtYWMiOiJiYWFjODE2NTBjOGQ0NjkwNmJlYzVjZjFhOTJjMjdmYzBkMTA2MDEzNTM0MTljMmE2Njc1Njc0NTlmMGQ5ZWY4In0=" #domain: null #expire: 1694959389 #path: "/" #secure: false #httpOnly: true -raw: false -sameSite: null } ] ] ] #headerNames: array:4 [ "cache-control" => "Cache-Control" "date" => "Date" "content-type" => "Content-Type" "set-cookie" => "Set-Cookie" ] #headers: array:3 [ "cache-control" => array:1 [ 0 => "no-cache, private" ] "date" => array:1 [ 0 => "Tue, 18 Sep 2018 14:03:13 GMT" ] "content-type" => array:1 [ 0 => "text/html; charset=UTF-8" ] ] #cacheControl: [] } #content: "网页未找到
" #version: "1.1" #statusCode: 404 #statusText: "Not Found" #charset: null +original: "网页未找到
" +exception: null } }
我也试过直接调用控制器,但是正常请求填充的很多值在BackendController中都是空的。
我需要更改什么,设置为能够测试我的后端控制器?
答案
要测试控制器是否运行,您需要将执行上下文设置为后端并运行后端服务提供程序。
即便如此,您还需要为可以使用的内存数据库设置管理测试用户,否则控制器将尝试将您重定向到登录页面。
use BackendAuth;
use PluginTestCase;
use BackendModelsUser;
use BackendClassesController;
use MyAuthorMyPluginControllersMyController;
class BasicBackendControllerTest extends PluginTestCase
{
public function setUp()
{
parent::setup();
$user = User::create([
'email' => 'tester@admin.com',
'login' => 'tester',
'password' => 'test',
'password_confirmation' => 'test',
'send_invite' => false,
'is_activated' => true,
]);
$user->is_superuser = true;
$user->save();
$user = BackendAuth::authenticate([
'login' => 'tester',
'password' => 'test'
], true);
}
public function testCreateIndexView()
{
// default this will be at front-end. This needs to be set to
// back-end so the service provider we will load will load up
// the backend settings.
app()->setExecutionContext('back-end');
// registering and running the service provider
$provider = app()->register('BackendServiceProvider');
$provider->boot();
// the magic happens here that allows a controller to be run.
$provider->register();
// retrieve the controller instance from child class
$controller = new MyController();
$result = $controller->run('index');
$this->assertEquals(200, $result->status());
}
}
以上是关于如何为后端控制器执行单元测试的主要内容,如果未能解决你的问题,请参考以下文章