I’m trying to use methods like Yii::error() or Yii::warning() from inside a unit test. Whenever I try though I get a Fatal Error:
PHP Fatal error: Call to a member function getRequest() on a non-object in /myapp/vendor/yiisoft/yii2/log/Target.php on line 269
It seems that Yii::$app is not set/available in that function. These methods work properly when used in a web/browser context, but not within unit tests. Is anyone logging from within unit tests? Does it require any special configuration?
Thank you for your response CeBe. I went back and tried adding the call to mockApplication and then realized that already exists in yii\codeception\TestCase::setUp();
I’m trying to use a very basic unit test to debug, here is what I have:
namespace tests\codeception\unit\models;
use yii\codeception\TestCase;
class DummyTest extends TestCase
{
protected function setUp()
{
parent::setUp();
}
public function testDummy() {
\Yii::error("Error message");
\Yii::warning("Warning message");
}
}
Output shows:
$ php codecept.phar run --debug unit models/DummyTest.php
Codeception PHP Testing Framework v2.0.7
Powered by PHPUnit 4.3.4 by Sebastian Bergmann.
Unit Tests (1) -----------------------------------------------------------------------------------------------------------------
Modules:
--------------------------------------------------------------------------------------------------------------------------------
Trying to test dummy (tests\codeception\unit\models\DummyTest::testDummy) Ok
--------------------------------------------------------------------------------------------------------------------------------
Time: 91 ms, Memory: 9.50Mb
OK (1 test, 0 assertions)
PHP Fatal error: Call to a member function getRequest() on a non-object in /myapp/vendor/yiisoft/yii2/log/Target.php on line 269