aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/config/ConfigPhpTest.php
blob: a9aa80f5c89ba84690d40cbd54bc496d702b33c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Shaarli\Config;

/**
 * Class ConfigPhpTest
 *
 * We run tests in separate processes due to the usage for $GLOBALS
 * which are kept between tests.
 * @runTestsInSeparateProcesses
 */
class ConfigPhpTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var ConfigPhp
     */
    protected $configIO;

    protected function setUp(): void
    {
        $this->configIO = new ConfigPhp();
    }

    /**
     * Read a simple existing config file.
     */
    public function testRead()
    {
        $conf = $this->configIO->read('tests/utils/config/configPhp.php');
        $this->assertEquals('root', $conf['login']);
        $this->assertEquals('lala', $conf['redirector']);
        $this->assertEquals('data/datastore.php', $conf['config']['DATASTORE']);
        $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
    }

    /**
     * Read a non existent config file -> empty array.
     */
    public function testReadNonExistent()
    {
        $this->assertEquals(array(), $this->configIO->read('nope'));
    }

    /**
     * Read an empty existent config file -> array with blank default values.
     */
    public function testReadEmpty()
    {
        $dataFile = 'tests/utils/config/emptyConfigPhp.php';
        $conf = $this->configIO->read($dataFile);
        $this->assertEmpty($conf['login']);
        $this->assertEmpty($conf['title']);
        $this->assertEmpty($conf['titleLink']);
        $this->assertEmpty($conf['config']);
        $this->assertEmpty($conf['plugins']);
    }

    /**
     * Write a new config file.
     */
    public function testWriteNew()
    {
        $dataFile = 'tests/utils/config/configWrite.php';
        $data = array(
            'login' => 'root',
            'redirector' => 'lala',
            'config' => array(
                'DATASTORE' => 'data/datastore.php',
            ),
            'plugins' => array(
                'WALLABAG_VERSION' => '1',
            )
        );
        $this->configIO->write($dataFile, $data);
        $expected = '<?php 
$GLOBALS[\'login\'] = \'root\';
$GLOBALS[\'redirector\'] = \'lala\';
$GLOBALS[\'config\'][\'DATASTORE\'] = \'data/datastore.php\';
$GLOBALS[\'plugins\'][\'WALLABAG_VERSION\'] = \'1\';
';
        $this->assertEquals($expected, file_get_contents($dataFile));
        unlink($dataFile);
    }

    /**
     * Overwrite an existing setting.
     */
    public function testOverwrite()
    {
        $source = 'tests/utils/config/configPhp.php';
        $dest = 'tests/utils/config/configOverwrite.php';
        copy($source, $dest);
        $conf = $this->configIO->read($dest);
        $conf['redirector'] = 'blabla';
        $this->configIO->write($dest, $conf);
        $conf = $this->configIO->read($dest);
        $this->assertEquals('blabla', $conf['redirector']);
        unlink($dest);
    }
}