Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 1.42 KB

10-Test.md

File metadata and controls

70 lines (52 loc) · 1.42 KB

← Log | Test(中文) | Home →


Test

Mock Response

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ServerException;

$header = ['X-Foo' => 'Bar'];
$body   = [
    'Code'    => 'code',
    'Message' => 'message',
];

AlibabaCloud::mockResponse(200, $header, $body);
AlibabaCloud::mockResponse(500, $header, $body);

$result = AlibabaCloud::rpc()
                      ->product('ecs')
                      ->regionId('cn-hangzhou')
                      ->request();

print_r($result->toArray());

try {
    AlibabaCloud::rpc()
                ->product('ecs')
                ->regionId('cn-hangzhou')
                ->request();
} catch (ServerException $e) {
    print_r($e->getErrorMessage());
    print_r($e->getResult()->toArray());
}

Mock Exception

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use GuzzleHttp\Psr7\Request;

AlibabaCloud::mockRequestException('Error', new Request('GET', 'test'));

try {
    AlibabaCloud::rpc()->product('ecs')->regionId('cn-hangzhou')->request();
} catch (ClientException $e) {
    // Error
    echo $e->getErrorMessage();
}

Cancel Mock

<?php

use AlibabaCloud\Client\AlibabaCloud;

AlibabaCloud::cancelMock();

← Log | Test(中文) | Home →