From 4c053b13cb330efda9735eef4e768d5e1496e8ca Mon Sep 17 00:00:00 2001 From: Caio Fernandes Date: Sat, 10 Oct 2026 15:30:52 -0300 Subject: [PATCH] =?UTF-8?q?[SPEC-089]=20Tests=20independent=20from=20.env?= =?UTF-8?q?=20=E2=80=94=202.0.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tests set APP_ENV/APP_DEBUG/CORS_ALLOWED_ORIGINS as real environment variables (precedence over .env); CI also runs the suite with .env copied from .env.example, as in a created project. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 5 +++++ CHANGELOG.md | 9 +++++++++ tests/ApiTest.php | 24 +++++++++++++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4977f32..63fc5a9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,6 +52,11 @@ jobs: - name: Run PHPUnit run: vendor/bin/phpunit + - name: Run PHPUnit as a created project (with .env from .env.example) + run: | + cp .env.example .env + vendor/bin/phpunit + - name: Smoke test (built-in server) run: | php -S 127.0.0.1:8000 -t public > /dev/null 2>&1 & diff --git a/CHANGELOG.md b/CHANGELOG.md index 08682fe..342b1d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to the PivotPHP Skeleton project will be documented in this The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.1] - 2026-10-10 + +### Fixed + +- Tests no longer depend on the project's `.env`: they set `APP_ENV=testing`, `APP_DEBUG=false` and + `CORS_ALLOWED_ORIGINS` as real environment variables (which take precedence over `.env`). In a project + created with `create-project`, `testStatusReadsConfiguration` failed because `.env` sets + `APP_ENV=development`. + ## [2.0.0] - 2026-10-10 Template for the PivotPHP 4 ecosystem (SPEC-089). diff --git a/tests/ApiTest.php b/tests/ApiTest.php index e15696b..999570e 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -19,10 +19,22 @@ final class ApiTest extends TestCase private Application $app; private Psr17Factory $factory; + /** + * Real environment variables take precedence over .env, so the tests do not depend on the + * project's .env file. + */ + private const ENV = [ + 'APP_ENV' => 'testing', + 'APP_DEBUG' => 'false', + 'CORS_ALLOWED_ORIGINS' => self::ORIGIN, + ]; + protected function setUp(): void { - putenv('CORS_ALLOWED_ORIGINS=' . self::ORIGIN); - $_ENV['CORS_ALLOWED_ORIGINS'] = self::ORIGIN; + foreach (self::ENV as $key => $value) { + putenv("{$key}={$value}"); + $_ENV[$key] = $value; + } $this->app = require __DIR__ . '/../bootstrap/app.php'; $this->factory = new Psr17Factory(); @@ -30,8 +42,10 @@ protected function setUp(): void protected function tearDown(): void { - putenv('CORS_ALLOWED_ORIGINS'); - unset($_ENV['CORS_ALLOWED_ORIGINS']); + foreach (array_keys(self::ENV) as $key) { + putenv($key); + unset($_ENV[$key]); + } } /** @@ -77,7 +91,7 @@ public function testStatusReadsConfiguration(): void $body = self::body($this->request('GET', '/api/status')); $this->assertSame('PivotPHP Skeleton API', $body['name']); - $this->assertSame('production', $body['environment']); + $this->assertSame('testing', $body['environment']); } public function testListAndShowUsers(): void