I have some methods in my app that I check for if (env('APP_DEBUG'))
on and return true
(or whatever) if we're in debug mode rather than doing a certain DB or API call.
The problem is when I run phpunit, the app is still in debug mode, so it doesn't "really" run the test against the actual code.
Is it possible to check if a test is being run, so I could do if (env('APP_DEBUG') || $testing)
? Or is there a better way to handle this case?
How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.
Every Laravel release has the version of the framework as constant in the Application. php file. You can access this constant via the app() helper. If you don't want to create a /test route to get the version, you can use php artisan tinker to get into the tinker REPL and run it from there.
You're looking for App::runningUnitTests()
.
Remember that you shouldn't use env() outside your configuration files, instead use config('app.debug')
. This ensures that your code works with a cached configuration.
It's impossible to answer "is there a better way" without having more details.
Rather than update your application to check if tests are running, you could disable debug mode in your test environment by updating your phpunit.xml
file:
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="APP_DEBUG" value="false"/>
</php>
Also, you should not use the env
function outside of configuration files. The env
function will just return null if you are using the config cache
https://laravel.com/docs/5.5/configuration#configuration-caching
If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files.
Use config('app.debug')
instead of env('APP_DEBUG')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With