我希望通过单元测试来测试后端控制器是否正常运行,因此在进行更改时不必手动检查它们。
不幸的是,在我尝试执行的所有操作中,我都找不到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>
我试图访问后端URL的测试
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 Illuminate\Foundation\Testing\TestResponse {#55 +baseResponse: Illuminate\Http\Response {#1696 +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#1695 #computedCacheControl: array:2 [ "no-cache" => true "private" => true ] #cookies: array:1 [ "" => array:1 [ "/" => array:2 [ "october_session" => Symfony\Component\HttpFoundation\Cookie {#1073 #name: "october_session" #value: "eyJpdiI6Imh1SHlCUkhcL29rMlk2dFBkZTRNWlpnPT0iLCJ2YWx1ZSI6IjFJamxxRW9aQkM5bk9kM2VnMExLUTdVNER0MzlcL2xacFdkMCs0dTdXR1N0NVwva01KaTVwbnhVUVI2S0Q2MmRJaldWdEtyRldBcmNkc2dWMnhrVTJoZ3c9PSIsIm1hYyI6IjUyYTJiYWRlNDUzYTFjZjRhMDVhNzliMzJjNGVhZTBjNWViMDlmNTJkMWM3NjhlZGE4NTRjYjhiY2U3M2EzNmUifQ==" #domain: null #expire: 1537286593 #path: "/" #secure: false #httpOnly: true -raw: false -sameSite: null } "admin_auth" => Symfony\Component\HttpFoundation\Cookie {#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: "Page not found
" #version: "1.1" #statusCode: 404 #statusText: "Not Found" #charset: null +original: "Page not found
" +exception: null } }
我也尝试过直接调用控制器,但是正常请求填充的很多值在BackendController中为空。
我需要更改哪些设置以便能够测试我的后端控制器?
答案 0 :(得分:1)
要测试控制器是否将运行,您需要将执行上下文设置为后端并运行后端服务提供程序。
即使如此,您仍需要为可以使用的内存数据库设置管理员测试用户,因为否则控制器将尝试将您重定向到登录页面。
use BackendAuth;
use PluginTestCase;
use Backend\Models\User;
use Backend\Classes\Controller;
use MyAuthor\MyPlugin\Controllers\MyController;
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('\Backend\ServiceProvider');
$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());
}
}