0

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 ?

Aref
  • 39
  • 1
  • 2
  • 11
  • Are your other `env` settings working properly? – apokryfos Jul 13 '16 at 12:05
  • To clarify my previous comment, are your other `env` settings defined in `phpunit.xml` working properly or are they ignored? – apokryfos Jul 13 '16 at 12:08
  • @apokryfos yes. It worked properly in non-testing environment. I added its configuration to the post. – Aref Jul 13 '16 at 12:09
  • @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 Jul 13 '16 at 12:14
  • Check what the value of `env('DB_CONNECTION', 'mysql')` is in your tests. It may be that it just reads from `.env`. Check https://laracasts.com/index.php/discuss/channels/testing/laravel-5-testing-environment-1?page=1 for a way to use `.env.testing` – apokryfos Jul 13 '16 at 12:19
  • @apokryfos Thanks for your guidance. It made me realize that the issue might root in using facebook web driver along with PHPunit to perform acceptance tests. How can I make facebook web driver to use phpunit.xml configs or a .env.testng file ? – Aref Jul 13 '16 at 12:41
  • When you say facebook web driver you mean for Selenium testing integration? – apokryfos Jul 13 '16 at 13:08
  • @apokryfos yep. https://github.com/facebook/php-webdriver – Aref Jul 13 '16 at 13:13
  • Selenium acts on the system via the webserver and not via PHPUnit as far as I know. It's as though a normal user is using the site (which is the whole point). You may need to do something like rename your `.env` to `.env.bak` and replace it with an `.env` created for testing and restore it after the tests finish. – apokryfos Jul 13 '16 at 13:16
  • @apokryfos thanks for your advice. How about using Codeception? If I use that, can it detect the right config? – Aref Jul 13 '16 at 13:36
  • No idea, from a glance at the site I'm inclined to say that it's a similar problem to Selenium. Since it does use the webdriver or curl calls (which are not intercepted by phpunit either). – apokryfos Jul 13 '16 at 14:09

1 Answers1

0

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

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

Kapil Garg
  • 462
  • 1
  • 5
  • 17
  • Thanks for your suggestion but it wasn't the reason of my problem. It seems that acceptance tests work inside a webserver so test data can’t be cleaned up by rolling back transaction. I gave up the Facebook webdriver framework and tried Codeception but have the same problem in Codecption too. If you've worked with Codeception, please take a look at the below question and help me. http://stackoverflow.com/questions/38411641/setup-testing-database-for-acceptance-tests-in-codeception-laravel . Thanks in advance. – Aref Jul 17 '16 at 09:06