diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 00000000..850cc597 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,2 @@ +coverage_clover: coverage.xml +json_path: coverage.json diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..b09d0a53 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +language: php +php: + - 5.6 + - 5.5 + - 5.4 + - 5.3 +install: + - composer self-update + - composer install --no-interaction +script: + - php vendor/bin/phpunit +after_success: + - php vendor/bin/coveralls -v +env: + global: + - secure: SzmQ854lQmhV6ZkAG7lQNTY3CkazrXnDSb6VMwPU/sdaLGxPO159AW3fJS5d0sO/XN1P8x5WNkoA4i9soDlLBRibEEISNUM/2EMnpszsRymZ9o97PrS2IgORXTUL/OF+rpATzyNVB2p+2l9hBLiGf17exMSA5iOeY7W6E+VKPGi8TFykgbGUnLKU0h1hV3rzmtfGjOXcSpvYU/hxeZD/J/+6m5Gic9b/pNS+AbfTj7Y7Ru9tNsnyUP29V/vtEYtpQir3ZxQiSiUv9idybgGnJBOMYydJofb/mpFYHhYLSWqtMKGNLpeawmqs4z8S1Tvx5U5uzW5+h/mpzhvBaFlWGpm8t89BQxun5LVX5NiYCrV7TqaLitGp1cSpMjMDnrnSTNzk1exVz+rWZZcWS7yB9ULYA681GA8StXWk167qB7Y30iK1dFK3+2mDN2cEY+qLs8+bupDowQ4eOM+eqfhxX8F8+ouKcKomETsjiIwL+CUsIe6wjvnYFWb1YlRhbsI75bblHApflohnt6gVSJ78ZPqID+u2oUMjmIWXLTnRR2Y2tgEW8uqHeIoQ8BBntLdQDmv0BO4FpnGQIwrUUwQYeNzEM0DOr3hWZhyDR6Xvl+9H0l52xjANaSqpuTZfC3zmeFTG7kIjydvxNePRrony6XAawL9BvI7aKWuVF6YVjPM= + - secure: nEhsU8aUQqsAJeuger+boh51oTpeo4YNG7vUWbKxdwVDIrcLb+l7r7RvTlxU7mt8IZTWwicgri18mh+Wi04BwX4ulBA1SCs8jPbL51KEo5izoDGGtLSd2fuPHdslYSrwagrvq90EPnDT/7fHWn/TAoT+rueZzjNyCu5IGSgL3GnXaUThsJ82NMePL2YRdP4Q1qmtZPRFBOkOQ6F0heuV8fw8sLyTO3txaCQum9YneGxrWxOl/E8zB0qtlnPwLE8ogaHZMQh2/jThmTbI5UqwRTxV4f0qoD5eJYH+j0fslsSAjsg/HPnSuVcnccK3zSU+s2sV4dPCcISzECJvZEObwipfxOGhdqt5gMcxHhn8qVsbT97iIh106pG/BJCDgQd2EeVW8WfCi6cCuCKIMipvVkMypkmjQHWU1XaqPzILl7g5diW9Ctp2C4Akq5dYdrdu8IrnVK1ShtkQVaWU+S/Bht8VU5gYP7olPW/GdTz7sceU1NOIC4NPXqmWKbfavR98U5dkHMLMvzABYL1Q87h+KhPD1c14NUyw3YENUW7REiF/X5lERRm5H0kJ/1JqAa+AgeHQEGmPVuZV2s/na4b0S1479QRVmSM/6ZzXQpU+Y8jCRfETpUFA4S331369kirHgCqDlxyIntuEKrzivD02/O+5C3eJ0WHRz6QsN2/R4qg= + - secure: ZTvzNXEZP4efl+a/3VGMmdabfUQp83v5/lecMns039Ro7UuZYPdtbPtpPnpjaTI6Htd22A4Rva5BU/3JCQJAyQvpbKNn5sGou2SmfQu3o0SyhggSB7gWjYAf707aW1j4bHYfP8IjDS5NjuVk3AqXeNSUuLRUXRmwSOB0lSYiRhiTJY+pUdBl382Hx4NbhIU/gmOzRoJCs7coTip8IURXYEHPi5dnDWluajxI+TgNXFccSgEleeQDJajYgXmpLb2EhSj8piipOnVgaCEE5bh5fbp32024Qq38SGHKcbfnwj2IInpZpZESJknRKoqAlFjdOJhork82dBcvAr5JxCBZKx5IuwXcTjxkQ6tRtBeqhPLPFuX3MQ8WrtU+wniPM0RCH/VoFkUKO7JGQDwmoi2AKago4PsuDs4P6Y6CeuOVpcso731GwwMNhIJcyrJJivXprQNEGsEw+9wLjU1qNYs6IIA3S/gPzFrNbdX5Wf8vxt0vLpgYvBNtPnLMejMtknuyfVzf5iKuVVoGPDTEz+ajs06+jfoPfm/4sLTaLghuVH7Adm74OpF769JQNnQYKwJuu4bNlcbLJChulCEMBOx7myqo/9O6RCTuqzHaGmVWNot4RGqRFHgJGl/JJf0WpAVitbhbRH3kGoyKb6jFM74CJbPsE7OORlJLDC3cdD3C8Pk= + - secure: Qr5NR4CVzBKCQgRgMH0x772TPJ1+brx3UCvtRNu8fi4j3p8bz+HDMjBaBDSFmEB09nunLI55/8mj88/5GXmnpFs8+CPTkcW+QZOcxg3cxpI4SNmxoB12/ZawlFHAqSUaRRE7RUWVkY3KL8tIGjEZcFyUBQ1DVNX3OMpiKs3NLtHa7oUIknyBxdSokm4kpLhSXYe7WmO0vhuZbMZE0S1EISToiBS6AdhGUEbTLJ/vNsIDY07fu6+Vh3HxVbyUFPqUZGlkZpQ+2xMJ3kiqPBMrXtRF/IhhPjORDil6Ns9SQ8/AAlaCddvYvRaT4Pjv2/aX+t3l28qI1qmryPtWXpce5UXecWGYqdRpSJc6Y/pEt4m4FeeGoEFWnSPGIs7FRmeiis8q2rojGZ18i4vI/k4iHmqEBnTlMp3SWnRb9L1adJ8ZAWln8aC88gkQXm67w7+1CxLycerbYj9H1ugqHENuHcxv4uHUcZgEENX3EWatu8i9+K2IUuU/2zcmpu7qtsziYcoyW8DOOmYpJfXGMLtmF9+pqp/Tp6i0tltFSEfmY3N8o7xvv3enLvFHsjL+3ElFdd1blUPSrvZJHgA9M3lJ+QF1RJZCpJqgPlQ0XOZK1Bf4P46zpEj01wKaK4JQrkLPRXhbBOuIJn5O6WlFJyPX4+SaBfwTzb4AvM4aUg2TgTg= diff --git a/README.md b/README.md index 641773f8..b6be984a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # Aliyun OSS SDK for PHP +[![Latest Stable Version](https://poser.pugx.org/aliyuncs/oss-sdk-php/v/stable)](https://packagist.org/packages/aliyuncs/oss-sdk-php) +[![Build Status](https://travis-ci.org/aliyun/aliyun-oss-php-sdk.svg?branch=master)](https://travis-ci.org/aliyun/aliyun-oss-php-sdk) +[![Coverage Status](https://coveralls.io/repos/github/aliyun/aliyun-oss-php-sdk/badge.svg?branch=master)](https://coveralls.io/github/aliyun/aliyun-oss-php-sdk?branch=master) + ## 概述 阿里云对象存储(Object Storage Service,简称OSS),是阿里云对外提供的海量、安全、低成本、高可靠的云存储服务。用户可以通过调用API,在任何应用、任何时间、任何地点上传和下载数据,也可以通过用户Web控制台对数据进行简单的管理。OSS适合存放任意文件类型,适合各种网站、开发企业及开发者使用。 @@ -57,9 +61,9 @@ $accessKeyId = "<您从OSS获得的AccessKeyId>"; ; $accessKeySecret = "<您从OSS获得的AccessKeySecret>"; $endpoint = "<您选定的OSS数据中心访问域名,例如oss-cn-hangzhou.aliyuncs.com>"; try { - $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); + $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); } catch (OssException $e) { - print $e->getMessage(); + print $e->getMessage(); } ``` @@ -73,26 +77,26 @@ $bucket = "<您使用的Bucket名字,注意命名规范>"; $object = "<您使用的Object名字,注意命名规范>"; $content = "Hello, OSS!"; // 上传的文件内容 try { - $ossClient->putObject($bucket, $object, $content); + $ossClient->putObject($bucket, $object, $content); } catch (OssException $e) { - print $e->getMessage(); -} + print $e->getMessage(); +} ``` ### 存储空间操作 存储空间(又称Bucket)是一个用户用来管理所存储Object的存储空间,对于用户来说是一个管理Object的单元,所有的Object都必须隶属于某个Bucket。您可以按照下面的代码新建一个Bucket: - + ```php "; try { - $ossClient->createBucket($bucket); + $ossClient->createBucket($bucket); } catch (OssException $e) { - print $e->getMessage(); + print $e->getMessage(); } ``` - + ### 返回结果处理 OssClient提供的接口返回返回数据分为两种: @@ -105,14 +109,14 @@ OssClient提供的接口返回返回数据分为两种: $bucketListInfo = $ossClient->listBuckets(); $bucketList = $bucketListInfo->getBucketList(); foreach($bucketList as $bucket) { - print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n"); + print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n"); } ``` 上面代码中的$bucketListInfo的数据类型是 `OSS\Model\BucketListInfo` - + ### 运行Sample程序 - + 1. 修改 `samples/Config.php`, 补充配置信息 2. 执行 `cd samples/ && php RunAll.php` @@ -120,7 +124,7 @@ foreach($bucketList as $bucket) { 1. 修改 `tests/OSS/Tests/Config.php`, 补充配置信息 2. 执行 `./vendor/bin/phpunit tests/OSS/Tests/` - + ### 生成API文档 1. 到根目录下,执行 `composer install`, 下载依赖的库 diff --git a/composer.json b/composer.json index 367950cb..1ecb58c9 100644 --- a/composer.json +++ b/composer.json @@ -11,12 +11,11 @@ } ], "require": { - "php":">=5.3" + "php":">=5.3" }, "require-dev" : { - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "~2.3", - "phpdocumentor/phpdocumentor" : "2.*" + "phpunit/phpunit": "~4.0", + "satooshi/php-coveralls": "~1.0" }, "minimum-stability": "stable", "autoload": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..41a11a35 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,16 @@ + + + + ./src + + + + + + + + ./tests + ./tests/OSS/Tests/BucketCnameTest.php + + + diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index de4d153e..982297bd 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -443,7 +443,7 @@ public function deleteBucketCors($bucket, $options = NULL) * 为指定Bucket增加CNAME绑定 * * @param string $bucket bucket名称 - * @param CnameConfig $cnameConfig + * @param string $cname * @param array $options * @throws OssException * @return null diff --git a/tests/OSS/Tests/BucketCnameTest.php b/tests/OSS/Tests/BucketCnameTest.php index fbc59e6d..20625db6 100644 --- a/tests/OSS/Tests/BucketCnameTest.php +++ b/tests/OSS/Tests/BucketCnameTest.php @@ -73,6 +73,7 @@ public function testDeleteCname() $ret = $this->client->getBucketCname($this->bucketName); $this->assertEquals(1, count($ret->getCnames())); - $this->assertEquals('www.qq.com', $ret->getCnames()[0]['Domain']); + $cnames = $ret->getCnames(); + $this->assertEquals('www.qq.com', $cnames[0]['Domain']); } } diff --git a/tests/OSS/Tests/Common.php b/tests/OSS/Tests/Common.php index 0c5e6365..454f2f04 100644 --- a/tests/OSS/Tests/Common.php +++ b/tests/OSS/Tests/Common.php @@ -3,7 +3,6 @@ namespace OSS\Tests; require_once __DIR__ . '/../../../autoload.php'; -require_once __DIR__ . '/Config.php'; use OSS\OssClient; use OSS\Core\OssException; @@ -15,11 +14,6 @@ */ class Common { - const endpoint = Config::OSS_ENDPOINT; - const accessKeyId = Config::OSS_ACCESS_ID; - const accessKeySecret = Config::OSS_ACCESS_KEY; - const bucket = Config::OSS_TEST_BUCKET; - /** * 根据Config配置,得到一个OssClient实例 * @@ -28,7 +22,10 @@ class Common public static function getOssClient() { try { - $ossClient = new OssClient(self::accessKeyId, self::accessKeySecret, self::endpoint, false); + $ossClient = new OssClient( + getenv('OSS_ACCESS_KEY_ID'), + getenv('OSS_ACCESS_KEY_SECRET'), + getenv('OSS_ENDPOINT'), false); } catch (OssException $e) { printf(__FUNCTION__ . "creating OssClient instance: FAILED\n"); printf($e->getMessage() . "\n"); @@ -39,7 +36,7 @@ public static function getOssClient() public static function getBucketName() { - return self::bucket; + return getenv('OSS_BUCKET'); } /** @@ -60,4 +57,14 @@ public static function createBucket() } print(__FUNCTION__ . ": OK" . "\n"); } + + /** + * Wait for bucket meta sync + */ + public static function waitMetaSync() + { + if (getenv('TRAVIS')) { + sleep(30); + } + } } diff --git a/tests/OSS/Tests/Config.php b/tests/OSS/Tests/Config.php deleted file mode 100644 index 7c17b275..00000000 --- a/tests/OSS/Tests/Config.php +++ /dev/null @@ -1,16 +0,0 @@ -assertEquals(0, $status); } + private function getContentType($bucket, $object) + { + $client = Common::getOssClient(); + $headers = $client->getObjectMeta($bucket, $object); + return $headers['content-type']; + } + public function testByFileName() { $client = Common::getOssClient(); @@ -28,7 +35,7 @@ public function testByFileName() $this->runCmd('touch ' . $file); $client->uploadFile($bucket, $object, $file); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('text/html', $type); @@ -37,7 +44,7 @@ public function testByFileName() $this->runCmd('dd if=/dev/random of=' . $file . ' bs=1024 count=100'); $client->multiuploadFile($bucket, $object, $file, array('partSize' => 100)); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('application/json', $type); } @@ -49,7 +56,7 @@ public function testByObjectKey() $object = "test/x.txt"; $client->putObject($bucket, $object, "hello world"); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('text/plain', $type); @@ -58,7 +65,7 @@ public function testByObjectKey() $this->runCmd('touch ' . $file); $client->uploadFile($bucket, $object, $file); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('text/html', $type); @@ -67,7 +74,7 @@ public function testByObjectKey() $this->runCmd('touch ' . $file); $client->uploadFile($bucket, $object, $file); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('text/plain', $type); @@ -76,7 +83,7 @@ public function testByObjectKey() $this->runCmd('dd if=/dev/random of=' . $file . ' bs=1024 count=100'); $client->multiuploadFile($bucket, $object, $file, array('partSize' => 100)); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('audio/mpeg', $type); @@ -85,7 +92,7 @@ public function testByObjectKey() $this->runCmd('dd if=/dev/random of=' . $file . ' bs=1024 count=100'); $client->multiuploadFile($bucket, $object, $file, array('partSize' => 100)); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('application/json', $type); } @@ -99,7 +106,7 @@ public function testByUser() $client->putObject($bucket, $object, "hello world", array( 'Content-Type' => 'text/html' )); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('text/html', $type); @@ -110,7 +117,7 @@ public function testByUser() $client->uploadFile($bucket, $object, $file, array( 'Content-Type' => 'application/json' )); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('application/json', $type); @@ -122,7 +129,7 @@ public function testByUser() 'partSize' => 100, 'Content-Type' => 'audio/mpeg' )); - $type = $client->getObjectMeta($bucket, $object)['content-type']; + $type = $this->getContentType($bucket, $object); $this->assertEquals('audio/mpeg', $type); } diff --git a/tests/OSS/Tests/CorsConfigTest.php b/tests/OSS/Tests/CorsConfigTest.php index 207f0aa6..ddc4d3ab 100644 --- a/tests/OSS/Tests/CorsConfigTest.php +++ b/tests/OSS/Tests/CorsConfigTest.php @@ -66,11 +66,12 @@ public function testParseValidXml() $corsConfig->parseFromXml($this->validXml); $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($corsConfig->serializeToXml())); $this->assertNotNull($corsConfig->getRules()); - $this->assertNotNull($corsConfig->getRules()[0]->getAllowedHeaders()); - $this->assertNotNull($corsConfig->getRules()[0]->getAllowedMethods()); - $this->assertNotNull($corsConfig->getRules()[0]->getAllowedOrigins()); - $this->assertNotNull($corsConfig->getRules()[0]->getExposeHeaders()); - $this->assertNotNull($corsConfig->getRules()[0]->getMaxAgeSeconds()); + $rules = $corsConfig->getRules(); + $this->assertNotNull($rules[0]->getAllowedHeaders()); + $this->assertNotNull($rules[0]->getAllowedMethods()); + $this->assertNotNull($rules[0]->getAllowedOrigins()); + $this->assertNotNull($rules[0]->getExposeHeaders()); + $this->assertNotNull($rules[0]->getMaxAgeSeconds()); } public function testParseValidXml2() diff --git a/tests/OSS/Tests/HeaderResultTest.php b/tests/OSS/Tests/HeaderResultTest.php index 5ea275e1..dae49754 100644 --- a/tests/OSS/Tests/HeaderResultTest.php +++ b/tests/OSS/Tests/HeaderResultTest.php @@ -17,6 +17,7 @@ public function testGetHeader() $result = new HeaderResult($response); $this->assertTrue($result->isOK()); $this->assertTrue(is_array($result->getData())); - $this->assertEquals($result->getData()['key'], 'value'); + $data = $result->getData(); + $this->assertEquals($data['key'], 'value'); } } diff --git a/tests/OSS/Tests/ListMultipartUploadResultTest.php b/tests/OSS/Tests/ListMultipartUploadResultTest.php index bd223b1b..5c757d31 100644 --- a/tests/OSS/Tests/ListMultipartUploadResultTest.php +++ b/tests/OSS/Tests/ListMultipartUploadResultTest.php @@ -86,9 +86,10 @@ public function testParseValidXml() $this->assertEquals("xx", $listMultipartUploadInfo->getPrefix()); $this->assertEquals(1000, $listMultipartUploadInfo->getMaxUploads()); $this->assertEquals("false", $listMultipartUploadInfo->getIsTruncated()); - $this->assertEquals("multipart.data", $listMultipartUploadInfo->getUploads()[0]->getKey()); - $this->assertEquals("0004B999EF518A1FE585B0C9360DC4C8", $listMultipartUploadInfo->getUploads()[0]->getUploadId()); - $this->assertEquals("2012-02-23T04:18:23.000Z", $listMultipartUploadInfo->getUploads()[0]->getInitiated()); + $uploads = $listMultipartUploadInfo->getUploads(); + $this->assertEquals("multipart.data", $uploads[0]->getKey()); + $this->assertEquals("0004B999EF518A1FE585B0C9360DC4C8", $uploads[0]->getUploadId()); + $this->assertEquals("2012-02-23T04:18:23.000Z", $uploads[0]->getInitiated()); } public function testParseValidXmlWithEncodedKey() @@ -105,8 +106,9 @@ public function testParseValidXmlWithEncodedKey() $this->assertEquals("php+prefix", $listMultipartUploadInfo->getPrefix()); $this->assertEquals(1000, $listMultipartUploadInfo->getMaxUploads()); $this->assertEquals("true", $listMultipartUploadInfo->getIsTruncated()); - $this->assertEquals("php+key-1", $listMultipartUploadInfo->getUploads()[0]->getKey()); - $this->assertEquals("0004B999EF518A1FE585B0C9360DC4C8", $listMultipartUploadInfo->getUploads()[0]->getUploadId()); - $this->assertEquals("2012-02-23T04:18:23.000Z", $listMultipartUploadInfo->getUploads()[0]->getInitiated()); + $uploads = $listMultipartUploadInfo->getUploads(); + $this->assertEquals("php+key-1", $uploads[0]->getKey()); + $this->assertEquals("0004B999EF518A1FE585B0C9360DC4C8", $uploads[0]->getUploadId()); + $this->assertEquals("2012-02-23T04:18:23.000Z", $uploads[0]->getInitiated()); } } diff --git a/tests/OSS/Tests/ListObjectsResultTest.php b/tests/OSS/Tests/ListObjectsResultTest.php index 9e83d872..85f262ca 100644 --- a/tests/OSS/Tests/ListObjectsResultTest.php +++ b/tests/OSS/Tests/ListObjectsResultTest.php @@ -93,8 +93,9 @@ public function testParseValidXml1() $this->assertEquals(1000, $objectListInfo->getMaxKeys()); $this->assertEquals('/', $objectListInfo->getDelimiter()); $this->assertEquals('false', $objectListInfo->getIsTruncated()); - $this->assertEquals('oss-php-sdk-test/', $objectListInfo->getPrefixList()[0]->getPrefix()); - $this->assertEquals('test/', $objectListInfo->getPrefixList()[1]->getPrefix()); + $prefixes = $objectListInfo->getPrefixList(); + $this->assertEquals('oss-php-sdk-test/', $prefixes[0]->getPrefix()); + $this->assertEquals('test/', $prefixes[1]->getPrefix()); } public function testParseValidXml2() @@ -113,12 +114,13 @@ public function testParseValidXml2() $this->assertEquals(1000, $objectListInfo->getMaxKeys()); $this->assertEquals('/', $objectListInfo->getDelimiter()); $this->assertEquals('false', $objectListInfo->getIsTruncated()); - $this->assertEquals('oss-php-sdk-test/upload-test-object-name.txt', $objectListInfo->getObjectList()[0]->getKey()); - $this->assertEquals('2015-11-18T03:36:00.000Z', $objectListInfo->getObjectList()[0]->getLastModified()); - $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objectListInfo->getObjectList()[0]->getETag()); - $this->assertEquals('Normal', $objectListInfo->getObjectList()[0]->getType()); - $this->assertEquals(13115, $objectListInfo->getObjectList()[0]->getSize()); - $this->assertEquals('Standard', $objectListInfo->getObjectList()[0]->getStorageClass()); + $objects = $objectListInfo->getObjectList(); + $this->assertEquals('oss-php-sdk-test/upload-test-object-name.txt', $objects[0]->getKey()); + $this->assertEquals('2015-11-18T03:36:00.000Z', $objects[0]->getLastModified()); + $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objects[0]->getETag()); + $this->assertEquals('Normal', $objects[0]->getType()); + $this->assertEquals(13115, $objects[0]->getSize()); + $this->assertEquals('Standard', $objects[0]->getStorageClass()); } public function testParseValidXmlWithEncodedKey() @@ -138,11 +140,12 @@ public function testParseValidXmlWithEncodedKey() $this->assertEquals(1000, $objectListInfo->getMaxKeys()); $this->assertEquals('/', $objectListInfo->getDelimiter()); $this->assertEquals('true', $objectListInfo->getIsTruncated()); - $this->assertEquals('php/a+b', $objectListInfo->getObjectList()[0]->getKey()); - $this->assertEquals('2015-11-18T03:36:00.000Z', $objectListInfo->getObjectList()[0]->getLastModified()); - $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objectListInfo->getObjectList()[0]->getETag()); - $this->assertEquals('Normal', $objectListInfo->getObjectList()[0]->getType()); - $this->assertEquals(13115, $objectListInfo->getObjectList()[0]->getSize()); - $this->assertEquals('Standard', $objectListInfo->getObjectList()[0]->getStorageClass()); + $objects = $objectListInfo->getObjectList(); + $this->assertEquals('php/a+b', $objects[0]->getKey()); + $this->assertEquals('2015-11-18T03:36:00.000Z', $objects[0]->getLastModified()); + $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objects[0]->getETag()); + $this->assertEquals('Normal', $objects[0]->getType()); + $this->assertEquals(13115, $objects[0]->getSize()); + $this->assertEquals('Standard', $objects[0]->getStorageClass()); } } diff --git a/tests/OSS/Tests/ListPartsResultTest.php b/tests/OSS/Tests/ListPartsResultTest.php index ceb5eb2e..c446714f 100644 --- a/tests/OSS/Tests/ListPartsResultTest.php +++ b/tests/OSS/Tests/ListPartsResultTest.php @@ -53,9 +53,10 @@ public function testParseValidXml() $this->assertEquals(1000, $listPartsInfo->getMaxParts()); $this->assertEquals("false", $listPartsInfo->getIsTruncated()); $this->assertEquals(3, count($listPartsInfo->getListPart())); - $this->assertEquals(1, $listPartsInfo->getListPart()[0]->getPartNumber()); - $this->assertEquals('2012-02-23T07:01:34.000Z', $listPartsInfo->getListPart()[0]->getLastModified()); - $this->assertEquals('"3349DC700140D7F86A078484278075A9"', $listPartsInfo->getListPart()[0]->getETag()); - $this->assertEquals(6291456, $listPartsInfo->getListPart()[0]->getSize()); + $parts = $listPartsInfo->getListPart(); + $this->assertEquals(1, $parts[0]->getPartNumber()); + $this->assertEquals('2012-02-23T07:01:34.000Z', $parts[0]->getLastModified()); + $this->assertEquals('"3349DC700140D7F86A078484278075A9"', $parts[0]->getETag()); + $this->assertEquals(6291456, $parts[0]->getSize()); } } diff --git a/tests/OSS/Tests/OssClientBucketCorsTest.php b/tests/OSS/Tests/OssClientBucketCorsTest.php index d39fdc6b..daa980c5 100644 --- a/tests/OSS/Tests/OssClientBucketCorsTest.php +++ b/tests/OSS/Tests/OssClientBucketCorsTest.php @@ -47,6 +47,7 @@ public function testBucket() } try { + Common::waitMetaSync(); $object = "cors/test.txt"; $this->ossClient->putObject($this->bucket, $object, file_get_contents(__FILE__)); $headers = $this->ossClient->optionsObject($this->bucket, $object, "http://www.a.com", "GET", "", null); @@ -56,7 +57,7 @@ public function testBucket() } try { - sleep(1); + Common::waitMetaSync(); $corsConfig2 = $this->ossClient->getBucketCors($this->bucket); $this->assertNotNull($corsConfig2); $this->assertEquals($corsConfig->serializeToXml(), $corsConfig2->serializeToXml()); @@ -65,13 +66,14 @@ public function testBucket() } try { + Common::waitMetaSync(); $this->ossClient->deleteBucketCors($this->bucket); } catch (OssException $e) { $this->assertFalse(True); } try { - sleep(5); + Common::waitMetaSync(); $corsConfig3 = $this->ossClient->getBucketCors($this->bucket); $this->assertNotNull($corsConfig3); $this->assertNotEquals($corsConfig->serializeToXml(), $corsConfig3->serializeToXml()); diff --git a/tests/OSS/Tests/OssClientBucketLifecycleTest.php b/tests/OSS/Tests/OssClientBucketLifecycleTest.php index af6f6250..46da1f06 100644 --- a/tests/OSS/Tests/OssClientBucketLifecycleTest.php +++ b/tests/OSS/Tests/OssClientBucketLifecycleTest.php @@ -31,7 +31,7 @@ public function testBucket() } try { - sleep(5); + Common::waitMetaSync(); $lifecycleConfig2 = $this->ossClient->getBucketLifecycle($this->bucket); $this->assertEquals($lifecycleConfig->serializeToXml(), $lifecycleConfig2->serializeToXml()); } catch (OssException $e) { @@ -39,13 +39,14 @@ public function testBucket() } try { + Common::waitMetaSync(); $this->ossClient->deleteBucketLifecycle($this->bucket); } catch (OssException $e) { $this->assertTrue(false); } try { - sleep(3); + Common::waitMetaSync(); $lifecycleConfig3 = $this->ossClient->getBucketLifecycle($this->bucket); $this->assertNotEquals($lifecycleConfig->serializeToXml(), $lifecycleConfig3->serializeToXml()); } catch (OssException $e) { diff --git a/tests/OSS/Tests/OssClientBucketLoggingTest.php b/tests/OSS/Tests/OssClientBucketLoggingTest.php index 3f15d715..16a10ebf 100644 --- a/tests/OSS/Tests/OssClientBucketLoggingTest.php +++ b/tests/OSS/Tests/OssClientBucketLoggingTest.php @@ -20,19 +20,20 @@ public function testBucket() $this->assertTrue(false); } try { - sleep(2); + Common::waitMetaSync(); $loggingConfig2 = $this->ossClient->getBucketLogging($this->bucket); $this->assertEquals($loggingConfig->serializeToXml(), $loggingConfig2->serializeToXml()); } catch (OssException $e) { $this->assertTrue(false); } try { + Common::waitMetaSync(); $this->ossClient->deleteBucketLogging($this->bucket); } catch (OssException $e) { $this->assertTrue(false); } try { - sleep(3); + Common::waitMetaSync(); $loggingConfig3 = $this->ossClient->getBucketLogging($this->bucket); $this->assertNotEquals($loggingConfig->serializeToXml(), $loggingConfig3->serializeToXml()); } catch (OssException $e) { diff --git a/tests/OSS/Tests/OssClientBucketRefererTest.php b/tests/OSS/Tests/OssClientBucketRefererTest.php index acf85f2b..ba7d14f5 100644 --- a/tests/OSS/Tests/OssClientBucketRefererTest.php +++ b/tests/OSS/Tests/OssClientBucketRefererTest.php @@ -23,13 +23,14 @@ public function testBucket() $this->assertTrue(false); } try { - sleep(5); + Common::waitMetaSync(); $refererConfig2 = $this->ossClient->getBucketReferer($this->bucket); $this->assertEquals($refererConfig->serializeToXml(), $refererConfig2->serializeToXml()); } catch (OssException $e) { $this->assertTrue(false); } try { + Common::waitMetaSync(); $nullRefererConfig = new RefererConfig(); $nullRefererConfig->setAllowEmptyReferer(false); $this->ossClient->putBucketReferer($this->bucket, $nullRefererConfig); @@ -37,7 +38,7 @@ public function testBucket() $this->assertTrue(false); } try { - sleep(5); + Common::waitMetaSync(); $refererConfig3 = $this->ossClient->getBucketLogging($this->bucket); $this->assertNotEquals($refererConfig->serializeToXml(), $refererConfig3->serializeToXml()); } catch (OssException $e) { diff --git a/tests/OSS/Tests/OssClientBucketTest.php b/tests/OSS/Tests/OssClientBucketTest.php index 48bfd9cd..d58eb2d7 100644 --- a/tests/OSS/Tests/OssClientBucketTest.php +++ b/tests/OSS/Tests/OssClientBucketTest.php @@ -41,6 +41,7 @@ public function testBucket() $this->assertTrue(is_array($bucketList)); $this->assertGreaterThan(0, count($bucketList)); $this->ossClient->putBucketAcl($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE); + Common::waitMetaSync(); $this->assertEquals($this->ossClient->getBucketAcl($this->bucket), OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE); $this->assertTrue($this->ossClient->doesBucketExist($this->bucket)); diff --git a/tests/OSS/Tests/OssClientBucketWebsiteTest.php b/tests/OSS/Tests/OssClientBucketWebsiteTest.php index e8f17e6f..dfa9cc17 100644 --- a/tests/OSS/Tests/OssClientBucketWebsiteTest.php +++ b/tests/OSS/Tests/OssClientBucketWebsiteTest.php @@ -23,19 +23,20 @@ public function testBucket() } try { - sleep(2); + Common::waitMetaSync(); $websiteConfig2 = $this->ossClient->getBucketWebsite($this->bucket); $this->assertEquals($websiteConfig->serializeToXml(), $websiteConfig2->serializeToXml()); } catch (OssException $e) { $this->assertTrue(false); } try { + Common::waitMetaSync(); $this->ossClient->deleteBucketWebsite($this->bucket); } catch (OssException $e) { $this->assertTrue(false); } try { - sleep(1); + Common::waitMetaSync(); $websiteConfig3 = $this->ossClient->getBucketLogging($this->bucket); $this->assertNotEquals($websiteConfig->serializeToXml(), $websiteConfig3->serializeToXml()); } catch (OssException $e) { diff --git a/tests/OSS/Tests/OssClientMultipartUploadTest.php b/tests/OSS/Tests/OssClientMultipartUploadTest.php index 57dc750c..da3e0568 100644 --- a/tests/OSS/Tests/OssClientMultipartUploadTest.php +++ b/tests/OSS/Tests/OssClientMultipartUploadTest.php @@ -248,7 +248,6 @@ function testPutObjectsByDir() $this->assertFalse(true); } - sleep(1); $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, 'samples/codes/' . "OssClientMultipartUploadTest.php")); } @@ -269,7 +268,7 @@ public function testListMultipartUploads() { $options = null; try { - $listMultipartUploadInfo = $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options); + $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options); $this->assertNotNull($listMultipartUploadInfo); } catch (OssException $e) { $this->assertFalse(true); diff --git a/tests/OSS/Tests/OssClientSignatureTest.php b/tests/OSS/Tests/OssClientSignatureTest.php index 0d85ae39..109121d0 100644 --- a/tests/OSS/Tests/OssClientSignatureTest.php +++ b/tests/OSS/Tests/OssClientSignatureTest.php @@ -22,7 +22,7 @@ function testGetSignedUrlForGettingObject() } catch (OssException $e) { $this->assertFalse(true); } - sleep(5); + $request = new RequestCore($signedUrl); $request->set_method('GET'); $request->add_header('Content-Type', ''); @@ -38,7 +38,6 @@ public function testGetSignedUrlForPuttingObject() try { $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT"); $content = file_get_contents(__FILE__); - sleep(5); $request = new RequestCore($signedUrl); $request->set_method('PUT'); $request->add_header('Content-Type', ''); @@ -61,7 +60,6 @@ public function testGetSignedUrlForPuttingObjectFromFile() $options = array('Content-Type' => 'txt'); try { $signedUrl = $this->ossClient->signUrl($this->bucket, $object, $timeout, "PUT", $options); - sleep(5); $request = new RequestCore($signedUrl); $request->set_method('PUT'); $request->add_header('Content-Type', 'txt'); @@ -80,6 +78,7 @@ public function testGetSignedUrlForPuttingObjectFromFile() public function tearDown() { $this->ossClient->deleteObject($this->bucket, "a.file"); + parent::tearDown(); } public function setUp() diff --git a/tests/OSS/Tests/OssClientTest.php b/tests/OSS/Tests/OssClientTest.php index 8d38c8cf..6ecff186 100644 --- a/tests/OSS/Tests/OssClientTest.php +++ b/tests/OSS/Tests/OssClientTest.php @@ -5,15 +5,12 @@ use OSS\Core\OssException; use OSS\OssClient; -require_once __DIR__ . DIRECTORY_SEPARATOR . 'Config.php'; - - class OssClientTest extends \PHPUnit_Framework_TestCase { public function testConstrunct() { try { - $ossClient = new OssClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, Config::OSS_ENDPOINT); + $ossClient = new OssClient('id', 'key', 'http://oss-cn-hangzhou.aliyuncs.com'); $this->assertFalse($ossClient->isUseSSL()); $ossClient->setUseSSL(true); $this->assertTrue($ossClient->isUseSSL()); @@ -31,7 +28,7 @@ public function testConstrunct() public function testConstrunct2() { try { - $ossClient = new OssClient(Config::OSS_ACCESS_ID, "", Config::OSS_ENDPOINT); + $ossClient = new OssClient('id', "", 'http://oss-cn-hangzhou.aliyuncs.com'); $this->assertFalse(true); } catch (OssException $e) { $this->assertEquals("access key secret is empty", $e->getMessage()); @@ -41,7 +38,7 @@ public function testConstrunct2() public function testConstrunct3() { try { - $ossClient = new OssClient("", Config::OSS_ACCESS_KEY, Config::OSS_ENDPOINT); + $ossClient = new OssClient("", 'key', 'http://oss-cn-hangzhou.aliyuncs.com'); $this->assertFalse(true); } catch (OssException $e) { $this->assertEquals("access key id is empty", $e->getMessage()); @@ -51,7 +48,7 @@ public function testConstrunct3() public function testConstrunct4() { try { - $ossClient = new OssClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, ""); + $ossClient = new OssClient('id', 'key', ""); $this->assertFalse(true); } catch (OssException $e) { $this->assertEquals('endpoint is empty', $e->getMessage()); @@ -61,7 +58,7 @@ public function testConstrunct4() public function testConstrunct5() { try { - $ossClient = new OssClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, "123.123.123.1"); + $ossClient = new OssClient('id', 'key', "123.123.123.1"); } catch (OssException $e) { $this->assertTrue(false); } @@ -70,7 +67,7 @@ public function testConstrunct5() public function testConstrunct6() { try { - $ossClient = new OssClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, "https://123.123.123.1"); + $ossClient = new OssClient('id', 'key', "https://123.123.123.1"); $this->assertTrue($ossClient->isUseSSL()); } catch (OssException $e) { $this->assertTrue(false); @@ -80,7 +77,7 @@ public function testConstrunct6() public function testConstrunct7() { try { - $ossClient = new OssClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, "http://123.123.123.1"); + $ossClient = new OssClient('id', 'key', "http://123.123.123.1"); $this->assertFalse($ossClient->isUseSSL()); } catch (OssException $e) { $this->assertTrue(false); @@ -90,7 +87,7 @@ public function testConstrunct7() public function testConstrunct8() { try { - $ossClient = new OssClient(Config::OSS_ACCESS_ID, Config::OSS_ACCESS_KEY, "http://123.123.123.1", true); + $ossClient = new OssClient('id', 'key', "http://123.123.123.1", true); $ossClient->listBuckets(); $this->assertFalse(true); } catch (OssException $e) { diff --git a/tests/OSS/Tests/TestOssClientBase.php b/tests/OSS/Tests/TestOssClientBase.php index 9e73ff7a..12b81a26 100644 --- a/tests/OSS/Tests/TestOssClientBase.php +++ b/tests/OSS/Tests/TestOssClientBase.php @@ -20,14 +20,31 @@ class TestOssClientBase extends \PHPUnit_Framework_TestCase public function setUp() { - $this->bucket = Common::getBucketName(); + $this->bucket = Common::getBucketName() . rand(100000, 999999); $this->ossClient = Common::getOssClient(); $this->ossClient->createBucket($this->bucket); } public function tearDown() { - + if (!$this->ossClient->doesBucketExist($this->bucket)) { + return; + } + + $objects = $this->ossClient->listObjects( + $this->bucket, array('max-keys' => 1000, 'delimiter' => ''))->getObjectList(); + $keys = array(); + foreach ($objects as $obj) { + $keys[] = $obj->getKey(); + } + if (count($keys) > 0) { + $this->ossClient->deleteObjects($this->bucket, $keys); + } + $uploads = $this->ossClient->listMultipartUploads($this->bucket)->getUploads(); + foreach ($uploads as $up) { + $this->ossClient->abortMultipartUpload($this->bucket, $up->getKey(), $up->getUploadId()); + } + + $this->ossClient->deleteBucket($this->bucket); } - }