aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/config/ConfigJsonTest.php
blob: 5b3bce46559b03365f1fe20a99c2e5bb4e2f12e0 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

require_once 'application/config/ConfigJson.php';

/**
 * Class ConfigJsonTest
 */
class ConfigJsonTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var ConfigJson
     */
    protected $configIO;

    public function setUp()
    {
        $this->configIO = new ConfigJson();
    }

    /**
     * Read a simple existing config file.
     */
    public function testRead()
    {
        $conf = $this->configIO->read('tests/utils/config/configJson.json.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 a non existent config file -> empty array.
     *
     * @expectedException Exception
     * @expectedExceptionMessage An error occured while parsing JSON file: error code #4
     */
    public function testReadInvalidJson()
    {
        $this->configIO->read('tests/utils/config/configInvalid.json.php');
    }

    /**
     * Write a new config file.
     */
    public function testWriteNew()
    {
        $dataFile = 'tests/utils/config/configWrite.json.php';
        $data = array(
            'login' => 'root',
            'redirector' => 'lala',
            'config' => array(
                'DATASTORE' => 'data/datastore.php',
            ),
            'plugins' => array(
                'WALLABAG_VERSION' => '1',
            )
        );
        $this->configIO->write($dataFile, $data);
        // PHP 5.3 doesn't support json pretty print.
        if (defined('JSON_PRETTY_PRINT')) {
            $expected = '{
    "login": "root",
    "redirector": "lala",
    "config": {
        "DATASTORE": "data\/datastore.php"
    },
    "plugins": {
        "WALLABAG_VERSION": "1"
    }
}';
        } else {
            $expected = '{"login":"root","redirector":"lala","config":{"DATASTORE":"data\/datastore.php"},"plugins":{"WALLABAG_VERSION":"1"}}';
        }
        $expected = ConfigJson::$PHP_HEADER . $expected;
        $this->assertEquals($expected, file_get_contents($dataFile));
        unlink($dataFile);
    }

    /**
     * Overwrite an existing setting.
     */
    public function testOverwrite()
    {
        $source = 'tests/utils/config/configJson.json.php';
        $dest = 'tests/utils/config/configOverwrite.json.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);
    }

    /**
     * Write to invalid path.
     *
     * @expectedException IOException
     */
    public function testWriteInvalidArray()
    {
        $conf = array('conf' => 'value');
        @$this->configIO->write(array(), $conf);
    }

    /**
     * Write to invalid path.
     *
     * @expectedException IOException
     */
    public function testWriteInvalidBlank()
    {
        $conf = array('conf' => 'value');
        @$this->configIO->write('', $conf);
    }
}