zaster's avatar

Database gets empty - with php artisan test

This happens with

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

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
            <!-- <directory suffix="Test.php">./tests/Feature1</directory> -->
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>
0 likes
7 replies
Tray2's avatar

The database should normally be empty when you start the test and be empty when the test finishes.

Are you using DatabaseMigrations in your test class?

zaster's avatar

@tray2

I am not using

DatabaseMigrations in any of my test classes

I use RefreshDatabase in all of my test classes

Also now i changed my test database from sqlite to mysql

phpuni.xml

	   <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="mysql"/>
        <server name="DB_DATABASE" value="mydb_testing"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>

created the mysql database (mydb_testing)

and why is it deleting data in my application database , when i run tests (mydb) ?

automica's avatar

Do you have an .env.testing defining any database settings?

zaster's avatar

@automica I tried that and it didn't work. So i deleted it.(the .env.testing)

automica's avatar
automica
Best Answer
Level 54

Excellent. If you are having to clear cache, are you running any cache commands in your normal development workflow?

I’ve had that issue before and ended up hard coding the DB settings in phpunit.xml as that was more reliable than switching .env

I’ve only experienced that on a laravel 7 project though so may have been something specific to our project setup

zaster's avatar

@automica

I have been running

php artisan config:cache

through a combination of artisan commands without understanding them(Using Alias).

E.g. -

php artisan cache:clear
php artisan config:cache

Now i have corrected those

Actually there is no need to run

php artisan cache:clear && php artisan config:clear

as you have mentioned.

Thank you @automica

Please or to participate in this conversation.