From 4520afb271816bfe464e4dbdd1e3c829027d4f8b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 24 Oct 2024 14:08:48 +0200 Subject: [PATCH 1/2] Normalize composer.json scripts with composer capabilities --- composer.json | 52 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 3e2a089d..e1e072a2 100644 --- a/composer.json +++ b/composer.json @@ -114,31 +114,47 @@ ], "cs": "phpcs -s", "cs:fix": "phpcbf", - "stan": "APP_ENV=test php vendor/bin/phpstan analyse", + "stan": ["@putenv APP_ENV=test", "phpstan analyse"], "test": [ "@parallel test:unit test:db", "@parallel test:api test:cli" ], - "test:unit": "COLUMNS=120 vendor/bin/phpunit --order-by=random --testdox --testdox-summary", - "test:unit:ci": "XDEBUG_MODE=coverage composer test:unit -- --coverage-php=build/coverage-unit.cov", - "test:unit:pretty": "XDEBUG_MODE=coverage composer test:unit -- --coverage-html build/coverage-unit/coverage-html", + "test:unit": ["@putenv COLUMNS=120", "phpunit --order-by=random --testdox --testdox-summary"], + "test:unit:ci": ["@putenv XDEBUG_MODE=coverage", "@test:unit --coverage-php=build/coverage-unit.cov"], + "test:unit:pretty": ["@putenv XDEBUG_MODE=coverage", "@test:unit --coverage-html build/coverage-unit/coverage-html"], "test:db": "@parallel test:db:sqlite:ci test:db:mysql test:db:maria test:db:postgres test:db:ms", - "test:db:sqlite": "APP_ENV=test php vendor/bin/phpunit --order-by=random --testdox --testdox-summary -c phpunit-db.xml", - "test:db:sqlite:ci": "XDEBUG_MODE=coverage composer test:db:sqlite -- --coverage-php build/coverage-db.cov", - "test:db:mysql": "DB_DRIVER=mysql composer test:db:sqlite -- $*", - "test:db:maria": "DB_DRIVER=maria composer test:db:sqlite -- $*", - "test:db:postgres": "DB_DRIVER=postgres composer test:db:sqlite -- $*", - "test:db:ms": "DB_DRIVER=mssql composer test:db:sqlite -- $*", + "test:db:sqlite": ["@putenv APP_ENV=test", "phpunit --order-by=random --testdox --testdox-summary -c phpunit-db.xml"], + "test:db:sqlite:ci": ["@putenv XDEBUG_MODE=coverage", "@test:db:sqlite --coverage-php build/coverage-db.cov"], + "test:db:mysql": ["@putenv DB_DRIVER=mysql", "@test:db:sqlite"], + "test:db:maria": ["@putenv DB_DRIVER=maria", "@test:db:sqlite"], + "test:db:postgres": ["@putenv DB_DRIVER=postgres", "@test:db:sqlite"], + "test:db:ms": ["@putenv DB_DRIVER=mssql", "@test:db:sqlite"], "test:api": "bin/test/run-api-tests.sh", - "test:api:sqlite": "DB_DRIVER=sqlite composer test:api -- $*", - "test:api:mysql": "DB_DRIVER=mysql composer test:api -- $*", - "test:api:maria": "DB_DRIVER=maria composer test:api -- $*", - "test:api:mssql": "DB_DRIVER=mssql composer test:api -- $*", - "test:api:ci": "GENERATE_COVERAGE=yes composer test:api && vendor/bin/phpcov merge build/coverage-api --php build/coverage-api.cov && rm build/coverage-api/*.cov", - "test:api:pretty": "GENERATE_COVERAGE=yes composer test:api && vendor/bin/phpcov merge build/coverage-api --html build/coverage-api/coverage-html && rm build/coverage-api/*.cov", + "test:api:sqlite": ["@putenv DB_DRIVER=sqlite", "@test:api"], + "test:api:mysql": ["@putenv DB_DRIVER=mysql", "@test:api"], + "test:api:maria": ["@putenv DB_DRIVER=maria", "@test:api"], + "test:api:mssql": ["@putenv DB_DRIVER=mssql", "@test:api"], + "test:api:ci": [ + "@putenv GENERATE_COVERAGE=yes", + "@test:api", + "phpcov merge build/coverage-api --php build/coverage-api.cov && rm build/coverage-api/*.cov" + ], + "test:api:pretty": [ + "@putenv GENERATE_COVERAGE=yes", + "@test:api", + "phpcov merge build/coverage-api --html build/coverage-api/coverage-html && rm build/coverage-api/*.cov" + ], "test:cli": "bin/test/run-cli-tests.sh", - "test:cli:ci": "GENERATE_COVERAGE=yes composer test:cli && vendor/bin/phpcov merge build/coverage-cli --php build/coverage-cli.cov && rm build/coverage-cli/*.cov", - "test:cli:pretty": "GENERATE_COVERAGE=yes composer test:cli && vendor/bin/phpcov merge build/coverage-cli --html build/coverage-cli/coverage-html && rm build/coverage-cli/*.cov", + "test:cli:ci": [ + "@putenv GENERATE_COVERAGE=yes", + "@test:cli", + "vendor/bin/phpcov merge build/coverage-cli --php build/coverage-cli.cov && rm build/coverage-cli/*.cov" + ], + "test:cli:pretty": [ + "@putenv GENERATE_COVERAGE=yes", + "@test:cli", + "phpcov merge build/coverage-cli --html build/coverage-cli/coverage-html && rm build/coverage-cli/*.cov" + ], "swagger:validate": "php-openapi validate docs/swagger/swagger.json", "swagger:inline": "php-openapi inline docs/swagger/swagger.json docs/swagger/swagger-inlined.json", "clean:dev": "rm -f data/database.sqlite && rm -f config/params/generated_config.php" From 2a734b5d89d7c817880282592f38cde676ccdcb6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 24 Oct 2024 14:20:45 +0200 Subject: [PATCH 2/2] Ensure proper env vars are promoted for dev and test envs --- bin/test/run-api-tests.sh | 4 ---- bin/test/run-cli-tests.sh | 5 ----- config/container.php | 7 +++++-- config/test/shlink-test.env | 5 ----- config/test/shlink_test_env.php | 15 +++++++++++++++ 5 files changed, 20 insertions(+), 16 deletions(-) delete mode 100644 config/test/shlink-test.env create mode 100644 config/test/shlink_test_env.php diff --git a/bin/test/run-api-tests.sh b/bin/test/run-api-tests.sh index 4c519a9b..ffc152b7 100755 --- a/bin/test/run-api-tests.sh +++ b/bin/test/run-api-tests.sh @@ -17,12 +17,8 @@ touch $OUTPUT_LOGS # Try to stop server just in case it hanged in last execution [ "$TEST_RUNTIME" = 'rr' ] && bin/rr stop -f -w . -# Resolve .env file absolute path, as it fails to load with relative paths -TESTS_DOTENV="${PWD}/config/test/shlink-test.env" - echo 'Starting server...' [ "$TEST_RUNTIME" = 'rr' ] && bin/rr serve -p -w . -c=config/roadrunner/.rr.test.yml \ - --dotenv "$TESTS_DOTENV" \ -o=logs.output="${PWD}/${OUTPUT_LOGS}" \ -o=logs.channels.http.output="${PWD}/${OUTPUT_LOGS}" \ -o=logs.channels.server.output="${PWD}/${OUTPUT_LOGS}" & diff --git a/bin/test/run-cli-tests.sh b/bin/test/run-cli-tests.sh index 96ea6cfa..60b97fcc 100755 --- a/bin/test/run-cli-tests.sh +++ b/bin/test/run-cli-tests.sh @@ -7,11 +7,6 @@ export GENERATE_COVERAGE="${GENERATE_COVERAGE:-"no"}" [ "$GENERATE_COVERAGE" != 'no' ] && export XDEBUG_MODE=coverage -# Load and export test env vars -set -a -. ./config/test/shlink-test.env -set +a - vendor/bin/phpunit --order-by=random --testdox --testdox-summary -c phpunit-cli.xml $* TESTS_EXIT_CODE=$? diff --git a/config/container.php b/config/container.php index e207fe16..6ac07896 100644 --- a/config/container.php +++ b/config/container.php @@ -15,8 +15,11 @@ chdir(dirname(__DIR__)); require 'vendor/autoload.php'; -// Promote env vars from installer or dev config -loadEnvVarsFromConfig('config/params/*.php', enumValues(EnvVars::class)); +// Promote env vars from installer, dev config or test config +loadEnvVarsFromConfig( + EnvVars::isTestEnv() ? 'config/test/shlink_test_env.php' : 'config/params/*.php', + enumValues(EnvVars::class), +); // This is one of the first files loaded. Configure the timezone and memory limit here ini_set('memory_limit', EnvVars::MEMORY_LIMIT->loadFromEnv()); diff --git a/config/test/shlink-test.env b/config/test/shlink-test.env deleted file mode 100644 index 48af9b6e..00000000 --- a/config/test/shlink-test.env +++ /dev/null @@ -1,5 +0,0 @@ -APP_ENV=test - -# URL shortener -DEFAULT_DOMAIN=s.test -IS_HTTPS_ENABLED=false diff --git a/config/test/shlink_test_env.php b/config/test/shlink_test_env.php new file mode 100644 index 00000000..f32d06db --- /dev/null +++ b/config/test/shlink_test_env.php @@ -0,0 +1,15 @@ +value => 'test', + + // URL shortener + EnvVars::DEFAULT_DOMAIN->value => 's.test', + EnvVars::IS_HTTPS_ENABLED->value => false, + +];