RequestAsyncTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Result\Result;
  6. use Exception;
  7. use GuzzleHttp\Exception\RequestException;
  8. use PHPUnit\Framework\TestCase;
  9. use Psr\Http\Message\ResponseInterface;
  10. use AlibabaCloud\Client\Support\Stringy;
  11. /**
  12. * Class RequestAsyncTest
  13. *
  14. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request
  15. * @coversDefaultClass \AlibabaCloud\Client\Request\RpcRequest
  16. */
  17. class RequestAsyncTest extends TestCase
  18. {
  19. /**
  20. * @throws ClientException
  21. * @throws Exception
  22. */
  23. public function testRpAsync()
  24. {
  25. AlibabaCloud::accessKeyClient(
  26. \getenv('ACCESS_KEY_ID'),
  27. \getenv('ACCESS_KEY_SECRET')
  28. )->asDefaultClient()->regionId('cn-hangzhou');
  29. $promise = AlibabaCloud::rpc()
  30. ->method('POST')
  31. ->product('Cdn')
  32. ->version('2014-11-11')
  33. ->action('DescribeCdnService')
  34. ->connectTimeout(25)
  35. ->timeout(30)
  36. ->requestAsync();
  37. $promise->then(
  38. static function (Result $result) {
  39. self::assertArrayHasKey('Code', $result);
  40. self::assertEquals(
  41. 'Forbidden.RAM',
  42. $result['Code']
  43. );
  44. self::assertNotEmpty(
  45. 403,
  46. $result->getStatusCode()
  47. );
  48. return $result;
  49. },
  50. static function (RequestException $e) {
  51. self::assertEquals(
  52. 'POST',
  53. $e->getRequest()->getMethod()
  54. );
  55. }
  56. )->wait();
  57. }
  58. /**
  59. * @throws ClientException
  60. * @throws Exception
  61. */
  62. public function testRpAsyncTimeout()
  63. {
  64. AlibabaCloud::accessKeyClient(
  65. \getenv('ACCESS_KEY_ID'),
  66. \getenv('ACCESS_KEY_SECRET')
  67. )->asDefaultClient()->regionId('cn-hangzhou');
  68. $promise = AlibabaCloud::rpc()
  69. ->method('POST')
  70. ->product('Cdn')
  71. ->version('2014-11-11')
  72. ->action('DescribeCdnService')
  73. ->connectTimeout(0.001)
  74. ->timeout(30)
  75. ->requestAsync();
  76. $promise->then(
  77. static function (ResponseInterface $res) {
  78. self::assertNotEmpty(200, $res->getStatusCode());
  79. return $res;
  80. },
  81. static function (Exception $e) {
  82. self::assertTrue(Stringy::contains($e->getMessage(), 'cURL error'));
  83. }
  84. )->wait();
  85. }
  86. }