2016-08-12 83 views
0

我在写一个使用Symfony的php网站。最简单的方法来创建一个模拟/存根一个FilterUserResponseEvent实例?

我正在编写一个模块,用于侦听FilterUserResponseEvent对象。

我想创建一个模拟FilterUserResponseEvent对象,该对象将包含一个合适的请求对象,该对象包含一个我可以设置的cookie。

即我想制造一个合适的$event变量进入下面的函数。我希望能够在我的测试中预先定义$value = $request->cookies->get('cookie');的结果。

use FOS\UserBundle\Event\FilterUserResponseEvent; 

public function onRegistrationCompleted(FilterUserResponseEvent $event) 
{ 

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

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

} 

我该怎么做?我想下面的代码

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

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

,但它给了我这个错误:

Error: Call to undefined method stdClass::get() 

回答

1

我觉得这是更好地只使用从http基金会分量RequestCookie类。

<?php 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\ParameterBag; 

$request = new Request(); 
$request->cookies = new ParameterBag($your_cookies); 

Request类不需要任何特定的arg从环境中正常工作。同样如您在Http-Foundation的Tests中看到的,不使用存根。

+0

谢谢。你已经提醒我,有时候了解某些东西的最好方法就是查看源代码! – Ginger

相关问题