初始上传

This commit is contained in:
2026-04-04 17:27:12 +08:00
parent 4d80d28eb4
commit b7e11774ee
11191 changed files with 1588469 additions and 0 deletions

View File

@@ -0,0 +1,211 @@
<?php
// Hack to override the time returned from the S3SignatureV4
// @codingStandardsIgnoreStart
namespace Qiniu {
function time()
{
return isset($_SERVER['override_qiniu_auth_time'])
? 1234567890
: \time();
}
}
namespace Qiniu\Tests {
use Qiniu\Auth;
use Qiniu\Http\Header;
// @codingStandardsIgnoreEnd
class AuthTest extends \PHPUnit_Framework_TestCase
{
public function testSign()
{
global $dummyAuth;
$token = $dummyAuth->sign('test');
$this->assertEquals('abcdefghklmnopq:mSNBTR7uS2crJsyFr2Amwv1LaYg=', $token);
}
public function testSignWithData()
{
global $dummyAuth;
$token = $dummyAuth->signWithData('test');
$this->assertEquals('abcdefghklmnopq:-jP8eEV9v48MkYiBGs81aDxl60E=:dGVzdA==', $token);
}
public function testSignRequest()
{
global $dummyAuth;
$token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', '');
$this->assertEquals('abcdefghklmnopq:cFyRVoWrE3IugPIMP5YJFTO-O-Y=', $token);
$ctype = 'application/x-www-form-urlencoded';
$token = $dummyAuth->signRequest('http://www.qiniu.com?go=1', 'test', $ctype);
$this->assertEquals($token, 'abcdefghklmnopq:svWRNcacOE-YMsc70nuIYdaa1e4=');
}
public function testPrivateDownloadUrl()
{
global $dummyAuth;
$_SERVER['override_qiniu_auth_time'] = true;
$url = $dummyAuth->privateDownloadUrl('http://www.qiniu.com?go=1');
$expect = 'http://www.qiniu.com?go=1&e=1234571490&token=abcdefghklmnopq:8vzBeLZ9W3E4kbBLFLW0Xe0u7v4=';
$this->assertEquals($expect, $url);
unset($_SERVER['override_qiniu_auth_time']);
}
public function testUploadToken()
{
global $dummyAuth;
$_SERVER['override_qiniu_auth_time'] = true;
$token = $dummyAuth->uploadToken('1', '2', 3600, array('endUser' => 'y'));
// @codingStandardsIgnoreStart
$exp = 'abcdefghklmnopq:yyeexeUkPOROoTGvwBjJ0F0VLEo=:eyJlbmRVc2VyIjoieSIsInNjb3BlIjoiMToyIiwiZGVhZGxpbmUiOjEyMzQ1NzE0OTB9';
// @codingStandardsIgnoreEnd
$this->assertEquals($exp, $token);
unset($_SERVER['override_qiniu_auth_time']);
}
public function testVerifyCallback()
{
}
public function testSignQiniuAuthorization()
{
$auth = new Auth("ak", "sk");
// ---
$url = "";
$method = "";
$headers = new Header(array(
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
"Content-Type" => array("application/x-www-form-urlencoded"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);
// ---
$url = "";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/json"),
));
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:K1DI0goT05yhGizDFE5FiPJxAj4=", $sign);
// ---
$url = "";
$method = "GET";
$headers = new Header(array(
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
"Content-Type" => array("application/x-www-form-urlencoded"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:0i1vKClRDWFyNkcTFzwcE7PzX74=", $sign);
// ---
$url = "";
$method = "POST";
$headers = new Header(array(
"Content-Type" => array("application/json"),
"X-Qiniu" => array("b"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:0ujEjW_vLRZxebsveBgqa3JyQ-w=", $sign);
// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
"Content-Type" => array("application/x-www-form-urlencoded"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:GShw5NitGmd5TLoo38nDkGUofRw=", $sign);
// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/json"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "{\"name\": \"test\"}";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:DhNA1UCaBqSHCsQjMOLRfVn63GQ=", $sign);
// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);
// ---
$url = "http://upload.qiniup.com";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www"),
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:KUAhrYh32P9bv0COD8ugZjDCmII=", $sign);
// ---
$url = "http://upload.qiniup.com/mkfile/sdf.jpg";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:fkRck5_LeyfwdkyyLk-hyNwGKac=", $sign);
$url = "http://upload.qiniup.com/mkfile/sdf.jpg?s=er3&df";
$method = "";
$headers = new Header(array(
"Content-Type" => array("application/x-www-form-urlencoded"),
"X-Qiniu-Bbb" => array("BBB", "AAA"),
"X-Qiniu-Aaa" => array("DDD", "CCC"),
"X-Qiniu-" => array("a"),
"X-Qiniu" => array("b"),
));
$body = "name=test&language=go";
list($sign, $err) = $auth->signQiniuAuthorization($url, $method, $body, $headers);
$this->assertNull($err);
$this->assertEquals("ak:PUFPWsEUIpk_dzUvvxTTmwhp3p4=", $sign);
}
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Qiniu\Tests;
use Qiniu;
class Base64Test extends \PHPUnit_Framework_TestCase
{
public function testUrlSafe()
{
$a = '你好';
$b = \Qiniu\base64_urlSafeEncode($a);
$this->assertEquals($a, \Qiniu\base64_urlSafeDecode($b));
}
}

View File

@@ -0,0 +1,598 @@
<?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()
{
// delete
$this->bucketManager->deleteBucketLifecycleRule($this->bucketName, 'demo');
// add
list($ret, $error) = $this->bucketManager->bucketLifecycleRule(
$this->bucketName,
'demo',
'test',
80,
70,
72,
74
);
$this->assertNull($error);
$this->assertNotNull($ret);
// get
list($ret, $error) = $this->bucketManager->getBucketLifecycleRules($this->bucketName);
$this->assertNull($error);
$this->assertNotNull($ret);
$rule = null;
foreach ($ret as $r) {
if ($r["name"] === "demo") {
$rule = $r;
break;
}
}
$this->assertNotNull($rule);
$this->assertEquals("test", $rule["prefix"]);
$this->assertEquals(80, $rule["delete_after_days"]);
$this->assertEquals(70, $rule["to_line_after_days"]);
$this->assertEquals(72, $rule["to_archive_after_days"]);
$this->assertEquals(74, $rule["to_deep_archive_after_days"]);
// update
list($ret, $error) = $this->bucketManager->updateBucketLifecycleRule(
$this->bucketName,
'demo',
'testupdate',
90,
75,
80,
85
);
$this->assertNull($error);
$this->assertNotNull($ret);
// get
list($ret, $error) = $this->bucketManager->getBucketLifecycleRules($this->bucketName);
$this->assertNull($error);
$this->assertNotNull($ret);
$rule = null;
foreach ($ret as $r) {
if ($r["name"] === "demo") {
$rule = $r;
break;
}
}
$this->assertNotNull($rule);
$this->assertEquals("testupdate", $rule["prefix"]);
$this->assertEquals(90, $rule["delete_after_days"]);
$this->assertEquals(75, $rule["to_line_after_days"]);
$this->assertEquals(80, $rule["to_archive_after_days"]);
$this->assertEquals(85, $rule["to_deep_archive_after_days"]);
// delete
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 testBatchChangeTypeAndBatchRestoreAr()
{
$key = 'toChangeTypeThenRestore' . rand();
$this->bucketManager->copy($this->bucketName, $this->key, $this->bucketName, $key);
$ops = BucketManager::buildBatchChangeType($this->bucketName, array($key => 2)); // 2 Archive
list($ret, $error) = $this->bucketManager->batch($ops);
$this->assertNull($error);
$this->assertEquals(200, $ret[0]['code']);
$ops = BucketManager::buildBatchRestoreAr($this->bucketName, array($key => 1)); // 1 day
list($ret, $error) = $this->bucketManager->batch($ops);
$this->assertNull($error);
$this->assertEquals(200, $ret[0]['code']);
$this->bucketManager->delete($this->bucketName, $key);
}
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 testArchiveRestoreAr()
{
$key = 'archiveToRestore' . rand();
$this->bucketManager->delete($this->bucketName, $key);
$this->bucketManager->copy(
$this->bucketName,
$this->key,
$this->bucketName,
$key
);
$this->bucketManager->changeType($this->bucketName, $key, 2);
list(, $err) = $this->bucketManager->restoreAr($this->bucketName, $key, 2);
$this->assertNull($err);
list($ret, $err) = $this->bucketManager->stat($this->bucketName, $key);
$this->assertNull($err);
$this->assertEquals(2, $ret["type"]);
// restoreStatus
// null means frozen;
// 1 means be unfreezing;
// 2 means be unfrozen;
$this->assertNotNull($ret["restoreStatus"]);
$this->assertContains($ret["restoreStatus"], array(1, 2));
$this->bucketManager->delete($this->bucketName, $key);
}
public function testDeepArchiveRestoreAr()
{
$key = 'deepArchiveToRestore' . rand();
$this->bucketManager->delete($this->bucketName, $key);
$this->bucketManager->copy(
$this->bucketName,
$this->key,
$this->bucketName,
$key
);
$this->bucketManager->changeType($this->bucketName, $key, 3);
list(, $err) = $this->bucketManager->restoreAr($this->bucketName, $key, 1);
$this->assertNull($err);
list($ret, $err) = $this->bucketManager->stat($this->bucketName, $key);
$this->assertNull($err);
$this->assertEquals(3, $ret["type"]);
// restoreStatus
// null means frozen;
// 1 means be unfreezing;
// 2 means be unfrozen;
$this->assertNotNull($ret["restoreStatus"]);
$this->assertContains($ret["restoreStatus"], array(1, 2));
$this->bucketManager->delete($this->bucketName, $key);
}
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);
}
}

View File

@@ -0,0 +1,146 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: wf
* Date: 2017/6/21
* Time: AM8:46
*/
namespace Qiniu\Tests;
use Qiniu\Cdn\CdnManager;
use Qiniu\Http\Client;
class CdnManagerTest extends \PHPUnit_Framework_TestCase
{
protected $cdnManager;
protected $encryptKey;
protected $testStartDate;
protected $testEndDate;
protected $testGranularity;
protected $testLogDate;
protected $refreshUrl;
protected $refreshDirs;
protected $customDomain;
protected $customDomain2;
protected function setUp()
{
global $testAuth;
$this->cdnManager = new CdnManager($testAuth);
global $timestampAntiLeechEncryptKey;
$this->encryptKey = $timestampAntiLeechEncryptKey;
global $testStartDate;
$this->testStartDate = $testStartDate;
global $testEndDate;
$this->testEndDate = $testEndDate;
global $testGranularity;
$this->testGranularity = $testGranularity;
global $testLogDate;
$this->testLogDate = $testLogDate;
global $customDomain;
$this->refreshUrl = $customDomain . '/sdktest.png';
$this->refreshDirs = $customDomain;
$this->customDomain = $customDomain;
global $customDomain2;
$this->customDomain2 = $customDomain2;
}
public function testRefreshUrls()
{
list($ret, $err) = $this->cdnManager->refreshUrls(array($this->refreshUrl));
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testRefreshDirs()
{
list($ret, $err) = $this->cdnManager->refreshDirs(array($this->refreshDirs));
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testRefreshUrlsAndDirs()
{
list($ret, $err) = $this->cdnManager->refreshUrlsAndDirs(array($this->refreshUrl), array($this->refreshDirs));
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testGetCdnRefreshList()
{
list($ret, $err) = $this->cdnManager->getCdnRefreshList(null, null, null, 'success');
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testPrefetchUrls()
{
list($ret, $err) = $this->cdnManager->prefetchUrls(array($this->refreshUrl));
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testGetCdnPrefetchList()
{
list($ret, $err) = $this->cdnManager->getCdnPrefetchList(null, null, 'success');
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testGetBandwidthData()
{
list($ret, $err) = $this->cdnManager->getBandwidthData(
array($this->customDomain2),
$this->testStartDate,
$this->testEndDate,
$this->testGranularity
);
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testGetFluxData()
{
list($ret, $err) = $this->cdnManager->getFluxData(
array($this->customDomain2),
$this->testStartDate,
$this->testEndDate,
$this->testGranularity
);
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testGetCdnLogList()
{
$domain = getenv('QINIU_TEST_DOMAIN');
list($ret, $err) = $this->cdnManager->getCdnLogList(array($domain), $this->testLogDate);
$this->assertNull($err);
$this->assertNotNull($ret);
}
public function testCreateTimestampAntiLeechUrl()
{
$signUrl = $this->cdnManager->createTimestampAntiLeechUrl($this->refreshUrl, $this->encryptKey, 3600);
$response = Client::get($signUrl);
$this->assertNull($response->error);
$this->assertEquals($response->statusCode, 200);
$signUrl = $this->cdnManager->createTimestampAntiLeechUrl(
$this->refreshUrl . '?qiniu',
$this->encryptKey,
3600
);
$response = Client::get($signUrl);
$this->assertNull($response->error);
$this->assertEquals($response->statusCode, 200);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Qiniu\Tests {
use Qiniu\Config;
class ConfigTest extends \PHPUnit_Framework_TestCase
{
protected $accessKey;
protected $bucketName;
protected function setUp()
{
global $accessKey;
$this->accessKey = $accessKey;
global $bucketName;
$this->bucketName = $bucketName;
}
public function testGetApiHost()
{
$conf = new Config();
$hasException = false;
$apiHost = '';
try {
$apiHost = $conf->getApiHost($this->accessKey, $this->bucketName);
} catch (\Exception $e) {
$hasException = true;
}
$this->assertFalse($hasException);
$this->assertEquals('http://api.qiniu.com', $apiHost);
}
public function testGetApiHostErrored()
{
$conf = new Config();
$hasException = false;
try {
$conf->getApiHost($this->accessKey, "fakebucket");
} catch (\Exception $e) {
$hasException = true;
}
$this->assertTrue($hasException);
}
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Qiniu\Tests;
use Qiniu;
class Crc32Test extends \PHPUnit_Framework_TestCase
{
public function testData()
{
$a = '你好';
$b = \Qiniu\crc32_data($a);
$this->assertEquals('1352841281', $b);
}
public function testFile()
{
$b = \Qiniu\crc32_file(__file__);
$c = \Qiniu\crc32_file(__file__);
$this->assertEquals($c, $b);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Qiniu\Tests;
use Qiniu\Http\Client;
class DownloadTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
global $testAuth;
$base_url = 'http://sdk.peterpy.cn/gogopher.jpg';
$private_url = $testAuth->privateDownloadUrl($base_url);
$response = Client::get($private_url);
$this->assertEquals(200, $response->statusCode);
}
public function testFop()
{
global $testAuth;
$base_url = 'http://sdk.peterpy.cn/gogopher.jpg?exif';
$private_url = $testAuth->privateDownloadUrl($base_url);
$response = Client::get($private_url);
$this->assertEquals(200, $response->statusCode);
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Qiniu\Tests;
use Qiniu\Etag;
class EtagTest extends \PHPUnit_Framework_TestCase
{
public function test0M()
{
$file = qiniuTempFile(0, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('Fto5o-5ea0sNMlW_75VgGJCv2AcJ', $r);
$this->assertNull($error);
}
public function testLess4M()
{
$file = qiniuTempFile(3 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('Fs5BpnAjRykYTg6o5E09cjuXrDkG', $r);
$this->assertNull($error);
}
public function test4M()
{
$file = qiniuTempFile(4 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('FiuKULnybewpEnrfTmxjsxc-3dWp', $r);
$this->assertNull($error);
}
public function testMore4M()
{
$file = qiniuTempFile(5 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('lhvyfIWMYFTq4s4alzlhXoAkqfVL', $r);
$this->assertNull($error);
}
public function test8M()
{
$file = qiniuTempFile(8 * 1024 * 1024, false);
list($r, $error) = Etag::sum($file);
unlink($file);
$this->assertEquals('lmRm9ZfGZ86bnMys4wRTWtJj9ClG', $r);
$this->assertNull($error);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Qiniu\Tests;
use Qiniu\Processing\Operation;
use Qiniu\Processing\PersistentFop;
class FopTest extends \PHPUnit_Framework_TestCase
{
public function testExifPub()
{
$fop = new Operation('sdk.peterpy.cn');
list($exif, $error) = $fop->execute('gogopher.jpg', 'exif');
$this->assertNull($error);
$this->assertNotNull($exif);
}
public function testExifPrivate()
{
global $testAuth;
$fop = new Operation('private-res.qiniudn.com', $testAuth);
list($exif, $error) = $fop->execute('noexif.jpg', 'exif');
$this->assertNotNull($error);
$this->assertNull($exif);
}
public function testbuildUrl()
{
$fops = 'imageView2/2/h/200';
$fop = new Operation('testres.qiniudn.com');
$url = $fop->buildUrl('gogopher.jpg', $fops);
$this->assertEquals($url, 'http://testres.qiniudn.com/gogopher.jpg?imageView2/2/h/200');
$fops = array('imageView2/2/h/200', 'imageInfo');
$url = $fop->buildUrl('gogopher.jpg', $fops);
$this->assertEquals($url, 'http://testres.qiniudn.com/gogopher.jpg?imageView2/2/h/200|imageInfo');
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Qiniu\Tests;
use Qiniu\Storage\FormUploader;
use Qiniu\Storage\UploadManager;
use Qiniu\Config;
class FormUpTest extends \PHPUnit_Framework_TestCase
{
protected $bucketName;
protected $auth;
protected $cfg;
protected function setUp()
{
global $bucketName;
$this->bucketName = $bucketName;
global $testAuth;
$this->auth = $testAuth;
$this->cfg = new Config();
}
public function testData()
{
$token = $this->auth->uploadToken($this->bucketName);
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', null);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
}
public function testData2()
{
$upManager = new UploadManager();
$token = $this->auth->uploadToken($this->bucketName);
list($ret, $error) = $upManager->put($token, 'formput', 'hello world', null, 'text/plain', null);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
}
public function testDataFailed()
{
$token = $this->auth->uploadToken('fakebucket');
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', null);
$this->assertNull($ret);
$this->assertNotNull($error);
}
public function testFile()
{
$key = 'formPutFile';
$token = $this->auth->uploadToken($this->bucketName, $key);
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', null);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
}
public function testFile2()
{
$key = 'formPutFile';
$token = $this->auth->uploadToken($this->bucketName, $key);
$upManager = new UploadManager();
list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain', null);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
}
public function testFileFailed()
{
$key = 'fakekey';
$token = $this->auth->uploadToken('fakebucket', $key);
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', null);
$this->assertNull($ret);
$this->assertNotNull($error);
}
}

View File

@@ -0,0 +1,167 @@
<?php
namespace Qiniu\Tests;
use Qiniu\Http\Header;
class HeaderTest extends \PHPUnit_Framework_TestCase
{
protected $heads = array(
':status' => array('200'),
':x-test-1' => array('hello1'),
':x-Test-2' => array('hello2'),
'content-type' => array('application/json'),
'CONTENT-LENGTH' => array(1234),
'oRiGin' => array('https://www.qiniu.com'),
'ReFer' => array('www.qiniu.com'),
'Last-Modified' => array('Mon, 06 Sep 2021 06:44:52 GMT'),
'acCePt-ChArsEt' => array('utf-8'),
'x-test-3' => array('hello3'),
'cache-control' => array('no-cache', 'no-store'),
);
public function testNormalizeKey()
{
$except = array(
':status',
':x-test-1',
':x-Test-2',
'Content-Type',
'Content-Length',
'Origin',
'Refer',
'Last-Modified',
'Accept-Charset',
'X-Test-3',
'Cache-Control'
);
$actual = array_map(function ($str) {
return Header::normalizeKey($str);
}, array_keys($this->heads));
$this->assertEquals($actual, $except);
}
public function testInvalidKeyName()
{
$except = array(
'a:x-test-1',
);
$actual = array_map(function ($str) {
return Header::normalizeKey($str);
}, $except);
$this->assertEquals($except, $actual);
}
public function testGetRawData()
{
$header = new Header($this->heads);
foreach ($this->heads as $k => $v) {
$rawHeader = $header->getRawData();
$this->assertEquals($v, $rawHeader[Header::normalizeKey($k)]);
}
}
public function testOffsetExists()
{
$header = new Header($this->heads);
foreach (array_keys($this->heads) as $k) {
$this->assertNotNull($header[$k]);
}
$except = array(
':status',
':x-test-1',
':x-Test-2',
'Content-Type',
'Content-Length',
'Origin',
'Refer',
'Last-Modified',
'Accept-Charset',
'X-Test-3',
'Cache-Control'
);
foreach ($except as $k) {
$this->assertNotNull($header[$k], $k." is null");
}
}
public function testOffsetGet()
{
$header = new Header($this->heads);
foreach ($this->heads as $k => $v) {
$this->assertEquals($v[0], $header[$k]);
}
$this->assertNull($header['no-exist']);
}
public function testOffsetSet()
{
$header = new Header($this->heads);
$header["X-Test-3"] = "hello";
$this->assertEquals("hello", $header["X-Test-3"]);
$header["x-test-3"] = "hello test3";
$this->assertEquals("hello test3", $header["x-test-3"]);
$header[":x-Test-2"] = "hello";
$this->assertEquals("hello", $header[":x-Test-2"]);
$header[":x-test-2"] = "hello test2";
$this->assertEquals("hello", $header[":x-Test-2"]);
}
public function testOffsetUnset()
{
$header = new Header($this->heads);
unset($header["X-Test-3"]);
$this->assertFalse(isset($header["X-Test-3"]));
$header = new Header($this->heads);
unset($header["x-test-3"]);
$this->assertFalse(isset($header["x-test-3"]));
$header = new Header($this->heads);
unset($header[":x-test-2"]);
$this->assertTrue(isset($header[":x-Test-2"]));
$header = new Header($this->heads);
unset($header[":x-Test-2"]);
$this->assertFalse(isset($header[":x-Test-2"]));
}
public function testGetIterator()
{
$header = new Header($this->heads);
$hasException = false;
try {
foreach ($header as $k => $v) {
$hasException = !isset($header[$k]);
}
} catch (\Exception $e) {
$hasException = true;
}
$this->assertFalse($hasException);
}
public function testCount()
{
$header = new Header($this->heads);
$this->assertEquals(count($this->heads), count($header));
}
public function testFromRaw()
{
$lines = array();
foreach ($this->heads as $k => $vs) {
foreach ($vs as $v) {
array_push($lines, $k . ": " . $v);
}
}
$raw = implode("\r\n", $lines);
$headerFromRaw = Header::fromRawText($raw);
$this->assertEquals(new Header($this->heads), $headerFromRaw);
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace Qiniu\Tests;
use Qiniu\Http\Client;
class HttpTest extends \PHPUnit_Framework_TestCase
{
public function testGet()
{
$response = Client::get('qiniu.com');
$this->assertEquals($response->statusCode, 200);
$this->assertNotNull($response->body);
$this->assertNull($response->error);
}
public function testGetQiniu()
{
$response = Client::get('upload.qiniu.com');
$this->assertEquals(405, $response->statusCode);
$this->assertNotNull($response->body);
$this->assertNotNull($response->xReqId());
$this->assertNotNull($response->xLog());
$this->assertNotNull($response->error);
}
public function testDelete()
{
$response = Client::delete('uc.qbox.me/bucketTagging', array());
$this->assertEquals($response->statusCode, 401);
$this->assertNotNull($response->body);
$this->assertNotNull($response->error);
}
public function testDeleteQiniu()
{
$response = Client::delete('uc.qbox.me/bucketTagging', array());
$this->assertEquals(401, $response->statusCode);
$this->assertNotNull($response->body);
$this->assertNotNull($response->xReqId());
$this->assertNotNull($response->xLog());
$this->assertNotNull($response->error);
}
public function testPost()
{
$response = Client::post('qiniu.com', null);
$this->assertEquals($response->statusCode, 200);
$this->assertNotNull($response->body);
$this->assertNull($response->error);
}
public function testPostQiniu()
{
$response = Client::post('upload.qiniu.com', null);
$this->assertEquals($response->statusCode, 400);
$this->assertNotNull($response->body);
$this->assertNotNull($response->xReqId());
$this->assertNotNull($response->xLog());
$this->assertNotNull($response->error);
}
public function testPut()
{
$response = Client::PUT('uc.qbox.me/bucketTagging', null);
$this->assertEquals($response->statusCode, 401);
$this->assertNotNull($response->body);
$this->assertNotNull($response->error);
}
public function testPutQiniu()
{
$response = Client::put('uc.qbox.me/bucketTagging', null);
$this->assertEquals(401, $response->statusCode);
$this->assertNotNull($response->body);
$this->assertNotNull($response->xReqId());
$this->assertNotNull($response->xLog());
$this->assertNotNull($response->error);
}
}

View File

@@ -0,0 +1,261 @@
<?php
namespace Qiniu\Tests;
/**
* imageprocess test
*
* @package Qiniu
* @subpackage test
* @author Sherlock Ren <sherlock_ren@icloud.com>
*/
class ImageUrlBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* 缩略图测试
*
* @test
* @return void
* @author Sherlock Ren <sherlock_ren@icloud.com>
*/
public function testThumbutl()
{
$imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
$url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
$url2 = $url . '?watermark/1/gravity/SouthEast/dx/0/dy/0/image/'
. 'aHR0cDovL2Fkcy1jZG4uY2h1Y2h1amllLmNvbS9Ga1R6bnpIY2RLdmRBUFc5cHZZZ3pTc21UY0tB';
// 异常测试
$this->assertEquals($url, $imageUrlBuilder->thumbnail($url, 1, 0, 0));
$this->assertEquals($url, \Qiniu\thumbnail($url, 1, 0, 0));
// 简单缩略测试
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/ignore-error/1/',
$imageUrlBuilder->thumbnail($url, 1, 200, 200)
);
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/ignore-error/1/',
\Qiniu\thumbnail($url, 1, 200, 200)
);
// 输出格式测试
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
$imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png')
);
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
\Qiniu\thumbnail($url, 1, 200, 200, 'png')
);
// 渐进显示测试
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/format/png/interlace/1/ignore-error/1/',
$imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png', 1)
);
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/',
\Qiniu\thumbnail($url, 1, 200, 200, 'png', 2)
);
// 图片质量测试
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/format/png/interlace/1/q/80/ignore-error/1/',
$imageUrlBuilder->thumbnail($url, 1, 200, 200, 'png', 1, 80)
);
$this->assertEquals(
$url . '?imageView2/1/w/200/h/200/format/png/interlace/1/ignore-error/1/',
\Qiniu\thumbnail($url, 1, 200, 200, 'png', 1, 101)
);
// 多参数测试
$this->assertEquals(
$url2 . '|imageView2/1/w/200/h/200/ignore-error/1/',
$imageUrlBuilder->thumbnail($url2, 1, 200, 200)
);
$this->assertEquals(
$url2 . '|imageView2/1/w/200/h/200/ignore-error/1/',
\Qiniu\thumbnail($url2, 1, 200, 200)
);
}
/**
* 图片水印测试
*
* @test
* @param void
* @return void
* @author Sherlock Ren <sherlock_ren@icloud.com>
*/
public function waterImgTest()
{
$imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
$url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
$url2 = $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/';
$image = 'http://developer.qiniu.com/resource/logo-2.jpg';
// 水印简单测试
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/',
$imageUrlBuilder->waterImg($url, $image)
);
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/gravity/SouthEast/',
$imageUrlBuilder->waterImg($url, $image, 101)
);
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw==/',
$imageUrlBuilder->waterImg($url, $image, 101, 'sdfsd')
);
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/',
\Qiniu\waterImg($url, $image)
);
// 横轴边距测试
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/dx/10/',
$imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10)
);
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/',
\Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad')
);
// 纵轴边距测试
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/dx/10/dy/10/',
$imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10, 10)
);
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/',
\Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad', 'asdf')
);
// 自适应原图的短边比例测试
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/dx/10/dy/10/ws/0.5/',
$imageUrlBuilder->waterImg($url, $image, 100, 'SouthEast', 10, 10, 0.5)
);
$this->assertEquals(
$url . '?watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/',
\Qiniu\waterImg($url, $image, 100, 'SouthEast', 'sad', 'asdf', 2)
);
// 多参数测试
$this->assertEquals(
$url2 . '|watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/',
$imageUrlBuilder->waterImg($url2, $image)
);
$this->assertEquals(
$url2 . '|watermark/1/image/aHR0cDovL2RldmVsb3Blci5xaW5pdS5jb20vcmVzb3VyY2UvbG9nby0yLmpwZw=='
. '/dissolve/100/gravity/SouthEast/',
\Qiniu\waterImg($url2, $image)
);
}
/**
* 文字水印测试
*
* @test
* @param void
* @return void
* @author Sherlock Ren <sherlock_ren@icloud.com>
*/
public function waterTextTest()
{
$imageUrlBuilder = new \Qiniu\Processing\ImageUrlBuilder();
$url = 'http://78re52.com1.z0.glb.clouddn.com/resource/gogopher.jpg';
$url2 = $url . '?imageView2/1/w/200/h/200/format/png/ignore-error/1/';
$text = '测试一下';
$font = '微软雅黑';
$fontColor = '#FF0000';
// 水印简单测试
$this->assertEquals($url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
. 'fontsize/500/dissolve/100/gravity/SouthEast/', $imageUrlBuilder->waterText($url, $text, $font, 500));
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
. 'dissolve/100/gravity/SouthEast/',
\Qiniu\waterText($url, $text, $font, 'sdf')
);
// 字体颜色测试
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/'
. 'I0ZGMDAwMA==/dissolve/100/gravity/SouthEast/',
$imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor)
);
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA=='
. '/dissolve/100/gravity/SouthEast/',
\Qiniu\waterText($url, $text, $font, 'sdf', $fontColor)
);
// 透明度测试
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
. '/dissolve/80/gravity/SouthEast/',
$imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80)
);
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA=='
. '/gravity/SouthEast/',
\Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101)
);
// 水印位置测试
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
. '/dissolve/80/gravity/East/',
$imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East')
);
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
\Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf')
);
// 横轴距离测试
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
. '/dissolve/80/gravity/East/dx/10/',
$imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East', 10)
);
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
\Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf', 'sdfs')
);
// 纵轴距离测试
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fontsize/500/fill/I0ZGMDAwMA=='
. '/dissolve/80/gravity/East/dx/10/dy/10/',
$imageUrlBuilder->waterText($url, $text, $font, 500, $fontColor, 80, 'East', 10, 10)
);
$this->assertEquals(
$url . '?watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/fill/I0ZGMDAwMA==/',
\Qiniu\waterText($url, $text, $font, 'sdf', $fontColor, 101, 'sdfsdf', 'sdfs', 'ssdf')
);
// 多参数测试
$this->assertEquals(
$url2 . '|watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
. 'fontsize/500/dissolve/100/gravity/SouthEast/',
$imageUrlBuilder->waterText($url2, $text, $font, 500)
);
$this->assertEquals(
$url2 . '|watermark/2/text/5rWL6K-V5LiA5LiL/font/5b6u6L2v6ZuF6buR/'
. 'fontsize/500/dissolve/100/gravity/SouthEast/',
\Qiniu\waterText($url2, $text, $font, 500)
);
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Qiniu\Tests;
use Qiniu\Processing\Operation;
use Qiniu\Processing\PersistentFop;
class PfopTest extends \PHPUnit_Framework_TestCase
{
public function testPfop()
{
global $testAuth;
$bucket = 'testres';
$key = 'sintel_trailer.mp4';
$pfop = new PersistentFop($testAuth, null);
$fops = 'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240';
list($id, $error) = $pfop->execute($bucket, $key, $fops);
$this->assertNull($error);
list($status, $error) = $pfop->status($id);
$this->assertNotNull($status);
$this->assertNull($error);
}
public function testPfops()
{
global $testAuth;
$bucket = 'testres';
$key = 'sintel_trailer.mp4';
$fops = array(
'avthumb/m3u8/segtime/10/vcodec/libx264/s/320x240',
'vframe/jpg/offset/7/w/480/h/360',
);
$pfop = new PersistentFop($testAuth, null);
list($id, $error) = $pfop->execute($bucket, $key, $fops);
$this->assertNull($error);
list($status, $error) = $pfop->status($id);
$this->assertNotNull($status);
$this->assertNull($error);
}
public function testMkzip()
{
global $testAuth;
$bucket = 'phpsdk';
$key = 'php-logo.png';
$pfop = new PersistentFop($testAuth, null);
$url1 = 'http://phpsdk.qiniudn.com/php-logo.png';
$url2 = 'http://phpsdk.qiniudn.com/php-sdk.html';
$zipKey = 'test.zip';
$fops = 'mkzip/2/url/' . \Qiniu\base64_urlSafeEncode($url1);
$fops .= '/url/' . \Qiniu\base64_urlSafeEncode($url2);
$fops .= '|saveas/' . \Qiniu\base64_urlSafeEncode("$bucket:$zipKey");
list($id, $error) = $pfop->execute($bucket, $key, $fops);
$this->assertNull($error);
list($status, $error) = $pfop->status($id);
$this->assertNotNull($status);
$this->assertNull($error);
}
}

View File

@@ -0,0 +1,312 @@
<?php
namespace Qiniu\Tests;
use phpDocumentor\Reflection\DocBlock\Tags\Version;
use Qiniu\Region;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\ResumeUploader;
use Qiniu\Storage\UploadManager;
use Qiniu\Http\Client;
use Qiniu\Config;
use Qiniu\Zone;
class ResumeUpTest extends \PHPUnit_Framework_TestCase
{
private static $keyToDelete = array();
public static function tearDownAfterClass()
{
global $bucketName;
global $testAuth;
$config = new Config();
$bucketManager = new BucketManager($testAuth, $config);
foreach (self::$keyToDelete as $key) {
$bucketManager->delete($bucketName, $key);
}
}
protected $bucketName;
protected $auth;
protected function setUp()
{
global $bucketName;
$this->bucketName = $bucketName;
global $testAuth;
$this->auth = $testAuth;
}
public function test4ML()
{
$key = "resumePutFile4ML_".rand();
$upManager = new UploadManager();
$token = $this->auth->uploadToken($this->bucketName, $key);
$tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
$resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
$this->assertNotFalse($resumeFile);
list($ret, $error) = $upManager->putFile(
$token,
$key,
$tempFile,
null,
'application/octet-stream',
false,
$resumeFile
);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
unlink($resumeFile);
$domain = getenv('QINIU_TEST_DOMAIN');
$response = Client::get("http://$domain/$key");
$this->assertEquals(200, $response->statusCode);
$this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
unlink($tempFile);
}
public function test4ML2()
{
$key = 'resumePutFile4ML_'.rand();
$zone = new Zone(array('upload.fake.qiniu.com'), array('upload.qiniup.com'));
$cfg = new Config($zone);
$upManager = new UploadManager($cfg);
$token = $this->auth->uploadToken($this->bucketName, $key);
$tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
$resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
$this->assertNotFalse($resumeFile);
list($ret, $error) = $upManager->putFile(
$token,
$key,
$tempFile,
null,
'application/octet-stream',
false,
$resumeFile
);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
unlink($resumeFile);
$domain = getenv('QINIU_TEST_DOMAIN');
$response = Client::get("http://$domain/$key");
$this->assertEquals(200, $response->statusCode);
$this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
unlink($tempFile);
}
// public function test8M()
// {
// $key = 'resumePutFile8M';
// $upManager = new UploadManager();
// $token = $this->auth->uploadToken($this->bucketName, $key);
// $tempFile = qiniuTempFile(8*1024*1024+10);
// list($ret, $error) = $upManager->putFile($token, $key, $tempFile);
// $this->assertNull($error);
// $this->assertNotNull($ret['hash']);
// unlink($tempFile);
// }
public function testFileWithFileType()
{
$config = new Config();
$bucketManager = new BucketManager($this->auth, $config);
$testCases = array(
array(
"fileType" => 1,
"name" => "IA"
),
array(
"fileType" => 2,
"name" => "Archive"
),
array(
"fileType" => 3,
"name" => "DeepArchive"
)
);
foreach ($testCases as $testCase) {
$key = 'FileType'.$testCase["name"].rand();
$police = array(
"fileType" => $testCase["fileType"],
);
$token = $this->auth->uploadToken($this->bucketName, $key, 3600, $police);
$upManager = new UploadManager();
list($ret, $error) = $upManager->putFile($token, $key, __file__, null, 'text/plain');
$this->assertNull($error);
$this->assertNotNull($ret);
array_push(self::$keyToDelete, $key);
list($ret, $err) = $bucketManager->stat($this->bucketName, $key);
$this->assertNull($err);
$this->assertEquals($testCase["fileType"], $ret["type"]);
}
}
public function testResumeUploadWithParams()
{
$key = "resumePutFile4ML_".rand();
$upManager = new UploadManager();
$policy = array('returnBody' => '{"hash":$(etag),"fname":$(fname),"var_1":$(x:var_1),"var_2":$(x:var_2)}');
$token = $this->auth->uploadToken($this->bucketName, $key, 3600, $policy);
$tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
$resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
$this->assertNotFalse($resumeFile);
list($ret, $error) = $upManager->putFile(
$token,
$key,
$tempFile,
["x:var_1" => "val_1", "x:var_2" => "val_2", "x-qn-meta-m1" => "val_1", "x-qn-meta-m2" => "val_2"],
'application/octet-stream',
false,
$resumeFile
);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
$this->assertEquals("val_1", $ret['var_1']);
$this->assertEquals("val_2", $ret['var_2']);
$this->assertEquals(basename($tempFile), $ret['fname']);
unlink($resumeFile);
$domain = getenv('QINIU_TEST_DOMAIN');
$response = Client::get("http://$domain/$key");
$this->assertEquals(200, $response->statusCode);
$this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
$this->assertEquals("val_1", $response->headers()["X-Qn-Meta-M1"]);
$this->assertEquals("val_2", $response->headers()["X-Qn-Meta-M2"]);
unlink($tempFile);
}
public function testResumeUploadV2()
{
$zone = new Zone(array('up.qiniup.com'));
$cfg = new Config($zone);
$upManager = new UploadManager($cfg);
$testFileSize = array(
config::BLOCK_SIZE / 2,
config::BLOCK_SIZE,
config::BLOCK_SIZE + 10,
config::BLOCK_SIZE * 2,
config::BLOCK_SIZE * 2.5
);
$partSize = 5 * 1024 * 1024;
foreach ($testFileSize as $item) {
$key = 'resumePutFile4ML_'.rand()."_";
$token = $this->auth->uploadToken($this->bucketName, $key);
$tempFile = qiniuTempFile($item);
$resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
$this->assertNotFalse($resumeFile);
list($ret, $error) = $upManager->putFile(
$token,
$key,
$tempFile,
null,
'application/octet-stream',
false,
$resumeFile,
'v2',
$partSize
);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
unlink($resumeFile);
$domain = getenv('QINIU_TEST_DOMAIN');
$response = Client::get("http://$domain/$key");
$this->assertEquals(200, $response->statusCode);
$this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
unlink($tempFile);
}
}
public function testResumeUploadV2WithParams()
{
$key = "resumePutFile4ML_".rand();
$upManager = new UploadManager();
$policy = array('returnBody' => '{"hash":$(etag),"fname":$(fname),"var_1":$(x:var_1),"var_2":$(x:var_2)}');
$token = $this->auth->uploadToken($this->bucketName, $key, 3600, $policy);
$tempFile = qiniuTempFile(4 * 1024 * 1024 + 10);
$resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
$this->assertNotFalse($resumeFile);
list($ret, $error) = $upManager->putFile(
$token,
$key,
$tempFile,
["x:var_1" => "val_1", "x:var_2" => "val_2", "x-qn-meta-m1" => "val_1", "x-qn-meta-m2" => "val_2"],
'application/octet-stream',
false,
$resumeFile,
'v2'
);
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
$this->assertEquals("val_1", $ret['var_1']);
$this->assertEquals("val_2", $ret['var_2']);
$this->assertEquals(basename($tempFile), $ret['fname']);
unlink($resumeFile);
$domain = getenv('QINIU_TEST_DOMAIN');
$response = Client::get("http://$domain/$key");
$this->assertEquals(200, $response->statusCode);
$this->assertEquals(md5_file($tempFile, true), md5($response->body(), true));
$this->assertEquals("val_1", $response->headers()["X-Qn-Meta-M1"]);
$this->assertEquals("val_2", $response->headers()["X-Qn-Meta-M2"]);
unlink($tempFile);
}
// valid versions are tested above
// Use PHPUnit's Data Provider to test multiple Exception is better,
// but not match the test style of this project
public function testResumeUploadWithInvalidVersion()
{
$zone = new Zone(array('up.qiniup.com'));
$cfg = new Config($zone);
$upManager = new UploadManager($cfg);
$testFileSize = config::BLOCK_SIZE * 2;
$partSize = 5 * 1024 * 1024;
$testInvalidVersions = array(
// High probability invalid versions
'v',
'1',
'2'
);
$expectExceptionCount = 0;
foreach ($testInvalidVersions as $invalidVersion) {
$key = 'resumePutFile4ML_'.rand()."_";
$token = $this->auth->uploadToken($this->bucketName, $key);
$tempFile = qiniuTempFile($testFileSize);
$resumeFile = tempnam(sys_get_temp_dir(), 'resume_file');
$this->assertNotFalse($resumeFile);
try {
$upManager->putFile(
$token,
$key,
$tempFile,
null,
'application/octet-stream',
false,
$resumeFile,
$invalidVersion,
$partSize
);
} catch (\Exception $e) {
$isRightException = false;
$expectExceptionCount++;
while ($e) {
$isRightException = $e instanceof \UnexpectedValueException;
if ($isRightException) {
break;
}
$e = $e->getPrevious();
}
$this->assertTrue($isRightException);
}
unlink($resumeFile);
unlink($tempFile);
}
$this->assertEquals(count($testInvalidVersions), $expectExceptionCount);
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Qiniu\Tests;
use Qiniu;
use Qiniu\Zone;
class ZoneTest extends \PHPUnit_Framework_TestCase
{
protected $zone;
protected $zoneHttps;
protected $ak;
protected $bucketName;
protected $bucketNameBC;
protected $bucketNameNA;
protected $bucketNameFS;
protected $bucketNameAS;
protected function setUp()
{
global $bucketName;
$this->bucketName = $bucketName;
global $bucketNameBC;
$this->bucketNameBC = $bucketNameBC;
global $bucketNameNA;
$this->bucketNameNA = $bucketNameNA;
global $bucketNameFS;
$this->bucketNameFS = $bucketNameFS;
global $bucketNameAS;
$this->bucketNameAS = $bucketNameAS;
global $accessKey;
$this->ak = $accessKey;
$this->zone = new Zone();
$this->zoneHttps = new Zone('https');
}
public function testUpHosts()
{
list($ret, $err) = Zone::queryZone($this->ak, 'fakebucket');
$this->assertNull($ret);
$this->assertNotNull($err);
$zone = Zone::queryZone($this->ak, $this->bucketName);
$this->assertContains('upload.qiniup.com', $zone->cdnUpHosts);
$zone = Zone::queryZone($this->ak, $this->bucketNameBC);
$this->assertContains('upload-z1.qiniup.com', $zone->cdnUpHosts);
$zone = Zone::queryZone($this->ak, $this->bucketNameFS);
$this->assertContains('upload-z2.qiniup.com', $zone->cdnUpHosts);
$zone = Zone::queryZone($this->ak, $this->bucketNameNA);
$this->assertContains('upload-na0.qiniup.com', $zone->cdnUpHosts);
$zone = Zone::queryZone($this->ak, $this->bucketNameAS);
$this->assertContains('upload-as0.qiniup.com', $zone->cdnUpHosts);
}
public function testIoHosts()
{
$zone = Zone::queryZone($this->ak, $this->bucketName);
$this->assertEquals($zone->iovipHost, 'iovip.qbox.me');
$zone = Zone::queryZone($this->ak, $this->bucketNameBC);
$this->assertEquals($zone->iovipHost, 'iovip-z1.qbox.me');
$zone = Zone::queryZone($this->ak, $this->bucketNameFS);
$this->assertEquals($zone->iovipHost, 'iovip-z2.qbox.me');
$zone = Zone::queryZone($this->ak, $this->bucketNameNA);
$this->assertEquals($zone->iovipHost, 'iovip-na0.qbox.me');
$zone = Zone::queryZone($this->ak, $this->bucketNameAS);
$this->assertEquals($zone->iovipHost, 'iovip-as0.qbox.me');
}
public function testZonez0()
{
$zone = Zone::zonez0();
$this->assertContains('upload.qiniup.com', $zone->cdnUpHosts);
}
public function testZonez1()
{
$zone = Zone::zonez1();
$this->assertContains('upload-z1.qiniup.com', $zone->cdnUpHosts);
}
public function testZonez2()
{
$zone = Zone::zonez2();
$this->assertContains('upload-z2.qiniup.com', $zone->cdnUpHosts);
}
public function testZoneNa0()
{
$zone = Zone::zoneNa0();
$this->assertContains('upload-na0.qiniup.com', $zone->cdnUpHosts);
}
public function testZoneAs0()
{
$zone = Zone::zoneAs0();
$this->assertContains('upload-as0.qiniup.com', $zone->cdnUpHosts);
}
public function testQvmZonez0()
{
$zone = Zone::qvmZonez0();
$this->assertContains('free-qvm-z0-xs.qiniup.com', $zone->srcUpHosts);
}
public function testQvmZonez1()
{
$zone = Zone::qvmZonez1();
$this->assertContains('free-qvm-z1-zz.qiniup.com', $zone->srcUpHosts);
}
}

61
vendor/qiniu/php-sdk/tests/bootstrap.php vendored Executable file
View File

@@ -0,0 +1,61 @@
<?php
// @codingStandardsIgnoreFile
require_once __DIR__ . '/../autoload.php';
use Qiniu\Auth;
$accessKey = getenv('QINIU_ACCESS_KEY');
$secretKey = getenv('QINIU_SECRET_KEY');
$testAuth = new Auth($accessKey, $secretKey);
$bucketName = getenv('QINIU_TEST_BUCKET');
$key = 'php-logo.png';
$key2 = 'niu.jpg';
$testStartDate = '2020-08-18';
$testEndDate = '2020-08-19';
$testGranularity = 'day';
$testLogDate = date('Y-m-d',strtotime("-1 days"));
$bucketNameBC = 'phpsdk-bc';
$bucketNameNA = 'phpsdk-na';
$bucketNameFS = 'phpsdk-fs';
$bucketNameAS = 'phpsdk-as';
$dummyAccessKey = 'abcdefghklmnopq';
$dummySecretKey = '1234567890';
$dummyAuth = new Auth($dummyAccessKey, $dummySecretKey);
//cdn
$timestampAntiLeechEncryptKey = getenv('QINIU_TIMESTAMP_ENCRPTKEY');
$customDomain = "http://sdk.peterpy.cn";
$customDomain2 = "sdk.peterpy.cn";
$customCallbackURL = "https://qiniu.timhbw.com/notify/callback";
$tid = getenv('TRAVIS_JOB_NUMBER');
if (!empty($tid)) {
$pid = getmypid();
$tid = strstr($tid, '.');
$tid .= '.' . $pid;
}
function qiniuTempFile($size, $randomized = true)
{
$fileName = tempnam(sys_get_temp_dir(), 'qiniu_');
$file = fopen($fileName, 'wb');
if ($randomized) {
$rest_size = $size;
while ($rest_size > 0) {
$length = min($rest_size, 4 * 1024);
if (fwrite($file, random_bytes($length)) == false) {
return false;
}
$rest_size -= $length;
}
} else if ($size > 0) {
fseek($file, $size - 1);
fwrite($file, ' ');
}
fclose($file);
return $fileName;
}