>我有一个服务,我需要编写一个phpunit测试。
此服务具有接受来自JSON POST的Symfony\Component\HttpFoundation\Request对象的方法
。我需要以某种方式使用 JSON POST 模拟请求对象,以便我可以正确测试此方法
下面是服务方法的外观
namespace AppBundleService;
use SymfonyComponentHttpFoundationParameterBag;
use SymfonyComponentHttpFoundationRequest;
/**
* Class SubscriptionNotificationProcessor
* @package AppBundleService
*/
class SubscriptionNotificationProcessor
{
...
public function process(Request $request)
{
$parameterBag = $request->request;
$notification_type = $parameterBag->get('notification_type');
...
}
}
我的问题是,如何模拟填充了 JSON 数据的请求参数?
这是SymfonyComponentHttpFoundationRequest
的构造函数
public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
$this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
}
如您所见,$resquest
是第二个参数,您可以执行以下操作:
$request = new Request([], [json_encode([ 'foo' => 'bar'])], [], [], [], [], []);
现在,您可以将对象传递给服务process($request);