No Description

BucketTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. list($ret, $error) = $this->bucketManager->bucketLifecycleRule($this->bucketName, 'demo', 'test', 80, 70);
  92. $this->assertNull($error);
  93. $this->assertNotNull($ret);
  94. }
  95. public function testGetbucketLifecycleRule()
  96. {
  97. list($ret, $error) = $this->bucketManager->getBucketLifecycleRules($this->bucketName);
  98. $this->assertNull($error);
  99. $this->assertNotNull($ret);
  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->assertNull($error);
  111. $this->assertNotNull($ret);
  112. }
  113. public function testDeleteBucketLifecycleRule()
  114. {
  115. list($ret, $error) = $this->bucketManager->deleteBucketLifecycleRule($this->bucketName, 'demo');
  116. $this->assertNull($error);
  117. $this->assertNotNull($ret);
  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->assertNull($error);
  130. $this->assertNotNull($ret);
  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->assertNull($error);
  143. $this->assertNotNull($ret);
  144. }
  145. public function testGetBucketEvent()
  146. {
  147. list($ret, $error) = $this->bucketManager->getBucketEvents($this->bucketName);
  148. $this->assertNull($error);
  149. $this->assertNotNull($ret);
  150. }
  151. public function testDeleteBucketEvent()
  152. {
  153. list($ret, $error) = $this->bucketManager->deleteBucketEvent($this->bucketName, 'bucketevent');
  154. $this->assertNull($error);
  155. $this->assertNotNull($ret);
  156. }
  157. public function testStat()
  158. {
  159. list($stat, $error) = $this->bucketManager->stat($this->bucketName, $this->key);
  160. $this->assertNull($error);
  161. $this->assertNotNull($stat);
  162. $this->assertNotNull($stat['hash']);
  163. list($stat, $error) = $this->bucketManager->stat($this->bucketName, 'nofile');
  164. $this->assertEquals(612, $error->code());
  165. $this->assertNotNull($error->message());
  166. $this->assertNull($stat);
  167. list($stat, $error) = $this->bucketManager->stat('nobucket', 'nofile');
  168. $this->assertEquals(631, $error->code());
  169. $this->assertNotNull($error->message());
  170. $this->assertNull($stat);
  171. }
  172. public function testDelete()
  173. {
  174. list($ret, $error) = $this->bucketManager->delete($this->bucketName, 'del');
  175. $this->assertNotNull($error);
  176. $this->assertNull($ret);
  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 testPrefetchFailed()
  232. {
  233. list($ret, $error) = $this->bucketManager->prefetch(
  234. 'fakebucket',
  235. 'php-sdk.html'
  236. );
  237. $this->assertNotNull($error);
  238. $this->assertNull($ret);
  239. }
  240. public function testFetch()
  241. {
  242. list($ret, $error) = $this->bucketManager->fetch(
  243. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  244. $this->bucketName,
  245. 'fetch.html'
  246. );
  247. $this->assertNull($error);
  248. $this->assertArrayHasKey('hash', $ret);
  249. list($ret, $error) = $this->bucketManager->fetch(
  250. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  251. $this->bucketName,
  252. ''
  253. );
  254. $this->assertNull($error);
  255. $this->assertArrayHasKey('key', $ret);
  256. list($ret, $error) = $this->bucketManager->fetch(
  257. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  258. $this->bucketName
  259. );
  260. $this->assertNull($error);
  261. $this->assertArrayHasKey('key', $ret);
  262. }
  263. public function testFetchFailed()
  264. {
  265. list($ret, $error) = $this->bucketManager->fetch(
  266. 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
  267. 'fakebucket'
  268. );
  269. $this->assertNotNull($error);
  270. $this->assertNull($ret);
  271. }
  272. public function testAsynchFetch()
  273. {
  274. list($ret, $error) = $this->bucketManager->asynchFetch(
  275. 'http://devtools.qiniu.com/qiniu.png',
  276. $this->bucketName,
  277. null,
  278. 'qiniu.png'
  279. );
  280. $this->assertNull($error);
  281. $this->assertArrayHasKey('id', $ret);
  282. list($ret, $error) = $this->bucketManager->asynchFetch(
  283. 'http://devtools.qiniu.com/qiniu.png',
  284. $this->bucketName,
  285. null,
  286. ''
  287. );
  288. $this->assertNull($error);
  289. $this->assertArrayHasKey('id', $ret);
  290. list($ret, $error) = $this->bucketManager->asynchFetch(
  291. 'http://devtools.qiniu.com/qiniu.png',
  292. $this->bucketName
  293. );
  294. $this->assertNull($error);
  295. $this->assertArrayHasKey('id', $ret);
  296. }
  297. public function testAsynchFetchFailed()
  298. {
  299. list($ret, $error) = $this->bucketManager->asynchFetch(
  300. 'http://devtools.qiniu.com/qiniu.png',
  301. 'fakebucket'
  302. );
  303. $this->assertNotNull($error);
  304. $this->assertNull($ret);
  305. }
  306. public function testBatchCopy()
  307. {
  308. $key = 'copyto' . rand();
  309. $ops = BucketManager::buildBatchCopy(
  310. $this->bucketName,
  311. array($this->key => $key),
  312. $this->bucketName,
  313. true
  314. );
  315. list($ret, $error) = $this->bucketManager->batch($ops);
  316. $this->assertEquals(200, $ret[0]['code']);
  317. $ops = BucketManager::buildBatchDelete($this->bucketName, array($key));
  318. list($ret, $error) = $this->bucketManager->batch($ops);
  319. $this->assertEquals(200, $ret[0]['code']);
  320. }
  321. public function testBatchMove()
  322. {
  323. $key = 'movefrom' . rand();
  324. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  325. $key2 = $key . 'to';
  326. $ops = BucketManager::buildBatchMove(
  327. $this->bucketName,
  328. array($key => $key2),
  329. $this->bucketName,
  330. true
  331. );
  332. list($ret, $error) = $this->bucketManager->batch($ops);
  333. $this->assertEquals(200, $ret[0]['code']);
  334. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  335. $this->assertNull($error);
  336. }
  337. public function testBatchRename()
  338. {
  339. $key = 'rename' . rand();
  340. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  341. $key2 = $key . 'to';
  342. $ops = BucketManager::buildBatchRename($this->bucketName, array($key => $key2), true);
  343. list($ret, $error) = $this->bucketManager->batch($ops);
  344. $this->assertEquals(200, $ret[0]['code']);
  345. list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
  346. $this->assertNull($error);
  347. }
  348. public function testBatchStat()
  349. {
  350. $ops = BucketManager::buildBatchStat($this->bucketName, array('php-sdk.html'));
  351. list($ret, $error) = $this->bucketManager->batch($ops);
  352. $this->assertEquals(200, $ret[0]['code']);
  353. }
  354. public function testDeleteAfterDays()
  355. {
  356. $key = rand();
  357. list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  358. $this->assertNotNull($error);
  359. $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
  360. list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
  361. $this->assertEquals(null, $ret);
  362. }
  363. public function testGetCorsRules()
  364. {
  365. list($ret, $err) = $this->bucketManager->getCorsRules($this->bucketName);
  366. $this->assertNull($err);
  367. }
  368. public function testPutBucketAccessStyleMode()
  369. {
  370. list($ret, $err) = $this->bucketManager->putBucketAccessStyleMode($this->bucketName, 0);
  371. $this->assertNull($err);
  372. }
  373. public function testPutBucketAccessMode()
  374. {
  375. list($ret, $err) = $this->bucketManager->putBucketAccessMode($this->bucketName, 0);
  376. $this->assertNull($err);
  377. }
  378. public function testPutReferAntiLeech()
  379. {
  380. list($ret, $err) = $this->bucketManager->putReferAntiLeech($this->bucketName, 0, "1", "*");
  381. $this->assertNull($err);
  382. }
  383. public function testPutBucketMaxAge()
  384. {
  385. list($ret, $err) = $this->bucketManager->putBucketMaxAge($this->bucketName, 31536000);
  386. $this->assertNull($err);
  387. }
  388. public function testPutBucketQuota()
  389. {
  390. list($ret, $err) = $this->bucketManager->putBucketQuota($this->bucketName, -1, -1);
  391. $this->assertNull($err);
  392. }
  393. public function testGetBucketQuota()
  394. {
  395. list($ret, $err) = $this->bucketManager->getBucketQuota($this->bucketName);
  396. $this->assertNull($err);
  397. }
  398. public function testChangeType()
  399. {
  400. list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 0);
  401. $this->assertNull($err);
  402. list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 1);
  403. $this->assertNull($err);
  404. }
  405. public function testChangeStatus()
  406. {
  407. list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 1);
  408. $this->assertNull($err);
  409. list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 0);
  410. $this->assertNull($err);
  411. }
  412. }