Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up an in memory SQLite database for testing in Laravel 5.4

I am working on developing a suite of browser tests for a Laravel 5.4 application using Dusk. One of the tests checks whether an object is created and since this is a browser test the value is saved to the database when the submit button is clicked. Obviously I don't want my database to become cluttered with test data and there is also an issue since the name of the object has to be unique and therefore the test will fail after the first run. I decided the best solution to this problem was to implement a testing database which is cleared at the end of each test cycle. However I am having some problems setting it up.

Currently, my code does not throw any errors but phpunit just will not use the testing database. I installed codeception when I was researching how to implement the database but I'm not sure what to do with it. Here is the code I currently 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 their entirety, the others have the relevant portions. How can I modify this code to make phpunit recognise and use my sqlite_testing database?

like image 583
Kai D Avatar asked Jun 21 '17 13:06

Kai D


1 Answers

It's because your config is cached.

Clear your config cache with php artisan config:clear

Now phpunit will read the xml file.

Never cache your config in dev environment ;)

like image 165
Damosse31 Avatar answered Oct 06 '22 02:10

Damosse31