PHPUnit-getallheaders不起作用 [英] PHPUnit - getallheaders not work

查看:91
本文介绍了PHPUnit-getallheaders不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我的代码,并且标题有一些问题.在每个api中,我都使用

I'm testing my code, and i have some problem with header. In each api i use

$headers = getallheaders();

得到它,当我使用应用程序或crhome邮递员扩展程序进行测试时,此方法工作正常. 当我对测试进行测试时,就这样

to get that, and this works fine when i test with the app or crhome postman extension. When i lauch my test, like this

 $client = $this->createClient();
    $client->request('GET', '/api/shotcard',
        ['qrcode'=>'D0m1c173'], [],
        ['HTTP_API_TOKEN' => 'abc123']
    );

    $this->assertEquals(200, $client->getResponse()->getStatusCode());

我尝试用带有该测试令牌(而不是我将在应用程序中使用的令牌)的用户向该卡拍摄带有该qrcode的卡片,在这里我看到这样的呼叫:https://stackoverflow.com/a/11681422/5475228 . 测试以这种方式失败:

where i try to shot a card with that qrcode with a user with that test token (not the token i'll use in the application), i see a call like this here: https://stackoverflow.com/a/11681422/5475228 . The test fails in this way:

PHP致命错误:在第42行的/var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php中调用未定义函数AppBackendBundle \ Controller \ getallheaders()

PHP Fatal error: Call to undefined function AppBackendBundle\Controller\getallheaders() in /var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php on line 42

推荐答案

来自

如果您使用Nginx,PHP-FPM或任何其他运行PHP的FastCGI方法 您可能已经注意到函数getallheaders()没有 存在.有很多创造性的解决方法,但是PHP提供了 两项非常不错的功能可以减轻您的痛苦.

If you use Nginx, PHP-FPM or any other FastCGI method of running PHP you’ve probably noticed that the function getallheaders() does not exist. There are many creative workarounds in the wild, but PHP offers two very nice features to ease your pain.

joyview中,用户在PHP手册上的getallheaders()功能上发表的评论在gmail点com上

From user contributed comments at getallheaders() function on PHP manual by joyview at gmail dot com

if (!function_exists('getallheaders')) {
    function getallheaders() {
    $headers = [];
    foreach ($_SERVER as $name => $value) {
        if (substr($name, 0, 5) == 'HTTP_') {
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
        }
    }
    return $headers;
    }
}

这篇关于PHPUnit-getallheaders不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆