Saneesh's avatar

After executing the tests tables are removed

Hello,

I'm using sqlite for Laravel application and PHPUnit. After executing the tests the tables are removed from sqlite even though it was created :memory: for tests. So that I'm not able to login to the application with the registered user.

Note: I have used use DatabaseMigrations; statement in each test classes.

Detailed issue: https://stackoverflow.com/questions/60887625/laravel-application-and-phpunit-using-sqlite-after-executing-the-test-database

How can I fix this issue?

0 likes
12 replies
zoltiecodes's avatar

Hi. Sounds impossible. :D Can you show us the full phpunit.xml and .env files content please?

Saneesh's avatar

@zsoltgyure Thank you for your reply.

DB_CONNECTION=sqlite   <--
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=/home/my/path/db.sqlite   <--
DB_USERNAME=root
DB_PASSWORD=

This is the only section I have modified and NO modification in phpunit.xml by default sqlite and :memory: are there in the file.

Tray2's avatar

When using an Sqlite in memory database and database mirgrations it does the following

  1. Migrate the database
  2. Perform the test
  3. Rollback the database migration done before the test.

PHPUnit uses the phpunit.xml file to know which database to use and it does in your case don't use the one you specified in your .env file. To be able to log in using your browser you need to run php artisan migrate and then add a user so that you can use that to log in.

Saneesh's avatar

Sorry, I think I'm confused, let me clear the situation a little more. After installing I have created the user and successfully logIn and logOut. Then I executed the test (:memory:).

If I again trying to logIn there is no record in the database, I think, when the test executed it clear the database as well.

Is there any possible way to avoid clear the database after executing the test. So that the user previously created will be remain in the database and I can login.

Since the tests is executing with the database in memory, why it is clearing the database which was for the application and how can we avoid it?

Tray2's avatar

Then the test isn't using the phpunit.xml file but rather your .env file.

Are you sure the sqlite :memory: is ser in your phpunit.xml?

Are you running phpunit in the root of your project?

<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"/>
Saneesh's avatar

@tray2 Yes, it is exactly same

<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"/>
    </php>

The command : php artisan test

Tray2's avatar

If you run ./vendor/bin/phpunit does it still empty your sqlite db?

Tray2's avatar

What happens if you comment out all the sqlite settings in your .env file?

//DB_CONNECTION=sqlite   <--
//DB_HOST=127.0.0.1
//DB_PORT=3306
//DB_DATABASE=/home/my/path/db.sqlite   <--
//DB_USERNAME=root
//DB_PASSWORD=

And run the tests again?

Tray2's avatar

Then the test isn't using the phpunit.xml for some reason.

Saneesh's avatar

I have executed php artisan config:cache before php artisan serve so the .env also cached.

Please or to participate in this conversation.