123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482 |
- <?php
- namespace Qiniu\Tests;
- use Qiniu\Config;
- use Qiniu\Storage\BucketManager;
- class BucketTest extends \PHPUnit_Framework_TestCase
- {
- protected $bucketManager;
- protected $dummyBucketManager;
- protected $bucketName;
- protected $key;
- protected $key2;
- protected $customCallbackURL;
- protected function setUp()
- {
- global $bucketName;
- global $key;
- global $key2;
- $this->bucketName = $bucketName;
- $this->key = $key;
- $this->key2 = $key2;
- global $customCallbackURL;
- $this->customCallbackURL = $customCallbackURL;
- global $testAuth;
- $config = new Config();
- $this->bucketManager = new BucketManager($testAuth, $config);
- global $dummyAuth;
- $this->dummyBucketManager = new BucketManager($dummyAuth);
- }
- public function testBuckets()
- {
- list($list, $error) = $this->bucketManager->buckets();
- $this->assertNull($error);
- $this->assertTrue(in_array($this->bucketName, $list));
- list($list2, $error) = $this->dummyBucketManager->buckets();
- $this->assertEquals(401, $error->code());
- $this->assertNotNull($error->message());
- $this->assertNotNull($error->getResponse());
- $this->assertNull($list2);
- }
- public function testListbuckets()
- {
- list($ret, $error) = $this->bucketManager->listbuckets('z0');
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testCreateBucket()
- {
- list($ret, $error) = $this->bucketManager->createBucket('phpsdk-ci-test');
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testDeleteBucket()
- {
- list($ret, $error) = $this->bucketManager->deleteBucket('phpsdk-ci-test');
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testDomains()
- {
- list($ret, $error) = $this->bucketManager->domains($this->bucketName);
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testBucketInfo()
- {
- list($ret, $error) = $this->bucketManager->bucketInfo($this->bucketName);
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testBucketInfos()
- {
- list($ret, $error) = $this->bucketManager->bucketInfos('z0');
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testList()
- {
- list($ret, $error) = $this->bucketManager->listFiles($this->bucketName, null, null, 10);
- $this->assertNull($error);
- $this->assertNotNull($ret['items'][0]);
- $this->assertNotNull($ret['marker']);
- }
- public function testListFilesv2()
- {
- list($ret, $error) = $this->bucketManager->listFilesv2($this->bucketName, null, null, 10);
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testBucketLifecycleRule()
- {
- list($ret, $error) = $this->bucketManager->bucketLifecycleRule($this->bucketName, 'demo', 'test', 80, 70);
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testGetbucketLifecycleRule()
- {
- list($ret, $error) = $this->bucketManager->getBucketLifecycleRules($this->bucketName);
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testUpdatebucketLifecycleRule()
- {
- list($ret, $error) = $this->bucketManager->updateBucketLifecycleRule(
- $this->bucketName,
- 'demo',
- 'testupdate',
- 80,
- 70
- );
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testDeleteBucketLifecycleRule()
- {
- list($ret, $error) = $this->bucketManager->deleteBucketLifecycleRule($this->bucketName, 'demo');
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testPutBucketEvent()
- {
- list($ret, $error) = $this->bucketManager->putBucketEvent(
- $this->bucketName,
- 'bucketevent',
- 'test',
- 'img',
- array('copy'),
- $this->customCallbackURL
- );
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testUpdateBucketEvent()
- {
- list($ret, $error) = $this->bucketManager->updateBucketEvent(
- $this->bucketName,
- 'bucketevent',
- 'test',
- 'video',
- array('copy'),
- $this->customCallbackURL
- );
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testGetBucketEvent()
- {
- list($ret, $error) = $this->bucketManager->getBucketEvents($this->bucketName);
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testDeleteBucketEvent()
- {
- list($ret, $error) = $this->bucketManager->deleteBucketEvent($this->bucketName, 'bucketevent');
- $this->assertNull($error);
- $this->assertNotNull($ret);
- }
- public function testStat()
- {
- list($stat, $error) = $this->bucketManager->stat($this->bucketName, $this->key);
- $this->assertNull($error);
- $this->assertNotNull($stat);
- $this->assertNotNull($stat['hash']);
- list($stat, $error) = $this->bucketManager->stat($this->bucketName, 'nofile');
- $this->assertEquals(612, $error->code());
- $this->assertNotNull($error->message());
- $this->assertNull($stat);
- list($stat, $error) = $this->bucketManager->stat('nobucket', 'nofile');
- $this->assertEquals(631, $error->code());
- $this->assertNotNull($error->message());
- $this->assertNull($stat);
- }
- public function testDelete()
- {
- list($ret, $error) = $this->bucketManager->delete($this->bucketName, 'del');
- $this->assertNotNull($error);
- $this->assertNull($ret);
- }
- public function testRename()
- {
- $key = 'renamefrom' . rand();
- $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
- $key2 = 'renameto' . $key;
- list($ret, $error) = $this->bucketManager->rename($this->bucketName, $key, $key2);
- $this->assertNull($error);
- list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
- $this->assertNull($error);
- }
- public function testCopy()
- {
- $key = 'copyto' . rand();
- $this->bucketManager->delete($this->bucketName, $key);
- list($ret, $error) = $this->bucketManager->copy(
- $this->bucketName,
- $this->key,
- $this->bucketName,
- $key
- );
- $this->assertNull($error);
- //test force copy
- list($ret, $error) = $this->bucketManager->copy(
- $this->bucketName,
- $this->key2,
- $this->bucketName,
- $key,
- true
- );
- $this->assertNull($error);
- list($key2Stat,) = $this->bucketManager->stat($this->bucketName, $this->key2);
- list($key2CopiedStat,) = $this->bucketManager->stat($this->bucketName, $key);
- $this->assertEquals($key2Stat['hash'], $key2CopiedStat['hash']);
- list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key);
- $this->assertNull($error);
- }
- public function testChangeMime()
- {
- list($ret, $error) = $this->bucketManager->changeMime(
- $this->bucketName,
- 'php-sdk.html',
- 'text/html'
- );
- $this->assertNull($error);
- }
- public function testPrefetch()
- {
- list($ret, $error) = $this->bucketManager->prefetch(
- $this->bucketName,
- 'php-sdk.html'
- );
- $this->assertNull($error);
- }
- public function testPrefetchFailed()
- {
- list($ret, $error) = $this->bucketManager->prefetch(
- 'fakebucket',
- 'php-sdk.html'
- );
- $this->assertNotNull($error);
- $this->assertNull($ret);
- }
- public function testFetch()
- {
- list($ret, $error) = $this->bucketManager->fetch(
- 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
- $this->bucketName,
- 'fetch.html'
- );
- $this->assertNull($error);
- $this->assertArrayHasKey('hash', $ret);
- list($ret, $error) = $this->bucketManager->fetch(
- 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
- $this->bucketName,
- ''
- );
- $this->assertNull($error);
- $this->assertArrayHasKey('key', $ret);
- list($ret, $error) = $this->bucketManager->fetch(
- 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
- $this->bucketName
- );
- $this->assertNull($error);
- $this->assertArrayHasKey('key', $ret);
- }
- public function testFetchFailed()
- {
- list($ret, $error) = $this->bucketManager->fetch(
- 'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
- 'fakebucket'
- );
- $this->assertNotNull($error);
- $this->assertNull($ret);
- }
- public function testAsynchFetch()
- {
- list($ret, $error) = $this->bucketManager->asynchFetch(
- 'http://devtools.qiniu.com/qiniu.png',
- $this->bucketName,
- null,
- 'qiniu.png'
- );
- $this->assertNull($error);
- $this->assertArrayHasKey('id', $ret);
- list($ret, $error) = $this->bucketManager->asynchFetch(
- 'http://devtools.qiniu.com/qiniu.png',
- $this->bucketName,
- null,
- ''
- );
- $this->assertNull($error);
- $this->assertArrayHasKey('id', $ret);
- list($ret, $error) = $this->bucketManager->asynchFetch(
- 'http://devtools.qiniu.com/qiniu.png',
- $this->bucketName
- );
- $this->assertNull($error);
- $this->assertArrayHasKey('id', $ret);
- }
- public function testAsynchFetchFailed()
- {
- list($ret, $error) = $this->bucketManager->asynchFetch(
- 'http://devtools.qiniu.com/qiniu.png',
- 'fakebucket'
- );
- $this->assertNotNull($error);
- $this->assertNull($ret);
- }
- public function testBatchCopy()
- {
- $key = 'copyto' . rand();
- $ops = BucketManager::buildBatchCopy(
- $this->bucketName,
- array($this->key => $key),
- $this->bucketName,
- true
- );
- list($ret, $error) = $this->bucketManager->batch($ops);
- $this->assertEquals(200, $ret[0]['code']);
- $ops = BucketManager::buildBatchDelete($this->bucketName, array($key));
- list($ret, $error) = $this->bucketManager->batch($ops);
- $this->assertEquals(200, $ret[0]['code']);
- }
- public function testBatchMove()
- {
- $key = 'movefrom' . rand();
- $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
- $key2 = $key . 'to';
- $ops = BucketManager::buildBatchMove(
- $this->bucketName,
- array($key => $key2),
- $this->bucketName,
- true
- );
- list($ret, $error) = $this->bucketManager->batch($ops);
- $this->assertEquals(200, $ret[0]['code']);
- list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
- $this->assertNull($error);
- }
- public function testBatchRename()
- {
- $key = 'rename' . rand();
- $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
- $key2 = $key . 'to';
- $ops = BucketManager::buildBatchRename($this->bucketName, array($key => $key2), true);
- list($ret, $error) = $this->bucketManager->batch($ops);
- $this->assertEquals(200, $ret[0]['code']);
- list($ret, $error) = $this->bucketManager->delete($this->bucketName, $key2);
- $this->assertNull($error);
- }
- public function testBatchStat()
- {
- $ops = BucketManager::buildBatchStat($this->bucketName, array('php-sdk.html'));
- list($ret, $error) = $this->bucketManager->batch($ops);
- $this->assertEquals(200, $ret[0]['code']);
- }
- public function testDeleteAfterDays()
- {
- $key = rand();
- list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
- $this->assertNotNull($error);
- $this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
- list($ret, $error) = $this->bucketManager->deleteAfterDays($this->bucketName, $key, 1);
- $this->assertEquals(null, $ret);
- }
- public function testGetCorsRules()
- {
- list($ret, $err) = $this->bucketManager->getCorsRules($this->bucketName);
- $this->assertNull($err);
- }
- public function testPutBucketAccessStyleMode()
- {
- list($ret, $err) = $this->bucketManager->putBucketAccessStyleMode($this->bucketName, 0);
- $this->assertNull($err);
- }
- public function testPutBucketAccessMode()
- {
- list($ret, $err) = $this->bucketManager->putBucketAccessMode($this->bucketName, 0);
- $this->assertNull($err);
- }
- public function testPutReferAntiLeech()
- {
- list($ret, $err) = $this->bucketManager->putReferAntiLeech($this->bucketName, 0, "1", "*");
- $this->assertNull($err);
- }
- public function testPutBucketMaxAge()
- {
- list($ret, $err) = $this->bucketManager->putBucketMaxAge($this->bucketName, 31536000);
- $this->assertNull($err);
- }
- public function testPutBucketQuota()
- {
- list($ret, $err) = $this->bucketManager->putBucketQuota($this->bucketName, -1, -1);
- $this->assertNull($err);
- }
- public function testGetBucketQuota()
- {
- list($ret, $err) = $this->bucketManager->getBucketQuota($this->bucketName);
- $this->assertNull($err);
- }
- public function testChangeType()
- {
- list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 0);
- $this->assertNull($err);
- list($ret, $err) = $this->bucketManager->changeType($this->bucketName, $this->key, 1);
- $this->assertNull($err);
- }
- public function testChangeStatus()
- {
- list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 1);
- $this->assertNull($err);
- list($ret, $err) = $this->bucketManager->changeStatus($this->bucketName, $this->key, 0);
- $this->assertNull($err);
- }
- }
|