Friday, October 10, 2014

Repopulation of database using mysql instead of db module


What is DB module:

The DB module is used to cleaning database before each test. So this module was added into codeception.yml. To have your database properly cleaned you should configure it to access the database. Also provides actions to perform checks in database.

The cofig can be done in below formats:


modules:
   enabled: [Db]
   config:
      Db:
         dsn: 'mysql:host=localhost;dbname=test1'
         user: 'root'
         password: ''
         dump: 'tests/_data/dump.sql'
         populate: true
         cleanup: false

So config for db module is done.
Now comes to the real work scenarios.


You must have experienced that Db module is quite slow when loading large database dumps and it is also not a good idea when dealing with  complex SQLs.

So i think it's an  good idea to leave tests/_data/dump.sql to be empty file and leave cleanup option of Db module enabled, so database will be cleaned up before each test, yet dump not to be loaded. We will use realdump.sql file to populate database.

To load dump before each test you can use AcceptanceHelper or any other Helper class you have. Probably you will need to create your own helper with codecept g:helper DbHelper command.

In its before method you can load db dump (using mysql in this example)

<?php
public function _before(\Codeception\TestCase $test)
{
    exec("mysql -u user -ppassword test1 < tests/_data/real_dump.sql");
}

5 comments:

  1. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
    "

    ReplyDelete

Powered by Blogger.