We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Migration launch from php script PHPUnit

I'm triying to run my generated mysql migration on a sqlite for test purposes. I want to recreate my db on an sqlite db on a test to unittest models, behaviours, relationships, etc... Is there any way to run the migration from my php script?

edited May '15

Have you tried a suite listener? Take a look at this Listener I'm using:

class TestListener extends \PHPUnit_Framework_BaseTestListener
{
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        if ($suite->getName() == "integration") {
                $command = "mysql -h {$config->database->host} -u {$config->database->username} -p{$config->database->password} -e 'CREATE DATABASE IF NOT EXISTS {$config->database->dbname};' 2>/dev/null";
                exec($command);
                $command = "mysqldump -h {$config->database->host} -u {$config->database->username} -p{$config->database->password} -d {$config->database->original_db_name} 2>/dev/null | mysql -h {$config->database->host} -u {$config->database->username} -p{$config->database->password} -D{$config->database->dbname} 2>/dev/null";
                exec($command);
        }
    }
}

And then on the phpunit.xml config:

<listeners>
    <listener class="tests\base\TestListener" file="./base/TestListener.php"></listener>
</listeners>

Hope it helps you

(More info about listeners: https://phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestListener )

edited May '15

I always forget phpunit listeners :) altough i have no problem in setting it on the test setUp or whatever, what I want is to use the migrations to populate the structure of my sqlite test database using the generated migration from the real one. I suppose that migrations handle all the necesary grammar stuff etc so I just need to run it with a new db adapter and its done. At least I hope so.

The workflow is:

generate migration from my mysqldb

later when I run my phpunit the test will use those migrations to create the db on another db config so I can test my last changes on db without any configuration.

Have you tried a suite listener? Take a look at this Listener I'm using:

Anyone? I would like to run-generate from the migration programatically, so I can use it on specific tests.