Does Laravel automatically load .env.testing variables when Http::fake() is active?
0 reputation · 17 Nov 2020, 21:38 UTC
0 reputation · 17 Nov 2020, 21:38 UTC
When testing Laravel applications that call external APIs, developers often use Http::fake() to stub outbound requests and avoid sending real calls or exposing production credentials. The goal is to confirm that the test suite uses only the credentials defined in .env.testing while Http::fake() is active, ensuring no accidental use of live keys.
However, it is unclear whether Laravel automatically loads the .env.testing environment file when Http::fake() is invoked, or whether the application continues to rely on the existing configuration cache and may inadvertently read production values unless the developer manually overrides the relevant config entries.
Should developers rely on Laravel to automatically load .env.testing variables when Http::fake() is active, or must they explicitly override configuration values to guarantee test-only credentials are used?
34775 reputation · 18 Nov 2020, 00:14 UTC
No. Laravel does not automatically load .env.testing variables simply because Http::fake() is active. There is no causal link between the HTTP client's mocking utility and the application's environment loading mechanism.
Http::fake() is a runtime utility used to intercept outbound requests. It operates at the service level to prevent real network calls from leaving the application. In contrast, environment variable loading happens during the application's bootstrap phase, long before individual test methods are executed.
Laravel loads .env.testing based on the environment configuration provided to the test runner (typically PHPUnit or Pest), not based on the code within the test itself. The standard flow is as follows:
phpunit.xml. is defined, Laravel looks for a .env.testing file.Http::fake() is called during the test execution phase.To guarantee that your tests do not use production keys, you must rely on the test bootstrap configuration rather than the HTTP fake. Follow these steps to verify your setup:
phpunit.xml file contains the environment directive: <php>
<env name="APP_ENV" value="testing" />
</php>.env.testing file exists in your project root with the correct dummy credentials.public function test_api_call() {
dump(env('APP_ENV')); // Should output 'testing'
Http::fake();
// ... test logic
}If you need to change a specific value for a single test without changing the global .env.testing file, use the Config facade instead of relying on environment files:
config(['services.api.key' => 'test-key-123']);
Diagnostic Note: Are you running tests via a custom CI script or a non-standard test runner? If so, the APP_ENV variable may not be passed correctly, which would cause Laravel to fall back to .env.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 18 Nov 2020, 01:49 UTC
Http::fake() only intercepts the outgoing request; it does not reload environment or config. If a config cache was built against production values, those cached values are used for the whole test run even when APP_ENV is testing and Http::fake() is in place.
That means the HTTP client is still constructed with the base URL, headers and keys read from the cached config, so the fake will see production-derived configuration. Clearing the cache or ensuring the test bootstrap runs without a cached config is required for .env.testing to take effect.