Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NielsNumbers's avatar

PHPUNIT withSession fails on CI / CD pipeline?

All tests from my Laravel application are passing localy. However on my CI/CD pipleline on Gitlab one tests fails that is using withSession:

$response = $this->withSession(['is_admin' => 1])->actingAs($user)->post('/user/my-profile/edit/' . $user->id, $postData);

Any idea what could cause it?

I am using

  <env name="SESSION_DRIVER" value="array"/>

inside phpunit.xml and thats the content of my .gitlab-ci.yml:

test-backoffice:
  stage: test
  image: php:7.4
  services:
      - mysql:5.7
  variables:
    # MYSQL_DATABASE: $BACKOFFICE_TEST_DB
    MYSQL_DATABASE: "backoffice"
    # MYSQL_ROOT_PASSWORD: $BACKOFFICE_TEST_PW
    MYSQL_ROOT_PASSWORD: "test"
  cache:
    paths:
      - vendor/
      - node_modules/
  before_script:
  - apt-get update -yqq
  - apt-get install gnupg -yqq
  - apt-get install libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev libzip-dev -yqq
  - docker-php-ext-install pdo_mysql curl json intl gd xml zip bz2 opcache mysqli
  - pecl install xdebug
  - docker-php-ext-enable xdebug
  - curl -sS https://getcomposer.org/installer | php
  - php composer.phar config gitlab-token.gitlab.com ${GITLAB_TOKEN}
  - php composer.phar install
  - php composer.phar dump-autoload
  - export DB_HOST=mysql
  - export DB_DATABASE_TEST=backoffice
  - export DB_USERNAME=root
  - export DB_PASSWORD=test
  - export DB_CONNECTION=testing
  - export APP_ENV=local
  - export APP_KEY="base64:dxGYMvIIqBi8DzCXR5/+2aOTl7e099ZKjrIvND7cJ1A="
  # - php artisan config:cache TODO: try without 
  - php artisan testdb:migrate
  script:
    - php vendor/bin/phpunit --colors=never
0 likes
1 reply
bobbybouwmann's avatar

Mmh, it seems that the default SESSION_DRIVER, which is configured in your config files is still being used. As far as I know, the ENV variables from PHPUnit only overwrite existing variables.

What happens if you add export SESSION_DRIVER=array to your script?

In general, it's recommend to add the following line cp .env.example .env. This way you can make sure you have all required environment variables available when running your tests ;)

2 likes

Please or to participate in this conversation.