ConfigPhpTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. require_once 'application/config/ConfigPhp.php';
  3. /**
  4. * Class ConfigPhpTest
  5. */
  6. class ConfigPhpTest extends PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @var ConfigPhp
  10. */
  11. protected $configIO;
  12. public function setUp()
  13. {
  14. $this->configIO = new ConfigPhp();
  15. }
  16. /**
  17. * Read a simple existing config file.
  18. */
  19. public function testRead()
  20. {
  21. $conf = $this->configIO->read('tests/utils/config/configPhp.php');
  22. $this->assertEquals('root', $conf['login']);
  23. $this->assertEquals('lala', $conf['redirector']);
  24. $this->assertEquals('data/datastore.php', $conf['config']['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. * Write a new config file.
  36. */
  37. public function testWriteNew()
  38. {
  39. $dataFile = 'tests/utils/config/configWrite.php';
  40. $data = array(
  41. 'login' => 'root',
  42. 'redirector' => 'lala',
  43. 'config' => array(
  44. 'DATASTORE' => 'data/datastore.php',
  45. ),
  46. 'plugins' => array(
  47. 'WALLABAG_VERSION' => '1',
  48. )
  49. );
  50. $this->configIO->write($dataFile, $data);
  51. $expected = '<?php
  52. $GLOBALS[\'login\'] = \'root\';
  53. $GLOBALS[\'redirector\'] = \'lala\';
  54. $GLOBALS[\'config\'][\'DATASTORE\'] = \'data/datastore.php\';
  55. $GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\';
  56. ';
  57. $this->assertEquals($expected, file_get_contents($dataFile));
  58. unlink($dataFile);
  59. }
  60. /**
  61. * Overwrite an existing setting.
  62. */
  63. public function testOverwrite()
  64. {
  65. $source = 'tests/utils/config/configPhp.php';
  66. $dest = 'tests/utils/config/configOverwrite.php';
  67. copy($source, $dest);
  68. $conf = $this->configIO->read($dest);
  69. $conf['redirector'] = 'blabla';
  70. $this->configIO->write($dest, $conf);
  71. $conf = $this->configIO->read($dest);
  72. $this->assertEquals('blabla', $conf['redirector']);
  73. unlink($dest);
  74. }
  75. }