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.

3 Answers 3

6

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>
Sign up to request clarification or add additional context in comments.

7 Comments

I have done all those things.. i have unit version 7.5.6.
Have you updated phpunit version ? Can you please share your phpunit.xml file.
I updated the answer please have a look that might fix your problem.
that didnt work.. i did artisan clear as well but instead of using the sqlite its using mysql.. and clearing out my tables
maybe sqlite is the problem ... I have unzipped sqlite3.def sqlite3.dll sqlite3.exe in C:\sqlite and set the environment variable.. but sqlite3 on bash is not running it
|
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.

Comments

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.

Comments

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.