RpcTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace AlibabaCloud\Tea\Rpc\Tests;
  3. use AlibabaCloud\Tea\Exception\TeaError;
  4. use AlibabaCloud\Tea\Rpc\Rpc;
  5. use AlibabaCloud\Tea\Tea;
  6. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  7. use GuzzleHttp\Handler\MockHandler;
  8. use GuzzleHttp\HandlerStack;
  9. use GuzzleHttp\Psr7\Response;
  10. use PHPUnit\Framework\TestCase;
  11. /**
  12. * @internal
  13. * @coversNothing
  14. */
  15. class RpcTest extends TestCase
  16. {
  17. public function testDoRequestWithUnsetException()
  18. {
  19. // throws an exception when `config` is null
  20. $this->expectException(TeaError::class);
  21. $this->expectExceptionMessage("'config' can not be unset");
  22. new Rpc(null);
  23. }
  24. public function testDoRequestWithParameterUnsetException()
  25. {
  26. // Throws an exception when the value of accessKeyId|accessKeySecret|credential is not set.
  27. $this->expectException(TeaError::class);
  28. $this->expectExceptionMessage("'accessKeyId' and 'accessKeySecret' or 'credential' can not be unset");
  29. new Rpc(new Rpc\Config([]));
  30. }
  31. public function testDoRequestWithInvalidRegionID()
  32. {
  33. $config = new Rpc\Config([
  34. 'accessKeyId' => 'accesskey_id',
  35. 'accessKeySecret' => 'accesskey_secret',
  36. 'type' => 'access_key',
  37. 'regionId' => '域名',
  38. ]);
  39. $this->expectException(\InvalidArgumentException::class);
  40. $this->expectExceptionMessage('regionId is not match ^[a-zA-Z0-9_-]+$');
  41. new Rpc($config);
  42. }
  43. public function testDoRequest()
  44. {
  45. $config = new Rpc\Config([
  46. 'accessKeyId' => 'accesskey_id',
  47. 'accessKeySecret' => 'accesskey_secret',
  48. 'type' => 'access_key',
  49. 'regionId' => 'cn-hangzhou',
  50. ]);
  51. $mock = new MockHandler([
  52. new Response(200, ['X-Foo' => 'Bar'], json_encode([
  53. 'foo' => 'bar',
  54. ])),
  55. ]);
  56. $handlerStack = HandlerStack::create($mock);
  57. Tea::config(['handler' => $handlerStack]);
  58. $rpc = new Rpc($config);
  59. /** @var Response $res */
  60. $res = $rpc->doRequest(
  61. 'testApi',
  62. 'HTTP',
  63. 'GET',
  64. '2019-12-12',
  65. 'AK',
  66. null,
  67. null,
  68. new RuntimeOptions()
  69. );
  70. $this->assertEquals(['foo' => 'bar'], $res);
  71. }
  72. public function testDefaultAny()
  73. {
  74. $this->assertEquals('foo', Rpc::defaultAny('foo', 'bar'));
  75. $this->assertEquals('bar', Rpc::defaultAny(null, 'bar'));
  76. }
  77. }