在我的yii2控制器中,我正在运行:
$address = \Yii::$app->request->post('address');
如何在PHPUnit中模拟此请求?我曾尝试使用嘲弄,但无法弄清楚如何模拟\ Yii :: $ app部分?
答案 0 :(得分:1)
在Yii2中模拟请求,请尝试
use yii\web\Request;
use Codeception\Stub;
private function mockRequest($attributes){
// mock a request
$_SERVER['REQUEST_URI'] = 'http://localhost';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
\Yii::$app->requestedAction = new Action('fake', $this->model);
\Yii::$app->setHomeUrl('http://localhost');
return Stub::make(Request::class, [
'getUserIP' =>'127.0.0.1',
'enableCookieValidation' => false,
'getUserAgent' => 'Dummy User Agent',
'getBodyParams' => [
'MyModel' => $attributes
],
]);
}