ConfigManagerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Shaarli\Config;
  3. /**
  4. * Unit tests for Class ConfigManagerTest
  5. *
  6. * Note: it only test the manager with ConfigJson,
  7. * ConfigPhp is only a workaround to handle the transition to JSON type.
  8. */
  9. class ConfigManagerTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var ConfigManager
  13. */
  14. protected $conf;
  15. public function setUp()
  16. {
  17. $this->conf = new ConfigManager('tests/utils/config/configJson');
  18. }
  19. /**
  20. * Simple config test:
  21. * 1. Set settings.
  22. * 2. Check settings value.
  23. */
  24. public function testSetGet()
  25. {
  26. $this->conf->set('paramInt', 42);
  27. $this->conf->set('paramString', 'value1');
  28. $this->conf->set('paramBool', false);
  29. $this->conf->set('paramArray', array('foo' => 'bar'));
  30. $this->conf->set('paramNull', null);
  31. $this->assertEquals(42, $this->conf->get('paramInt'));
  32. $this->assertEquals('value1', $this->conf->get('paramString'));
  33. $this->assertFalse($this->conf->get('paramBool'));
  34. $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
  35. $this->assertEquals(null, $this->conf->get('paramNull'));
  36. }
  37. /**
  38. * Set/write/get config test:
  39. * 1. Set settings.
  40. * 2. Write it to the config file.
  41. * 3. Read the file.
  42. * 4. Check settings value.
  43. */
  44. public function testSetWriteGet()
  45. {
  46. $this->conf->set('paramInt', 42);
  47. $this->conf->set('paramString', 'value1');
  48. $this->conf->set('paramBool', false);
  49. $this->conf->set('paramArray', array('foo' => 'bar'));
  50. $this->conf->set('paramNull', null);
  51. $this->conf->setConfigFile('tests/utils/config/configTmp');
  52. $this->conf->write(true);
  53. $this->conf->reload();
  54. unlink($this->conf->getConfigFileExt());
  55. $this->assertEquals(42, $this->conf->get('paramInt'));
  56. $this->assertEquals('value1', $this->conf->get('paramString'));
  57. $this->assertFalse($this->conf->get('paramBool'));
  58. $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
  59. $this->assertEquals(null, $this->conf->get('paramNull'));
  60. }
  61. /**
  62. * Test set/write/get with nested keys.
  63. */
  64. public function testSetWriteGetNested()
  65. {
  66. $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');
  67. $this->conf->setConfigFile('tests/utils/config/configTmp');
  68. $this->conf->write(true);
  69. $this->conf->reload();
  70. unlink($this->conf->getConfigFileExt());
  71. $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
  72. }
  73. public function testSetDeleteNested()
  74. {
  75. $this->conf->set('foo.bar.key.stuff', 'testSetDeleteNested');
  76. $this->assertTrue($this->conf->exists('foo.bar'));
  77. $this->assertTrue($this->conf->exists('foo.bar.key.stuff'));
  78. $this->assertEquals('testSetDeleteNested', $this->conf->get('foo.bar.key.stuff'));
  79. $this->conf->remove('foo.bar');
  80. $this->assertFalse($this->conf->exists('foo.bar.key.stuff'));
  81. $this->assertFalse($this->conf->exists('foo.bar'));
  82. }
  83. /**
  84. * Set with an empty key.
  85. *
  86. * @expectedException \Exception
  87. * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
  88. */
  89. public function testSetEmptyKey()
  90. {
  91. $this->conf->set('', 'stuff');
  92. }
  93. /**
  94. * Set with an array key.
  95. *
  96. * @expectedException \Exception
  97. * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
  98. */
  99. public function testSetArrayKey()
  100. {
  101. $this->conf->set(array('foo' => 'bar'), 'stuff');
  102. }
  103. /**
  104. * Remove with an empty key.
  105. *
  106. * @expectedException \Exception
  107. * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
  108. */
  109. public function testRmoveEmptyKey()
  110. {
  111. $this->conf->remove('');
  112. }
  113. /**
  114. * Try to write the config without mandatory parameter (e.g. 'login').
  115. *
  116. * @expectedException Shaarli\Config\Exception\MissingFieldConfigException
  117. */
  118. public function testWriteMissingParameter()
  119. {
  120. $this->conf->setConfigFile('tests/utils/config/configTmp');
  121. $this->assertFalse(file_exists($this->conf->getConfigFileExt()));
  122. $this->conf->reload();
  123. $this->conf->write(true);
  124. }
  125. /**
  126. * Try to get non existent config keys.
  127. */
  128. public function testGetNonExistent()
  129. {
  130. $this->assertEquals('', $this->conf->get('nope.test'));
  131. $this->assertEquals('default', $this->conf->get('nope.test', 'default'));
  132. }
  133. /**
  134. * Test the 'exists' method with existent values.
  135. */
  136. public function testExistsOk()
  137. {
  138. $this->assertTrue($this->conf->exists('credentials.login'));
  139. $this->assertTrue($this->conf->exists('config.foo'));
  140. }
  141. /**
  142. * Test the 'exists' method with non existent or invalid values.
  143. */
  144. public function testExistsKo()
  145. {
  146. $this->assertFalse($this->conf->exists('nope'));
  147. $this->assertFalse($this->conf->exists('nope.nope'));
  148. $this->assertFalse($this->conf->exists(''));
  149. $this->assertFalse($this->conf->exists(false));
  150. }
  151. /**
  152. * Reset the ConfigManager instance.
  153. */
  154. public function testReset()
  155. {
  156. $confIO = $this->conf->getConfigIO();
  157. $this->conf->reset();
  158. $this->assertFalse($confIO === $this->conf->getConfigIO());
  159. }
  160. /**
  161. * Reload the config from file.
  162. */
  163. public function testReload()
  164. {
  165. $this->conf->setConfigFile('tests/utils/config/configTmp');
  166. $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
  167. file_put_contents($this->conf->getConfigFileExt(), $newConf);
  168. $this->conf->reload();
  169. unlink($this->conf->getConfigFileExt());
  170. // Previous conf no longer exists, and new values have been loaded.
  171. $this->assertFalse($this->conf->exists('credentials.login'));
  172. $this->assertEquals('value', $this->conf->get('key'));
  173. }
  174. }