Tests for Console Commands

Hi

I have no clue how to create a test for a console command in Yii2 neither unit test nor functional test. Can someone show me an example or a guide please ?

Update:

How can I run a command controller in test environment ?

Cheers

Check how its done in Yii core tests.

It does not look like Yii is using Codeception at all, which is a shame (considering that it is the de facto testing framework for the framework). :)

Yii is using it for testing app templates. For unit testing, indeed pure phpunit is used. There will be no difference if you’ll run these tests using Codeception though…

For anyone looking for the answer, you can test console applications by creating a test configuration in the same way you do for any other Yii2 application (see the advanced template for examples), then creating a test as follows:


class CronCest

{

    public function sendPromos(FunctionalTester $I)

    {

         Yii::$app->createControllerByID('cron')->run('send-promos');

    }

}

That’ll run the same thing as if it has been run from the console as


yii cron/send-promos

2 Likes