ConfigManagerTest.php 2.8 KB

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