ListObjectsResultTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Result\ListObjectsResult;
  4. use OSS\Http\ResponseCore;
  5. class ListObjectsResultTest extends \PHPUnit_Framework_TestCase
  6. {
  7. private $validXml1 = <<<BBBB
  8. <?xml version="1.0" encoding="UTF-8"?>
  9. <ListBucketResult>
  10. <Name>testbucket-hf</Name>
  11. <Prefix></Prefix>
  12. <Marker></Marker>
  13. <MaxKeys>1000</MaxKeys>
  14. <Delimiter>/</Delimiter>
  15. <IsTruncated>false</IsTruncated>
  16. <CommonPrefixes>
  17. <Prefix>oss-php-sdk-test/</Prefix>
  18. </CommonPrefixes>
  19. <CommonPrefixes>
  20. <Prefix>test/</Prefix>
  21. </CommonPrefixes>
  22. </ListBucketResult>
  23. BBBB;
  24. private $validXml2 = <<<BBBB
  25. <?xml version="1.0" encoding="UTF-8"?>
  26. <ListBucketResult>
  27. <Name>testbucket-hf</Name>
  28. <Prefix>oss-php-sdk-test/</Prefix>
  29. <Marker>xx</Marker>
  30. <MaxKeys>1000</MaxKeys>
  31. <Delimiter>/</Delimiter>
  32. <IsTruncated>false</IsTruncated>
  33. <Contents>
  34. <Key>oss-php-sdk-test/upload-test-object-name.txt</Key>
  35. <LastModified>2015-11-18T03:36:00.000Z</LastModified>
  36. <ETag>"89B9E567E7EB8815F2F7D41851F9A2CD"</ETag>
  37. <Type>Normal</Type>
  38. <Size>13115</Size>
  39. <StorageClass>Standard</StorageClass>
  40. <Owner>
  41. <ID>cname_user</ID>
  42. <DisplayName>cname_user</DisplayName>
  43. </Owner>
  44. </Contents>
  45. </ListBucketResult>
  46. BBBB;
  47. private $validXmlWithEncodedKey = <<<BBBB
  48. <?xml version="1.0" encoding="UTF-8"?>
  49. <ListBucketResult>
  50. <Name>testbucket-hf</Name>
  51. <EncodingType>url</EncodingType>
  52. <Prefix>php%2Fprefix</Prefix>
  53. <Marker>php%2Fmarker</Marker>
  54. <NextMarker>php%2Fnext-marker</NextMarker>
  55. <MaxKeys>1000</MaxKeys>
  56. <Delimiter>%2F</Delimiter>
  57. <IsTruncated>true</IsTruncated>
  58. <Contents>
  59. <Key>php/a%2Bb</Key>
  60. <LastModified>2015-11-18T03:36:00.000Z</LastModified>
  61. <ETag>"89B9E567E7EB8815F2F7D41851F9A2CD"</ETag>
  62. <Type>Normal</Type>
  63. <Size>13115</Size>
  64. <StorageClass>Standard</StorageClass>
  65. <Owner>
  66. <ID>cname_user</ID>
  67. <DisplayName>cname_user</DisplayName>
  68. </Owner>
  69. </Contents>
  70. </ListBucketResult>
  71. BBBB;
  72. public function testParseValidXml1()
  73. {
  74. $response = new ResponseCore(array(), $this->validXml1, 200);
  75. $result = new ListObjectsResult($response);
  76. $this->assertTrue($result->isOK());
  77. $this->assertNotNull($result->getData());
  78. $this->assertNotNull($result->getRawResponse());
  79. $objectListInfo = $result->getData();
  80. $this->assertEquals(2, count($objectListInfo->getPrefixList()));
  81. $this->assertEquals(0, count($objectListInfo->getObjectList()));
  82. $this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
  83. $this->assertEquals('', $objectListInfo->getPrefix());
  84. $this->assertEquals('', $objectListInfo->getMarker());
  85. $this->assertEquals(1000, $objectListInfo->getMaxKeys());
  86. $this->assertEquals('/', $objectListInfo->getDelimiter());
  87. $this->assertEquals('false', $objectListInfo->getIsTruncated());
  88. $prefixes = $objectListInfo->getPrefixList();
  89. $this->assertEquals('oss-php-sdk-test/', $prefixes[0]->getPrefix());
  90. $this->assertEquals('test/', $prefixes[1]->getPrefix());
  91. }
  92. public function testParseValidXml2()
  93. {
  94. $response = new ResponseCore(array(), $this->validXml2, 200);
  95. $result = new ListObjectsResult($response);
  96. $this->assertTrue($result->isOK());
  97. $this->assertNotNull($result->getData());
  98. $this->assertNotNull($result->getRawResponse());
  99. $objectListInfo = $result->getData();
  100. $this->assertEquals(0, count($objectListInfo->getPrefixList()));
  101. $this->assertEquals(1, count($objectListInfo->getObjectList()));
  102. $this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
  103. $this->assertEquals('oss-php-sdk-test/', $objectListInfo->getPrefix());
  104. $this->assertEquals('xx', $objectListInfo->getMarker());
  105. $this->assertEquals(1000, $objectListInfo->getMaxKeys());
  106. $this->assertEquals('/', $objectListInfo->getDelimiter());
  107. $this->assertEquals('false', $objectListInfo->getIsTruncated());
  108. $objects = $objectListInfo->getObjectList();
  109. $this->assertEquals('oss-php-sdk-test/upload-test-object-name.txt', $objects[0]->getKey());
  110. $this->assertEquals('2015-11-18T03:36:00.000Z', $objects[0]->getLastModified());
  111. $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objects[0]->getETag());
  112. $this->assertEquals('Normal', $objects[0]->getType());
  113. $this->assertEquals(13115, $objects[0]->getSize());
  114. $this->assertEquals('Standard', $objects[0]->getStorageClass());
  115. }
  116. public function testParseValidXmlWithEncodedKey()
  117. {
  118. $response = new ResponseCore(array(), $this->validXmlWithEncodedKey, 200);
  119. $result = new ListObjectsResult($response);
  120. $this->assertTrue($result->isOK());
  121. $this->assertNotNull($result->getData());
  122. $this->assertNotNull($result->getRawResponse());
  123. $objectListInfo = $result->getData();
  124. $this->assertEquals(0, count($objectListInfo->getPrefixList()));
  125. $this->assertEquals(1, count($objectListInfo->getObjectList()));
  126. $this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
  127. $this->assertEquals('php/prefix', $objectListInfo->getPrefix());
  128. $this->assertEquals('php/marker', $objectListInfo->getMarker());
  129. $this->assertEquals('php/next-marker', $objectListInfo->getNextMarker());
  130. $this->assertEquals(1000, $objectListInfo->getMaxKeys());
  131. $this->assertEquals('/', $objectListInfo->getDelimiter());
  132. $this->assertEquals('true', $objectListInfo->getIsTruncated());
  133. $objects = $objectListInfo->getObjectList();
  134. $this->assertEquals('php/a+b', $objects[0]->getKey());
  135. $this->assertEquals('2015-11-18T03:36:00.000Z', $objects[0]->getLastModified());
  136. $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objects[0]->getETag());
  137. $this->assertEquals('Normal', $objects[0]->getType());
  138. $this->assertEquals(13115, $objects[0]->getSize());
  139. $this->assertEquals('Standard', $objects[0]->getStorageClass());
  140. }
  141. }