Question

I want to test my Laravel 4 applications. But I can't do it without creating database. The problem is, that I want to approach these things:

  • Separate DB for tests and for development (in memory also will be good)
  • Doing migrations and seeds before testing
  • Clearing database after testing

How to approach that?

Was it helpful?

Solution

I think this will help you get started

1. Override Database settings for testing only

Inside app/config/testing create a new file named database.php and inside it place:

return array(

  'default' => 'mysql',

  'connections' => array(

    'mysql' => array(

      'driver' => 'mysql',

      'host' => 'localhost',

      'database' => 'TEST_DB_NAME', // Create also the Database

      'username' => 'USERNAME',

      'password' => 'PASSWORD',

      'charset' => 'utf8',

      'collation' => 'utf8_unicode_ci',

      'prefix' => '',

    )
  )
);

This database will bee used only for testing purposes.

2. Add this info to codeception.yml also

modules:

  config:

    Db:

      dsn: 'mysql:host=localhost;dbname=TEST_DB_NAME'

      user: 'USERNAME'

      password: 'PASSWORD'

      dump: app/tests/_data/dump.sql

3. Migrate and seed the testing database

php artisan migrate --seed --env="testing"

4. To clean up the database

php artisan migrate:reset --env="testing"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top