]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/config/ConfigPhpTest.php
Run Unit Tests against PHP 7.4
[github/shaarli/Shaarli.git] / tests / config / ConfigPhpTest.php
1 <?php
2 namespace Shaarli\Config;
3
4 /**
5 * Class ConfigPhpTest
6 *
7 * We run tests in separate processes due to the usage for $GLOBALS
8 * which are kept between tests.
9 * @runTestsInSeparateProcesses
10 */
11 class ConfigPhpTest extends \PHPUnit\Framework\TestCase
12 {
13 /**
14 * @var ConfigPhp
15 */
16 protected $configIO;
17
18 public function setUp()
19 {
20 $this->configIO = new ConfigPhp();
21 }
22
23 /**
24 * Read a simple existing config file.
25 */
26 public function testRead()
27 {
28 $conf = $this->configIO->read('tests/utils/config/configPhp.php');
29 $this->assertEquals('root', $conf['login']);
30 $this->assertEquals('lala', $conf['redirector']);
31 $this->assertEquals('data/datastore.php', $conf['config']['DATASTORE']);
32 $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
33 }
34
35 /**
36 * Read a non existent config file -> empty array.
37 */
38 public function testReadNonExistent()
39 {
40 $this->assertEquals(array(), $this->configIO->read('nope'));
41 }
42
43 /**
44 * Read an empty existent config file -> array with blank default values.
45 */
46 public function testReadEmpty()
47 {
48 $dataFile = 'tests/utils/config/emptyConfigPhp.php';
49 $conf = $this->configIO->read($dataFile);
50 $this->assertEmpty($conf['login']);
51 $this->assertEmpty($conf['title']);
52 $this->assertEmpty($conf['titleLink']);
53 $this->assertEmpty($conf['config']);
54 $this->assertEmpty($conf['plugins']);
55 }
56
57 /**
58 * Write a new config file.
59 */
60 public function testWriteNew()
61 {
62 $dataFile = 'tests/utils/config/configWrite.php';
63 $data = array(
64 'login' => 'root',
65 'redirector' => 'lala',
66 'config' => array(
67 'DATASTORE' => 'data/datastore.php',
68 ),
69 'plugins' => array(
70 'WALLABAG_VERSION' => '1',
71 )
72 );
73 $this->configIO->write($dataFile, $data);
74 $expected = '<?php
75 $GLOBALS[\'login\'] = \'root\';
76 $GLOBALS[\'redirector\'] = \'lala\';
77 $GLOBALS[\'config\'][\'DATASTORE\'] = \'data/datastore.php\';
78 $GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\';
79 ';
80 $this->assertEquals($expected, file_get_contents($dataFile));
81 unlink($dataFile);
82 }
83
84 /**
85 * Overwrite an existing setting.
86 */
87 public function testOverwrite()
88 {
89 $source = 'tests/utils/config/configPhp.php';
90 $dest = 'tests/utils/config/configOverwrite.php';
91 copy($source, $dest);
92 $conf = $this->configIO->read($dest);
93 $conf['redirector'] = 'blabla';
94 $this->configIO->write($dest, $conf);
95 $conf = $this->configIO->read($dest);
96 $this->assertEquals('blabla', $conf['redirector']);
97 unlink($dest);
98 }
99 }