How to ensure that Laravel 5.5 is running a test? How to ensure that Laravel 5.5 is running a test? laravel laravel

How to ensure that Laravel 5.5 is running a test?


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').