session_regenerate_id() - 单元测试 Yii 控制器中已经发送的标头
我正在尝试对我的控制器(Yii 框架)进行单元测试.
I'm trying to unit-test my controller (Yii framework).
/**
* @dataProvider provider
*/
public function testActionEdit_view_login($controller){
$user = new CWebUser;
$user->id = 978;
$identity = new UserIdentity('me@test.com', '123456');
$user->login($identity);
$controller->actionEdit();
$output = ob_get_contents();
assertContains('Add/Change Profile Picture:', $output);
assertContains('bio', $output);
assertContains('specialties', $output);
assertContains('change login', $output);
assertContains('New Password', $output);
}
当我这样做
$user->login($identity);
为了登录,我收到以下错误:
in order to login, I get the following error:
session_regenerate_id(): Cannot regenerate session id - headers already sent
我已经尝试通过将其放在课程开头来缓冲输出:
I've already tried buffering the output by putting this at the beginning of the class:
public static function setUpBeforeClass(){
ob_start();
}
我还将 ob_clean() 放在 setUp() 中,将 ob_end_clean() 放在 tearDownAfterClass() 中.
I also put ob_clean() in setUp() and ob_end_clean() in tearDownAfterClass().
我仍然收到标题已经发送的消息.文件中没有空格或换行符,当我注释掉特定的测试方法时,它完美地工作.login() 似乎导致了问题.
Still I get the message that headers have already been sent. There are no spaces or newlines in the file, when I comment out the specific test method, it works perfectly. The login() seems to cause the problem.
有人知道如何防止这种情况发生/可能对控制器进行不同的单元测试吗?
Anyone got ideas how to prevent this / maybe unit-test the controller differently?
谢谢,先生
明白了.我在 ob_start() 之前包含了一些 Yii 文件,它们似乎已经打印了标题.现在我也缓冲了.
Got it. I had included some Yii files before the ob_start(), which seem to have printed the headers. Now I buffer that, too.