aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/config/ConfigManagerTest.php
blob: 436e3d673850245ecd16c027716df25e94edc5ca (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php

/**
 * Unit tests for Class ConfigManagerTest
 *
 * Note: it only test the manager with ConfigJson,
 *  ConfigPhp is only a workaround to handle the transition to JSON type.
 */
class ConfigManagerTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var ConfigManager
     */
    protected $conf;

    public function setUp()
    {
        $this->conf = new ConfigManager('tests/utils/config/configJson');
    }

    /**
     * Simple config test:
     *   1. Set settings.
     *   2. Check settings value.
     */
    public function testSetGet()
    {
        $this->conf->set('paramInt', 42);
        $this->conf->set('paramString', 'value1');
        $this->conf->set('paramBool', false);
        $this->conf->set('paramArray', array('foo' => 'bar'));
        $this->conf->set('paramNull', null);

        $this->assertEquals(42, $this->conf->get('paramInt'));
        $this->assertEquals('value1', $this->conf->get('paramString'));
        $this->assertFalse($this->conf->get('paramBool'));
        $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
        $this->assertEquals(null, $this->conf->get('paramNull'));
    }

    /**
     * Set/write/get config test:
     *   1. Set settings.
     *   2. Write it to the config file.
     *   3. Read the file.
     *   4. Check settings value.
     */
    public function testSetWriteGet()
    {
        $this->conf->set('paramInt', 42);
        $this->conf->set('paramString', 'value1');
        $this->conf->set('paramBool', false);
        $this->conf->set('paramArray', array('foo' => 'bar'));
        $this->conf->set('paramNull', null);

        $this->conf->setConfigFile('tests/utils/config/configTmp');
        $this->conf->write(true);
        $this->conf->reload();
        unlink($this->conf->getConfigFileExt());

        $this->assertEquals(42, $this->conf->get('paramInt'));
        $this->assertEquals('value1', $this->conf->get('paramString'));
        $this->assertFalse($this->conf->get('paramBool'));
        $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray'));
        $this->assertEquals(null, $this->conf->get('paramNull'));
    }

    /**
     * Test set/write/get with nested keys.
     */
    public function testSetWriteGetNested()
    {
        $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested');

        $this->conf->setConfigFile('tests/utils/config/configTmp');
        $this->conf->write(true);
        $this->conf->reload();
        unlink($this->conf->getConfigFileExt());

        $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff'));
    }

    /**
     * Set with an empty key.
     *
     * @expectedException Exception
     * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
     */
    public function testSetEmptyKey()
    {
        $this->conf->set('', 'stuff');
    }

    /**
     * Set with an array key.
     *
     * @expectedException Exception
     * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*#
     */
    public function testSetArrayKey()
    {
        $this->conf->set(array('foo' => 'bar'), 'stuff');
    }

    /**
     * Try to write the config without mandatory parameter (e.g. 'login').
     *
     * @expectedException MissingFieldConfigException
     */
    public function testWriteMissingParameter()
    {
        $this->conf->setConfigFile('tests/utils/config/configTmp');
        $this->assertFalse(file_exists($this->conf->getConfigFileExt()));
        $this->conf->reload();

        $this->conf->write(true);
    }

    /**
     * Try to get non existent config keys.
     */
    public function testGetNonExistent()
    {
        $this->assertEquals('', $this->conf->get('nope.test'));
        $this->assertEquals('default', $this->conf->get('nope.test', 'default'));
    }

    /**
     * Test the 'exists' method with existent values.
     */
    public function testExistsOk()
    {
        $this->assertTrue($this->conf->exists('credentials.login'));
        $this->assertTrue($this->conf->exists('config.foo'));
    }

    /**
     * Test the 'exists' method with non existent or invalid values.
     */
    public function testExistsKo()
    {
        $this->assertFalse($this->conf->exists('nope'));
        $this->assertFalse($this->conf->exists('nope.nope'));
        $this->assertFalse($this->conf->exists(''));
        $this->assertFalse($this->conf->exists(false));
    }

    /**
     * Reset the ConfigManager instance.
     */
    public function testReset()
    {
        $confIO = $this->conf->getConfigIO();
        $this->conf->reset();
        $this->assertFalse($confIO === $this->conf->getConfigIO());
    }

    /**
     * Reload the config from file.
     */
    public function testReload()
    {
        $this->conf->setConfigFile('tests/utils/config/configTmp');
        $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }';
        file_put_contents($this->conf->getConfigFileExt(), $newConf);
        $this->conf->reload();
        unlink($this->conf->getConfigFileExt());
        // Previous conf no longer exists, and new values have been loaded.
        $this->assertFalse($this->conf->exists('credentials.login'));
        $this->assertEquals('value', $this->conf->get('key'));
    }
}