10

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?

1
  • 1
    You cannot use :memory: because it runs in a separate process, so you must use an actual sqlite.database file. stackoverflow.com/a/43479233/4233593 Commented Sep 15, 2017 at 21:52

1 Answer 1

8

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 ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Still can't find my tables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.