OssClientTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. class OssClientTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testConstrunct()
  8. {
  9. try {
  10. $ossClient = new OssClient('id', 'key', 'http://oss-cn-hangzhou.aliyuncs.com');
  11. $this->assertFalse($ossClient->isUseSSL());
  12. $ossClient->setUseSSL(true);
  13. $this->assertTrue($ossClient->isUseSSL());
  14. $this->assertTrue(true);
  15. $this->assertEquals(3, $ossClient->getMaxRetries());
  16. $ossClient->setMaxTries(4);
  17. $this->assertEquals(4, $ossClient->getMaxRetries());
  18. $ossClient->setTimeout(10);
  19. $ossClient->setConnectTimeout(20);
  20. } catch (OssException $e) {
  21. assertFalse(true);
  22. }
  23. }
  24. public function testConstrunct2()
  25. {
  26. try {
  27. $ossClient = new OssClient('id', "", 'http://oss-cn-hangzhou.aliyuncs.com');
  28. $this->assertFalse(true);
  29. } catch (OssException $e) {
  30. $this->assertEquals("access key secret is empty", $e->getMessage());
  31. }
  32. }
  33. public function testConstrunct3()
  34. {
  35. try {
  36. $ossClient = new OssClient("", 'key', 'http://oss-cn-hangzhou.aliyuncs.com');
  37. $this->assertFalse(true);
  38. } catch (OssException $e) {
  39. $this->assertEquals("access key id is empty", $e->getMessage());
  40. }
  41. }
  42. public function testConstrunct4()
  43. {
  44. try {
  45. $ossClient = new OssClient('id', 'key', "");
  46. $this->assertFalse(true);
  47. } catch (OssException $e) {
  48. $this->assertEquals('endpoint is empty', $e->getMessage());
  49. }
  50. }
  51. public function testConstrunct5()
  52. {
  53. try {
  54. $ossClient = new OssClient('id', 'key', "123.123.123.1");
  55. } catch (OssException $e) {
  56. $this->assertTrue(false);
  57. }
  58. }
  59. public function testConstrunct6()
  60. {
  61. try {
  62. $ossClient = new OssClient('id', 'key', "https://123.123.123.1");
  63. $this->assertTrue($ossClient->isUseSSL());
  64. } catch (OssException $e) {
  65. $this->assertTrue(false);
  66. }
  67. }
  68. public function testConstrunct7()
  69. {
  70. try {
  71. $ossClient = new OssClient('id', 'key', "http://123.123.123.1");
  72. $this->assertFalse($ossClient->isUseSSL());
  73. } catch (OssException $e) {
  74. $this->assertTrue(false);
  75. }
  76. }
  77. public function testConstrunct8()
  78. {
  79. try {
  80. $ossClient = new OssClient('id', 'key', "http://123.123.123.1", true);
  81. $ossClient->listBuckets();
  82. $this->assertFalse(true);
  83. } catch (OssException $e) {
  84. }
  85. }
  86. public function testConstrunct9()
  87. {
  88. try {
  89. $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
  90. $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
  91. $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
  92. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
  93. $ossClient->listBuckets();
  94. } catch (OssException $e) {
  95. $this->assertFalse(true);
  96. }
  97. }
  98. public function testSupportPutEmptyObject()
  99. {
  100. try {
  101. $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
  102. $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
  103. $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
  104. $bucket = getenv('OSS_BUCKET');
  105. $ossClient = new OssClient($accessKeyId, $accessKeySecret , $endpoint, false);
  106. $ossClient->putObject($bucket,'test_emptybody','');
  107. } catch (OssException $e) {
  108. $this->assertFalse(true);
  109. }
  110. }
  111. public function testCreateObjectDir()
  112. {
  113. try {
  114. $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
  115. $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
  116. $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
  117. $bucket = getenv('OSS_BUCKET');
  118. $object='test-dir';
  119. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
  120. $ossClient->createObjectDir($bucket,$object);
  121. } catch (OssException $e) {
  122. $this->assertFalse(true);
  123. }
  124. }
  125. public function testGetBucketCors()
  126. {
  127. try {
  128. $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
  129. $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
  130. $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
  131. $bucket = getenv('OSS_BUCKET');
  132. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
  133. $ossClient->getBucketCors($bucket);
  134. } catch (OssException $e) {
  135. $this->assertFalse(true);
  136. }
  137. }
  138. public function testGetBucketCname()
  139. {
  140. try {
  141. $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
  142. $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
  143. $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
  144. $bucket = getenv('OSS_BUCKET');
  145. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
  146. $ossClient->getBucketCname($bucket);
  147. } catch (OssException $e) {
  148. $this->assertFalse(true);
  149. }
  150. }
  151. public function testProxySupport()
  152. {
  153. $accessKeyId = ' ' . getenv('OSS_ACCESS_KEY_ID') . ' ';
  154. $accessKeySecret = ' ' . getenv('OSS_ACCESS_KEY_SECRET') . ' ';
  155. $endpoint = ' ' . getenv('OSS_ENDPOINT') . '/ ';
  156. $bucket = getenv('OSS_BUCKET') . '-proxy';
  157. $requestProxy = getenv('OSS_PROXY');
  158. $key = 'test-proxy-srv-object';
  159. $content = 'test-content';
  160. $proxys = parse_url($requestProxy);
  161. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, null, $requestProxy);
  162. $result = $ossClient->createBucket($bucket);
  163. $this->checkProxy($result, $proxys);
  164. $result = $ossClient->putObject($bucket, $key, $content);
  165. $this->checkProxy($result, $proxys);
  166. $result = $ossClient->getObject($bucket, $key);
  167. $this->assertEquals($content, $result);
  168. // list object
  169. $objectListInfo = $ossClient->listObjects($bucket);
  170. $objectList = $objectListInfo->getObjectList();
  171. $this->assertNotNull($objectList);
  172. $this->assertTrue(is_array($objectList));
  173. $objects = array();
  174. foreach ($objectList as $value) {
  175. $objects[] = $value->getKey();
  176. }
  177. $this->assertEquals(1, count($objects));
  178. $this->assertTrue(in_array($key, $objects));
  179. $result = $ossClient->deleteObject($bucket, $key);
  180. $this->checkProxy($result,$proxys);
  181. $result = $ossClient->deleteBucket($bucket);
  182. $this->checkProxy($result, $proxys);
  183. }
  184. private function checkProxy($result, $proxys)
  185. {
  186. $this->assertEquals($result['info']['primary_ip'], $proxys['host']);
  187. $this->assertEquals($result['info']['primary_port'], $proxys['port']);
  188. $this->assertTrue(array_key_exists('via', $result));
  189. }
  190. }