0

For some reason my application can't use :memory: as the database while testing is printing the error: SQLSTATE[HY000] [1049] Unknown database ':memory:'

How can I get my test to use database in memory? I have looked on Stack Overflow, LaraCast, and Reddit but haven't found a solution. Is there an alternative way to do this?

PhpUnit.xml

<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_DRIVER" value="array"/>
    <server name="QUEUE_CONNECTION" value="sync"/>
    <server name="SESSION_DRIVER" value="array"/>
    <server name="TELESCOPE_ENABLED" value="false"/>
</php>

config/database.php

'sqlite' => [
    'driver' => 'sqlite',
    'url' => env('DATABASE_URL'),
    'database' => env('DB_DATABASE', database_path('database.sqlite')),
    //'database' => database_path('database.sqlite'),
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

I have run php artisan config:clear & cache:clear.

6
  • How are you running your tests? Commented Oct 27, 2020 at 13:42
  • @KennyHorna Through CLI using vendor/bin/phpunit --filter="myTestCase" Commented Oct 27, 2020 at 13:43
  • Did you try to specify the config file? vendor/bin/phpunit --filter="myTestCase" --configuration phpunit.xml Commented Oct 27, 2020 at 13:45
  • yeah same error occures Commented Oct 27, 2020 at 13:48
  • Check this thread, maybe it could help you Commented Oct 27, 2020 at 13:50

1 Answer 1

1

So eventually decided to remove the <server name="DB_DATABASE" value=":memory:"/> which kind of worked but still gave misc errors, but it made me realize that my phpunit.xml was not overriding my database connection.

So the long and short of the story is I ended up creating a testing connection that uses the :memory: database which worked.

'testing' => [
    'driver' => 'sqlite',
    'database' => ':memory:',
],

As to what was initially preventing it from working the standard way of doing it I still don't know. but this resolved my issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.