Yii 3.0. Getting started with testing


(Odai Alali) #1

I am trying to write testing for a behavior written with yii3 (multilingual behavior).

I am not that experienced with PHPUnit so I am reading the documentation and the tests in yii repos.

I was not able to mock the application. I am always getting the following exception

yii\di\exceptions\InvalidConfigException: Failed to instantiate "app".       
                                                                             
/app/vendor/yiisoft/di/src/AbstractContainer.php:417                         
/app/vendor/yiisoft/di/src/AbstractContainer.php:340                         
/app/vendor/yiisoft/di/src/AbstractContainer.php:186                         
/app/vendor/yiisoft/di/src/AbstractContainer.php:131                         
/app/vendor/yiisoft/di/src/Container.php:55                                  
/app/vendor/yiisoft/yii-core/tests/TestCase.php:90                           
/app/tests/PolyglotBehaviorTest.php:19                                       
/app/vendor/phpunit/phpunit/phpunit:53                                       
                                                                             
Caused by                                                                    
ReflectionException: Class app does not exist                                
                                                                             
/app/vendor/yiisoft/di/src/AbstractContainer.php:415                         
/app/vendor/yiisoft/di/src/AbstractContainer.php:340                         
/app/vendor/yiisoft/di/src/AbstractContainer.php:186                         
/app/vendor/yiisoft/di/src/AbstractContainer.php:131                         
/app/vendor/yiisoft/di/src/Container.php:55                                  
/app/vendor/yiisoft/yii-core/tests/TestCase.php:90                           
/app/tests/PolyglotBehaviorTest.php:19                                       
/app/vendor/phpunit/phpunit/phpunit:53                                       

my test class is

class PolyglotBehaviorTest  extends \yii\tests\TestCase
{
    protected function setUp()
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->mockApplication();
    }

    public function testSql()
    {
        $this->assertEquals(2, 01+1);
    }
}

What am I doing wrong? Anybody have an advice where to start with this topic?


(Odai Alali) #2

If anybody interested, requiring yiisoft/yii-console solved my problem. the class app\console\Application is obviously needed to run tests in console environment when calling $this->mockApplication(). It took me sometime to figure out that this class is now in a separate package and to understand how hiqdev/composer-config-plugin works.

The new configuration with hiqdev/composer-config-plugin is really confusing.