RoaRequestTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. use AlibabaCloud\Client\Tests\Mock\Services\Nlp\NlpRequest;
  8. /**
  9. * Class RoaRequestTest
  10. *
  11. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request
  12. */
  13. class RoaRequestTest extends TestCase
  14. {
  15. /**
  16. * @throws ClientException
  17. */
  18. public function testRoa()
  19. {
  20. // Setup
  21. $clusterId = \time();
  22. // Test
  23. try {
  24. AlibabaCloud::accessKeyClient(
  25. getenv('ACCESS_KEY_ID'),
  26. getenv('ACCESS_KEY_SECRET')
  27. )->asDefaultClient()->regionId('cn-hangzhou');
  28. $result = AlibabaCloud::roa()
  29. ->pathPattern('/clusters/[ClusterId]/services')
  30. ->method('GET')
  31. ->product('CS')
  32. ->version('2015-12-15')
  33. ->action('DescribeClusterServices')
  34. ->pathParameter('ClusterId', $clusterId)
  35. ->connectTimeout(25)
  36. ->timeout(30)
  37. ->request();
  38. self::assertNotEmpty($result->toArray());
  39. } catch (ServerException $e) {
  40. self::assertEquals(
  41. "cluster ($clusterId) not found in our records",
  42. $e->getErrorMessage()
  43. );
  44. }
  45. }
  46. /**
  47. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  48. * @expectedExceptionMessageRegExp /ServiceUnavailable: The request has failed due to a temporary failure of the server./
  49. * @throws ClientException
  50. * @throws ServerException
  51. */
  52. public function testRoaContent()
  53. {
  54. AlibabaCloud::accessKeyClient(
  55. \getenv('ACCESS_KEY_ID'),
  56. \getenv('ACCESS_KEY_SECRET')
  57. )->name('content')->regionId('cn-shanghai');
  58. $request = new NlpRequest();
  59. $request->pathParameter('Domain', 'general');
  60. $request->jsonBody([
  61. 'lang' => 'ZH',
  62. 'text' => 'Iphone专用数据线'
  63. ]);
  64. $result = $request->client('content')
  65. ->connectTimeout(25)
  66. ->timeout(30)
  67. ->request();
  68. self::assertEquals('Iphone', $result['data'][0]['word']);
  69. }
  70. /**
  71. * @throws ClientException
  72. * @throws ServerException
  73. */
  74. public function testSearchImage()
  75. {
  76. AlibabaCloud::accessKeyClient(
  77. \getenv('ACCESS_KEY_ID'),
  78. \getenv('ACCESS_KEY_SECRET')
  79. )->regionId('cn-shanghai')->name('im');
  80. $request = AlibabaCloud::roa()
  81. ->connectTimeout(40)
  82. ->timeout(50)
  83. ->client('im')
  84. ->product('ImageSearch')
  85. ->version('2019-03-25')
  86. ->method('POST')
  87. ->action('SearchImage')
  88. ->pathPattern('/v2/image/search')
  89. ->accept('application/json')
  90. ->contentType('application/x-www-form-urlencoded; charset=UTF-8');
  91. $content = file_get_contents(__DIR__ . '/ImageSearch.jpg');
  92. try {
  93. $result = $request->options([
  94. 'form_params' => [
  95. 'InstanceName' => getenv('IMAGE_SEARCH_INSTANCE_NAME'),
  96. 'PicContent' => base64_encode($content),
  97. 'Start' => 0,
  98. 'Num' => 10
  99. ]
  100. ])
  101. ->request();
  102. self::assertArrayHasKey('Actions', $result);
  103. } catch (ServerException $e) {
  104. self::assertEquals(400, $e->getCode());
  105. }
  106. }
  107. }