BucketTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. namespace Qiniu\Tests;
  3. use Qiniu\Config;
  4. use Qiniu\Storage\BucketManager;
  5. class BucketTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $bucketManager;
  8. protected $dummyBucketManager;
  9. protected $bucketName;
  10. protected $key;
  11. protected $key2;
  12. protected $customCallbackURL;
  13. protected function setUp()
  14. {
  15. global $bucketName;
  16. global $key;
  17. global $key2;
  18. $this->bucketName = $bucketName;
  19. $this->key = $key;
  20. $this->key2 = $key2;
  21. global $customCallbackURL;
  22. $this->customCallbackURL = $customCallbackURL;
  23. global $testAuth;
  24. $config = new Config();
  25. $this->bucketManager = new BucketManager($testAuth, $config);
  26. global $dummyAuth;
  27. $this->dummyBucketManager = new BucketManager($dummyAuth);
  28. }
  29. public function testBuckets()
  30. {
  31. list($list, $error) = $this->bucketManager->buckets();
  32. $this->assertTrue(in_array($this->bucketName, $list));
  33. $this->assertNull($error);
  34. list($list2, $error) = $this->dummyBucketManager->buckets();
  35. $this->assertEquals(401, $error->code());
  36. $this->assertNull($list2);
  37. $this->assertNotNull($error->message());
  38. $this->assertNotNull($error->getResponse());
  39. }
  40. public function testListbuckets()
  41. {
  42. list($ret, $error) = $this->bucketManager->listbuckets('z0');
  43. $this->assertNotNull($ret);
  44. $this->assertNull($error);
  45. }
  46. public function testCreateBucket()
  47. {
  48. list($ret, $error) = $this->bucketManager->createBucket('phpsdk-ci-test');
  49. $this->assertNotNull($ret);
  50. $this->assertNull($error);
  51. }
  52. public function testDeleteBucket()
  53. {
  54. list($ret, $error) = $this->bucketManager->deleteBucket('phpsdk-ci-test');
  55. $this->assertNotNull($ret);
  56. $this->assertNull($error);
  57. }
  58. public function testDomains()
  59. {
  60. list($ret, $error) = $this->bucketManager->domains($this->bucketName);
  61. $this->assertNotNull($ret);
  62. $this->assertNull($error);
  63. }
  64. public function testBucketInfo()
  65. {
  66. list($ret, $error) = $this->bucketManager->bucketInfo($this->bucketName);
  67. $this->assertNotNull($ret);
  68. $this->assertNull($error);
  69. }
  70. public function testBucketInfos()
  71. {
  72. list($ret, $error) = $this->bucketManager->bucketInfos('z0');
  73. $this->assertNotNull($ret);
  74. $this->assertNull($error);
  75. }
  76. public function testList()
  77. {
  78. list($ret, $error) = $this->bucketManager->listFiles($this->bucketName, null, null, 10);
  79. $this->assertNotNull($ret['items'][0]);
  80. $this->assertNotNull($ret['marker']);
  81. $this->assertNull($error);
  82. }
  83. public function testListFilesv2()
  84. {
  85. list($ret, $error) = $this->bucketManager->listFilesv2($this->bucketName, null, null, 10);
  86. $this->assertNotNull($ret);
  87. $this->assertNull($error);
  88. }
  89. public function testBucketLifecycleRule()
  90. {
  91. list($ret, $error) = $this->bucketManager->bucketLifecycleRule($this->bucketName, 'demo', 'test', 80, 70);
  92. $this->assertNotNull($ret);
  93. $this->assertNull($error);
  94. }
  95. public function testGetbucketLifecycleRule()
  96. {
  97. list($ret, $error) = $this->bucketManager->getBucketLifecycleRules($this->bucketName);
  98. $this->assertNotNull($ret);
  99. $this->assertNull($error);
  100. }
  101. public function testUpdatebucketLifecycleRule()
  102. {
  103. list($ret, $error) = $this->bucketManager->updateBucketLifecycleRule(
  104. $this->bucketName,
  105. 'demo',
  106. 'testupdate',
  107. 80,
  108. 70
  109. );
  110. $this->assertNotNull($ret);
  111. $this->assertNull($error);
  112. }
  113. public function testDeleteBucketLifecycleRule()
  114. {
  115. list($ret, $error) = $this->bucketManager->deleteBucketLifecycleRule($this->bucketName, 'demo');
  116. $this->assertNotNull($ret);
  117. $this->assertNull($error);
  118. }
  119. public function testPutBucketEvent()
  120. {
  121. list($ret, $error) = $this->bucketManager->putBucketEvent(
  122. $this->bucketName,
  123. 'bucketevent',
  124. 'test',
  125. 'img',
  126. array('copy'),
  127. $this->customCallbackURL
  128. );
  129. $this->assertNotNull($ret);
  130. $this->assertNull($error);
  131. }
  132. public function testUpdateBucketEvent()
  133. {
  134. list($ret, $error) = $this->bucketManager->updateBucketEvent(
  135. $this->bucketName,
  136. 'bucketevent',
  137. 'test',
  138. 'video',
  139. array('copy'),
  140. $this->customCallbackURL
  141. );
  142. $this->assertNotNull($ret);
  143. $this->assertNull($error);
  144. }
  145. public function testGetBucketEvent()
  146. {
  147. list($ret, $error) = $this->bucketManager->getBucketEvents($this->bucketName);
  148. $this->assertNotNull($ret);
  149. $this->assertNull($error);
  150. }
  151. public function testDeleteBucketEvent()
  152. {
  153. list($ret, $error) = $this->bucketManager->deleteBucketEvent($this->bucketName, 'bucketevent');
  154. $this->assertNotNull($ret);
  155. $this->assertNull($error);
  156. }
  157. public function testStat()
  158. {
  159. list($stat, $error) = $this->bucketManager->stat($this->bucketName, $this->key);
  160. $this->assertNotNull($stat);
  161. $this->assertNull($error);
  162. $this->assertNotNull($stat['hash']);
  163. list($stat, $error) = $this->bucketManager->stat($this->bucketName, 'nofile');
  164. $this->assertNull($stat);
  165. $this->assertEquals(612, $error->code());
  166. $this->assertNotNull($error->message());
  167. list($stat, $error) = $this->bucketManager->stat('nobucket', 'nofile');
  168. $this->assertNull($stat);
  169. $this->assertEquals(631, $error->code());
  170. $this->assertNotNull($error->message());
  171. }
  172. public function testDelete()
  173. {
  174. list($ret, $error) = $this->bucketManager->delete($this->bucketName, 'del');
  175. $this->assertNull($ret);
  176. $this->assertNotNull($error);
  177. }
  178. public function testRename()
  179. {
  180. $key = 'renamefrom' . rand();
  181. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  182. $key2 = 'renameto' . $key;
  183. list($ret, $error) = $this->bucketManager->rename($this->bucketName, $key, $key2);
  184. $this->assertNull($error);
  185. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  186. $this->assertNull($error);
  187. }
  188. public function testCopy()
  189. {
  190. $key = 'copyto' . rand();
  191. $this->bucketManager->delete($this->bucketName, $key);
  192. list($ret, $error) = $this->bucketManager->copy(
  193. $this->bucketName,
  194. $this->key,
  195. $this->bucketName,
  196. $key
  197. );
  198. $this->assertNull($error);
  199. //test force copy
  200. list($ret, $error) = $this->bucketManager->copy(
  201. $this->bucketName,
  202. $this->key2,
  203. $this->bucketName,
  204. $key,
  205. true
  206. );
  207. $this->assertNull($error);
  208. list($key2Stat,) = $this->bucketManager->stat($this->bucketName, $this->key2);
  209. list($key2CopiedStat,) = $this->bucketManager->stat($this->bucketName, $key);
  210. $this->assertEquals($key2Stat['hash'], $key2CopiedStat['hash']);
  211. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key);
  212. $this->assertNull($error);
  213. }
  214. public function testChangeMime()
  215. {
  216. list($ret, $error) = $this->bucketManager->changeMime(
  217. $this->bucketName,
  218. 'php-sdk.html',
  219. 'text/html'
  220. );
  221. $this->assertNull($error);
  222. }
  223. public function testPrefetch()
  224. {
  225. list($ret, $error) = $this->bucketManager->prefetch(
  226. $this->bucketName,
  227. 'php-sdk.html'
  228. );
  229. $this->assertNull($error);
  230. }
  231. public function testFetch()
  232. {
  233. list($ret, $error) = $this->bucketManager->fetch(
  234. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  235. $this->bucketName,
  236. 'fetch.html'
  237. );
  238. $this->assertArrayHasKey('hash', $ret);
  239. $this->assertNull($error);
  240. list($ret, $error) = $this->bucketManager->fetch(
  241. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  242. $this->bucketName,
  243. ''
  244. );
  245. $this->assertArrayHasKey('key', $ret);
  246. $this->assertNull($error);
  247. list($ret, $error) = $this->bucketManager->fetch(
  248. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  249. $this->bucketName
  250. );
  251. $this->assertArrayHasKey('key', $ret);
  252. $this->assertNull($error);
  253. }
  254. public function testAsynchFetch()
  255. {
  256. list($ret, $error) = $this->bucketManager->asynchFetch(
  257. 'http://devtools.qiniu.com/qiniu.png',
  258. $this->bucketName,
  259. null,
  260. 'qiniu.png'
  261. );
  262. $this->assertArrayHasKey('id', $ret);
  263. $this->assertNull($error);
  264. list($ret, $error) = $this->bucketManager->asynchFetch(
  265. 'http://devtools.qiniu.com/qiniu.png',
  266. $this->bucketName,
  267. null,
  268. ''
  269. );
  270. $this->assertArrayHasKey('id', $ret);
  271. $this->assertNull($error);
  272. list($ret, $error) = $this->bucketManager->asynchFetch(
  273. 'http://devtools.qiniu.com/qiniu.png',
  274. $this->bucketName
  275. );
  276. $this->assertArrayHasKey('id', $ret);
  277. $this->assertNull($error);
  278. }
  279. public function testBatchCopy()
  280. {
  281. $key = 'copyto' . rand();
  282. $ops = BucketManager::buildBatchCopy(
  283. $this->bucketName,
  284. array($this->key => $key),
  285. $this->bucketName,
  286. true
  287. );
  288. list($ret, $error) = $this->bucketManager->batch($ops);
  289. $this->assertEquals(200, $ret[0]['code']);
  290. $ops = BucketManager::buildBatchDelete($this->bucketName, array($key));
  291. list($ret, $error) = $this->bucketManager->batch($ops);
  292. $this->assertEquals(200, $ret[0]['code']);
  293. }
  294. public function testBatchMove()
  295. {
  296. $key = 'movefrom' . rand();
  297. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  298. $key2 = $key . 'to';
  299. $ops = BucketManager::buildBatchMove(
  300. $this->bucketName,
  301. array($key => $key2),
  302. $this->bucketName,
  303. true
  304. );
  305. list($ret, $error) = $this->bucketManager->batch($ops);
  306. $this->assertEquals(200, $ret[0]['code']);
  307. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  308. $this->assertNull($error);
  309. }
  310. public function testBatchRename()
  311. {
  312. $key = 'rename' . rand();
  313. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  314. $key2 = $key . 'to';
  315. $ops = BucketManager::buildBatchRename($this->bucketName, array($key => $key2), true);
  316. list($ret, $error) = $this->bucketManager->batch($ops);
  317. $this->assertEquals(200, $ret[0]['code']);
  318. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  319. $this->assertNull($error);
  320. }
  321. public function testBatchStat()
  322. {
  323. $ops = BucketManager::buildBatchStat($this->bucketName, array('php-sdk.html'));
  324. list($ret, $error) = $this->bucketManager->batch($ops);
  325. $this->assertEquals(200, $ret[0]['code']);
  326. }
  327. public function testDeleteAfterDays()
  328. {
  329. $key = rand();
  330. list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  331. $this->assertNotNull($error);
  332. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  333. list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  334. $this->assertEquals(null, $ret);
  335. }
  336. public function testGetCorsRules()
  337. {
  338. list($ret, $err) = $this->bucketManager->getCorsRules($this->bucketName);
  339. $this->assertNull($err);
  340. }
  341. public function testPutBucketAccessStyleMode()
  342. {
  343. list($ret, $err) = $this->bucketManager->putBucketAccessStyleMode($this->bucketName, 0);
  344. $this->assertNull($err);
  345. }
  346. public function testPutBucketAccessMode()
  347. {
  348. list($ret, $err) = $this->bucketManager->putBucketAccessMode($this->bucketName, 0);
  349. $this->assertNull($err);
  350. }
  351. public function testPutReferAntiLeech()
  352. {
  353. list($ret, $err) = $this->bucketManager->putReferAntiLeech($this->bucketName, 0, "1", "*");
  354. $this->assertNull($err);
  355. }
  356. public function testPutBucketMaxAge()
  357. {
  358. list($ret, $err) = $this->bucketManager->putBucketMaxAge($this->bucketName, 31536000);
  359. $this->assertNull($err);
  360. }
  361. public function testPutBucketQuota()
  362. {
  363. list($ret, $err) = $this->bucketManager->putBucketQuota($this->bucketName, -1, -1);
  364. $this->assertNull($err);
  365. }
  366. public function testGetBucketQuota()
  367. {
  368. list($ret, $err) = $this->bucketManager->getBucketQuota($this->bucketName);
  369. $this->assertNull($err);
  370. }
  371. public function testChangeType()
  372. {
  373. list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 0);
  374. $this->assertNull($err);
  375. list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 1);
  376. $this->assertNull($err);
  377. }
  378. public function testChangeStatus()
  379. {
  380. list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 1);
  381. $this->assertNull($err);
  382. list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 0);
  383. $this->assertNull($err);
  384. }
  385. }