Test.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\common\services\qcloud\Tests;
  3. require('include.php');
  4. use app\common\services\qcloud\Api;
  5. class Test extends \PHPUnit_Framework_TestCase {
  6. private $cosClient;
  7. private $cosApi;
  8. private $bucket;
  9. private $cospath;
  10. private $localpath;
  11. protected function setUp() {
  12. $config = array(
  13. 'app_id' => '1252448703',
  14. 'secret_id' => getenv('COS_KEY'),
  15. 'secret_key' => getenv('COS_SECRET'),
  16. 'region' => getenv('COS_REGION_V4'), // bucket所属地域:华北 'tj' 华东 'sh' 华南 'gz'
  17. 'timeout' => 60
  18. );
  19. $this->cosApi = new Api($config);
  20. $this->bucket = 'testbucketv4'. $config['region'];
  21. $this->cospath = '→↓←→↖↗↙↘! \"#$%&\'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
  22. $this->localpath = "111";
  23. $this->folder = "新建folder";
  24. $file = $this->localpath;
  25. file_put_contents($file,"12345",FILE_APPEND);
  26. }
  27. protected function tearDown() {
  28. unlink ($this->localpath);
  29. }
  30. public function testUploadFile() {
  31. try {
  32. $rt = $this->cosApi->upload($this->bucket, $this->localpath, $this->cospath);
  33. $this->cosApi->delFile($this->bucket, $this->cospath);
  34. $this->assertEquals(0, $rt['code']);
  35. } catch (\Exception $e) {
  36. $this->assertFalse(true, $e);
  37. }
  38. }
  39. public function testStatFile() {
  40. try {
  41. $file = $this->localpath;
  42. file_put_contents($file,"12345",FILE_APPEND);
  43. $this->cosApi->upload($this->bucket, $this->localpath, $this->cospath);
  44. $rt = $this->cosApi->stat($this->bucket, $this->cospath);
  45. $this->cosApi->delFile($this->bucket, $this->cospath);
  46. $this->assertEquals(0, $rt['code']);
  47. $this->cosApi->delFile($this->bucket, $this->cospath);
  48. } catch (\Exception $e) {
  49. $this->assertFalse(true, $e);
  50. }
  51. }
  52. public function testDownloadFile() {
  53. try {
  54. $this->cosApi->upload($this->bucket, $this->localpath, $this->cospath);
  55. $rt = $this->cosApi->download($this->bucket, $this->cospath, $this->localpath);
  56. $this->cosApi->delFile($this->bucket, $this->cospath);
  57. $this->assertEquals(0, $rt['code']);
  58. } catch (\Exception $e) {
  59. $this->assertFalse(true, $e);
  60. }
  61. }
  62. public function testUpdateFileInfo() {
  63. try {
  64. $this->cosApi->upload($this->bucket, $this->localpath, $this->cospath);
  65. $bizAttr = '';
  66. $authority = 'eWPrivateRPublic';
  67. $customerHeaders = array(
  68. 'Cache-Control' => 'no',
  69. 'Content-Type' => 'application/pdf',
  70. 'Content-Language' => 'ch',
  71. );
  72. $rt = $this->cosApi->update($this->bucket, $this->cospath, $bizAttr, $authority, $customerHeaders);
  73. $this->cosApi->delFile($this->bucket, $this->cospath);
  74. $this->assertEquals(0, $rt['code']);
  75. } catch (\Exception $e) {
  76. $this->assertFalse(true, $e);
  77. }
  78. }
  79. public function testCopyFile() {
  80. try {
  81. $this->cosApi->upload($this->bucket, $this->localpath, $this->cospath);
  82. $rt = $this->cosApi->copyFile($this->bucket, $this->cospath, $this->cospath . '_copy');
  83. $this->cosApi->delFile($this->bucket, $this->cospath);
  84. $this->cosApi->delFile($this->bucket, $this->cospath . '_copy');
  85. $this->assertEquals(0, $rt['code']);
  86. } catch (\Exception $e) {
  87. $this->assertFalse(true, $e);
  88. }
  89. }
  90. public function testMoveFile() {
  91. try {
  92. $this->cosApi->upload($this->bucket, $this->localpath, $this->cospath);
  93. $rt = $this->cosApi->moveFile($this->bucket, $this->cospath, $this->cospath . '_move');
  94. $this->cosApi->delFile($this->bucket, $this->cospath);
  95. $this->cosApi->delFile($this->bucket, $this->cospath . '_move');
  96. $this->assertEquals(0, $rt['code']);
  97. } catch (\Exception $e) {
  98. $this->assertFalse(true, $e);
  99. }
  100. }
  101. public function testDeleteFile() {
  102. try {
  103. $this->cosApi->upload($this->bucket, $this->localpath, $this->cospath);
  104. $rt = $this->cosApi->delFile($this->bucket, $this->cospath);
  105. $this->assertEquals(0, $rt['code']);
  106. } catch (\Exception $e) {
  107. $this->assertFalse(true, $e);
  108. }
  109. }
  110. public function testCreateFolder() {
  111. try {
  112. $rt = $this->cosApi->createFolder($this->bucket, $this->folder);
  113. $rt = $this->cosApi->delFolder($this->bucket, $this->folder);
  114. $this->assertEquals(0, $rt['code']);
  115. } catch (\Exception $e) {
  116. $this->assertFalse(true, $e);
  117. }
  118. }
  119. public function testListFolder() {
  120. try {
  121. $this->cosApi->createFolder($this->bucket, $this->folder);
  122. $rt = $this->cosApi->listFolder($this->bucket, $this->folder);
  123. $rt = $this->cosApi->delFolder($this->bucket, $this->folder);
  124. $this->assertEquals(0, $rt['code']);
  125. } catch (\Exception $e) {
  126. $this->assertFalse(true, $e);
  127. }
  128. }
  129. public function testUpdateFolderInfo() {
  130. try {
  131. $bizAttr = "";
  132. $this->cosApi->createFolder($this->bucket, $this->folder);
  133. $rt = $this->cosApi->updateFolder($this->bucket, $this->folder, $bizAttr);
  134. $rt = $this->cosApi->delFolder($this->bucket, $this->folder);
  135. $this->assertEquals(0, $rt['code']);
  136. } catch (\Exception $e) {
  137. $this->assertFalse(true, $e);
  138. }
  139. }
  140. public function testStatFolder() {
  141. try {
  142. $this->cosApi->createFolder($this->bucket, $this->folder);
  143. $rt = $this->cosApi->statFolder($this->bucket, $this->folder);
  144. $rt = $this->cosApi->delFolder($this->bucket, $this->folder);
  145. $this->assertEquals(0, $rt['code']);
  146. } catch (\Exception $e) {
  147. $this->assertFalse(true, $e);
  148. }
  149. }
  150. public function testDelteFolder() {
  151. try {
  152. $this->cosApi->createFolder($this->bucket, $this->folder);
  153. $rt = $this->cosApi->delFolder($this->bucket, $this->folder);
  154. $this->assertEquals(0, $rt['code']);
  155. } catch (\Exception $e) {
  156. $this->assertFalse(true, $e);
  157. }
  158. }
  159. }