I'm implementing some logic that requires code to behave differently in a production environment.
I want to write a test that asserts this actually happens, but I'm having difficulty mocking the environment.
I've seen it suggested to use putenv('APP_ENV=production');
but it doesn't seem to work.
How can I make the test pass?
use Illuminate\Support\Facades\App;
use Tests\TestCase;
class EnvTest extends TestCase
{
public function testEnv()
{
// This assertion is fine
$env = App::environment();
$this->assertEquals('testing', $env);
putenv('APP_ENV=production');
// This assertion fails
$env = App::environment();
$this->assertEquals('production', $env);
}
}
Result
Time: 160 ms, Memory: 18.00MB
There was 1 failure:
1) EnvTest::testEnv
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'production'
+'testing'