1

I am currently working on a PHP Laminas application. I am new to Laminas and PHPUnit. This application is working correctly in web mode (apache and development server). I am adding unit tests recently. Unfortunately, I am getting this error when I try to run my first test case.

Laminas\ServiceManager\Exception\ServiceNotCreatedException : Service with name "Laminas\Session\Config\ConfigInterface" could not be created. Reason: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time
...
Caused by
PHPUnit\Framework\Error\Warning: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

I am using PHPUnit 9.6.10, PHP 7.4.20 and Laminas 3.0.1. I followed this tutorial.

I have researched a lot about this issue, but I cannot see what really the issue is.

This is my test case:

<?php

namespace PortadaTest\Controller;

use Portada\Controller\IndexController;
use Laminas\Stdlib\ArrayUtils;
use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;


class PortadaControllerTest extends AbstractHttpControllerTestCase
{

    protected $traceError = false;

    protected function setUp(): void
    {
        $configOverrides = [];
        $this->setApplicationConfig(ArrayUtils::merge(
            include 'config/application.config.php',
            $configOverrides
        ));
        parent::setUp();
    }

    public function testIndexActionCanBeAccessed()
    {
        $this->dispatch('/');
        $this->assertResponseStatusCode(200);

        $this->assertModuleName('application');
        $this->assertControllerName('application_index');
        $this->assertControllerClass('IndexController');
        $this->assertMatchedRouteName('home');
    }
}


I have already consulted these resources:

Maybe I don't understand everything, but I'm not sure how to get a solution for it.

Erick Villatoro
  • 170
  • 1
  • 9
  • The full stacktrace could be helpful. For test you could try with process isolation (a Phpunit feature) if it already helps. If so, consider to bootstrap (another Phpunit feature) so that when the session handling component initializes on the CLI SAPI, it does during bootstrapping and you can make it warning free and also initializing only once per process (only by test-only code, so no overhead for production code). /Edit: CLI SAPI differences summary here: https://www.php.net/manual/en/features.commandline.differences.php – hakre Aug 24 '23 at 21:18

1 Answers1

0

The issue is "Headers already sent" - that is any ini_set must be before any change to the HTTP headers, and before any output is sent.

I would imagine the issue is in your config/application.config.php...or else your AbstractHttpControllerTestCase class.

It could be that...

  1. you have white space, text, etc before the opening [ here ]<?php
  2. you have some empty lines after php closing ?>[ here ]
  3. you have echo, print() or printr()... or something else that is outputting content
  4. you are explicitly calling ini_set after a call to header

Review these two places and ensure you are not doing any of the above.

Fraser
  • 15,275
  • 8
  • 53
  • 104