LifecycleConfigTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Core\OssException;
  4. use OSS\Model\LifecycleAction;
  5. use OSS\Model\LifecycleConfig;
  6. use OSS\Model\LifecycleRule;
  7. class LifecycleConfigTest extends \PHPUnit_Framework_TestCase
  8. {
  9. private $validLifecycle = <<<BBBB
  10. <?xml version="1.0" encoding="utf-8"?>
  11. <LifecycleConfiguration>
  12. <Rule>
  13. <ID>delete obsoleted files</ID>
  14. <Prefix>obsoleted/</Prefix>
  15. <Status>Enabled</Status>
  16. <Expiration><Days>3</Days></Expiration>
  17. </Rule>
  18. <Rule>
  19. <ID>delete temporary files</ID>
  20. <Prefix>temporary/</Prefix>
  21. <Status>Enabled</Status>
  22. <Expiration><Date>2022-10-12T00:00:00.000Z</Date></Expiration>
  23. <Expiration2><Date>2022-10-12T00:00:00.000Z</Date></Expiration2>
  24. </Rule>
  25. </LifecycleConfiguration>
  26. BBBB;
  27. private $validLifecycle2 = <<<BBBB
  28. <?xml version="1.0" encoding="utf-8"?>
  29. <LifecycleConfiguration>
  30. <Rule><ID>delete temporary files</ID>
  31. <Prefix>temporary/</Prefix>
  32. <Status>Enabled</Status>
  33. <Expiration><Date>2022-10-12T00:00:00.000Z</Date></Expiration>
  34. <Expiration2><Date>2022-10-12T00:00:00.000Z</Date></Expiration2>
  35. </Rule>
  36. </LifecycleConfiguration>
  37. BBBB;
  38. private $nullLifecycle = <<<BBBB
  39. <?xml version="1.0" encoding="utf-8"?>
  40. <LifecycleConfiguration/>
  41. BBBB;
  42. public function testConstructValidConfig()
  43. {
  44. $lifecycleConfig = new LifecycleConfig();
  45. $actions = array();
  46. $actions[] = new LifecycleAction("Expiration", "Days", 3);
  47. $lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions);
  48. $lifecycleConfig->addRule($lifecycleRule);
  49. $actions = array();
  50. $actions[] = new LifecycleAction("Expiration", "Date", '2022-10-12T00:00:00.000Z');
  51. $actions[] = new LifecycleAction("Expiration2", "Date", '2022-10-12T00:00:00.000Z');
  52. $lifecycleRule = new LifecycleRule("delete temporary files", "temporary/", "Enabled", $actions);
  53. $lifecycleConfig->addRule($lifecycleRule);
  54. try {
  55. $lifecycleConfig->addRule(null);
  56. $this->assertFalse(true);
  57. } catch (OssException $e) {
  58. $this->assertEquals('lifecycleRule is null', $e->getMessage());
  59. }
  60. $this->assertEquals($this->cleanXml(strval($lifecycleConfig)), $this->cleanXml($this->validLifecycle));
  61. }
  62. public function testParseValidXml()
  63. {
  64. $lifecycleConfig = new LifecycleConfig();
  65. $lifecycleConfig->parseFromXml($this->validLifecycle);
  66. $this->assertEquals($this->cleanXml($lifecycleConfig->serializeToXml()), $this->cleanXml($this->validLifecycle));
  67. $this->assertEquals(2, count($lifecycleConfig->getRules()));
  68. $rules = $lifecycleConfig->getRules();
  69. $this->assertEquals('delete temporary files', $rules[1]->getId());
  70. }
  71. public function testParseValidXml2()
  72. {
  73. $lifecycleConfig = new LifecycleConfig();
  74. $lifecycleConfig->parseFromXml($this->validLifecycle2);
  75. $this->assertEquals($this->cleanXml($lifecycleConfig->serializeToXml()), $this->cleanXml($this->validLifecycle2));
  76. $this->assertEquals(1, count($lifecycleConfig->getRules()));
  77. $rules = $lifecycleConfig->getRules();
  78. $this->assertEquals('delete temporary files', $rules[0]->getId());
  79. }
  80. public function testParseNullXml()
  81. {
  82. $lifecycleConfig = new LifecycleConfig();
  83. $lifecycleConfig->parseFromXml($this->nullLifecycle);
  84. $this->assertEquals($this->cleanXml($lifecycleConfig->serializeToXml()), $this->cleanXml($this->nullLifecycle));
  85. $this->assertEquals(0, count($lifecycleConfig->getRules()));
  86. }
  87. public function testLifecycleRule()
  88. {
  89. $lifecycleRule = new LifecycleRule("x", "x", "x", array('x'));
  90. $lifecycleRule->setId("id");
  91. $lifecycleRule->setPrefix("prefix");
  92. $lifecycleRule->setStatus("Enabled");
  93. $lifecycleRule->setActions(array());
  94. $this->assertEquals('id', $lifecycleRule->getId());
  95. $this->assertEquals('prefix', $lifecycleRule->getPrefix());
  96. $this->assertEquals('Enabled', $lifecycleRule->getStatus());
  97. $this->assertEmpty($lifecycleRule->getActions());
  98. }
  99. public function testLifecycleAction()
  100. {
  101. $action = new LifecycleAction('x', 'x', 'x');
  102. $this->assertEquals($action->getAction(), 'x');
  103. $this->assertEquals($action->getTimeSpec(), 'x');
  104. $this->assertEquals($action->getTimeValue(), 'x');
  105. $action->setAction('y');
  106. $action->setTimeSpec('y');
  107. $action->setTimeValue('y');
  108. $this->assertEquals($action->getAction(), 'y');
  109. $this->assertEquals($action->getTimeSpec(), 'y');
  110. $this->assertEquals($action->getTimeValue(), 'y');
  111. }
  112. private function cleanXml($xml)
  113. {
  114. return str_replace("\n", "", str_replace("\r", "", $xml));
  115. }
  116. }