0

Context:

I have tried the solution in PHPUnit code coverage session already started, with no success. I am still getting the error when I try to generate the code coverage. And I am stumped on what is causing the error. I have put the information showing that I have used the answer from the linked question and the proof of the issue still happening. I am running in a Docker instance running CentOS.

Question:

How does one get code coverage to work? And why am i getting this error session_start(): Cannot start session when headers already sent?

Versions:

PHP 7.2.7
PHPUnit 7.3.1

Outputs:

When I run without code coverage, I get the list of successful tests.

 [root@fada2474faea iop]# vendor/bin/phpunit tests/file6test.php --coverage-html coverage
PHPUnit 7.3.1 by Sebastian Bergmann and contributors.

session_start(): Cannot start session when headers already sent

Tests passes without code coverage enabled.

[root@fada2474faea iop]# vendor/bin/phpunit tests/file6test.php 
PHPUnit 7.3.1 by Sebastian Bergmann and contributors.

.....                                                               5 / 5 (100%)

Time: 46 ms, Memory: 6.00MB

OK (5 tests, 5 assertions)

the code updated with the answer from the other question linked

[root@fada2474faea iop]# grep -i -R -A1 -B2 session_start *.php
access.php-if(session_status() == PHP_SESSION_NONE)
access.php-{
access.php:    session_start();
access.php-}
--
file1.php-  if(session_status() == PHP_SESSION_NONE)
file1.php-  {
file1.php:      session_start();
file1.php-  }
--
file2.php- if(session_status() == PHP_SESSION_NONE)
file2.php- {
file2.php:     session_start();
file2.php- }
--
logout.php-if(session_status() == PHP_SESSION_NONE)
logout.php-{
logout.php:    session_start();
logout.php-}
--
file3.php- if(session_status() == PHP_SESSION_NONE)
file3.php- {
file3.php:     session_start();
file3.php- }
--
api.php-if(session_status() == PHP_SESSION_NONE)
api.php-{
api.php:    session_start();
api.php-}
--
file4.php-if(session_status() == PHP_SESSION_NONE)
file4.php-{
file4.php:    session_start();
file4.php-}
--
file5.php-        if(session_status() == PHP_SESSION_NONE)
file5.php-        {
file5.php:            session_start();
file5.php-        }
thebtm
  • 215
  • 9
  • 14
  • cli does not have session.. think about abstracting session code into a class you you can mock. *How does one get code coverage to work?* You cover lines of code in a working test suit, if you mean whys it not shown? You need to set it up to show, also you need xdebug installed. – Lawrence Cherone Aug 16 '18 at 16:08
  • then why am i getting this error `session_start(): Cannot start session when headers already sent` – thebtm Aug 16 '18 at 16:09
  • You get the error because of how phpunit works.. Good first comment here.. https://github.com/sebastianbergmann/phpunit/issues/720 – Lawrence Cherone Aug 16 '18 at 16:12
  • After reading that, I still don't feel like I am still getting a slightly different error, even running `vendor/bin/phpunit` without the explicit files and still not getting the error without the `--coverage-html coverage` – thebtm Aug 16 '18 at 16:26

0 Answers0