Nathália Pissuti
Nathália Pissuti

Reputation: 349

PHPUnit\Framework\Error\Warning: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

I'm following zend's tutorial to use zend-test on my project, but I'm getting this error.

To build my project's base I did the biggest part of this book.

I'm also using PHP 7.2.

When I search google I found a lot of people having trouble using PHP 7.2 with PHP Unit, but it looks like it's ok by now. Did zend-test also being update? If no, what can I do to use zend-test by now? If yes, how do I get the last version?

EDIT

After some test I found the problem, it is on the bootstrap function:

public function onBootstrap( MvcEvent $event ) {
    $application = $event->getApplication();
    $serviceManager = $application->getServiceManager();
    $sessionManager = $serviceManager->get(SessionManager::class);

    $eventManager = $event->getApplication()->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();
    // Registra o método fo event listener
    $sharedEventManager->attach(AbstractActionController::class,
            MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 100);

    // Caso a sessão não seja válida, esquece ela
    $this->forgetInvalidSession($sessionManager);
}

To be more specific, the problem starts at this line:

$sessionManager = $serviceManager->get(SessionManager::class);

EDIT 2

Removing that line e it's uses, it kind than work, but starts to give the same error in another module in a similar line:

$authService = $container->get(\Zend\Authentication\AuthenticationService::class);

Upvotes: 3

Views: 2457

Answers (1)

HoldOffHunger
HoldOffHunger

Reputation: 20948

Use the --stderr argument for phpunit.

phpunit YourTestFile.php  --stderr

From the documentation:

--stderr                    Write to STDERR instead of STDOUT

PHPUnit is writing its own output (for instance, I see: "PHPUnit 8.5.2 by Sebastian Bergmann and contributors." whenever I run it). This is considered STDOUT of the page in PHP, which, of course, means that "text has already been sent". If you divert the output to STDERR, STDOUT will just be the output of the PHP file that you are testing.

Upvotes: 2

Related Questions