How to use phinx migration tool using code validation scheme

I have experience working with JavaScript applications on client and server sites. But now I am building my first php web application and are looking for the best stack of development tools. I use phinx to share a database structure between test, development, and production environments. I am going to use code to test database operations.

The problem is that the code generation assumes that I will place the sql create sql commands in tests/_data/dump.sqland removes all the tables that I created in the phinx migration file. I can install cleanup: falsein codeception.yml, but in this case I will need to clear the db tables before each test. And I do not know how to do this. I did not find the possibility of manually cleaning db before each test in coding.

How can I get code and phinx coordination?

PS: I found a discussion on the use of migrations in coding , and it seems that the benefits of this are not obvious to everyone.

+4
source share
1 answer

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);
    }
} 

( ):

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

(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 (phinx.yml) , Codeception (codeception.yml), , PhinxApplication .

!

+1

Source: https://habr.com/ru/post/1607070/


All Articles