ListObjectsV2ResultTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Result\ListObjectsV2Result;
  4. use OSS\Http\ResponseCore;
  5. class ListObjectsV2ResultTest 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. <StartAfter></StartAfter>
  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. <StartAfter>xx</StartAfter>
  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. </Contents>
  41. <KeyCount>1</KeyCount>
  42. </ListBucketResult>
  43. BBBB;
  44. private $validXmlWithEncodedKey = <<<BBBB
  45. <?xml version="1.0" encoding="UTF-8"?>
  46. <ListBucketResult>
  47. <Name>testbucket-hf</Name>
  48. <EncodingType>url</EncodingType>
  49. <Prefix>php%2Fprefix</Prefix>
  50. <StartAfter>php%2Fmarker</StartAfter>
  51. <ContinuationToken>1gJiYw--</ContinuationToken>
  52. <NextContinuationToken>CgJiYw--</NextContinuationToken>
  53. <MaxKeys>1000</MaxKeys>
  54. <Delimiter>%2F</Delimiter>
  55. <IsTruncated>true</IsTruncated>
  56. <Contents>
  57. <Key>php/a%2Bb</Key>
  58. <LastModified>2015-11-18T03:36:00.000Z</LastModified>
  59. <ETag>"89B9E567E7EB8815F2F7D41851F9A2CD"</ETag>
  60. <Type>Normal</Type>
  61. <Size>13115</Size>
  62. <StorageClass>Standard</StorageClass>
  63. <Owner>
  64. <ID>cname_user</ID>
  65. <DisplayName>cname_user</DisplayName>
  66. </Owner>
  67. </Contents>
  68. <KeyCount>1</KeyCount>
  69. </ListBucketResult>
  70. BBBB;
  71. private $validXmlWithRestoreInfo = <<<BBBB
  72. <?xml version="1.0" encoding="UTF-8"?>
  73. <ListBucketResult>
  74. <Name>testbucket-hf</Name>
  75. <EncodingType>url</EncodingType>
  76. <Prefix>php%2Fprefix</Prefix>
  77. <StartAfter>php%2Fmarker</StartAfter>
  78. <ContinuationToken>1gJiYw--</ContinuationToken>
  79. <NextContinuationToken>CgJiYw--</NextContinuationToken>
  80. <MaxKeys>1000</MaxKeys>
  81. <Delimiter>%2F</Delimiter>
  82. <IsTruncated>true</IsTruncated>
  83. <Contents>
  84. <Key>php/a%2Bb</Key>
  85. <LastModified>2015-11-18T03:36:00.000Z</LastModified>
  86. <ETag>"89B9E567E7EB8815F2F7D41851F9A2CD"</ETag>
  87. <Type>Normal</Type>
  88. <Size>13115</Size>
  89. <StorageClass>Standard</StorageClass>
  90. <Owner>
  91. <ID>cname_user</ID>
  92. <DisplayName>cname_user</DisplayName>
  93. </Owner>
  94. <RestoreInfo>ongoing-request="false", expiry-date="Tue, 25 Apr 2023 07:30:00 GMT"</RestoreInfo>
  95. </Contents>
  96. <KeyCount>1</KeyCount>
  97. </ListBucketResult>
  98. BBBB;
  99. public function testParseValidXml1()
  100. {
  101. $response = new ResponseCore(array(), $this->validXml1, 200);
  102. $result = new ListObjectsV2Result($response);
  103. $this->assertTrue($result->isOK());
  104. $this->assertNotNull($result->getData());
  105. $this->assertNotNull($result->getRawResponse());
  106. $objectListInfo = $result->getData();
  107. $this->assertEquals(2, count($objectListInfo->getPrefixList()));
  108. $this->assertEquals(0, count($objectListInfo->getObjectList()));
  109. $this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
  110. $this->assertEquals('', $objectListInfo->getPrefix());
  111. $this->assertEquals('', $objectListInfo->getStartAfter());
  112. $this->assertEquals(1000, $objectListInfo->getMaxKeys());
  113. $this->assertEquals('/', $objectListInfo->getDelimiter());
  114. $this->assertEquals('false', $objectListInfo->getIsTruncated());
  115. $this->assertEquals(0, $objectListInfo->getKeyCount());
  116. $prefixes = $objectListInfo->getPrefixList();
  117. $this->assertEquals('oss-php-sdk-test/', $prefixes[0]->getPrefix());
  118. $this->assertEquals('test/', $prefixes[1]->getPrefix());
  119. }
  120. public function testParseValidXml2()
  121. {
  122. $response = new ResponseCore(array(), $this->validXml2, 200);
  123. $result = new ListObjectsV2Result($response);
  124. $this->assertTrue($result->isOK());
  125. $this->assertNotNull($result->getData());
  126. $this->assertNotNull($result->getRawResponse());
  127. $objectListInfo = $result->getData();
  128. $this->assertEquals(0, count($objectListInfo->getPrefixList()));
  129. $this->assertEquals(1, count($objectListInfo->getObjectList()));
  130. $this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
  131. $this->assertEquals('oss-php-sdk-test/', $objectListInfo->getPrefix());
  132. $this->assertEquals('xx', $objectListInfo->getStartAfter());
  133. $this->assertEquals(1000, $objectListInfo->getMaxKeys());
  134. $this->assertEquals('/', $objectListInfo->getDelimiter());
  135. $this->assertEquals('false', $objectListInfo->getIsTruncated());
  136. $this->assertEquals(1, $objectListInfo->getKeyCount());
  137. $objects = $objectListInfo->getObjectList();
  138. $this->assertEquals('oss-php-sdk-test/upload-test-object-name.txt', $objects[0]->getKey());
  139. $this->assertEquals('2015-11-18T03:36:00.000Z', $objects[0]->getLastModified());
  140. $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objects[0]->getETag());
  141. $this->assertEquals('Normal', $objects[0]->getType());
  142. $this->assertEquals(13115, $objects[0]->getSize());
  143. $this->assertEquals('Standard', $objects[0]->getStorageClass());
  144. }
  145. public function testParseValidXmlWithEncodedKey()
  146. {
  147. $response = new ResponseCore(array(), $this->validXmlWithEncodedKey, 200);
  148. $result = new ListObjectsV2Result($response);
  149. $this->assertTrue($result->isOK());
  150. $this->assertNotNull($result->getData());
  151. $this->assertNotNull($result->getRawResponse());
  152. $objectListInfo = $result->getData();
  153. $this->assertEquals(0, count($objectListInfo->getPrefixList()));
  154. $this->assertEquals(1, count($objectListInfo->getObjectList()));
  155. $this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
  156. $this->assertEquals('php/prefix', $objectListInfo->getPrefix());
  157. $this->assertEquals('php/marker', $objectListInfo->getStartAfter());
  158. $this->assertEquals('CgJiYw--', $objectListInfo->getNextContinuationToken());
  159. $this->assertEquals('1gJiYw--', $objectListInfo->getContinuationToken());
  160. $this->assertEquals(1000, $objectListInfo->getMaxKeys());
  161. $this->assertEquals('/', $objectListInfo->getDelimiter());
  162. $this->assertEquals('true', $objectListInfo->getIsTruncated());
  163. $this->assertEquals(1, $objectListInfo->getKeyCount());
  164. $objects = $objectListInfo->getObjectList();
  165. $this->assertEquals('php/a+b', $objects[0]->getKey());
  166. $this->assertEquals('2015-11-18T03:36:00.000Z', $objects[0]->getLastModified());
  167. $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objects[0]->getETag());
  168. $this->assertEquals('Normal', $objects[0]->getType());
  169. $this->assertEquals(13115, $objects[0]->getSize());
  170. $this->assertEquals('Standard', $objects[0]->getStorageClass());
  171. }
  172. public function testParseValidXmlWithRestoreInfo()
  173. {
  174. $response = new ResponseCore(array(), $this->validXmlWithRestoreInfo, 200);
  175. $result = new ListObjectsV2Result($response);
  176. $this->assertTrue($result->isOK());
  177. $this->assertNotNull($result->getData());
  178. $this->assertNotNull($result->getRawResponse());
  179. $objectListInfo = $result->getData();
  180. $this->assertEquals(0, count($objectListInfo->getPrefixList()));
  181. $this->assertEquals(1, count($objectListInfo->getObjectList()));
  182. $this->assertEquals('testbucket-hf', $objectListInfo->getBucketName());
  183. $this->assertEquals('php/prefix', $objectListInfo->getPrefix());
  184. $this->assertEquals('php/marker', $objectListInfo->getStartAfter());
  185. $this->assertEquals('CgJiYw--', $objectListInfo->getNextContinuationToken());
  186. $this->assertEquals('1gJiYw--', $objectListInfo->getContinuationToken());
  187. $this->assertEquals(1000, $objectListInfo->getMaxKeys());
  188. $this->assertEquals('/', $objectListInfo->getDelimiter());
  189. $this->assertEquals('true', $objectListInfo->getIsTruncated());
  190. $this->assertEquals(1, $objectListInfo->getKeyCount());
  191. $objects = $objectListInfo->getObjectList();
  192. $this->assertEquals('php/a+b', $objects[0]->getKey());
  193. $this->assertEquals('2015-11-18T03:36:00.000Z', $objects[0]->getLastModified());
  194. $this->assertEquals('"89B9E567E7EB8815F2F7D41851F9A2CD"', $objects[0]->getETag());
  195. $this->assertEquals('Normal', $objects[0]->getType());
  196. $this->assertEquals(13115, $objects[0]->getSize());
  197. $this->assertEquals('Standard', $objects[0]->getStorageClass());
  198. $this->assertEquals('ongoing-request="false", expiry-date="Tue, 25 Apr 2023 07:30:00 GMT"', $objects[0]->getRestoreInfo());
  199. $this->assertEquals('cname_user', $objects[0]->getOwner()->getId());
  200. $this->assertEquals('cname_user', $objects[0]->getOwner()->getDisplayName());
  201. }
  202. }