Service with name "Laminas\Session\Config\ConfigInterface" could not be created

249 Views Asked by At

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.

1

There are 1 best solutions below

0
Fraser On

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.