2

This error is bugging me. my laravel version is 5.6 I don't think I have made any changes to my phpunit.xml file except these

<php>
        <env name="APP_ENV" value="testing"/>            
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="MAIL_DRIVER" value="array"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>

    </php>

I'm writing test like this:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class DemoTest extends TestCase
{
    use DatabaseMigrations;

    public function test_it_is_nothing()
    {
        $this->get('/volunteers')->assertStatus('200');
    }
}

I have also referenced to this post Laravel multiple databases PHPUnit but not helpful.

rakesh shrestha
  • 1,335
  • 19
  • 37

3 Answers3

4

Please make sure following things:-

  • make sure you have sqlite db installed
  • clear your config php artisan config:clear
  • also make sure your phpunit.xml exist on your project root directory
  • check if your have DB_DRIVER env in your phpunit.xml please remove it
  • when running test try to point your config to phpunit.xml vendor/bin/phpunit --config PATH_TO_YOUR_PHPUNIT_XML_FILE
  • try to update your phpunit version

If none of above work please try adding another db config to your config/database.php

'sqlite_testing' => [
    'driver'   => 'sqlite',
    'database' => ':memory:',
    'prefix'   => '',
]

then your phpunit.xml would look like this

<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
    ...
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_DEFAULT" value="sqlite_testing" />
    </php>
</phpunit>
ahmed waleed
  • 477
  • 1
  • 4
  • 12
0

For me, the problem was that I was having another connection for the model being tested. e.g:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Salesperson extends Model
{
    use SoftDeletes;

    protected $connection = 'mysql_dynamic_db';

    /**
    * The attributes that are mass assignable.
    *
    * @var array
    */
    protected $fillable = [
        'name',
        'f_name',
        'cnic',
        'address',
        'mobile',
        'email',
        'salary',
        'comm_percent',
    ];
}

So for the time when I run tests, I just comment the following line:

protected $connection = 'mysql_dynamic_db';

and the above error that says unknown database :memory: is gone. Now all works fine.

Noor Ahmed
  • 178
  • 12
0

First, check if the line extension=pdo_sqlite is not commented in your php.ini.

change it

;extension=pdo_sqlite

to

extension=pdo_sqlite

And go to your config/database.php file and change the sqlite database like the example below

'connections' => [
  'sqlite' => [
    'driver' => 'sqlite',
    'url' => env('DATABASE_URL'),
    'database' => env('DB_DATABASE',database_path('database.sqlite')),
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
  ],
  ....
];

to

'connections' => [
  'sqlite' => [
    'driver' => 'sqlite',
    'url' => env('DATABASE_URL'),
    'database' => ':memory:',
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
  ],
];

You can also change the database, but use one for testing, don't use your production.

You can simply change the name :memory: to the name of the database you are using like this

this

<env name="DB_DATABASE" value=":memory:"/>

to

<env name="DB_DATABASE" value="name_database"/>

Don't forget to change the DB_CONNECTION if necessary.