Search code examples
phpgitlabgitlab-ci-runnersymfony5gitlab-ci.yml

Gitlab CI runner running in Symfony DEV env for PHPUnit tests despite configuring it for TEST env


I am having difficulty in getting my PHPUnit tests for my Symfony 5.4 application running in a Gitlab runner.

My .gitlab-ci.yml:

phpunit:
    stage: test
    image: php:8.2-alpine
    variables:
        APP_ENV: test
    before_script:
        - echo "@community http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
        - apk update
        - apk add --no-cache git unzip icu icu-dev zlib-dev postgresql-dev
        - docker-php-ext-install intl pdo pdo_pgsql
        - cp app/config/parameters.yml.dist app/config/parameters.yml
        - curl -sS https://getcomposer.org/installer | php
        - mv composer.phar /usr/local/bin/composer
        - composer validate
        - composer check-platform-reqs
        - composer install
        - composer outdated
        - composer dump-autoload --optimize
    script:
        - bin/console --env=test doctrine:schema:drop --force
        - bin/console --env=test doctrine:schema:create
        - bin/console lint:container
        - bin/console lint:twig @AppBundle
        - bin/console lint:yaml @AppBundle
        - bin/console about
        - APP_ENV=test bin/console debug:config framework
        - APP_ENV=test bin/phpunit

When it gets to the unit test stage at the end I can see it uses the project phpunit.xml.dist:

$ APP_ENV=test bin/phpunit
PHPUnit 10.5.29 by Sebastian Bergmann and contributors.
Runtime:       PHP 8.2.22
Configuration: /builds/crmpicco/rfc/phpunit.xml.dist

and my phpunit.xml.dist has the test env:

  <php>
    <env name="APP_ENV" value="test"/>
    <!-- the value is the FQCN of the application kernel -->
    <env name="KERNEL_CLASS" value="AppKernel"/>
  </php>

The tests fail when they try to interact with a sessions table which doesn't exist in the test (testdb) schema as I use a mock for that.

framework:
    test: ~
    session:
        storage_id: session.storage.mock_file
    profiler:
        collect: false

The runner appears to be running in "dev" mode and I have no idea why.


Solution

  • This was indeed a Symfony issue and for anyone looking in the future I noticed I had this config mis-configured in config_test.yml

    This is how it should look:

    framework:
        test: true
        session:
            storage_factory_id: session.storage.factory.mock_file
    

    There's more information in the Symfony documentation.