RoaRequestTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\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\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. * @throws ClientException
  48. * @throws ServerException
  49. */
  50. public function testRoaContent()
  51. {
  52. $this->expectException(ServerException::class);
  53. $reg = '/InvalidAction.NotFound: Specified api is not found, please check your url and method./';
  54. if (method_exists($this, 'expectExceptionMessageMatches')) {
  55. $this->expectExceptionMessageMatches($reg);
  56. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  57. $this->expectExceptionMessageRegExp($reg);
  58. }
  59. AlibabaCloud::accessKeyClient(
  60. \getenv('ACCESS_KEY_ID'),
  61. \getenv('ACCESS_KEY_SECRET')
  62. )->name('content')->regionId('cn-shanghai');
  63. $request = new NlpRequest();
  64. $request->pathParameter('Domain', 'general');
  65. $request->jsonBody([
  66. 'lang' => 'ZH',
  67. 'text' => 'Iphone专用数据线'
  68. ]);
  69. $result = $request->client('content')
  70. ->connectTimeout(25)
  71. ->timeout(30)
  72. ->request();
  73. self::assertEquals('Iphone', $result['data'][0]['word']);
  74. }
  75. /**
  76. * @throws ClientException
  77. * @throws ServerException
  78. */
  79. public function testSearchImage()
  80. {
  81. AlibabaCloud::accessKeyClient(
  82. \getenv('ACCESS_KEY_ID'),
  83. \getenv('ACCESS_KEY_SECRET')
  84. )->regionId('cn-shanghai')->name('im');
  85. $request = AlibabaCloud::roa()
  86. ->connectTimeout(40)
  87. ->timeout(50)
  88. ->client('im')
  89. ->product('ImageSearch')
  90. ->version('2019-03-25')
  91. ->method('POST')
  92. ->action('SearchImage')
  93. ->pathPattern('/v2/image/search')
  94. ->accept('application/json')
  95. ->contentType('application/x-www-form-urlencoded; charset=UTF-8');
  96. $content = file_get_contents(__DIR__ . '/ImageSearch.jpg');
  97. try {
  98. $result = $request->options([
  99. 'form_params' => [
  100. 'InstanceName' => getenv('IMAGE_SEARCH_INSTANCE_NAME'),
  101. 'PicContent' => base64_encode($content),
  102. 'Start' => 0,
  103. 'Num' => 10
  104. ]
  105. ])
  106. ->request();
  107. self::assertArrayHasKey('Actions', $result);
  108. } catch (ServerException $e) {
  109. self::assertEquals(400, $e->getCode());
  110. }
  111. }
  112. }