M
M
Marcuzy2015-07-01 13:01:42
Yii
Marcuzy, 2015-07-01 13:01:42

How to initialize Yii::$app for a test?

I created an application based on https://github.com/trntv/yii2-starter-kit
I'm trying to master TDD, there is a model with such validation rules

public function rules()
  {
    return [
      [['brand_name', 'article_name', 'amount', 'price'], 'required'],
      
      [['price'], 'number'],
      [['amount'], 'number'],

      [['brand_name'], 'string', 'max'=>Part::BRAND_NAME_MAX_LENGTH, 'min'=>Part::BRAND_NAME_MIN_LENGTH],
      [['article_name'], 'string', 'max'=>Part::ARTICLE_NAME_MAX_LENGTH, 'min'=>Part::ARTICLE_NAME_MIN_LENGTH],
    ];
  }

The test creates an instance of the model, fills it with data, and when the validate() method is called, the test fails with an error
1) tests\codeception\backend\unit\PriceRowTest::testValidateForColumnsOrder1
yii\base\ErrorException: Trying to get property of non-object
[yii\base\ErrorException] Trying to get property of non-object

#1  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\validators\StringValidator.php:85
#2  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\base\Object.php:107
#3  yii\base\Object->__construct
#4  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\di\Container.php:372
#5  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\di\Container.php:151
#6  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\BaseYii.php:344
#7  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\validators\Validator.php:206
#8  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\base\Model.php:441
#9  D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\base\Model.php:405
#10 D:\OpenServer_\OpenServer\domains\abparts.local\vendor\yiisoft\yii2\base\Model.php:185

Walking through the stack, I found that in StringValidator Yii::$app is not an object, i.e. the application is not initialized for the test. How to initialize it correctly, as I understand it, it needs to be done somewhere in bootstrap.php, and why is it not initialized in the default template?
UPD:
Added the following lines to the setUp method of the test
$config = require(__DIR__ . '/../../config/backend/unit.php');
(new \yii\console\Application($config));

and everything worked, however, why can't this be done in the _bootstrap.php file (tried, but no effect).
UPD2:
Solution:
class FooTest extends TestCase
{
  protected function setUp()
    {
        parent::setUp(); //важно вызвать родительский метод
    }

    //....
}

In parent::setUp(); mockApplication() is called.
And it’s more correct to use _before/_after instead of setUp / tearDown, if codeception is used, then no additional. freezes do not appear.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Mikhail Osher, 2015-07-01
@Marcuzy

https://github.com/yiisoft/yii2/blob/master/tests/...

protected function setUp()
{
    parent::setUp();
    $this->mockApplication();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question