Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BishoyWagih's avatar

Refresh database Trait remove all data from mysql database

i just start laravel php unit but when i use Refreshdatabase Trait inside Test class, all records from mysql database get removed, although i updated phpunitxml.php connection to

   <env name="APP_ENV" value="testing"/>
    <env name="DB_CONNECTION" value="sqlite"/>
    <env name="DB_DATABASE" value=":memory:"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="ENABLE_REGISTRATION" value="true"/>
    <env name="MAIL_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>

here is my test class

namespace Tests\Feature\Category;

use Tests\TestCase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\RefreshDatabase; use App\models\Permission\Permission; use App\Models\Auth\Role;

class CategoryTest extends TestCase { use RefreshDatabase;

/** @test */
public function it_can_see_all_categories()
{
    $this->assertTrue(true);
}
0 likes
17 replies
bobbybouwmann's avatar

Did you clear your cache?

php artisan cache:clear

Also! Stop poking me! Just ask your question and someone will answer it!

1 like
BishoyWagih's avatar

yes i cleared my cache, but still all records deleted..

BrandonSurowiec's avatar

I've had this happen to me before too, where the phpunit.xml wasn't setting the custom .env variables, but my env file was overriding them.

Pretty sure I did a workaround by setting the env vars in the createApplication() trait/method like:


    public function createApplication()
    {
    
        putenv("DB_CONNECTION=sqlite");

        //    ...
BishoyWagih's avatar

sry @BrandonSurowiec but clearing mysql database still happens, here is my createApplication trait

public function createApplication()
{
    putenv("DB_CONNECTION=sqlite");

    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Kernel::class)->bootstrap();

    return $app;
}
BrandonSurowiec's avatar
Level 14

Try this:

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Kernel::class)->bootstrap();

    config(['database.default' => 'sqlite']);

    return $app;
}

And you could also just try commenting out DB_CONNECTION from your .env file (if you have one.)

1 like
bobbybouwmann's avatar

Config looks good. Using the phpunit.xml file is the way to go!

Make sure you never ever cache the config locally! This will make this kind of stuff happen. What you also can d is clear all the other caches just to be sure!

3 likes
bobbybouwmann's avatar

It's working, but it's not the right way! You have a caching issue right now!

2 likes
BrandonSurowiec's avatar

@bobbybouwmann You're right, but this is an odd edge-case. He cleared the cache and has his phpunit.xml set correctly and it still doesn't work. This same thing happened to one of my projects and I never use the caching feature locally. The rest of my projects act perfectly normal.

There is something else going on. If I ever spend the time digging into it I'll update this thread.

BishoyWagih's avatar

actually i cleared the cache and then reconfigured it

php artisan cache:clear php artisan config:cache

so if you think the problem with the cache so what is the right way to fix this issue without BrandonSurowiec Solution..

BrandonSurowiec's avatar

You shouldn't run php artisan config:cache locally that will cause the errors you were getting.

1 like
BishoyWagih's avatar

what if i already run it before?, how can i fix this to use sqlite for database testing?

BrandonSurowiec's avatar

php artisan cache:clear will clear the cache. That should be all you need.

bobbybouwmann's avatar

Never ever ever again run php artisan config:cache again in your local environment! You should only run php artisan config:clear once now and you should be good to go!

5 likes
juliancms's avatar

In case someone is still facing a similar issue, another possible reason is that the .env.testing file is configured with the APP_ENV as local: APP_ENV=local

Make sure you have it as testing: APP_ENV=testing and after that clear cache php artisan config:clear

And remember to configure as previously said, .env.testing like this:

APP_ENV=testing
DB_CONNECTION=sqlite
DB_DATABASE=:memory:

And phpunit.xml like this:

<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

Please or to participate in this conversation.