ConfigPhpTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Shaarli\Config;
  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. * Read an empty existent config file -> array with blank default values.
  36. */
  37. public function testReadEmpty()
  38. {
  39. $dataFile = 'tests/utils/config/emptyConfigPhp.php';
  40. $conf = $this->configIO->read($dataFile);
  41. $this->assertEmpty($conf['login']);
  42. $this->assertEmpty($conf['title']);
  43. $this->assertEmpty($conf['titleLink']);
  44. $this->assertEmpty($conf['config']);
  45. $this->assertEmpty($conf['plugins']);
  46. }
  47. /**
  48. * Write a new config file.
  49. */
  50. public function testWriteNew()
  51. {
  52. $dataFile = 'tests/utils/config/configWrite.php';
  53. $data = array(
  54. 'login' => 'root',
  55. 'redirector' => 'lala',
  56. 'config' => array(
  57. 'DATASTORE' => 'data/datastore.php',
  58. ),
  59. 'plugins' => array(
  60. 'WALLABAG_VERSION' => '1',
  61. )
  62. );
  63. $this->configIO->write($dataFile, $data);
  64. $expected = '<?php
  65. $GLOBALS[\'login\'] = \'root\';
  66. $GLOBALS[\'redirector\'] = \'lala\';
  67. $GLOBALS[\'config\'][\'DATASTORE\'] = \'data/datastore.php\';
  68. $GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\';
  69. ';
  70. $this->assertEquals($expected, file_get_contents($dataFile));
  71. unlink($dataFile);
  72. }
  73. /**
  74. * Overwrite an existing setting.
  75. */
  76. public function testOverwrite()
  77. {
  78. $source = 'tests/utils/config/configPhp.php';
  79. $dest = 'tests/utils/config/configOverwrite.php';
  80. copy($source, $dest);
  81. $conf = $this->configIO->read($dest);
  82. $conf['redirector'] = 'blabla';
  83. $this->configIO->write($dest, $conf);
  84. $conf = $this->configIO->read($dest);
  85. $this->assertEquals('blabla', $conf['redirector']);
  86. unlink($dest);
  87. }
  88. }