Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ssquare's avatar

How to setup environment for testing for laravel using .env.testing

I am trying to run some basic testing on my laravel app.

What I have done so far:

cp .env .env.testing

my .env.testing looks like :

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:WSkPBl1NrEIjoOjV32QQocm/Bxaq+wU9cm959+cabcmM=
APP_DEBUG=true
APP_URL=http://laracms.test

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=sqlite
DB_DATABASE="var/www/laracms/database/laracms.sqlite"

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
MAIL_DRIVER=smtp

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

and my test code:

    /** @test */
    public function a_confirmation_email_was_sent_after_registration()
    {
        Event::fake();
        $email = '[email protected]';
        $response = $this->post('/api/auth/register', [
            'name' => 'John Doe',
            'email' => $email,
            'password' => 'password',
            'password_confirmation' => 'password'
        ]);
        Event::assertDispatched(Registered::class);
    }

Whenever I tried to run this test, I received this error:

1) Tests\Feature\AuthTest::a_confirmation_email_was_sent_after_registration
Illuminate\Database\QueryException: Database (var/www/laracms/database/laracms.sqlite) does not exist. (SQL: PRAGMA foreign_keys = ON;)

However, I have already created the laracms.sqlite file https://ibb.co/XttQLbY.

I know we could also run testing by uncommenting following two lines on phpunit.xml

        <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->

However, I want a sqlite file to be created so that I could check the data on certain cases using db browser.

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

Change to an absolute path:

DB_DATABASE="/var/www/laracms/database/laracms.sqlite"

Please or to participate in this conversation.