battis/phpunit-sessions

Extension to PHPUnit to handle PHP sessions more gracefully

v0.1.3 2024-03-18 16:17 UTC

This package is auto-updated.

Last update: 2024-10-13 02:17:42 UTC


README

codecov

Extension to PHPUnit to handle PHP sessions more gracefully

Install

composer require --dev battis/phpunit-sessions

Configure

In phpunit.xml:

<phpunit

    ...

    bootstrap="tests/bootstrap.php"
>

  ...

  <extensions>
      <extension class="Battis\PHPUnit\Sessions\Extension" />
  </extensions>
</phpunit>

In tests/bootstrap.php:

require_once __DIR__ . "/../vendor/autoload.php";

Battis\PHPUnit\Sessions\Bootstrap::execute();

How it works

Fundamentally, there are two problems with working with sessions and cookies when testing in PHP:

  1. PHPUnit runs from the CLI, so $_COOKIES and $_SESSIONS don't exist.
  2. PHPUnit starts output before any of the code under test that works with sessions is executed, generating errors about output being sent before headers, etc.

To address this, this extension enables an output buffer as PHPUnit starts, buffering all of the output until after the last test is run. (You lose nothing but the immediacy of the output -- you'll still see it all, but only after a heart-stopping pause.)

In addition, when the script run from the CLI, $_SESSIONS AND $_COOKIES are initialized as empty arrays which can then be manipulated as usual.