CnameConfigTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace OSS\Tests;
  3. use OSS\Model\CnameConfig;
  4. use OSS\Core\OssException;
  5. class CnameConfigTest extends \PHPUnit_Framework_TestCase
  6. {
  7. private $xml1 = <<<BBBB
  8. <?xml version="1.0" encoding="utf-8"?>
  9. <BucketCnameConfiguration>
  10. <Cname>
  11. <Domain>www.foo.com</Domain>
  12. <Status>enabled</Status>
  13. <LastModified>20150101</LastModified>
  14. </Cname>
  15. <Cname>
  16. <Domain>bar.com</Domain>
  17. <Status>disabled</Status>
  18. <LastModified>20160101</LastModified>
  19. </Cname>
  20. </BucketCnameConfiguration>
  21. BBBB;
  22. public function testFromXml()
  23. {
  24. $cnameConfig = new CnameConfig();
  25. $cnameConfig->parseFromXml($this->xml1);
  26. $cnames = $cnameConfig->getCnames();
  27. $this->assertEquals(2, count($cnames));
  28. $this->assertEquals('www.foo.com', $cnames[0]['Domain']);
  29. $this->assertEquals('enabled', $cnames[0]['Status']);
  30. $this->assertEquals('20150101', $cnames[0]['LastModified']);
  31. $this->assertEquals('bar.com', $cnames[1]['Domain']);
  32. $this->assertEquals('disabled', $cnames[1]['Status']);
  33. $this->assertEquals('20160101', $cnames[1]['LastModified']);
  34. }
  35. public function testToXml()
  36. {
  37. $cnameConfig = new CnameConfig();
  38. $cnameConfig->addCname('www.foo.com');
  39. $cnameConfig->addCname('bar.com');
  40. $xml = $cnameConfig->serializeToXml();
  41. $comp = new CnameConfig();
  42. $comp->parseFromXml($xml);
  43. $cnames1 = $cnameConfig->getCnames();
  44. $cnames2 = $comp->getCnames();
  45. $this->assertEquals(count($cnames1), count($cnames2));
  46. $this->assertEquals(count($cnames1[0]), count($cnames2[0]));
  47. $this->assertEquals(1, count($cnames1[0]));
  48. $this->assertEquals($cnames1[0]['Domain'], $cnames2[0]['Domain']);
  49. }
  50. public function testCnameNumberLimit()
  51. {
  52. $cnameConfig = new CnameConfig();
  53. for ($i = 0; $i < CnameConfig::OSS_MAX_RULES; $i += 1) {
  54. $cnameConfig->addCname(strval($i) . '.foo.com');
  55. }
  56. try {
  57. $cnameConfig->addCname('www.foo.com');
  58. $this->assertFalse(true);
  59. } catch (OssException $e) {
  60. $this->assertEquals(
  61. $e->getMessage(),
  62. "num of cname in the config exceeds self::OSS_MAX_RULES: " . strval(CnameConfig::OSS_MAX_RULES));
  63. }
  64. }
  65. }