thinway's avatar
Level 11

[Sail] Run database tests with sqlite

Hi guys, I'm trying to run database tests in my local environment that is running with Sail. Looks that everything is working fine, but what I try to run a simple database test it looks my environment can't connect to the sqlite instance that is running in the Laravel container. For example, with the default users table migration I can migrate the table without any problems in the database container. I have this simple test:

<?php

namespace Tests\Unit;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\TestCase;

class UserTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    /**
     * @test
     */
    public function user_database_has_expected_columns()
    {
        $this->assertTrue(
            Schema::hasColumns('users', [
                'id', 'name', 'email', 'avatar', 'email_verified_at', 'password', 'created_at', 'updated_at'
            ]), 1
        );
    }
}

with this setup in my phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

But when I tried to run the test it doesn't pass, I think because it can't connect with sqlite instance:

sail test

   PASS  Tests\Unit\ExampleTest
  ✓ example

   FAIL  Tests\Unit\UserTest
  ⨯ user database has expected columns

   PASS  Tests\Feature\ExampleTest
  ✓ example

  ---

  • Tests\Unit\UserTest > user database has expected columns
   PHPUnit\Framework\Error\Warning

  Trying to access array offset on value of type null

  at vendor/laravel/framework/src/Illuminate/Support/Facades/Schema.php:45
     41▕      * @return \Illuminate\Database\Schema\Builder
     42▕      */
     43▕     protected static function getFacadeAccessor()
     44▕     {
  ➜  45▕         return static::$app['db']->connection()->getSchemaBuilder();
     46▕     }
     47▕ }
     48▕

      +3 vendor frames
  4   tests/Unit/UserTest.php:23
      Illuminate\Support\Facades\Facade::__callStatic()


  Tests:  1 failed, 2 passed
  Time:   1.07s

Any ideas guys? How can I run my db tests over sqlite like if I were working with valet in my local environment?

Thx

0 likes
0 replies

Please or to participate in this conversation.