Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Symfony2控制器内模拟一个Guzzle请求_Php_Testing_Phpunit_Guzzle - Fatal编程技术网

Php 在Symfony2控制器内模拟一个Guzzle请求

Php 在Symfony2控制器内模拟一个Guzzle请求,php,testing,phpunit,guzzle,Php,Testing,Phpunit,Guzzle,我正在创建一个功能测试,我需要模拟从Guzzle发出的请求到外部API的响应。我怎样才能模拟这个请求?我找到了模拟请求本身的例子,但我想模拟在另一个请求中发出的请求 例如: 我有控制器: use GuzzleHttp\Client; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFou

我正在创建一个功能测试,我需要模拟从Guzzle发出的请求到外部API的响应。我怎样才能模拟这个请求?我找到了模拟请求本身的例子,但我想模拟在另一个请求中发出的请求

例如:

我有控制器:

use GuzzleHttp\Client;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

class MyController extends Controller
{
    public function indexAction(Request $request)
    {
        $response = Client()->post('http://some.url', [
            'form_params' => [
                'param1' => 'value1'
            ]
        ]);

        $result = $this->doSomethingWithTheResponse($response);
        return new JsonResponse(['status' => $result], Response::HTTP_OK);
    }
}
我有一个测试班:

class MyTestClass extends WebTestCase
{
    public function testSomething()
    {
        $client = static::createClient();
        $client->request('GET', 'path_to_my_controller');
        $response = json_decode($client->getResponse()->getContent());

        $this->assertEquals($response, $expectedResponse);
    }
}
如何在控制器中模拟guzzle请求,并在测试类中仍然使用
$client->request()