Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use phinx migration tool with codeception test framework

I has some experience in client and server side JavaScript apps development. But now i design my first web application on php and look for best development tools stack. I use phinx to share my database structure between testing, development and production environments. I am going to use codeception for database operations testing.

The problem is that codeception expect that i will place tables creation sql commands in tests/_data/dump.sql and deletes all tables i created in phinx migration file. I can set cleanup: false in codeception.yml but i would have to clean db tables before each test in this case. And i don't know how. I found no abilities for manual cleaning db before each test in codeception.

How i can get codeception and phinx coordination?

PS: I found discussion about using migrations in codeception and it seems that benefits of it are obvious not for everyone.

like image 864
user3414982 Avatar asked Oct 30 '22 19:10

user3414982


1 Answers

With Codeception you can create a helper for any thing you want, including the migrations loading.

Here's a helper to load the database migrations before each test. I have no chance to test this code, but the main idea should be clear here.

Codeception helper:

namespace Codeception\Module;

use Codeception\Module;
use Codeception\TestInterface;
use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;

class FixtureHelper extends Module
{
    /**
     * Run database migrations before each test if database population enabled.
     *
     * @param TestInterface $test
     */
    public function _before(TestInterface $test)
    {
        $populate = $this->getModule('Db')->_getConfig('populate');

        if ($populate) {
            $this->migrateDatabase();
        }
    }

    /**
     * Run the database migrations.
     */
    public function migrateDatabase()
    {
        // Run Phinx console application.
        $app = new PhinxApplication();
        $app->setAutoExit(false);

        $output = new NullOutput();
        //$output = new ConsoleOutput();

        // Run database migrations for test environment.
        $input = new StringInput('migrate -e test');
        $app->run($input, $output);

        // ... you also can load the fixtures here
        //$input = new StringInput('seed:run -s <my-seeds> -e test');
        //$app->run($input, $output);
    }
} 

Codeception configuration (for functional testing):

actor: FunctionalTester
modules:
  enabled:
    - ... your modules
    - FunctionalHelper
    - FixtureHelper
  config:
    Db:
      dsn: '... dsn'
      user: '%DB_USER%'
      password: '%DB_PASSWORD%'
      dump: 'tests/_data/dump.sql'
      populate: true
      cleanup: true
    FixtureHelper:
      depends: Db

Database dump (tests/_data/dump.sql):

-- Dump should not be empty because cleanup will not work. 
-- So at least any silly sql query.
SELECT 1+2 AS veryComplicatedCalculations;

Phinx config (phinx.yml) must be located in the same directory as the Codeception config (codeception.yml) or you must make sure that PhinxApplication loads your config.

Hope this help!

like image 130
Anton Pelykh Avatar answered Nov 02 '22 09:11

Anton Pelykh