Skip to content

Instantly share code, notes, and snippets.

@zabbarob
Last active August 20, 2016 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zabbarob/f4e66fa097194e328e7ce284666cf0c4 to your computer and use it in GitHub Desktop.
Save zabbarob/f4e66fa097194e328e7ce284666cf0c4 to your computer and use it in GitHub Desktop.
Use in-memory database when running tests for a Laravel application.

Configuring the database and PHPUnit was described on the Laracasts Forum. How to use Artisan commands instead of Laravel's database migration trait to reset the database after each test was described on the ScriptScoop Forum.

Laravel 5.1's database migration trait crashes on migration rollback during test tear-down since the in-memory database is already destroyed at that point.

Directory structure:

./tests/MemoryDbTest.php
./config/database.php
./phpunit.xml
...
'connections' => [
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
...
...
use Illuminate\Support\Facades\Artisan;
class MemoryDbTest extends \TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
// optional: Artisan::call('db:seed', ['--class' => 'TestSeeder'])
}
...
...
<php>
<env name="DB_CONNECTION" value="sqlite_testing"/>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment