Setting up an in-memory SQLite database for testing in Laravel 5.4
I am working on developing a browser test suite for a Laravel 5.4 application using Dusk. One of the tests checks to see if the object has been created, and since this is a browser check, the value is saved to the database when the submit button is clicked. Obviously I don't want my database to be cluttered with test data, and there is also a problem as the object name must be unique and therefore the test will fail after the first run. I decided that the best solution to this problem was to implement a test database that is cleaned up at the end of each test cycle. However, I am having trouble getting it set up.
Currently my code is not throwing any errors, but phpunit simply won't use the test database. I installed the code when I was looking into how to implement the database, but I'm not sure what to do with it. Here is the code I have:
.env
DB_CONNECTION=mysql
.env.testing
APP_ENV=testing
APP_DEBUG=true
APP_KEY=this_has_a_real_key
APP_URL=http://localhost:8000
DB_CONNECTION=sqlite_testing
database.php
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
...
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
...
phpunit.xml
<env name="DB_CONNECTION" value="sqlite_testing" />
DuskTestCase.php
abstract class DuskTestCase extends BaseTestCase
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
putenv('DB_CONNECTION=sqlite_testing');
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
return $app;
}
public function setUp()
{
parent::setUp();
//Artisan::call('migrate:refresh');
Artisan::call('migrate');
Artisan::call('db:seed');
}
/**
* Prepare for Dusk test execution.
*
* @beforeClass
* @return void
*/
public static function prepare()
{
static::startChromeDriver();
}
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()
);
}
public function tearDown()
{
Artisan::call('migrate:reset');
parent::tearDown();
}
}
DuskTestCase and .env.testing are shown in full, the rest have corresponding parts. How can I modify this code to make phpunit recognize and use my sqlite_testing database?
source to share