- fix warnings in php tests in GitHub Actions #515
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thephpunit.xml
configuration.Configuration Updates:
.env.ci
]: Removed unnecessary environment variables and updated theDB_HOST
to use container service from GitHub Actions.Workflow Modifications:
.github/workflows/test-and-lint-php.yml
:env.ci
to.env.ci
and addedtest-and-lint-php.yml
to the list of files that trigger the workflow.pgsql
topostgres
and updated thePOSTGRES_DB
totoby-test
for consistency..env.ci
to.env
before running tests with colored output.PHPUnit Configuration Enhancements:
phpunit.xml
:<server>
to<env>
for environment variables and removed theDB_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.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 inphpunit.xml
). If this file not exist, it fallback to.env
- if this file not exist the result isfile_get_contents
warning in tests.The previous command
php artisan test --env=ci
loaded variables from.env.ci
only inphp artisan test
command, but this command callphpunit
binary (in our case) which bootstrap Laravel app again = load env files again.--env=ci
param is not passed to thephpunit
command, so phpunit loads environments fromphpunit.xml
config, whereAPP_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 definedAPP_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
Helpful articles
Similar issues:
.env
file is missing pestphp/pest#1276