简体   繁体   中英

Headers already sent running unit tests in PHPStorm

I'm trying to use the integrated method of running phpunit tests within PHPStorm and having some issues getting it to work.

PHP: 5.6.10
PHPUnit: 4.5.1
PHPStorm: 8.0.3

Thanks to https://stackoverflow.com/a/30345501/1636594 I learned that I had to downgrade my phpunit version to < 4.6.

Finding https://stackoverflow.com/a/25903332/1636594 I tried both @runInSeparateProcess notation and strerr="false|true" in my configuration. With process isolation, I saw the same issue as this user (basically phpunit --help instead of each test in isolation). With stderr="false|true" I get the same either way (Cannot modify header information... blah blah).

The tests run fine without process isolation or stderr set to either one of true or false, both give successful passing tests. 在终端中通过的测试的屏幕截图,在集成运行器中失败

For now I'm obviously just running my tests in the terminal, but I would really like to be able to use the coverage feature but its basically useless for any line of code after a header call.

I had same problem. When I try to run any of the codeception tests (specifically for Yii2) with the built-in PHPStorm tools, I get the message:

session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

I didn't reach a long time, but yesterday after midnight dances with universal tambourines I found out literally the following:

PHPStorm (jetBrains for Google) uses the Codeception Framework plugin to place the file:

C:/Users/dev/AppData/Local/Temp/ide-codeception_before_24.php:405

from which, using the printEvent method, logs to the console via Printer::write() :

vendor/phpunit/phpunit/src/Util/Printer.php:99

how to fix it nicely, I did not find, so "here is our answer to Chamberlain!": we write ob_start() in tests/_bootstrap.php :

<?php
require_once __DIR__. '/../vendor/yiisoft/yii2/Yii.php';
require_once __DIR__. '/ .. / vendor / autoload.php';

ob_start();

the buffer is flushed to the console automatically at the end of the tests. We get the following:

运行代码接收运行测试功能的示例

This can be fixed by setting the PHP error reporting level to E_ERROR before the the call to the code that eventually results in the error:

...
error_reporting(E_ERROR);
expect_not($this->model->login());
...

For a unit test, this can be achieved for all test functions by calling error_reporting() inside _before() :

protected function _before()
{
    parent::_before();
    error_reporting(E_ERROR);
}

At a higher level, we can replace the default codeception error reporting level of unit tests by setting error_level in unit.suite.yml :

class_name: UnitTester
modules:
  enabled:
    - Asserts
    - Yii2:
        part: [orm, email, fixtures]
error_level: "E_ERROR"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM