EndpointTraitTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Traits;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\Client\Regions\LocationService;
  7. use AlibabaCloud\Client\Request\RpcRequest;
  8. use PHPUnit\Framework\TestCase;
  9. /**
  10. * Class EndpointTraitTest
  11. *
  12. * @package AlibabaCloud\Client\Tests\Unit\Traits
  13. */
  14. class EndpointTraitTest extends TestCase
  15. {
  16. /**
  17. * @throws ClientException
  18. */
  19. public function testFindProductDomain()
  20. {
  21. static::assertEquals(
  22. 'ecs-cn-hangzhou.aliyuncs.com',
  23. AlibabaCloud::resolveHost('Ecs', 'cn-hangzhou')
  24. );
  25. static::assertEquals(
  26. 'kms.me-east-1.aliyuncs.com',
  27. AlibabaCloud::resolveHost('kms', 'me-east-1')
  28. );
  29. }
  30. /**
  31. * Test for AddEndpoint
  32. *
  33. * @throws ClientException
  34. */
  35. public function testAddHost()
  36. {
  37. // Setup
  38. $regionId = 'cn-hangzhou';
  39. $product = 'TestProduct';
  40. $host = 'testproduct.aliyuncs.com';
  41. // Test
  42. AlibabaCloud::addHost($product, $host, $regionId);
  43. // Assert
  44. self::assertEquals($host, AlibabaCloud::resolveHost($product, $regionId));
  45. }
  46. /**
  47. * @throws ClientException
  48. */
  49. public function testResolveHostByStatic()
  50. {
  51. $host = 'host.com';
  52. AlibabaCloud::addHost('product', $host, 'cn-hangzhou');
  53. $expected = AlibabaCloud::resolveHostByStatic('product', 'cn-hangzhou');
  54. self::assertEquals($host, $expected);
  55. }
  56. /**
  57. * Test for AddEndpoint
  58. *
  59. * @throws ClientException
  60. */
  61. public function testAddHostWithProductEmpty()
  62. {
  63. $this->expectException(ClientException::class);
  64. $this->expectExceptionMessage("Product cannot be empty");
  65. AlibabaCloud::addHost('', 'host', 'regionId');
  66. }
  67. /**
  68. * Test for AddEndpoint
  69. *
  70. * @throws ClientException
  71. */
  72. public function testAddHostWithProductFormat()
  73. {
  74. $this->expectException(ClientException::class);
  75. $this->expectExceptionMessage("Product must be a string");
  76. AlibabaCloud::addHost(null, 'host', 'regionId');
  77. }
  78. /**
  79. * Test for AddEndpoint
  80. *
  81. * @throws ClientException
  82. */
  83. public function testAddHostWithHostEmpty()
  84. {
  85. $this->expectException(ClientException::class);
  86. $this->expectExceptionMessage("Host cannot be empty");
  87. AlibabaCloud::addHost('product', '', 'regionId');
  88. }
  89. /**
  90. * Test for AddEndpoint
  91. *
  92. * @throws ClientException
  93. */
  94. public function testAddHostWithHostFormat()
  95. {
  96. $this->expectException(ClientException::class);
  97. $this->expectExceptionMessage("Host must be a string");
  98. AlibabaCloud::addHost('product', null, 'regionId');
  99. }
  100. /**
  101. * Test for AddEndpoint
  102. *
  103. * @throws ClientException
  104. */
  105. public function testAddHostWithRegionIdFormat()
  106. {
  107. $this->expectException(ClientException::class);
  108. $this->expectExceptionMessage("Region ID must be a string");
  109. AlibabaCloud::addHost('product', 'host', null);
  110. }
  111. /**
  112. * Test for AddEndpoint
  113. *
  114. * @throws ClientException
  115. */
  116. public function testAddHostWithRegionIdEmpty()
  117. {
  118. $this->expectException(ClientException::class);
  119. $this->expectExceptionMessage("Region ID cannot be empty");
  120. AlibabaCloud::addHost('product', 'host', '');
  121. }
  122. /**
  123. * @throws ClientException
  124. */
  125. public function testLocationServiceResolveHostWithException()
  126. {
  127. $this->expectException(ServerException::class);
  128. $reg = '/Please check the parameters RequestId:/';
  129. if (method_exists($this, 'expectExceptionMessageMatches')) {
  130. $this->expectExceptionMessageMatches($reg);
  131. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  132. $this->expectExceptionMessageRegExp($reg);
  133. }
  134. // Setup
  135. $accessKeyId = \getenv('ACCESS_KEY_ID');
  136. $accessKeySecret = \getenv('ACCESS_KEY_SECRET');
  137. AlibabaCloud::mockResponse(
  138. 400,
  139. [],
  140. [
  141. 'Message' => 'Please check the parameters',
  142. ]
  143. );
  144. AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
  145. ->regionId('cn-hangzhou')
  146. ->asDefaultClient();
  147. // Test
  148. $request = new RpcRequest();
  149. $request->connectTimeout(25)->timeout(30);
  150. $request->product = 'Dysmsapi';
  151. $request->serviceCode = 'dysmsapi';
  152. // Assert
  153. LocationService::resolveHost($request);
  154. }
  155. /**
  156. * @throws ClientException
  157. */
  158. public function testAddGlobalHost()
  159. {
  160. // Setup
  161. $product = 'a';
  162. $host = 'a.com';
  163. // Test
  164. AlibabaCloud::addHost($product, $host);
  165. // Assert
  166. self::assertEquals($host, AlibabaCloud::resolveHost($product));
  167. }
  168. /**
  169. * @throws ClientException
  170. */
  171. public function testGlobal()
  172. {
  173. // Assert
  174. self::assertEquals('', AlibabaCloud::resolveHost('dysmsapi'));
  175. self::assertEquals('dysmsapi.aliyuncs.com', AlibabaCloud::resolveHost('dysmsapi', 'cn-hangzhou'));
  176. }
  177. public function testIsGlobalProduct()
  178. {
  179. self::assertTrue(AlibabaCloud::isGlobalProduct('ccc'));
  180. self::assertFalse(AlibabaCloud::isGlobalProduct('no'));
  181. self::assertTrue(AlibabaCloud::isGlobalProduct('Ram'));
  182. self::assertTrue(AlibabaCloud::isGlobalProduct('ram'));
  183. self::assertFalse(AlibabaCloud::isGlobalProduct('tdsr'));
  184. self::assertFalse(AlibabaCloud::isGlobalProduct(''));
  185. self::assertFalse(AlibabaCloud::isGlobalProduct(null));
  186. }
  187. /**
  188. * Test for Null
  189. *
  190. * @throws ClientException
  191. */
  192. public function testNull()
  193. {
  194. // Setup
  195. $regionId = 'cn-hangzhou';
  196. $product = 'null';
  197. // Test
  198. self::assertEquals('', AlibabaCloud::resolveHost($product, $regionId));
  199. }
  200. }