Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Database tables deleted after running phpunit test

Every time I run a test all my database tables (except for the migrations table) are being deleted and I have to run the migrations again. For instance, if I have the following tables:

migrations
users
tableA
tableB

after running:

phpunit --filter user_can_view_a_record ViewRecordTest tests/Feature/ViewRecordTest.php

my tables are deleted and I end up with just the migrations table.

I'm using MySQL as database and according to the configuration I have set up the tests are being running in memory:

database.php

'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            //'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'database' => ':memory:',
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
            'sticky' => true
        ],
]

phpunit.xml

<?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>

Thanks

like image 840
MrCujo Avatar asked Sep 03 '18 18:09

MrCujo


3 Answers

the solution of Michael Ameyaw worked well for me in PHPStorm ! thank you.
I wanted to mention that it could be also a cache problem, so try this:

php artisan config:clear
like image 149
Nysso Avatar answered Oct 17 '22 00:10

Nysso


In general you should have separated database with same table structure like staging env. It is normal behavior of unit tests (I mean deleting of tables). Typical flow for testing is (for each test execution):

  1. delete all tables.
  2. Run all migrations (create tables, alter tables, etc).
  3. Load fixtures
  4. Execute tests on fresh database.

Imagine situation, that one of tests change data in for example user table (change email or first name etc) if phpunit don't drop all data next tests will work with incorrect data ( changed by another test). You can check laravel main documentation for more details.

like image 26
Igor Skobelev Avatar answered Oct 16 '22 23:10

Igor Skobelev


I got a solution, if you are using phpStorm look for test framework.

File >> settings >> File & Framework >> Test Framework.

Set a default configuration file to phpunit.xml in your root folder

like image 1
mkojoa Avatar answered Oct 17 '22 01:10

mkojoa