abduljakul-salsalani's avatar

Using Phpunit/ Why phpunit removes all data in my database

I'm trying to test my functions but everytime I try to run phpunit, it will delete all the records in my tables. I already setup my phpunit.xml file, to just in my test database, I just followed everything in the docs, do I have to setup a sqlite inmy machine, since I don't have that ? or what ? I only have laragon installed and phpmyadmin.

phpunit.xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit backupGlobals="false"
       backupStaticAttributes="false"
       bootstrap="vendor/autoload.php"
       colors="true"
       convertErrorsToExceptions="true"
       convertNoticesToExceptions="true"
       convertWarningsToExceptions="true"
       processIsolation="false"
       stopOnFailure="false">
       <testsuites>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <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="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
    </php>
    </phpunit>

0 likes
7 replies
impbob36's avatar

What're your database settings in .env file:

DB_CONNECTION=?
DB_DATABASE=?

If they are the same as phpunit.xml the tests are going to override your local installation.

D9705996's avatar

If you have added this to your phpunit.xml then phpunit will use an in-memory database for your tests.

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

Is you problem that when you run your tests you data in your non-test database is removed as this shouldn't happen with the setup above unless you are overriding anything within Laravel e.g. separate database connections on you eloquent models.

If this is still a problem I would find a test that is causing your data to be removed and work through exactly what that test is doing and the application code it is calling to figure out a bit more detail about why your data is being removed.

abduljakul-salsalani's avatar

@impbob

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ignis
DB_USERNAME=root
DB_PASSWORD=

I tried to use RefreshDatabase trait, maybe that's why

abduljakul-salsalani's avatar

@d9705996

I tried to use RefreshDatabase trait, maybe that's why but it should have been like that because I've already added

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>```
kerrin's avatar

Are you running phpunit from a terminal session or from within your IDE?

D9705996's avatar

@ABDULJAKUL-SALSALANI - The RefreshDatabase Trait will reset the database after every test. If you have configured a InMemory Database this runs artican migrate which you can see in the source

It would be worth making sure that your tests are picking up the correct settings from phpunit.xml by creating a new temporary test that dumps the configuration settings to make sure they match and run just that test with phpunit --filter

dump(env("DB_CONNECTION"));
dump(env("DB_DATABASE"));
Sergiu17's avatar

@abduljakul

Why phpunit removes all data in my database

(crying) because no one writes tests on production database

Please or to participate in this conversation.