Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- fix warnings in php tests in GitHub Actions #515

Merged
merged 4 commits into from
Dec 17, 2024
Merged

Conversation

mtracz
Copy link
Member

@mtracz mtracz commented Dec 17, 2024

This pull request includes changes to the configuration files and workflow setup to improve the testing environment and ensure consistency across different environments. The most important changes include updates to the .env.ci file, modifications to the GitHub Actions workflow, and enhancements to the phpunit.xml configuration.

Configuration Updates:

  • [.env.ci]: Removed unnecessary environment variables and updated the DB_HOST to use container service from GitHub Actions.

Workflow Modifications:

  • .github/workflows/test-and-lint-php.yml:
    • Corrected the file path from env.ci to .env.ci and added test-and-lint-php.yml to the list of files that trigger the workflow.
    • Changed the service label from pgsql to postgres and updated the POSTGRES_DB to toby-test for consistency.
    • Modified the test execution step to copy .env.ci to .env before running tests with colored output.

PHPUnit Configuration Enhancements:

  • phpunit.xml:
    • Added attributes to display details on tests that trigger warnings and to fail on warnings.
    • Changed <server> to <env> for environment variables and removed the DB_HOST entry to ensure it uses the correct environment setup.

Problem with tests

Actually all php tests are marked as warning due to file_get_contents() exception.
image

image

The problem

The problem is missing .env.testing or .env file on GitHub Actions runner during test execution.
Laravel try to load env file defined in APP_ENV (testing by default in phpunit.xml). If this file not exist, it fallback to .env - if this file not exist the result is file_get_contents warning in tests.

The previous command php artisan test --env=ci loaded variables from .env.ci only in php artisan test command, but this command call phpunit binary (in our case) which bootstrap Laravel app again = load env files again.
--env=ci param is not passed to the phpunit command, so phpunit loads environments from phpunit.xml config, where APP_ENV=testing is defined, so Laravel try to load .env.testing then .env then silently fail.

The solution

For local and CI tests we are using phpunit.xml config file, with few predefined variables. This allow us to smoothly run tests in local environment.
In CI environment we need to rename .env.ci to .env to allow Laravel use variables from this file.
DB_HOST for tests will be used from .env in local environment and from .env.ci renamed to .env in CI environment.
The rest variables can stay predefined in phpunit.xml.
In phpunit.xml file we have defined APP_ENV=testing and this file (.env.testing) not exist, so Laravel will fallback to .env and use these variables if they have not been defined in phpunit.xml file.

It's important to understand precedence of loaded environment variables source

  1. In app configuration
  2. PHPUnit server configuration
  3. System environment variables
  4. PHPUnit env configuration
  5. .env.testing file (or .env.dusk file)
  6. .env file

Helpful articles

Similar issues:

@mtracz mtracz requested a review from a team as a code owner December 17, 2024 09:49
# Conflicts:
#	.github/workflows/test-and-lint-php.yml
krzysztofrewak
krzysztofrewak previously approved these changes Dec 17, 2024
@mtracz mtracz merged commit 132b5cc into main Dec 17, 2024
3 checks passed
@mtracz mtracz deleted the fix-php-tests-in-ci branch December 17, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants