Php 如何对后端控制器执行单元测试

Php 如何对后端控制器执行单元测试,php,phpunit,octobercms,octobercms-plugins,octobercms-backend,Php,Phpunit,Octobercms,Octobercms Plugins,Octobercms Backend,我希望通过单元测试来测试我的后端控制器是否正常运行,这样在改变情况时就不必手动检查它们 不幸的是,在我尝试的每件事中,我都没有找到404 我的phpunit.xml <?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="../../../tests/bootstrap.php

我希望通过单元测试来测试我的后端控制器是否正常运行,这样在改变情况时就不必手动检查它们

不幸的是,在我尝试的每件事中,我都没有找到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>
dd()的输出

PHPUnit 5.7.27由塞巴斯蒂安·伯格曼和贡献者撰写。 创建exitcontrol管理公司表>删除表exitcontrol\u管理公司 CreateExitControl管理公司表>创建表exitcontrol\u管理公司 照亮\基础\测试\测试响应{#55 +baseResponse:illumb\Http\Response{#1696 +标题:Symfony\Component\HttpFoundation\ResponseHeaderBag{#1695 #computedCacheControl:数组:2[ “无缓存”=>true “private”=>true ] #cookies:数组:1[ “”=>数组:1[ “/”=>数组:2[ “十月会话”=>Symfony\Component\HttpFoundation\Cookie{1073 #名称:“10月会议” #价值:“EYJPDII6IMH1SHLCUKHCL29RMLK2DFBKZTRNWLP0ILCJ2YWX1ZSI6IJFJAMXXRW9AQKM5BKK9KM2VNMEXLUTDVNER0MZLCl2xACFDKMCS0DTDXR1N0NVWVA01KATVWBNHVUVI2S0Q2MMRJALDWDETYRLDBCMNKC2DWMNHRVTJOZ9PSISIMI6YYYJJJJJJJJJJJRYYYYYYJJJJJJJJJJJJJJNWMWNJHZLMWNJJHZLJHHLZHHHHHZY2NJZYY2Q=”2 #域:空 #到期日期:1537286593 #路径:“/” #安全:错误 #httpOnly:正确 -生:假 -sameSite:null } “admin#u auth”=>Symfony\Component\HttpFoundation\Cookie{1072 #名称:“管理员授权” #价值:“EYJPDII6INVWM1PFNEXZMYTCMZZZQWEJISTJOF9PSISINZHBHVLIJOIU2NITIUDNFD3JVEHDHENH2ALVYTXTMW1VYTFSBUHWDL3RCL2XDQNQYY3CWMVAZOFNDQJDRQXNZS1JFUMV2MFWWWW5XV0G3D00VOYSTIR2HXV25NMDNLC1WVDSTMA2HKYLZTV01VEMG0R1FPXC80ZW5YW5YWWYWCJDPZW9ARNJZZJJJZKW0MJKMJJJZYZYZZM0MJKMJ0MJJJZYZZYZZZZM0M=”价值 #域:空 #过期日期:1694959389 #路径:“/” #安全:错误 #httpOnly:正确 -生:假 -sameSite:null } ] ] ] #头部名称:数组:4[ “缓存控制”=>“缓存控制” “日期”=>“日期” “内容类型”=>“内容类型” “设置cookie”=>“设置cookie” ] #标题:数组:3[ “缓存控制”=>阵列:1[ 0=>“无缓存,专用” ] “日期”=>数组:1[ 0=>“2018年9月18日星期二14:03:13 GMT” ] “内容类型”=>数组:1[ 0=>“text/html;charset=UTF-8” ] ] #缓存控制:[] } #内容:“找不到页面

” #版本:“1.1” #状态代码:404 #statusText:“未找到” #字符集:null +原件:未找到页面

“ +例外:空 } } 我还尝试直接调用控制器,但是在BackendController中,由正常请求填充的许多值都是空的


我需要更改什么设置才能测试我的后端控制器?

要测试控制器是否运行,需要将执行上下文设置为后端并运行后端服务提供程序

即使如此,您仍然需要为可以使用的内存中数据库设置一个管理测试用户,因为否则控制器将尝试将您重定向到登录页面

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());
    }
}
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 } }
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());
    }
}