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

查看:30
本文介绍了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 Fatal error: Call to undefined function AppBackendBundle\Controller\getallheaders() in/var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php on line 42

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.

来自 getallheaders() 函数的用户贡献评论rel="noreferrer">gmail dot com 上的joyview

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天全站免登陆