deepu07's avatar
Level 11

PHPUnit Test

Hi Guys here is my phpunit.xml file

<php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="MAIL_DRIVER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="APP_DEBUG" value="false"/>
    </php>

somehow while running test cases I'm doing dd(App::runningUnitTests());it is giving back False. Even, I cleared cache and config. Am I doing anything wrong here? Any help that would be great. Thanks in Advance.

0 likes
12 replies
Tray2's avatar

Why are you running it that way?

Nakov's avatar

@deepu07 you can achieve the same using this check:

if(config('app.env') === 'testing') 
{
    ...
}   
deepu07's avatar
Level 11

@tray2 My app is integrated with the 3rd API. whenever I'm running test cases I jus wanna ignore the 3rd party logic.

deepu07's avatar
Level 11

@nakov if I do dd(config('app.env')); it is giving back 'local' from .env file. It is not giving 'testing`...

Nakov's avatar

@deepu07 how do you test this? Are you running a test to test this or?

Create a .env.testing file then and override those values. Don't understand why your phpunit.xml file is not read, is it maybe broken for some reason? Any errors in your IDE?

deepu07's avatar
Level 11

@nakov yes. I'm testing by running a test-case. Created a .env.testing file still no luck. that sucks.

Nakov's avatar

@deepu07 can you please check if in your config/app.php file for some reason you are hard-coding the env to local?

it should be this one:

'env' => env('APP_ENV', 'production'),

Try running:

php artisan config:clear

afterwards.

deepu07's avatar
Level 11

@nakov it looks same in config/app.php file 'env' => env('APP_ENV', 'production'), also I cleared cache couple times..

Nakov's avatar

@deepu07 are you running this locally, are you using docker maybe?

What I've seen used also is this:

<env name="APP_ENV" value="testing" force="true"/>

so the force attribute will force this value to be used. Can you check that please?

deepu07's avatar
Level 11

@nakov I'm using docker for a database connection. is something making noise? after adding force=true it is working finally. Thanks boss. but still wondering what makes overriding the env value without force...

deepu07's avatar
Level 11

oh yeah. that makes sense. I was using docker for the database connection. anyways you made my day though. Thank you so much.

Please or to participate in this conversation.