There are a few places in the docs where it says to "create a mock for request", but never does it say how. Now I need to, but I cannot find a nice way to do it, although several ways have worked.
What is the standard way in Yii2 to mock application components?
And if needed, I know I can add "class" => "\tests\mocks\RequestMock and do whatever I want.
But.
The config completely replaces the default config, so hit all the InvalidConfigExceptions listed in the yii\base\Application such as “The “id” configuration for the Application is required.” which I don’t see ever being set in the Yii unit tests. What am I missing?
Ah ok. I see you have yiiunit\TestCase that does the application merging. I had missed that.
So for others I created the following in my own namespace to extend from so that I could still use codeception\TestCase:
abstract class TestCase extends \yii\codeception\TestCase
{
/**
* Populates Yii::$app with a new application
* The application will be destroyed on tearDown() automatically.
* @param array $config The application configuration, if needed
*/
protected function mockApplication($config = [])
{
$appConfig = [];
if ($this->appConfig) {
$configFile = Yii::getAlias($this->appConfig);
if (!is_file($configFile)) {
throw new InvalidConfigException("The application configuration file does not exist: {$this->appConfig}");
}
$appConfig = require($configFile);
}
$mergedConfig = ArrayHelper::merge($appConfig, $config);
return parent::mockApplication($mergedConfig);
}
}