ConfigManagerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace clagiordano\weblibs\configmanager\tests;
  3. use clagiordano\weblibs\configmanager\ConfigManager;
  4. /**
  5. * Class ConfigManagerTest
  6. * @package clagiordano\weblibs\configmanager\tests
  7. */
  8. class ConfigManagerTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /** @var ConfigManager $config */
  11. private $config = null;
  12. private $configFile = 'testsdata/sample_config_data.php';
  13. public function setUp()
  14. {
  15. $this->config = new ConfigManager("TestConfigData.php");
  16. $this->assertInstanceOf('clagiordano\weblibs\configmanager\ConfigManager', $this->config);
  17. $this->assertFileExists($this->configFile);
  18. $this->config->loadConfig($this->configFile);
  19. }
  20. public function testBasicUsage()
  21. {
  22. $this->assertNotNull(
  23. $this->config->getValue('app')
  24. );
  25. }
  26. public function testFastUsage()
  27. {
  28. $this->assertNotNull(
  29. $this->config->getValue('app')
  30. );
  31. }
  32. public function testFastInvalidKey()
  33. {
  34. $this->assertNull(
  35. $this->config->getValue('invalidKey')
  36. );
  37. }
  38. public function testFastInvalidKeyWithDefault()
  39. {
  40. $this->assertEquals(
  41. $this->config->getValue('invalidKey', 'defaultValue'),
  42. 'defaultValue'
  43. );
  44. }
  45. public function testFastNestedConfig()
  46. {
  47. $this->assertNotNull(
  48. $this->config->getValue('other.multi.deep.nested')
  49. );
  50. }
  51. public function testCheckExistConfig()
  52. {
  53. $this->assertTrue(
  54. $this->config->existValue('other.multi.deep.nested')
  55. );
  56. }
  57. public function testCheckNotExistConfig()
  58. {
  59. $this->assertFalse(
  60. $this->config->existValue('invalid.config.path')
  61. );
  62. }
  63. public function testSetValue()
  64. {
  65. $this->config->setValue('other.multi.deep.nested', __FUNCTION__);
  66. $this->assertEquals(
  67. $this->config->getValue('other.multi.deep.nested'),
  68. __FUNCTION__
  69. );
  70. }
  71. public function testFailedSaveConfig()
  72. {
  73. $this->setExpectedException('Exception');
  74. $this->config->saveConfigFile('/invalid/path');
  75. }
  76. public function testSuccessSaveConfigOnTempAndReload()
  77. {
  78. $this->config->setValue('other.multi.deep.nested', "SUPERNESTED");
  79. $this->config->saveConfigFile("/tmp/testconfig.php", true);
  80. $this->assertEquals(
  81. $this->config->getValue('other.multi.deep.nested'),
  82. "SUPERNESTED"
  83. );
  84. }
  85. public function testOverwriteSameConfigFile()
  86. {
  87. $this->config->saveConfigFile();
  88. }
  89. public function testFailWriteConfig()
  90. {
  91. $this->setExpectedException('\RuntimeException');
  92. $this->config->saveConfigFile('/invalid/path/test.php');
  93. }
  94. }