EcsRamRoleCredentialTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Feature\Credentials;
  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\Dds\DescribeRegionsRequest;
  8. /**
  9. * Class EcsRamRoleCredentialTest
  10. *
  11. * @package AlibabaCloud\Client\Tests\Feature\Credentials
  12. */
  13. class EcsRamRoleCredentialTest extends TestCase
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $clientName = 'EcsRamRoleCredentialTest';
  19. /**
  20. * Sets up the fixture, for example, open a network connection.
  21. * This method is called before a test is executed.
  22. *
  23. * @before
  24. * @throws ClientException
  25. */
  26. protected function initialize()
  27. {
  28. parent::setUp();
  29. $regionId = 'cn-hangzhou';
  30. $roleName = 'EcsRamRoleTest';
  31. AlibabaCloud::ecsRamRoleClient($roleName)
  32. ->regionId($regionId)
  33. ->name($this->clientName);
  34. }
  35. /**
  36. * Tears down the fixture, for example, close a network connection.
  37. * This method is called after a test is executed.
  38. *
  39. * @after
  40. * @throws ClientException
  41. */
  42. protected function finalize()
  43. {
  44. parent::tearDown();
  45. AlibabaCloud::del($this->clientName);
  46. }
  47. /**
  48. * @throws ClientException
  49. */
  50. public function testGetSessionCredential()
  51. {
  52. $this->expectException(ClientException::class);
  53. $this->expectExceptionMessage('Timeout or instance does not belong to Alibaba Cloud');
  54. try {
  55. (new DescribeRegionsRequest())->client($this->clientName)
  56. ->connectTimeout(25)
  57. ->timeout(30)
  58. ->request();
  59. } catch (ServerException $e) {
  60. self::assertContains(
  61. $e->getErrorMessage(),
  62. [
  63. 'Error in retrieving assume role credentials.',
  64. 'The role was not found in the instance',
  65. ]
  66. );
  67. }
  68. }
  69. }