ListBucketsResultTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\Http\ResponseCore;
  5. use OSS\Result\ListBucketsResult;
  6. class ListBucketsResultTest extends \PHPUnit_Framework_TestCase
  7. {
  8. private $validXml = <<<BBBB
  9. <?xml version="1.0" encoding="UTF-8"?>
  10. <ListAllMyBucketsResult>
  11. <Owner>
  12. <ID>ut_test_put_bucket</ID>
  13. <DisplayName>ut_test_put_bucket</DisplayName>
  14. </Owner>
  15. <Buckets>
  16. <Bucket>
  17. <Location>oss-cn-hangzhou-a</Location>
  18. <Name>xz02tphky6fjfiuc0</Name>
  19. <CreationDate>2014-05-15T11:18:32.000Z</CreationDate>
  20. </Bucket>
  21. <Bucket>
  22. <Location>oss-cn-hangzhou-a</Location>
  23. <Name>xz02tphky6fjfiuc1</Name>
  24. <CreationDate>2014-05-15T11:18:32.000Z</CreationDate>
  25. </Bucket>
  26. </Buckets>
  27. </ListAllMyBucketsResult>
  28. BBBB;
  29. private $nullXml = <<<BBBB
  30. <?xml version="1.0" encoding="UTF-8"?>
  31. <ListAllMyBucketsResult>
  32. <Owner>
  33. <ID>ut_test_put_bucket</ID>
  34. <DisplayName>ut_test_put_bucket</DisplayName>
  35. </Owner>
  36. <Buckets>
  37. </Buckets>
  38. </ListAllMyBucketsResult>
  39. BBBB;
  40. public function testParseValidXml()
  41. {
  42. $response = new ResponseCore(array(), $this->validXml, 200);
  43. $result = new ListBucketsResult($response);
  44. $this->assertTrue($result->isOK());
  45. $this->assertNotNull($result->getData());
  46. $this->assertNotNull($result->getRawResponse());
  47. $bucketListInfo = $result->getData();
  48. $this->assertEquals(2, count($bucketListInfo->getBucketList()));
  49. }
  50. public function testParseNullXml()
  51. {
  52. $response = new ResponseCore(array(), $this->nullXml, 200);
  53. $result = new ListBucketsResult($response);
  54. $this->assertTrue($result->isOK());
  55. $this->assertNotNull($result->getData());
  56. $this->assertNotNull($result->getRawResponse());
  57. $bucketListInfo = $result->getData();
  58. $this->assertEquals(0, count($bucketListInfo->getBucketList()));
  59. }
  60. public function test403()
  61. {
  62. $errorHeader = array(
  63. 'x-oss-request-id' => '1a2b-3c4d'
  64. );
  65. $errorBody = <<< BBBB
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <Error>
  68. <Code>NoSuchBucket</Code>
  69. <Message>The specified bucket does not exist.</Message>
  70. <RequestId>566B870D207FB3044302EB0A</RequestId>
  71. <HostId>hello.oss-test.aliyun-inc.com</HostId>
  72. <BucketName>hello</BucketName>
  73. </Error>
  74. BBBB;
  75. $response = new ResponseCore($errorHeader, $errorBody, 403);
  76. try {
  77. new ListBucketsResult($response);
  78. } catch (OssException $e) {
  79. $this->assertEquals(
  80. $e->getMessage(),
  81. 'NoSuchBucket: The specified bucket does not exist. RequestId: 1a2b-3c4d');
  82. $this->assertEquals($e->getHTTPStatus(), '403');
  83. $this->assertEquals($e->getRequestId(), '1a2b-3c4d');
  84. $this->assertEquals($e->getErrorCode(), 'NoSuchBucket');
  85. $this->assertEquals($e->getErrorMessage(), 'The specified bucket does not exist.');
  86. $this->assertEquals($e->getDetails(), $errorBody);
  87. }
  88. }
  89. }