关键词:
【中文标题】PHPUnit - getallheaders 不起作用【英文标题】:PHPUnit - getallheaders not work 【发布时间】:2017-05-16 14:12:19 【问题描述】:我正在测试我的代码,但标题有问题。在我使用的每个 api 中
$headers = getallheaders();
要做到这一点,当我使用应用程序或 crhome 邮递员扩展程序进行测试时,这可以正常工作。 当我启动我的测试时,像这样
$client = $this->createClient();
$client->request('GET', '/api/shotcard',
['qrcode'=>'D0m1c173'], [],
['HTTP_API_TOKEN' => 'abc123']
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
我尝试使用该 qrcode 与具有该测试令牌(不是我将在应用程序中使用的令牌)的用户一起拍摄一张卡片,我在这里看到这样的调用:https://***.com/a/11681422/5475228。 测试以这种方式失败:
PHP 致命错误:在第 42 行的 /var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php 中调用未定义函数 AppBackendBundle\Controller\getallheaders()
【问题讨论】:
来自文档:此函数是 apache_request_headers() 的别名。请阅读 apache_request_headers() 文档以获取有关此函数如何工作的更多信息。 php.net/manual/en/function.apache-request-headers.php 您使用的是哪个版本的php?自 PHP 5.5.7 起,它应该可以从 CLI 获得 【参考方案1】:来自this文章:
如果您使用 Nginx、PHP-FPM 或任何其他运行 PHP 的 FastCGI 方法 你可能已经注意到函数
getallheaders()
没有 存在。野外有许多创造性的解决方法,但 PHP 提供 两个非常好的功能可以减轻您的痛苦。
来自用户在getallheaders()
joyview at gmail dot comjoyview at gmail dot com 的 PHP 手册函数中贡献的 cmets
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 不使用,我也尝试打印标题,PHPUnit 打印一个空值。我正在使用 php 5.5.9 不幸的是,这只能识别以HTTP_
为前缀的标头。此实现不会检索 Accept
、User-Agent
等标头以及任何自定义 HTTP 标头。
是的@OndřejBouda,但你可以实现添加else
声明的行为我想
@Matteo 抱歉评论有误,$_SERVER
实际上包含 Accept
和User-Agent
。但是,对于自定义 HTTP 标头,可能无法通过这种方式检索这些标头。变量$_SERVER
只包含几个标准的HTTP头,如documented,而getallheaders()
则返回客户端发送的所有头。
嗨@OndřejBouda HTTP_
技巧是关于BrowserKit
实现的,如here 所述示例:headers are also part of $_SERVER, by prefixing them with HTTP_
【参考方案2】:
我就是这样解决的(感谢https://***.com/a/11681422/5475228)
private function request_headers($type, Request $request)
if(function_exists("getallheaders"))
if($header = getallheaders()[$type])
return $header;
return $request->headers->get($type);
所以来自app的正常请求使用getallheaders()获取header,来自PHPUnit的请求从Request对象中获取。我不知道为什么(如果有人可以解释的话)但是有效。
【讨论】:
【参考方案3】:与@Matteos 代码略有不同,它删除了 Mod-Rewrite 并添加了通常由 getallheaders()
返回的 Content-Length 和 Content-Type。有趣的是,getallheaders()
返回的数组键的情况似乎无处不在,而且不一致,而显然这个版本确保了一致性。
$allHeaders = array();
foreach($_SERVER as $name => $value)
if($name != 'HTTP_MOD_REWRITE' && (substr($name, 0, 5) == 'HTTP_' || $name == 'CONTENT_LENGTH' || $name == 'CONTENT_TYPE'))
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', str_replace('HTTP_', '', $name)))));
$allHeaders[$name] = $value;
【讨论】:
运行phpunit时不显示测试反馈
】运行phpunit时不显示测试反馈【英文标题】:Notestfeedbackisdisplayedwhenrunningphpunit【发布时间】:2020-09-0916:55:57【问题描述】:尝试此命令时docker-composeexecappphp./vendor/bin/phpunit或者像/vendor/bin/phpunit这样的容器内结果如下:PHPUnit8.5.4b... 查看详情
MimeMessage.getAllHeaders() 在两个不同的项目中返回两个不同的类
】MimeMessage.getAllHeaders()在两个不同的项目中返回两个不同的类【英文标题】:MimeMessage.getAllHeaders()returnstwodifferentclassesintwodifferentprojects【发布时间】:2015-05-0407:55:51【问题描述】:我有一个关于邮件的项目并且可以正常工作,但... 查看详情
PHPUnit 类“错误”不扩展 PHPUnit_Framework_TestCase
】PHPUnit类“错误”不扩展PHPUnit_Framework_TestCase【英文标题】:PHPUnitClass"Error"doesnotextendPHPUnit_Framework_TestCase【发布时间】:2018-06-0508:37:45【问题描述】:我正在使用Laravel4.2和PHP5.6。我做了一个作曲家更新,现在当我运行我... 查看详情
`xhr.getAllHeaders()` 在浏览器扩展上下文中缺少一些标题?
】`xhr.getAllHeaders()`在浏览器扩展上下文中缺少一些标题?【英文标题】:`xhr.getAllHeaders()`ismissingsomeheadersinabrowserextensioncontext?【发布时间】:2021-12-1300:52:58【问题描述】:我正在开发浏览器扩展程序。在弹出页面上运行的脚本中... 查看详情
PHPUnit ReflectionException 方法套件不存在
】PHPUnitReflectionException方法套件不存在【英文标题】:PHPUnitReflectionExceptionMethodsuitedoesnotexist【发布时间】:2012-01-0811:27:15【问题描述】:我正在使用PHPUnit3.6.2版,并且总是得到PHPReflectionException:第113行的/pathTo/pear/PHPUnit/Runner/Base... 查看详情
PHPUnit 丢失类名(ReflectionClass 类不存在错误)
】PHPUnit丢失类名(ReflectionClass类不存在错误)【英文标题】:PHPUnitlosingclassname(ReflectionClassclassdoesnotexisterror)【发布时间】:2013-06-2023:14:09【问题描述】:我正在尝试让PHPUnit工作;我以前没用过。如果我在CLI中运行phpunit,它可... 查看详情
PHPUnit 在预期异常后不继续测试
】PHPUnit在预期异常后不继续测试【英文标题】:PHPUnitdoesn\'tcontinuetestafterexpectinganexception【发布时间】:2013-01-1118:48:59【问题描述】:为什么PHPUnit在这段代码中不做最后一个异常断言?publicfunctiontestConfigOverriding()$this->dependencyC... 查看详情
phpunit 测试返回 302 验证错误,为啥不返回 422
】phpunit测试返回302验证错误,为啥不返回422【英文标题】:phpunittestreturns302forbadvalidation,whynot422phpunit测试返回302验证错误,为什么不返回422【发布时间】:2016-05-0316:41:18【问题描述】:我有一个请求类,对于发布请求失败。当... 查看详情
phpunit xml配置颜色指令不起作用
】phpunitxml配置颜色指令不起作用【英文标题】:phpunitxmlconfigurationcolorsinstructiondosen\'twork【发布时间】:2016-05-1106:24:15【问题描述】:我为我的单元测试定义了一个phpunit.xml配置文件。在这个文件中,我指示在单元测试结果中显... 查看详情
不推荐使用 composer require-dev 安装 phpunit?
】不推荐使用composerrequire-dev安装phpunit?【英文标题】:Installphpunitwithcomposerrequire-devnotrecommended?【发布时间】:2016-04-1519:25:32【问题描述】:在我的composer包中,如果我的包中有phpunit测试,我会将phpunit添加到我的composer.json的requ... 查看详情
phpunit mock - 方法不存在
】phpunitmock-方法不存在【英文标题】:phpunitmock-methoddoesnotexist【发布时间】:2017-01-2819:03:52【问题描述】:我最近在基于CakePhp3.x的应用程序的IntegrationTestCase中将PHPunit从5.3更新到5.5。而且我不明白如何更新我的模拟生成脚本。最... 查看详情
PHPUnit 7.2 和 Codeception 2.4 不兼容
】PHPUnit7.2和Codeception2.4不兼容【英文标题】:PHPUnit7.2andCodeception2.4areincompatible【发布时间】:2019-01-0812:19:09【问题描述】:尝试使用Laravel框架5.6安装CodeCeption我收到此错误。我安装的步骤是1.composer需要codeception/codeception--dev2.php./... 查看详情
PHPUnit Guzzle 响应不起作用
】PHPUnitGuzzle响应不起作用【英文标题】:PHPUnitGuzzleResponsenotworking【发布时间】:2017-02-2310:05:41【问题描述】:我开始使用PHPUnit和Guzzle来测试一些API,但是当我运行以下单元测试时,我没有收到正确的json。代码有什么问题?<?... 查看详情
Lumen PHPUNIT 不使用 .env.testing
】LumenPHPUNIT不使用.env.testing【英文标题】:LumenPHPUNITnotusingthe.env.testing【发布时间】:2020-05-2319:40:37【问题描述】:我在Lumen中遇到了测试问题。我有我的phpunit.xml:<?xmlversion="1.0"encoding="UTF-8"?><phpunitbackupGlobals="false"backupStat... 查看详情
phpunit selenium2重复测试不起作用
】phpunitselenium2重复测试不起作用【英文标题】:phpunitselenium2repeattestnotworking【发布时间】:2018-05-1219:49:12【问题描述】:我正在尝试使用selenium2运行phpunit来测试我的php代码。phpversion:5.6laravelversion:5.2phpunitvesrion:PHPUnit3.7.28bySebastia... 查看详情
PhpUnit 点击提交后不等待加载页面
】PhpUnit点击提交后不等待加载页面【英文标题】:PhpUnitNotwaitingtoloadpageafterclickingsubmit【发布时间】:2014-09-1814:29:36【问题描述】:我正在阅读一个教程,在PHPUNIT上我得到了很好的教程退出......并进行了一些登录我自己..但我进... 查看详情
php Codeception 不那么快开始:[PHPUnit\Framework\Exception] 未定义索引:ELEMENT
】phpCodeception不那么快开始:[PHPUnit\\\\Framework\\\\Exception]未定义索引:ELEMENT【英文标题】:phpCodeceptionnot-so-quickstart:[PHPUnit\\Framework\\Exception]Undefinedindex:ELEMENTphpCodeception不那么快开始:[PHPUnit\\Framework\\Exception]未定义索引: 查看详情
Composer 更新 codeception - phpunit 不更新
】Composer更新codeception-phpunit不更新【英文标题】:Composerupdatingcodeception-phpunitdoesnotupdate【发布时间】:2017-04-2501:00:36【问题描述】:我跑rm-rfvendor&&composerupdatecodeception/codeception-v我得到错误:您的需求无法解决为一组可安装... 查看详情