ConfigManagerTest.php 5.2 KB

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