Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ratiw/f3ac01f9013243ea03b9 to your computer and use it in GitHub Desktop.
Save ratiw/f3ac01f9013243ea03b9 to your computer and use it in GitHub Desktop.
Set DB_CONNECTION to use sqlite in-memory database when testing

#Set DB_CONNECTION to use sqlite in-memory database when testing

  • Open `phpunit.xml' file in the current project
  • Add the following environment variables to the appropriate section
  <env name="DB_CONNECTION" value="sqlite">
  <env name="DB_DATABASE" value=":memory:">
  • in config\database.php file, change the default sqlite connection to
  'connections' => [
    'driver'   => 'sqlite',
    'database' => env('DB_DATABASE', database_path('database.sqlite')),
    'prefix'   => '',
  ],
@maxalmonte14
Copy link

I think the environment tags in the phpunit.xml file should autoclose, otherwise, you're going to receive some sort of error like the following

Could not load "/some/fake/directory/phpunit.xml".

Opening and ending tag mismatch: env line 34 and php

Opening and ending tag mismatch: env line 33 and phpunit

Premature end of data in tag php line 25

Premature end of data in tag phpunit line 2

No problem when you add the / character to close the tags

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment