无法发送会话Cookie-标头已发送PHPUnit/Laravel [英] Cannot send session cookie - headers already sent PHPUnit / Laravel

查看:113
本文介绍了无法发送会话Cookie-标头已发送PHPUnit/Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在TestCase类上调用parent::setUp()进行单元测试一个类时,我遇到了一个奇怪的问题 当我运行phpunit时,将引发此错误:

I have this strange problem when i call the parent::setUp() on my TestCase class for unit test a class when i run phpunit it throw me this error:

1)MatchRequestRepositoryTest :: test_find_requests_by_match_id ErrorException:session_start():无法发送会话cookie-标头 已发送者(输出始于 /var/www/project.dev/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:459)

1) MatchRequestRepositoryTest::test_find_requests_by_match_id ErrorException: session_start(): Cannot send session cookie - headers already sent by (output started at /var/www/project.dev/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:459)

可能是什么问题?感谢您的帮助.

What can be the problem? Thanks for any help.

推荐答案

(更新:感谢Louis Charette的评论,这仅适用于php 7.1及更早版本(并且阅读了错误报告,听起来像是回归,他们因此,最好的解决方案是Jeff Puckett的( https://stackoverflow.com/a/38045422/841830),将命令行上的--stderr标志提供给phpunit.这将为您的代码保留stdout,为phpunit保留stderr,因此它们不会发生冲突.)

(UPDATE: Thanks to comment by Louis Charette, this will only work in php 7.1 and earlier (and reading the bug report, it sounds like a regression they don't intend to fix. The better solution is therefore Jeff Puckett's (https://stackoverflow.com/a/38045422/841830), of giving the --stderr flag on the commandline to phpunit. This keeps stdout for your code, stderr for phpunit, and so they don't clash.)

问题在于您可能在所使用的框架中有一些代码调用了session_start().那又想发送一个cookie.但是PHPUnit已经开始将输出写入stdout.

The problem is that you have some code, perhaps deep in the framework you use, that calls session_start(). That, in turn, wants to send a cookie. But PHPUnit has already started writing output to stdout.

这里要了解的一点是,这只是一个单元测试,没有人关心头文件.因此,只需取消显示错误消息即可.在不更改被测系统的情况下,执行此操作的方法是在您自己的单元测试中(在parent::setUp()之前或在setUp函数内部)调用session_start().并使用@前缀来抑制错误.例如

The point to understand here is that this is just a unit test, no-one cares about the header. So just suppress the error message. And the way you do that, without altering the system-under-test, is to call session_start() in your own unit test (either before parent::setUp() or inside that setUp function). And use the @ prefix to suppress errors. e.g.

function setUp() {
  @session_start();
  parent::setUp();
  ...
}

这篇关于无法发送会话Cookie-标头已发送PHPUnit/Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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