guzzle`http_errors`将错误从500更改为404

时间:2015-08-04 09:40:46

标签: php phpunit guzzle

我有一个函数im测试,它假设返回错误500但是在'http_errors' => 'false'定义中添加put之后,返回的错误从500变为404。 这是我的功能:

public function testApiAd_updateWithIllegalGroupId($adId) 
{
    $client = new Client(['base_uri' => self::$base_url]);
    try {
        $response = $client->put(self::$path.$adId, ['form_params' => [
          'name' => 'bellow content - guzzle testing',
          'description' => 'guzzle testing ad - demo',
          'group_id' => '999999999',
          ]]);
    } catch (Guzzle\Http\Exception\BadResponseException $e) {
        //Here i want to compare received error to 500
    }
}

现在这个函数将返回server error: 500,但它也会阻止类执行其余的测试,我无法断言它。 我如何在我的函数中使用gu getStatusCode()同时获得错误500而不是404,如上所述

1 个答案:

答案 0 :(得分:2)

BadResponseException包含原始Request和Response对象。所以你可以,catch阻止以下断言:

} catch (Guzzle\Http\Exception\BadResponseException $e) {
        //Here i want to compare received error to 500
        $responseCode = $e->getResponse()->getStatusCode();
        $this->assertEquals(500, $responseCode, "Server Error");
    }

更多信息in the doc