ChainProviderRequestTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request;
  3. use AlibabaCloud\Client\SDK;
  4. use PHPUnit\Framework\TestCase;
  5. use AlibabaCloud\Client\AlibabaCloud;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. use AlibabaCloud\Client\Exception\ServerException;
  8. use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
  9. use AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Credentials\Ini\VirtualAccessKeyCredential;
  10. /**
  11. * Class ChainRequestTest
  12. *
  13. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request
  14. */
  15. class ChainProviderRequestTest extends TestCase
  16. {
  17. /**
  18. * @throws ClientException
  19. * @throws ServerException
  20. */
  21. public function testDefaultProviderOnInstance()
  22. {
  23. // Setup
  24. CredentialsProvider::chain(
  25. CredentialsProvider::ini(),
  26. CredentialsProvider::instance(),
  27. CredentialsProvider::env()
  28. );
  29. $role = 'EcsRamRoleTest';
  30. putenv("ALIBABA_CLOUD_ECS_METADATA=$role");
  31. // Void ini
  32. $content = <<<EOT
  33. [void_client]
  34. enable = true
  35. type = access_key
  36. access_key_id = foo
  37. access_key_secret = var
  38. region_id = cn-hangzhou
  39. EOT;
  40. $file = (new VirtualAccessKeyCredential($content, 'testDefaultProviderOnInstance'))->url();
  41. putenv("ALIBABA_CLOUD_CREDENTIALS_FILE=$file");
  42. AlibabaCloud::flush();
  43. // Test
  44. AlibabaCloud::setDefaultRegionId('cn-hangzhou');
  45. try {
  46. AlibabaCloud::rpc()
  47. ->method('POST')
  48. ->product('Cdn')
  49. ->version('2014-11-11')
  50. ->action('DescribeCdnService')
  51. ->connectTimeout(25)
  52. ->timeout(30)
  53. ->debug(true)
  54. ->request();
  55. } catch (ClientException $e) {
  56. self::assertEquals(SDK::SERVER_UNREACHABLE, $e->getErrorCode());
  57. }
  58. }
  59. /**
  60. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  61. * @expectedExceptionMessageRegExp /Forbidden.RAM: User not authorized to operate on the specified resource, or this API doesn't support RAM/
  62. * @throws ClientException
  63. * @throws ServerException
  64. */
  65. public function testDefaultProviderOnEnv()
  66. {
  67. // Setup
  68. $id = \AlibabaCloud\Client\env('ACCESS_KEY_ID');
  69. $secret = \AlibabaCloud\Client\env('ACCESS_KEY_SECRET');
  70. putenv("ALIBABA_CLOUD_ACCESS_KEY_ID=$id");
  71. putenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET=$secret");
  72. // Test
  73. AlibabaCloud::setDefaultRegionId('cn-hangzhou');
  74. $result = AlibabaCloud::rpc()
  75. ->method('POST')
  76. ->product('Cdn')
  77. ->version('2014-11-11')
  78. ->action('DescribeCdnService')
  79. ->connectTimeout(25)
  80. ->timeout(30)
  81. ->request();
  82. // self::assertNotEmpty('PayByTraffic', $result['ChangingChargeType']);
  83. }
  84. /**
  85. * @depends testDefaultProviderOnEnv
  86. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  87. * @expectedExceptionMessageRegExp /Forbidden.RAM: User not authorized to operate on the specified resource, or this API doesn't support RAM/
  88. * @throws ServerException
  89. * @throws ClientException
  90. */
  91. public function testDefaultProviderOnIni()
  92. {
  93. // Setup
  94. $id = \AlibabaCloud\Client\env('ACCESS_KEY_ID');
  95. $secret = \AlibabaCloud\Client\env('ACCESS_KEY_SECRET');
  96. $content = <<<EOT
  97. [testDefaultProviderOnIni]
  98. enable = true
  99. type = access_key
  100. access_key_id = $id
  101. access_key_secret = $secret
  102. region_id = cn-hangzhou
  103. EOT;
  104. $file = (new VirtualAccessKeyCredential($content, 'testDefaultProviderOnIni'))->url();
  105. putenv("ALIBABA_CLOUD_CREDENTIALS_FILE=$file");
  106. // Test
  107. AlibabaCloud::setDefaultRegionId('cn-hangzhou');
  108. $result = AlibabaCloud::rpc()
  109. ->client('testDefaultProviderOnIni')
  110. ->method('POST')
  111. ->product('Cdn')
  112. ->version('2014-11-11')
  113. ->action('DescribeCdnService')
  114. ->connectTimeout(25)
  115. ->timeout(30)
  116. ->request();
  117. // self::assertNotEmpty('PayByTraffic', $result['ChangingChargeType']);
  118. }
  119. protected function setUp()
  120. {
  121. AlibabaCloud::flush();
  122. CredentialsProvider::flush();
  123. }
  124. }