ConfigJsonTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Shaarli\Config;
  3. /**
  4. * Class ConfigJsonTest
  5. */
  6. class ConfigJsonTest extends \PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @var ConfigJson
  10. */
  11. protected $configIO;
  12. public function setUp()
  13. {
  14. $this->configIO = new ConfigJson();
  15. }
  16. /**
  17. * Read a simple existing config file.
  18. */
  19. public function testRead()
  20. {
  21. $conf = $this->configIO->read('tests/utils/config/configJson.json.php');
  22. $this->assertEquals('root', $conf['credentials']['login']);
  23. $this->assertEquals('lala', $conf['redirector']['url']);
  24. $this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']);
  25. $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
  26. }
  27. /**
  28. * Read a non existent config file -> empty array.
  29. */
  30. public function testReadNonExistent()
  31. {
  32. $this->assertEquals(array(), $this->configIO->read('nope'));
  33. }
  34. /**
  35. * Read a non existent config file -> empty array.
  36. *
  37. * @expectedException \Exception
  38. * @expectedExceptionMessageRegExp /An error occurred while parsing JSON configuration file \([\w\/\.]+\): error code #4/
  39. */
  40. public function testReadInvalidJson()
  41. {
  42. $this->configIO->read('tests/utils/config/configInvalid.json.php');
  43. }
  44. /**
  45. * Write a new config file.
  46. */
  47. public function testWriteNew()
  48. {
  49. $dataFile = 'tests/utils/config/configWrite.json.php';
  50. $data = array(
  51. 'credentials' => array(
  52. 'login' => 'root',
  53. ),
  54. 'resource' => array(
  55. 'datastore' => 'data/datastore.php',
  56. ),
  57. 'redirector' => array(
  58. 'url' => 'lala',
  59. ),
  60. 'plugins' => array(
  61. 'WALLABAG_VERSION' => '1',
  62. )
  63. );
  64. $this->configIO->write($dataFile, $data);
  65. // PHP 5.3 doesn't support json pretty print.
  66. if (defined('JSON_PRETTY_PRINT')) {
  67. $expected = '{
  68. "credentials": {
  69. "login": "root"
  70. },
  71. "resource": {
  72. "datastore": "data\/datastore.php"
  73. },
  74. "redirector": {
  75. "url": "lala"
  76. },
  77. "plugins": {
  78. "WALLABAG_VERSION": "1"
  79. }
  80. }';
  81. } else {
  82. $expected = '{"credentials":{"login":"root"},"resource":{"datastore":"data\/datastore.php"},"redirector":{"url":"lala"},"plugins":{"WALLABAG_VERSION":"1"}}';
  83. }
  84. $expected = ConfigJson::getPhpHeaders() . $expected . ConfigJson::getPhpSuffix();
  85. $this->assertEquals($expected, file_get_contents($dataFile));
  86. unlink($dataFile);
  87. }
  88. /**
  89. * Overwrite an existing setting.
  90. */
  91. public function testOverwrite()
  92. {
  93. $source = 'tests/utils/config/configJson.json.php';
  94. $dest = 'tests/utils/config/configOverwrite.json.php';
  95. copy($source, $dest);
  96. $conf = $this->configIO->read($dest);
  97. $conf['redirector']['url'] = 'blabla';
  98. $this->configIO->write($dest, $conf);
  99. $conf = $this->configIO->read($dest);
  100. $this->assertEquals('blabla', $conf['redirector']['url']);
  101. unlink($dest);
  102. }
  103. /**
  104. * Write to invalid path.
  105. *
  106. * @expectedException \IOException
  107. */
  108. public function testWriteInvalidArray()
  109. {
  110. $conf = array('conf' => 'value');
  111. @$this->configIO->write(array(), $conf);
  112. }
  113. /**
  114. * Write to invalid path.
  115. *
  116. * @expectedException \IOException
  117. */
  118. public function testWriteInvalidBlank()
  119. {
  120. $conf = array('conf' => 'value');
  121. @$this->configIO->write('', $conf);
  122. }
  123. }