ChainProviderRequestTest.php 3.9 KB

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