Search code examples
phptestingyii2codeception

Yii 2 migrations for test database


I'm failing to see a clear way to do this, although I can see it being reiterated multiple times in the documentation, specifically:

Use yii migrate tool to upgrade your test database to the latest version;

found in http://www.yiiframework.com/doc-2.0/guide-test-fixture.html#summary (just to give an example)

What happens is that no matter how I configure the _console.php and _config.php within my tests/unit folder, if I run the migration tool is picking up the main app db:

$ cd tests/unit
$ ../../yii migrate/up
Yii Migration Tool (based on Yii v2.0.0-dev)

No new migration found. Your system is up-to-date.

And yes, the test database is correctly configured (running tests will throw an error saying there are no tables). I can obviously create the db structure myself, but this is all about getting this feature work as it should.

I'm currently using the Basic app installed via composer.


Solution

  • [edit] updated the answer to reflect the current version of Yii2

    Just posting the correct answer as I found it myself and I feel a bit stupid as it was under my eyes.

    There's another yii commandline within the test folder directory, which means you can easily do:

    $ cd tests/codeception/bin
    $ chmod a+x yii
    $ yii migrate/up
    

    and it will automatically use the database you've specified in tests/codeception/config/config.php.

    Enjoy