ClientTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Clients;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Clients\Client;
  6. use AlibabaCloud\Client\Exception\ServerException;
  7. use AlibabaCloud\Client\Exception\ClientException;
  8. use AlibabaCloud\Client\Credentials\AccessKeyCredential;
  9. use AlibabaCloud\Client\Signature\ShaHmac256WithRsaSignature;
  10. use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
  11. /**
  12. * Class ClientTest
  13. *
  14. * @package AlibabaCloud\Client\Tests\Unit\Clients
  15. */
  16. class ClientTest extends TestCase
  17. {
  18. /**
  19. * @return Client
  20. * @throws ClientException
  21. */
  22. public function testConstruct()
  23. {
  24. // Setup
  25. $accessKeyId = 'foo';
  26. $accessKeySecret = 'bar';
  27. $credential = new AccessKeyCredential($accessKeyId, $accessKeySecret);
  28. $signature = new ShaHmac256WithRsaSignature();
  29. // Test
  30. $client = new Client($credential, $signature);
  31. // Assert
  32. self::assertEquals($signature, $client->getSignature());
  33. self::assertEquals($credential, $client->getCredential());
  34. self::assertEquals($accessKeyId, $client->getCredential()->getAccessKeyId());
  35. self::assertEquals($accessKeySecret, $client->getCredential()->getAccessKeySecret());
  36. return $client;
  37. }
  38. /**
  39. * @depends testConstruct
  40. *
  41. * @param Client $client
  42. *
  43. * @throws ClientException
  44. */
  45. public function testName(Client $client)
  46. {
  47. // Setup
  48. $name = \uniqid('', true);
  49. // Test
  50. $client->name($name);
  51. // Assert
  52. self::assertEquals($client, AlibabaCloud::get($name));
  53. }
  54. /**
  55. * @depends testConstruct
  56. *
  57. * @param Client $client
  58. *
  59. * @throws ClientException
  60. */
  61. public function testNameEmpty(Client $client)
  62. {
  63. $this->expectException(ClientException::class);
  64. $this->expectExceptionMessage('Name cannot be empty');
  65. $client->name('');
  66. }
  67. /**
  68. * @depends testConstruct
  69. *
  70. * @param Client $client
  71. *
  72. * @throws ClientException
  73. */
  74. public function testNameFormat(Client $client)
  75. {
  76. $this->expectException(ClientException::class);
  77. $this->expectExceptionMessage('Name must be a string');
  78. $client->name(null);
  79. }
  80. /**
  81. * @depends testConstruct
  82. *
  83. * @param Client $client
  84. *
  85. * @throws ClientException
  86. */
  87. public function testAsDefaultClient(Client $client)
  88. {
  89. // Setup
  90. $name = CredentialsProvider::getDefaultName();
  91. // Test
  92. $client->asDefaultClient();
  93. // Assert
  94. self::assertEquals($client, AlibabaCloud::get($name));
  95. }
  96. /**
  97. * @depends testConstruct
  98. *
  99. * @param Client $client
  100. */
  101. public function testIsDebug(Client $client)
  102. {
  103. // Assert
  104. self::assertEquals(false, $client->isDebug());
  105. // Test
  106. $client->debug(true);
  107. // Assert
  108. self::assertEquals(true, $client->isDebug());
  109. }
  110. /**
  111. * @depends testConstruct
  112. *
  113. * @param Client $client
  114. *
  115. * @throws ClientException
  116. * @throws ServerException
  117. */
  118. public function testGetSessionCredential(Client $client)
  119. {
  120. self::assertInstanceOf(AccessKeyCredential::class, $client->getSessionCredential());
  121. }
  122. }