BucketTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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->assertNull($error);
  33. $this->assertTrue(in_array($this->bucketName, $list));
  34. list($list2, $error) = $this->dummyBucketManager->buckets();
  35. $this->assertEquals(401, $error->code());
  36. $this->assertNotNull($error->message());
  37. $this->assertNotNull($error->getResponse());
  38. $this->assertNull($list2);
  39. }
  40. public function testListbuckets()
  41. {
  42. list($ret, $error) = $this->bucketManager->listbuckets('z0');
  43. $this->assertNull($error);
  44. $this->assertNotNull($ret);
  45. }
  46. public function testCreateBucket()
  47. {
  48. list($ret, $error) = $this->bucketManager->createBucket('phpsdk-ci-test');
  49. $this->assertNull($error);
  50. $this->assertNotNull($ret);
  51. }
  52. public function testDeleteBucket()
  53. {
  54. list($ret, $error) = $this->bucketManager->deleteBucket('phpsdk-ci-test');
  55. $this->assertNull($error);
  56. $this->assertNotNull($ret);
  57. }
  58. public function testDomains()
  59. {
  60. list($ret, $error) = $this->bucketManager->domains($this->bucketName);
  61. $this->assertNull($error);
  62. $this->assertNotNull($ret);
  63. }
  64. public function testBucketInfo()
  65. {
  66. list($ret, $error) = $this->bucketManager->bucketInfo($this->bucketName);
  67. $this->assertNull($error);
  68. $this->assertNotNull($ret);
  69. }
  70. public function testBucketInfos()
  71. {
  72. list($ret, $error) = $this->bucketManager->bucketInfos('z0');
  73. $this->assertNull($error);
  74. $this->assertNotNull($ret);
  75. }
  76. public function testList()
  77. {
  78. list($ret, $error) = $this->bucketManager->listFiles($this->bucketName, null, null, 10);
  79. $this->assertNull($error);
  80. $this->assertNotNull($ret['items'][0]);
  81. $this->assertNotNull($ret['marker']);
  82. }
  83. public function testListFilesv2()
  84. {
  85. list($ret, $error) = $this->bucketManager->listFilesv2($this->bucketName, null, null, 10);
  86. $this->assertNull($error);
  87. $this->assertNotNull($ret);
  88. }
  89. public function testBucketLifecycleRule()
  90. {
  91. // delete
  92. $this->bucketManager->deleteBucketLifecycleRule($this->bucketName, 'demo');
  93. // add
  94. list($ret, $error) = $this->bucketManager->bucketLifecycleRule(
  95. $this->bucketName,
  96. 'demo',
  97. 'test',
  98. 80,
  99. 70,
  100. 72,
  101. 74
  102. );
  103. $this->assertNull($error);
  104. $this->assertNotNull($ret);
  105. // get
  106. list($ret, $error) = $this->bucketManager->getBucketLifecycleRules($this->bucketName);
  107. $this->assertNull($error);
  108. $this->assertNotNull($ret);
  109. $rule = null;
  110. foreach ($ret as $r) {
  111. if ($r["name"] === "demo") {
  112. $rule = $r;
  113. break;
  114. }
  115. }
  116. $this->assertNotNull($rule);
  117. $this->assertEquals("test", $rule["prefix"]);
  118. $this->assertEquals(80, $rule["delete_after_days"]);
  119. $this->assertEquals(70, $rule["to_line_after_days"]);
  120. $this->assertEquals(72, $rule["to_archive_after_days"]);
  121. $this->assertEquals(74, $rule["to_deep_archive_after_days"]);
  122. // update
  123. list($ret, $error) = $this->bucketManager->updateBucketLifecycleRule(
  124. $this->bucketName,
  125. 'demo',
  126. 'testupdate',
  127. 90,
  128. 75,
  129. 80,
  130. 85
  131. );
  132. $this->assertNull($error);
  133. $this->assertNotNull($ret);
  134. // get
  135. list($ret, $error) = $this->bucketManager->getBucketLifecycleRules($this->bucketName);
  136. $this->assertNull($error);
  137. $this->assertNotNull($ret);
  138. $rule = null;
  139. foreach ($ret as $r) {
  140. if ($r["name"] === "demo") {
  141. $rule = $r;
  142. break;
  143. }
  144. }
  145. $this->assertNotNull($rule);
  146. $this->assertEquals("testupdate", $rule["prefix"]);
  147. $this->assertEquals(90, $rule["delete_after_days"]);
  148. $this->assertEquals(75, $rule["to_line_after_days"]);
  149. $this->assertEquals(80, $rule["to_archive_after_days"]);
  150. $this->assertEquals(85, $rule["to_deep_archive_after_days"]);
  151. // delete
  152. list($ret, $error) = $this->bucketManager->deleteBucketLifecycleRule($this->bucketName, 'demo');
  153. $this->assertNull($error);
  154. $this->assertNotNull($ret);
  155. }
  156. public function testPutBucketEvent()
  157. {
  158. list($ret, $error) = $this->bucketManager->putBucketEvent(
  159. $this->bucketName,
  160. 'bucketevent',
  161. 'test',
  162. 'img',
  163. array('copy'),
  164. $this->customCallbackURL
  165. );
  166. $this->assertNull($error);
  167. $this->assertNotNull($ret);
  168. }
  169. public function testUpdateBucketEvent()
  170. {
  171. list($ret, $error) = $this->bucketManager->updateBucketEvent(
  172. $this->bucketName,
  173. 'bucketevent',
  174. 'test',
  175. 'video',
  176. array('copy'),
  177. $this->customCallbackURL
  178. );
  179. $this->assertNull($error);
  180. $this->assertNotNull($ret);
  181. }
  182. public function testGetBucketEvent()
  183. {
  184. list($ret, $error) = $this->bucketManager->getBucketEvents($this->bucketName);
  185. $this->assertNull($error);
  186. $this->assertNotNull($ret);
  187. }
  188. public function testDeleteBucketEvent()
  189. {
  190. list($ret, $error) = $this->bucketManager->deleteBucketEvent($this->bucketName, 'bucketevent');
  191. $this->assertNull($error);
  192. $this->assertNotNull($ret);
  193. }
  194. public function testStat()
  195. {
  196. list($stat, $error) = $this->bucketManager->stat($this->bucketName, $this->key);
  197. $this->assertNull($error);
  198. $this->assertNotNull($stat);
  199. $this->assertNotNull($stat['hash']);
  200. list($stat, $error) = $this->bucketManager->stat($this->bucketName, 'nofile');
  201. $this->assertEquals(612, $error->code());
  202. $this->assertNotNull($error->message());
  203. $this->assertNull($stat);
  204. list($stat, $error) = $this->bucketManager->stat('nobucket', 'nofile');
  205. $this->assertEquals(631, $error->code());
  206. $this->assertNotNull($error->message());
  207. $this->assertNull($stat);
  208. }
  209. public function testDelete()
  210. {
  211. list($ret, $error) = $this->bucketManager->delete($this->bucketName, 'del');
  212. $this->assertNotNull($error);
  213. $this->assertNull($ret);
  214. }
  215. public function testRename()
  216. {
  217. $key = 'renamefrom' . rand();
  218. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  219. $key2 = 'renameto' . $key;
  220. list($ret, $error) = $this->bucketManager->rename($this->bucketName, $key, $key2);
  221. $this->assertNull($error);
  222. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  223. $this->assertNull($error);
  224. }
  225. public function testCopy()
  226. {
  227. $key = 'copyto' . rand();
  228. $this->bucketManager->delete($this->bucketName, $key);
  229. list($ret, $error) = $this->bucketManager->copy(
  230. $this->bucketName,
  231. $this->key,
  232. $this->bucketName,
  233. $key
  234. );
  235. $this->assertNull($error);
  236. //test force copy
  237. list($ret, $error) = $this->bucketManager->copy(
  238. $this->bucketName,
  239. $this->key2,
  240. $this->bucketName,
  241. $key,
  242. true
  243. );
  244. $this->assertNull($error);
  245. list($key2Stat,) = $this->bucketManager->stat($this->bucketName, $this->key2);
  246. list($key2CopiedStat,) = $this->bucketManager->stat($this->bucketName, $key);
  247. $this->assertEquals($key2Stat['hash'], $key2CopiedStat['hash']);
  248. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key);
  249. $this->assertNull($error);
  250. }
  251. public function testChangeMime()
  252. {
  253. list($ret, $error) = $this->bucketManager->changeMime(
  254. $this->bucketName,
  255. 'php-sdk.html',
  256. 'text/html'
  257. );
  258. $this->assertNull($error);
  259. }
  260. public function testPrefetch()
  261. {
  262. list($ret, $error) = $this->bucketManager->prefetch(
  263. $this->bucketName,
  264. 'php-sdk.html'
  265. );
  266. $this->assertNull($error);
  267. }
  268. public function testPrefetchFailed()
  269. {
  270. list($ret, $error) = $this->bucketManager->prefetch(
  271. 'fakebucket',
  272. 'php-sdk.html'
  273. );
  274. $this->assertNotNull($error);
  275. $this->assertNull($ret);
  276. }
  277. public function testFetch()
  278. {
  279. list($ret, $error) = $this->bucketManager->fetch(
  280. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  281. $this->bucketName,
  282. 'fetch.html'
  283. );
  284. $this->assertNull($error);
  285. $this->assertArrayHasKey('hash', $ret);
  286. list($ret, $error) = $this->bucketManager->fetch(
  287. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  288. $this->bucketName,
  289. ''
  290. );
  291. $this->assertNull($error);
  292. $this->assertArrayHasKey('key', $ret);
  293. list($ret, $error) = $this->bucketManager->fetch(
  294. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  295. $this->bucketName
  296. );
  297. $this->assertNull($error);
  298. $this->assertArrayHasKey('key', $ret);
  299. }
  300. public function testFetchFailed()
  301. {
  302. list($ret, $error) = $this->bucketManager->fetch(
  303. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  304. 'fakebucket'
  305. );
  306. $this->assertNotNull($error);
  307. $this->assertNull($ret);
  308. }
  309. public function testAsynchFetch()
  310. {
  311. list($ret, $error) = $this->bucketManager->asynchFetch(
  312. 'http://devtools.qiniu.com/qiniu.png',
  313. $this->bucketName,
  314. null,
  315. 'qiniu.png'
  316. );
  317. $this->assertNull($error);
  318. $this->assertArrayHasKey('id', $ret);
  319. list($ret, $error) = $this->bucketManager->asynchFetch(
  320. 'http://devtools.qiniu.com/qiniu.png',
  321. $this->bucketName,
  322. null,
  323. ''
  324. );
  325. $this->assertNull($error);
  326. $this->assertArrayHasKey('id', $ret);
  327. list($ret, $error) = $this->bucketManager->asynchFetch(
  328. 'http://devtools.qiniu.com/qiniu.png',
  329. $this->bucketName
  330. );
  331. $this->assertNull($error);
  332. $this->assertArrayHasKey('id', $ret);
  333. }
  334. public function testAsynchFetchFailed()
  335. {
  336. list($ret, $error) = $this->bucketManager->asynchFetch(
  337. 'http://devtools.qiniu.com/qiniu.png',
  338. 'fakebucket'
  339. );
  340. $this->assertNotNull($error);
  341. $this->assertNull($ret);
  342. }
  343. public function testBatchCopy()
  344. {
  345. $key = 'copyto' . rand();
  346. $ops = BucketManager::buildBatchCopy(
  347. $this->bucketName,
  348. array($this->key => $key),
  349. $this->bucketName,
  350. true
  351. );
  352. list($ret, $error) = $this->bucketManager->batch($ops);
  353. $this->assertEquals(200, $ret[0]['code']);
  354. $ops = BucketManager::buildBatchDelete($this->bucketName, array($key));
  355. list($ret, $error) = $this->bucketManager->batch($ops);
  356. $this->assertEquals(200, $ret[0]['code']);
  357. }
  358. public function testBatchMove()
  359. {
  360. $key = 'movefrom' . rand();
  361. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  362. $key2 = $key . 'to';
  363. $ops = BucketManager::buildBatchMove(
  364. $this->bucketName,
  365. array($key => $key2),
  366. $this->bucketName,
  367. true
  368. );
  369. list($ret, $error) = $this->bucketManager->batch($ops);
  370. $this->assertEquals(200, $ret[0]['code']);
  371. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  372. $this->assertNull($error);
  373. }
  374. public function testBatchRename()
  375. {
  376. $key = 'rename' . rand();
  377. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  378. $key2 = $key . 'to';
  379. $ops = BucketManager::buildBatchRename($this->bucketName, array($key => $key2), true);
  380. list($ret, $error) = $this->bucketManager->batch($ops);
  381. $this->assertEquals(200, $ret[0]['code']);
  382. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  383. $this->assertNull($error);
  384. }
  385. public function testBatchStat()
  386. {
  387. $ops = BucketManager::buildBatchStat($this->bucketName, array('php-sdk.html'));
  388. list($ret, $error) = $this->bucketManager->batch($ops);
  389. $this->assertEquals(200, $ret[0]['code']);
  390. }
  391. public function testBatchChangeTypeAndBatchRestoreAr()
  392. {
  393. $key = 'toChangeTypeThenRestore' . rand();
  394. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  395. $ops = BucketManager::buildBatchChangeType($this->bucketName, array($key => 2)); // 2 Archive
  396. list($ret, $error) = $this->bucketManager->batch($ops);
  397. $this->assertNull($error);
  398. $this->assertEquals(200, $ret[0]['code']);
  399. $ops = BucketManager::buildBatchRestoreAr($this->bucketName, array($key => 1)); // 1 day
  400. list($ret, $error) = $this->bucketManager->batch($ops);
  401. $this->assertNull($error);
  402. $this->assertEquals(200, $ret[0]['code']);
  403. $this->bucketManager->delete($this->bucketName, $key);
  404. }
  405. public function testDeleteAfterDays()
  406. {
  407. $key = rand();
  408. list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  409. $this->assertNotNull($error);
  410. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  411. list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  412. $this->assertEquals(null, $ret);
  413. }
  414. public function testSetObjectLifecycle()
  415. {
  416. $key = 'setObjectLifeCycle' . rand();
  417. $this->bucketManager->delete($this->bucketName, $key);
  418. $this->bucketManager->copy(
  419. $this->bucketName,
  420. $this->key,
  421. $this->bucketName,
  422. $key
  423. );
  424. list($ret, $err) = $this->bucketManager->setObjectLifecycle(
  425. $this->bucketName,
  426. $key,
  427. 10,
  428. 20,
  429. 30,
  430. 40
  431. );
  432. $this->assertNull($err);
  433. $this->bucketManager->delete($this->bucketName, $key);
  434. }
  435. public function testSetObjectLifecycleWithCond()
  436. {
  437. $key = 'setObjectLifeCycleWithCond' . rand();
  438. $this->bucketManager->delete($this->bucketName, $key);
  439. $this->bucketManager->copy(
  440. $this->bucketName,
  441. $this->key,
  442. $this->bucketName,
  443. $key
  444. );
  445. list($ret, $err) = $this->bucketManager->stat($this->bucketName, $key);
  446. $this->assertNull($err);
  447. $key_hash = $ret['hash'];
  448. $key_fsize = $ret['fsize'];
  449. list($ret, $err) = $this->bucketManager->setObjectLifecycleWithCond(
  450. $this->bucketName,
  451. $key,
  452. array(
  453. 'hash' => $key_hash,
  454. 'fsize' => $key_fsize
  455. ),
  456. 10,
  457. 20,
  458. 30,
  459. 40
  460. );
  461. $this->assertNull($err);
  462. $this->bucketManager->delete($this->bucketName, $key);
  463. }
  464. public function testBatchSetObjectLifecycle()
  465. {
  466. $key = 'batchSetObjectLifeCycle' . rand();
  467. $this->bucketManager->delete($this->bucketName, $key);
  468. $this->bucketManager->copy(
  469. $this->bucketName,
  470. $this->key,
  471. $this->bucketName,
  472. $key
  473. );
  474. $ops = BucketManager::buildBatchSetObjectLifecycle(
  475. $this->bucketName,
  476. array($key),
  477. 10,
  478. 20,
  479. 30,
  480. 40
  481. );
  482. list($ret, $err) = $this->bucketManager->batch($ops);
  483. $this->assertNull($err);
  484. $this->assertEquals(200, $ret[0]['code']);
  485. $this->bucketManager->delete($this->bucketName, $key);
  486. }
  487. public function testGetCorsRules()
  488. {
  489. list($ret, $err) = $this->bucketManager->getCorsRules($this->bucketName);
  490. $this->assertNull($err);
  491. }
  492. public function testPutBucketAccessStyleMode()
  493. {
  494. list($ret, $err) = $this->bucketManager->putBucketAccessStyleMode($this->bucketName, 0);
  495. $this->assertNull($err);
  496. }
  497. public function testPutBucketAccessMode()
  498. {
  499. list($ret, $err) = $this->bucketManager->putBucketAccessMode($this->bucketName, 0);
  500. $this->assertNull($err);
  501. }
  502. public function testPutReferAntiLeech()
  503. {
  504. list($ret, $err) = $this->bucketManager->putReferAntiLeech($this->bucketName, 0, "1", "*");
  505. $this->assertNull($err);
  506. }
  507. public function testPutBucketMaxAge()
  508. {
  509. list($ret, $err) = $this->bucketManager->putBucketMaxAge($this->bucketName, 31536000);
  510. $this->assertNull($err);
  511. }
  512. public function testPutBucketQuota()
  513. {
  514. list($ret, $err) = $this->bucketManager->putBucketQuota($this->bucketName, -1, -1);
  515. $this->assertNull($err);
  516. }
  517. public function testGetBucketQuota()
  518. {
  519. list($ret, $err) = $this->bucketManager->getBucketQuota($this->bucketName);
  520. $this->assertNull($err);
  521. }
  522. public function testChangeType()
  523. {
  524. list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 0);
  525. $this->assertNull($err);
  526. list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 1);
  527. $this->assertNull($err);
  528. }
  529. public function testArchiveRestoreAr()
  530. {
  531. $key = 'archiveToRestore' . rand();
  532. $this->bucketManager->delete($this->bucketName, $key);
  533. $this->bucketManager->copy(
  534. $this->bucketName,
  535. $this->key,
  536. $this->bucketName,
  537. $key
  538. );
  539. $this->bucketManager->changeType($this->bucketName, $key, 2);
  540. list(, $err) = $this->bucketManager->restoreAr($this->bucketName, $key, 2);
  541. $this->assertNull($err);
  542. list($ret, $err) = $this->bucketManager->stat($this->bucketName, $key);
  543. $this->assertNull($err);
  544. $this->assertEquals(2, $ret["type"]);
  545. // restoreStatus
  546. // null means frozen;
  547. // 1 means be unfreezing;
  548. // 2 means be unfrozen;
  549. $this->assertNotNull($ret["restoreStatus"]);
  550. $this->assertContains($ret["restoreStatus"], array(1, 2));
  551. $this->bucketManager->delete($this->bucketName, $key);
  552. }
  553. public function testDeepArchiveRestoreAr()
  554. {
  555. $key = 'deepArchiveToRestore' . rand();
  556. $this->bucketManager->delete($this->bucketName, $key);
  557. $this->bucketManager->copy(
  558. $this->bucketName,
  559. $this->key,
  560. $this->bucketName,
  561. $key
  562. );
  563. $this->bucketManager->changeType($this->bucketName, $key, 3);
  564. list(, $err) = $this->bucketManager->restoreAr($this->bucketName, $key, 1);
  565. $this->assertNull($err);
  566. list($ret, $err) = $this->bucketManager->stat($this->bucketName, $key);
  567. $this->assertNull($err);
  568. $this->assertEquals(3, $ret["type"]);
  569. // restoreStatus
  570. // null means frozen;
  571. // 1 means be unfreezing;
  572. // 2 means be unfrozen;
  573. $this->assertNotNull($ret["restoreStatus"]);
  574. $this->assertContains($ret["restoreStatus"], array(1, 2));
  575. $this->bucketManager->delete($this->bucketName, $key);
  576. }
  577. public function testChangeStatus()
  578. {
  579. list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 1);
  580. $this->assertNull($err);
  581. list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 0);
  582. $this->assertNull($err);
  583. }
  584. }