5

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.

user3414982
  • 357
  • 1
  • 4
  • 15
  • Sorry, you are off into uncharted territory. I have heard of dozens of frameworks on top of MySQL, but `phinx` and `codeception` are new to me. Probably others are equally ignorant since it's been 4 weeks without a nibble. – Rick James Oct 10 '17 at 01:44

1 Answers1

2

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!

Anton Pelykh
  • 2,274
  • 1
  • 18
  • 21
  • Thanks for a good answer. I did it in a different - not that clean - manner. – Georgy Ivanov Oct 14 '17 at 06:22
  • Just as a sidenote: you can load the config file using the `-c` parameter: http://docs.phinx.org/en/latest/commands.html#configuration-file-parameter – Hans Jan 23 '19 at 20:00