configuration.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. class configurationTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_options;
  5. public function setUp()
  6. {
  7. /* Setup Routine */
  8. helper::confBackup();
  9. $this->_options = configuration::getDefaults();
  10. $this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
  11. $this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
  12. }
  13. public function tearDown()
  14. {
  15. /* Tear Down Routine */
  16. helper::confRestore();
  17. }
  18. public function testDefaultConfigFile()
  19. {
  20. $this->assertTrue(copy(CONF . '.bak', CONF), 'copy default configuration file');
  21. $conf = new configuration;
  22. $this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
  23. }
  24. public function testHandleFreshConfigFile()
  25. {
  26. helper::createIniFile(CONF, $this->_options);
  27. $conf = new configuration;
  28. $this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
  29. }
  30. public function testHandleMissingConfigFile()
  31. {
  32. @unlink(CONF);
  33. $conf = new configuration;
  34. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
  35. }
  36. /**
  37. * @expectedException Exception
  38. * @expectedExceptionCode 2
  39. */
  40. public function testHandleBlankConfigFile()
  41. {
  42. file_put_contents(CONF, '');
  43. new configuration;
  44. }
  45. public function testHandleMinimalConfigFile()
  46. {
  47. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  48. $conf = new configuration;
  49. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
  50. }
  51. /**
  52. * @expectedException Exception
  53. * @expectedExceptionCode 3
  54. */
  55. public function testHandleInvalidSection()
  56. {
  57. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  58. $conf = new configuration;
  59. $conf->getKey('foo', 'bar');
  60. }
  61. /**
  62. * @expectedException Exception
  63. * @expectedExceptionCode 4
  64. */
  65. public function testHandleInvalidKey()
  66. {
  67. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  68. $conf = new configuration;
  69. $conf->getKey('foo');
  70. }
  71. public function testHandleGetKey()
  72. {
  73. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  74. $conf = new configuration;
  75. $this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
  76. }
  77. public function testHandleWrongTypes()
  78. {
  79. $original_options = $this->_options;
  80. $original_options['main']['syntaxhighlightingtheme'] = 'foo';
  81. $options = $original_options;
  82. $options['main']['discussion'] = 'true';
  83. $options['main']['opendiscussion'] = 0;
  84. $options['main']['password'] = -1; // evaluates to TRUE
  85. $options['main']['fileupload'] = 'false';
  86. $options['expire_options']['foo'] = 'bar';
  87. $options['formatter_options'][] = 'foo';
  88. helper::createIniFile(CONF, $options);
  89. $conf = new configuration;
  90. $original_options['expire_options']['foo'] = intval('bar');
  91. $original_options['formatter_options'][0] = 'foo';
  92. $this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
  93. }
  94. public function testHandleMissingSubKeys()
  95. {
  96. $options = $this->_options;
  97. unset($options['expire_options']['1week']);
  98. unset($options['expire_options']['1year']);
  99. unset($options['expire_options']['never']);
  100. helper::createIniFile(CONF, $options);
  101. $conf = new configuration;
  102. $options['expire']['default'] = '5min';
  103. $this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
  104. }
  105. }