title: Setup testing database for acceptance tests in Codeception (Laravel)
body: <p>I'm using Codeception framework to perform acceptance tests in Laravel 5 application. I wanted to use a separate database for testing to keep real database from changing through testing. I configued the .yml files based on Codeception documentation. But the real database gets affected anyway. This is my configuration files: </p>
  
  <p>/codeception.yml</p>
  
  <pre><code>actor: Tester
   paths:
     tests: tests
      log: tests/_output
      data: tests/_data
     support: tests/_support
     envs: tests/_envs
   settings:
     bootstrap: _bootstrap.php
     colors: true
      memory_limit: 1024M
    extensions:
    enabled:
      - Codeception\Extension\RunFailed
    modules:
    config:
       Db:
          dsn: 'mysql:host=localhost;dbname=realDB'
          user: 'root'
          password: 'secret'
          dump: 'tests/_data/dump.sql'
  </code></pre>
  
  <p>/tests/acceptance.suite.yml</p>
  
  <pre><code>class_name: AcceptanceTester
  modules:
    enabled:
      - WebDriver:
          url: 'http://localhost:8000/'
          browser: firefox
      - Laravel5:
          part: ORM
          cleanup: false # can't wrap into transaction
       Db:
        populate: true
        cleanup: true
      - \Helper\Acceptance
  </code></pre>
  
  <p>The realDB is the real database and it changes after performing acceptance test. I tried different cases for cleanup in acceptance.suite.yml: 1) cleanup: false in Laravel module and cleanup: true in Db module 2) cleanup: true in Laravel module and cleanup: true in Db module. Codeception documentation says that for acceptance test, we need to "disable cleanup and use Db module to cleanup database betweem tests". But the realDB changes anyway. </p>
  
  <p>I've tried different testing frameworks for PHP such as PHPUnit in Laravel, PHP Selenium driver, Facebook web driver and in all cases the real database get affected when performing acceptance tests. How can I correctly configure  Codeception to prevent database changes? </p>
  
  <p>Any help would be appreciated. </p>
  
  <p><strong>[UPDATE1]</strong></p>
  
  <p>As @TheFallen suggested that I use different database for testing, I change the configuration files like this:</p>
  
  <p>/codeception.yml</p>
  
  <pre><code>actor: Tester
   paths:
     tests: tests
      log: tests/_output
      data: tests/_data
     support: tests/_support
     envs: tests/_envs
   settings:
     bootstrap: _bootstrap.php
     colors: true
      memory_limit: 1024M
    extensions:
    enabled:
      - Codeception\Extension\RunFailed
    modules:
    config:
       Db:
          dsn: 'mysql:host=localhost;dbname=testDB'
          user: 'root'
          password: 'secret'
          dump: 'tests/_data/dump.sql'
  </code></pre>
  
  <p>/tests/acceptance.suite.yml</p>
  
  <pre><code>class_name: AcceptanceTester
  modules:
    enabled:
      - WebDriver:
          url: 'http://localhost:8000/'
          browser: firefox
      - Laravel5:
          part: ORM
          environment_file: .env.testing
          cleanup: true
       Db:
        populate: true
        cleanup: true
      - \Helper\Acceptance 
  </code></pre>
  
  <p>/.env.testing</p>
  
  <pre><code>APPLICATION_URL=http://localhost:8000
  APP_DEBUG=true
  APP_ENV = testing
  MYSQL_MAIN_HOST=localhost
  MYSQL_MAIN_DATABASE=realDB
  MYSQL_MAIN_USER=root
  MYSQL_MAIN_PASSWORD=secret 
  CACHE_DRIVER=array 
  DB_CONNECTION=test_mysql
  TEST_MYSQL_MAIN_DATABASE=testDB
  </code></pre>
  
  <p>/config/database.php</p>
  
  <pre><code>return [
  
      'fetch' =&gt; PDO::FETCH_CLASS,
  
      'default' =&gt; env('DB_CONNECTION', 'mysql'),
  
      'connections' =&gt; [
  
          'test_mysql' =&gt; [
              'driver'    =&gt; 'mysql',
              'host'      =&gt; env('MYSQL_MAIN_HOST', 'localhost'),
              'database'  =&gt; env('TEST_MYSQL_MAIN_DATABASE', 'testDB'),
              'username' =&gt; env('MYSQL_MAIN_USER', 'root'),
              'password' =&gt; env('MYSQL_MAIN_PASSWORD', ''),
              'charset'   =&gt; 'utf8',
              'collation' =&gt; 'utf8_unicode_ci',
              'prefix'    =&gt; '',
              'strict'    =&gt; false,
          ],
  ...
  </code></pre>
  
  <p>The .env.testing is located in the root of Laravel application.
  But real database (realDB) still gets affected and only dump file get imported in testing database (testDB). It looks like application doesn't use .env.testing. How can I fix this? </p>
  
  <p>Please note that, an AJAX request is sent to call a function changing data in database in my acceptance test. I want to rollback database transactions done by AJAX requests.</p>
  
  <p><strong>[UPDATE2]</strong></p>
  
  <p>According to <a href="http://codeception.com/for/laravel" rel="nofollow">Codeception documentation</a>, Acceptance tests will be executed in development environment using real web server, so settings from .env.testing can’t be passed to them. :(</p>
  
  <p>After all that I've done to solve this problem, I conclude that it is impossible to keep real database from changing after performing acceptance tests triggering AJAX requests to perform database transactions unless I change the default database to testing one in .env file! </p>
  
  <p>If anyone has a better solution, please share it! </p>