Setting environment variable inside test?
Can anyone give me a pointer on how I go about setting an environment variable inside a test function?
I have a middleware in my app that checks for an environment variable - if this variable is true, it redirects to a 'closed' page.
How can I set this variable for a single test so I can check that the middleware works?
You should be able to pass your middleware as an argument
Sorry, I'm not making myself clear - I've written the middleware, it works fine.
I'm trying to work out how to test it using phpunit - so a function along the lines of
public function testClosedMiddleware()
{
setenv('APP_CLOSED',true);
$this->visit('/')
->seePageIs('/closed');
}
But how do I set the environment variable in the test?
Forgive me, I was thinking of Requests!
I'm not sure how you would go about testing it with PHPUnit. :-(
At the beginning of the specific test put:
putenv("APP_CLOSED=true");
don't forget to change it back to false at the end of the test, otherwise all your other tests will break (hopefully!)
putenv("APP_CLOSED=false");
http://php.net/manual/en/function.putenv.php
Please or to participate in this conversation.