0
votes

I want to use a separate sqlite testing database for acceptance testing by PHPunit along with Facebook web driver in Laravel 5.1. I've changed the default database in phpunit.xml. After performing the tests, transactions are done in MySQL database! Because, test data are saved into MySql. (In corresponding functions, model save method is used to save an instance of a model into database).

The following are my configuration and settings related to testing database.

/config/database.php

'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
   'testing' => [
    'driver'   => 'sqlite',
    'database' => ':memory:',
    'prefix'   => '',
],

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('MYSQL_MAIN_HOST', 'localhost'),
    'database'  => env('MYSQL_MAIN_DATABASE', 'db'),
    'username' => env('MYSQL_MAIN_USER', 'root'),
    'password' => env('MYSQL_MAIN_PASSWORD', 'secret'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
]

/phpunit.xml

<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="bootstrap/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="false">
   <testsuites>
    <testsuite name="Application Test Suite">
        <directory>./tests/</directory>
    </testsuite>
   </testsuites>
   <filter>
    <whitelist>
        <directory suffix=".php">app/</directory>
    </whitelist>
   </filter>
   <php>
    <env name="APP_ENV" value="testing"/>
    <env name="DB_CONNECTION" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
    <server name='HTTP_HOST' value='http://localhost:8000' />
    <server name='REQUEST_URI' value='http://localhost:8000' />
   </php>
</phpunit>

.../tests/TestCase.php

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Artisan;

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    use \Illuminate\Foundation\Testing\DatabaseMigrations;

    public function setUp()
    {
        parent::setUp();
        Artisan::call('migrate');
       Artisan::call('db:seed');
    }

    public function tearDown()
    {
        Artisan::call('migrate:rollback');
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}

/.env

APPLICATION_URL=http://localhost:8000
APP_DEBUG=true
APP_ENV = local
CACHE_DRIVER=array
DB_CONNECTION=mysql

I've searched a lot but no success yet. Why are transactions in in-memory Sqlite applied in MySql database? How can I make facebook web driver to use phpunit.xml ?

1
Are your other env settings working properly?apokryfos
To clarify my previous comment, are your other env settings defined in phpunit.xml working properly or are they ignored?apokryfos
@apokryfos yes. It worked properly in non-testing environment. I added its configuration to the post.Aref
@apokryfos yes other env settings in phpunit.xml works fine. I even think that phpunit recognizes the sqlite as its default database because when performing migration, it showed errors that only happens in sqlite ( and not mysql) . I fixed those errors. But I don't know why MySql database get affected after testing.Aref
Check what the value of env('DB_CONNECTION', 'mysql') is in your tests. It may be that it just reads from .env. Check laracasts.com/index.php/discuss/channels/testing/… for a way to use .env.testingapokryfos

1 Answers

0
votes

try to set this 'database'=>'/absolute/path/to/database.sqlite'

Read the docs https://laravel.com/docs/5.2/database