問題描述
我收到了許多與此完全相同的錯誤:
Zend_Session_Exception:會話必須在任何輸出發送到瀏覽器之前啟動;輸出開始于/usr/local/zend/share/pear/PHPUnit/Util/Printer.php/173
運行我的應用程序的測試套件時.這是 PHPUnit 3.5.10 和 PHP 5.3.5.
沒有神秘的、意外的空白輸出導致這種情況.我已經確定發送到瀏覽器的輸出"是正在執行的 PHPUnit 測試的實際輸出.如果我打開 PHPUnit/Util/Printer.php 并將 print $buffer
行與 if (strpos($buffer, 'PHPUnit 3.5.10 by Sebastian Bergmann') === false)
(有效停止PHPUnit的第一行輸出),然后我的第一個測試成功(直到測試用例輸出一個點表示測試成功,然后下一個測試失敗,因為輸出了點).>
我團隊中的另一位開發人員能夠成功運行完整的測試套件,所以我知道這不是應用程序代碼的問題.一定是我本地環境的一些配置設置或問題.
我已經檢查了 php.ini 以驗證 output_buffering
已打開并且 implicit_flush
已關閉,它們確實如此.
我還嘗試將 Zend_Session::$_unitTestEnabled = true;
添加到我的測試引導程序中,但這并沒有幫助(無論如何應該沒有必要,因為它可以在另一個開發人員的機器上運行在我們的 CI 服務器上沒有它).
除了忽略錯誤之外還有什么建議嗎?我從來沒有見過這樣的事情,真的很茫然.
謝謝!
更新:
為了進一步隔離問題,我通過執行以下測試腳本將 ZF 和我的應用程序排除在外:
assertTrue(true);}}
測試失敗:
1) SessionTest::testSessionsession_start():無法發送會話 cookie - 頭已經發送(輸出開始于/home/mmsa/test.php:1)
然而,完全相同的測試適用于朋友的機器.相同版本的 PHP 和 PHPUnit.
使用 -stderr 標志運行 phpunit,(新版本可能使用 --stderr 代替)例如
phpunit -stderr mytest.php# 或者phpunit --stderr mytest.php
這會將 phpunit 的輸出定向到 stderr,防止它中斷 HTTP 標頭生成.
該測試可能適用于您朋友的機器,因為他啟用了輸出緩沖(盡管我不確定這在 CLI 上下文中是否相關).
I am getting numerous errors exactly like this one:
Zend_Session_Exception: Session must be started before any output has been sent to the browser; output started in /usr/local/zend/share/pear/PHPUnit/Util/Printer.php/173
When running my application's test suite. This is with PHPUnit 3.5.10 and PHP 5.3.5.
There is no mysterious, unexpected whitespace output that is causing this. I've determined that the "output being sent to the browser" is the actual output from the PHPUnit tests being executed. If I open up PHPUnit/Util/Printer.php and wrap the print $buffer
line with if (strpos($buffer, 'PHPUnit 3.5.10 by Sebastian Bergmann') === false)
(effectively stopping the first line of output from PHPUnit), then my first test succeeds (until the test case outputs a dot indicating that the test succeeded, then the next test fails because the dot was output).
Another developer on my team is able to run the full test suite successfully, so I know it's not a problem with the application code. It must be some configuration setting or problem with my local environment.
I've already checked php.ini to verify that output_buffering
is turned on and implicit_flush
is turned off, and they are.
I've also tried adding Zend_Session::$_unitTestEnabled = true;
to my test bootstrap, but that didn't help (and shouldn't be necessary anyway because it works on another developer's machine and on our CI server without it).
Any suggestions besides ignoring the errors? I've never seen anything like this and am truly at a loss.
Thanks!
UPDATE:
To attempt to further isolate the problem, I took ZF and my application out of the equation by executing the following test script:
<?php
class SessionTest extends PHPUnit_Framework_TestCase
{
public function testSession()
{
session_start();
$this->assertTrue(true);
}
}
The test fails:
1) SessionTest::testSession
session_start(): Cannot send session cookie - headers already sent by (output started at /home/mmsa/test.php:1)
However, the exact same test works on a friend's machine. Same version of PHP and PHPUnit.
Run phpunit with the -stderr flag, (newer versions may use --stderr instead) e.g.
phpunit -stderr mytest.php
# or
phpunit --stderr mytest.php
This directs phpunit's output to stderr, preventing it from interrupting HTTP header generation.
It's possible that the test works on your friend's machine because he has output buffering enabled (although I'm not sure if that's relevant in a CLI context).
這篇關于PHPUnit 輸出導致 Zend_Session 異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!