狂饮后的请求没有嘲笑

时间:2019-12-09 02:15:10

标签: php laravel mocking phpunit guzzle

我正在尝试为使用Guzzle http客户端的端点编写测试用例。但这并没有被嘲笑。它仍然符合实际的请求并等待响应。我的代码如下。

测试用例

$mockHandler = new MockHandler([
    new Response(200, ['X-Foo' => 'Bar'], 'Hello, World'),
]);
$handler = HandlerStack::create($mockHandler);
$client = new Client(['handler' => $handler]);

$this->app->instance(\GuzzleHttp\Client::class, $client);

$response = $this->actingAs($user)
            ->get('/api/request?' . http_build_query($request));

我在以下服务类中使用Guzzle客户端的方式

class SomeService implements SomeContract
{
    public function __construct(\GuzzleHttp\Client $client, $extraInfo)
    {
        $this->client = $client;
        $this->setExtraInfo($extraInfo);
    }

    private function getToken($scope)
    {
        $response = $this->client->post(
            '/post/request/url',
            [
                'form_params' => [
                    "param_1" => "value_1",
                    "param_2" => 'value_2'
                ],
                'headers' => [
                    "Authorization"=> "Basic 5235252333",
                    "Content-Type" => 'application/x-www-form-urlencoded'
                ],
                self::COMMON_REQUEST_PARAMS,
                'cert' => ['cert path' , 'pass phrase']
            ]
        );

        $jsonOutput = json_decode($response->getBody()->getContents());

        return $jsonOutput->access_token;
    }
}

0 个答案:

没有答案