jeudi 4 octobre 2018

laravel phpunit - why are tests refreshing the live database

I have a laravel project which has a problem which means that, whenever I run a test, the db database gets refreshed (I am using use RefreshDatabase;), not the test-db database.

It is really perplexing as it all looks ok - I can't see what I have missed.

When I run dd(app()->environment());, it shows 'testing', so I would expect the db-test database to get refreshed.

I am using phpstorm, but I don't think that is the issue.

Here is the (sanitised) env file:

DB_HOST=127.0.0.1
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=

TEST_DB_HOST=127.0.0.1
TEST_DB_DATABASE=test-db
TEST_DB_USERNAME=root
TEST_DB_PASSWORD=

(sanitised) config/database.php:

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'db'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        // other settings
    ],

    'test-mysql' => [
        'driver' => 'mysql',
        'host' => env('TEST_DB_HOST', 'localhost'),
        'port' => env('TEST_DB_PORT', '3306'),
        'database' => env('TEST_DB_DATABASE', 'test-db'),
        'username' => env('TEST_DB_USERNAME', 'root'),
        'password' => env('TEST_DB_PASSWORD', ''),
        // other settings
    ],

And the (sanitised) 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"
         syntaxCheck="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="APP_URL" value="http://app.localhost"/>
        <env name="DB_CONNECTION" value="test-mysql"/>
        <env name="MAIL_DRIVER" value="log"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

Does anyone have any ideas of either what this might be or where to check next?

Aucun commentaire:

Enregistrer un commentaire