Skip to content

Instantly share code, notes, and snippets.

@zabbarob
Last active Aug 20, 2016
Embed
What would you like to do?
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