dqfxao2898 2016-08-12 20:44
浏览 9

最简洁的方法来创建一个模拟/存根FilterUserResponseEvent实例?

I am writing a php website using Symfony.

I am writing a module that listens for a FilterUserResponseEvent object.

I would like to create a mock FilterUserResponseEvent object that will contain a suitable request object that contains a cookie that I can set.

i.e. I would like to manufacture a suitable $event variable to pass into the function below. I would like to be able to pre-define the result of $value = $request->cookies->get('cookie'); inside my test.

use FOS\UserBundle\Event\FilterUserResponseEvent;

public function onRegistrationCompleted(FilterUserResponseEvent $event)
{

    $response = $event->getResponse();
    $request  = $event->getRequest();

    // get cookie
    $value = $request->cookies->get('cookie');

}

How could I do that? I am trying the following code

   $request = new stdClass();      
   $request->cookies = new stdClass();
   $request->cookies->get = function($key){
       return 'cookie';
   };

   print(  $request->cookies->get('asd'));

but it gives me this error:

Error: Call to undefined method stdClass::get()
  • 写回答

1条回答 默认 最新

  • dsieyx2015 2016-08-12 20:58
    关注

    I think it is better to just use the Request and Cookie class from Http-Foundation component.

    <?php
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\ParameterBag;
    
    $request = new Request();
    $request->cookies = new ParameterBag($your_cookies);
    

    Request class does not need any specific arg from environment to work properly. Also as you can see in Tests for Http-Foundation, no stub is used.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c