PHPUnit Unable to send cookie session - headers already sent

When using PHPUnit through the code snippet below in Eclipse, I get an error:

Unable to send cookie session - headers already sent (launch launched in C: \ wamp \ bin \ php \ php5.3.13 \ pear \ PHPUnit \ Util \ Printer.php: 172)

Session_start () is executed in "LoginTest". How can I stop PHPUnit from interfering with session cookie generation?

<?php error_reporting(E_ALL); ini_set('display_errors', 1); require_once 'C:\wamp\bin\php\php5.3.13\pear\PHPUnit\autoload.php'; class MyTestCase extends PHPUnit_Framework_TestCase { static function main(){ $suite = new PHPUnit_Framework_TestSuite("LoginTest"); //$suite = new PHPUnit_Framework_TestSuite("FriendListTest"); //$suite = new PHPUnit_Framework_TestSuite("UserTest"); PHPUnit_TextUI_TestRunner::run($suite); } } MyTestCase::main(); ?> 

A very similar problem with a solution that won't let me debug in Eclipse.

+4
source share
2 answers

At the top of the unit test task (or if all functions are affected, then in setUp() or even public static function setUpBeforeClass() ) add this code:

 @session_start(); 

Or in the long run:

 $prev = error_reporting(0); session_start(); error_reporting($prev); 

It seems like you should never do, but it's fine, because suppressing the error message was exactly what we wanted to do!


As I noticed in a comment on hakre's answer, messing with the ini settings never worked, they didn’t even try to do this combination before calling session_start() :

  ini_set("session.use_cookies",0); ini_set("session.use_only_cookies",0); ini_set("session.use_trans_sid",1); 
+3
source

How can I stop PHPUnit from interfering with session cookie generation?

The most straightforward seems to not use cookies for sessions:

http://php.net/session.use-cookies

0
source

Source: https://habr.com/ru/post/1481821/


All Articles