OssClientBucketTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\OssClient;
  5. require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
  6. class OssClientBucketTest extends TestOssClientBase
  7. {
  8. private $iaBucket;
  9. private $archiveBucket;
  10. public function testBucketWithInvalidName()
  11. {
  12. try {
  13. $this->ossClient->createBucket("s");
  14. $this->assertFalse(true);
  15. } catch (OssException $e) {
  16. $this->assertEquals('"s"bucket name is invalid', $e->getMessage());
  17. }
  18. }
  19. public function testBucketWithInvalidACL()
  20. {
  21. try {
  22. $this->ossClient->createBucket($this->bucket, "invalid");
  23. $this->assertFalse(true);
  24. } catch (OssException $e) {
  25. $this->assertEquals('invalid:acl is invalid(private,public-read,public-read-write)', $e->getMessage());
  26. }
  27. }
  28. public function testBucket()
  29. {
  30. $this->ossClient->createBucket($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  31. $bucketListInfo = $this->ossClient->listBuckets();
  32. $this->assertNotNull($bucketListInfo);
  33. $bucketList = $bucketListInfo->getBucketList();
  34. $this->assertTrue(is_array($bucketList));
  35. $this->assertGreaterThan(0, count($bucketList));
  36. $this->ossClient->putBucketAcl($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  37. Common::waitMetaSync();
  38. $this->assertEquals($this->ossClient->getBucketAcl($this->bucket), OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
  39. $this->assertTrue($this->ossClient->doesBucketExist($this->bucket));
  40. $this->assertFalse($this->ossClient->doesBucketExist($this->bucket . '-notexist'));
  41. $this->assertEquals($this->ossClient->getBucketLocation($this->bucket), 'oss-us-west-1');
  42. $res = $this->ossClient->getBucketMeta($this->bucket);
  43. $this->assertEquals('200', $res['info']['http_code']);
  44. $this->assertEquals('oss-us-west-1', $res['x-oss-bucket-region']);
  45. }
  46. public function testCreateBucketWithStorageType()
  47. {
  48. $object = 'storage-object';
  49. $this->ossClient->putObject($this->archiveBucket, $object,'testcontent');
  50. try {
  51. $this->ossClient->getObject($this->archiveBucket, $object);
  52. $this->assertTrue(false);
  53. } catch (OssException $e) {
  54. $this->assertEquals('403', $e->getHTTPStatus());
  55. $this->assertEquals('InvalidObjectState', $e->getErrorCode());
  56. }
  57. $this->ossClient->putObject($this->iaBucket, $object,'testcontent');
  58. $result = $this->ossClient->getObject($this->iaBucket, $object);
  59. $this->assertEquals($result, 'testcontent');
  60. $this->ossClient->putObject($this->bucket, $object,'testcontent');
  61. $result = $this->ossClient->getObject($this->bucket, $object);
  62. $this->assertEquals($result, 'testcontent');
  63. }
  64. public function setUp()
  65. {
  66. parent::setUp();
  67. $this->iaBucket = 'ia-' . $this->bucket;
  68. $this->archiveBucket = 'archive-' . $this->bucket;
  69. $options = array(
  70. OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_IA
  71. );
  72. $this->ossClient->createBucket($this->iaBucket, OssClient::OSS_ACL_TYPE_PRIVATE, $options);
  73. $options = array(
  74. OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_ARCHIVE
  75. );
  76. $this->ossClient->createBucket($this->archiveBucket, OssClient::OSS_ACL_TYPE_PRIVATE, $options);
  77. }
  78. public function tearDown()
  79. {
  80. parent::tearDown();
  81. $object = 'storage-object';
  82. $this->ossClient->deleteObject($this->iaBucket, $object);
  83. $this->ossClient->deleteObject($this->archiveBucket, $object);
  84. $this->ossClient->deleteBucket($this->iaBucket);
  85. $this->ossClient->deleteBucket($this->archiveBucket);
  86. }
  87. }