BucketTest.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace Qiniu\Tests;
  3. use Qiniu\Storage\BucketManager;
  4. class BucketTest extends \PHPUnit_Framework_TestCase
  5. {
  6. protected $bucketManager;
  7. protected $dummyBucketManager;
  8. protected $bucketName;
  9. protected $key;
  10. protected $key2;
  11. protected function setUp()
  12. {
  13. global $bucketName;
  14. global $key;
  15. global $key2;
  16. $this->bucketName = $bucketName;
  17. $this->key = $key;
  18. $this->key2 = $key2;
  19. global $testAuth;
  20. $this->bucketManager = new BucketManager($testAuth);
  21. global $dummyAuth;
  22. $this->dummyBucketManager = new BucketManager($dummyAuth);
  23. }
  24. public function testBuckets()
  25. {
  26. list($list, $error) = $this->bucketManager->buckets();
  27. $this->assertTrue(in_array($this->bucketName, $list));
  28. $this->assertNull($error);
  29. list($list2, $error) = $this->dummyBucketManager->buckets();
  30. $this->assertEquals(401, $error->code());
  31. $this->assertNull($list2);
  32. $this->assertNotNull($error->message());
  33. }
  34. public function testList()
  35. {
  36. list($ret, $error) = $this->bucketManager->listFiles($this->bucketName, null, null, 10);
  37. $this->assertNotNull($ret['items'][0]);
  38. $this->assertNotNull($ret['marker']);
  39. }
  40. public function testStat()
  41. {
  42. list($stat, $error) = $this->bucketManager->stat($this->bucketName, $this->key);
  43. $this->assertNotNull($stat);
  44. $this->assertNull($error);
  45. $this->assertNotNull($stat['hash']);
  46. list($stat, $error) = $this->bucketManager->stat($this->bucketName, 'nofile');
  47. $this->assertNull($stat);
  48. $this->assertEquals(612, $error->code());
  49. $this->assertNotNull($error->message());
  50. list($stat, $error) = $this->bucketManager->stat('nobucket', 'nofile');
  51. $this->assertNull($stat);
  52. $this->assertEquals(631, $error->code());
  53. $this->assertNotNull($error->message());
  54. }
  55. public function testDelete()
  56. {
  57. $error = $this->bucketManager->delete($this->bucketName, 'del');
  58. $this->assertEquals(612, $error->code());
  59. }
  60. public function testRename()
  61. {
  62. $key = 'renamefrom' . rand();
  63. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  64. $key2 = 'renameto' . $key;
  65. $error = $this->bucketManager->rename($this->bucketName, $key, $key2);
  66. $this->assertNull($error);
  67. $error = $this->bucketManager->delete($this->bucketName, $key2);
  68. $this->assertNull($error);
  69. }
  70. public function testCopy()
  71. {
  72. $key = 'copyto' . rand();
  73. $this->bucketManager->delete($this->bucketName, $key);
  74. $error = $this->bucketManager->copy(
  75. $this->bucketName,
  76. $this->key,
  77. $this->bucketName,
  78. $key
  79. );
  80. $this->assertNull($error);
  81. //test force copy
  82. $error = $this->bucketManager->copy(
  83. $this->bucketName,
  84. $this->key2,
  85. $this->bucketName,
  86. $key,
  87. true
  88. );
  89. $this->assertNull($error);
  90. list($key2Stat,) = $this->bucketManager->stat($this->bucketName, $this->key2);
  91. list($key2CopiedStat,) = $this->bucketManager->stat($this->bucketName, $key);
  92. $this->assertEquals($key2Stat['hash'], $key2CopiedStat['hash']);
  93. $error = $this->bucketManager->delete($this->bucketName, $key);
  94. $this->assertNull($error);
  95. }
  96. public function testChangeMime()
  97. {
  98. $error = $this->bucketManager->changeMime(
  99. $this->bucketName,
  100. 'php-sdk.html',
  101. 'text/html'
  102. );
  103. $this->assertNull($error);
  104. }
  105. public function testPrefetch()
  106. {
  107. $error = $this->bucketManager->prefetch(
  108. $this->bucketName,
  109. 'php-sdk.html'
  110. );
  111. $this->assertNull($error);
  112. }
  113. public function testFetch()
  114. {
  115. list($ret, $error) = $this->bucketManager->fetch(
  116. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  117. $this->bucketName,
  118. 'fetch.html'
  119. );
  120. $this->assertArrayHasKey('hash', $ret);
  121. $this->assertNull($error);
  122. list($ret, $error) = $this->bucketManager->fetch(
  123. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  124. $this->bucketName,
  125. ''
  126. );
  127. $this->assertArrayHasKey('key', $ret);
  128. $this->assertNull($error);
  129. list($ret, $error) = $this->bucketManager->fetch(
  130. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  131. $this->bucketName
  132. );
  133. $this->assertArrayHasKey('key', $ret);
  134. $this->assertNull($error);
  135. }
  136. public function testBatchCopy()
  137. {
  138. $key = 'copyto' . rand();
  139. $ops = BucketManager::buildBatchCopy(
  140. $this->bucketName,
  141. array($this->key => $key),
  142. $this->bucketName,
  143. true
  144. );
  145. list($ret, $error) = $this->bucketManager->batch($ops);
  146. $this->assertEquals(200, $ret[0]['code']);
  147. $ops = BucketManager::buildBatchDelete($this->bucketName, array($key));
  148. list($ret, $error) = $this->bucketManager->batch($ops);
  149. $this->assertEquals(200, $ret[0]['code']);
  150. }
  151. public function testBatchMove()
  152. {
  153. $key = 'movefrom' . rand();
  154. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  155. $key2 = $key . 'to';
  156. $ops = BucketManager::buildBatchMove(
  157. $this->bucketName,
  158. array($key => $key2),
  159. $this->bucketName,
  160. true
  161. );
  162. list($ret, $error) = $this->bucketManager->batch($ops);
  163. $this->assertEquals(200, $ret[0]['code']);
  164. $error = $this->bucketManager->delete($this->bucketName, $key2);
  165. $this->assertNull($error);
  166. }
  167. public function testBatchRename()
  168. {
  169. $key = 'rename' . rand();
  170. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  171. $key2 = $key . 'to';
  172. $ops = BucketManager::buildBatchRename($this->bucketName, array($key => $key2), true);
  173. list($ret, $error) = $this->bucketManager->batch($ops);
  174. $this->assertEquals(200, $ret[0]['code']);
  175. $error = $this->bucketManager->delete($this->bucketName, $key2);
  176. $this->assertNull($error);
  177. }
  178. public function testBatchStat()
  179. {
  180. $ops = BucketManager::buildBatchStat($this->bucketName, array('php-sdk.html'));
  181. list($ret, $error) = $this->bucketManager->batch($ops);
  182. $this->assertEquals(200, $ret[0]['code']);
  183. }
  184. public function testDeleteAfterDays()
  185. {
  186. $key = rand();
  187. $err = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  188. $this->assertEquals(612, $err->code());
  189. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  190. $err = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  191. $this->assertEquals(null, $err);
  192. }
  193. }