]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarObjects/SugarConfigTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / SugarObjects / SugarConfigTest.php
1 <?php
2 require_once 'include/SugarObjects/SugarConfig.php';
3
4 class SugarConfigTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     private $_old_sugar_config = null;
7
8     public function setUp() 
9     {
10         $this->_old_sugar_config = $GLOBALS['sugar_config'];
11         $GLOBALS['sugar_config'] = array();
12     }
13
14     public function tearDown() 
15     {
16         $config = SugarConfig::getInstance();
17         $config->clearCache();
18         $GLOBALS['sugar_config'] = $this->_old_sugar_config;
19     }
20
21     /**
22      * Stores a key/value pair in the config
23      *
24      * @internal override this in sub-classes if you are testing with the
25      *           config data stored somewhere other than the $sugar_config
26      *           super global
27      * @param string $key
28      * @param string $value
29      */
30     private function _addKeyValueToConfig(
31         $key, 
32         $value
33         ) 
34     {
35         $GLOBALS['sugar_config'][$key] = $value;
36     }
37
38     private function _generateRandomValue() 
39     {
40         $this->_random = 'Some Random Foobar: ' . rand(10000, 20000);
41         return $this->_getLastRandomValue();
42     }
43
44     private function _getLastRandomValue() 
45     {
46         return $this->_random;
47     }
48
49     public function testGetInstanceReturnsASugarConfigObject() 
50     {
51         $this->assertTrue(SugarConfig::getInstance() instanceOf SugarConfig, 'Returned object is not a SugarConfig object');
52     }
53
54     public function testGetInstanceReturnsASingleton() 
55     {
56         $one = SugarConfig::getInstance();
57         $two = SugarConfig::getInstance();
58         $this->assertSame($one, $two);
59     }
60
61     public function testReadsGlobalSugarConfigArray() 
62     {
63         for ($i = 0; $i < 10; $i++) {
64             $anonymous_key = 'key-' . $i;
65             $random_value = rand(10000, 20000);
66             $rawConfigArray[$anonymous_key] = $random_value;
67             $this->_addKeyValueToConfig($anonymous_key, $random_value);
68         }
69
70         $config = SugarConfig::getInstance();
71         foreach ($rawConfigArray as $key => $value) {
72             $this->assertEquals(
73                 $config->get($key), $value,
74                 "SugarConfig::get({$key}) should be equal to {$value}, got " . $config->get($key)
75             );
76         }
77     }
78
79     public function testAllowDotNotationForSubValuesWithinTheConfig() 
80     {
81         $random_value = 'Some Random Integer: ' . rand(1000, 2000);
82         $this->_addKeyValueToConfig('grandparent', array(
83                 'parent' => array(
84                 'child' => $random_value,
85             ),
86         ));
87
88         $config = SugarConfig::getInstance();
89         $this->assertEquals($random_value, $config->get('grandparent.parent.child'));
90     }
91
92     public function testReturnsNullOnUnknownKey() 
93     {
94         $config = SugarConfig::getInstance();
95         $this->assertNull($config->get('unknown-and-unknowable'));
96     }
97
98     public function testReturnsNullOnUnknownKeyWithinAHeirarchy() 
99     {
100         $this->_addKeyValueToConfig('grandparent', array(
101             'parent' => array(
102                 'child' => 'foobar',
103             ),
104         ));
105         $config= SugarConfig::getInstance();
106
107         $this->assertNull($config->get('some-unknown-grandparent.parent.child'));
108         $this->assertNull($config->get('grandparent.some-unknown-parent.child'));
109         $this->assertNull($config->get('grandparent.parent.some-unknown-child'));
110     }
111
112     public function testAllowSpecifyingDefault() 
113     {
114         $config = SugarConfig::getInstance();
115
116         $random = rand(10000, 20000);
117         $this->assertSame($random, $config->get('unknown-and-unknowable', $random));
118     }
119
120     public function testAllowSpecifyingDefaultForSubValues() 
121     {
122         $this->_addKeyValueToConfig('grandparent', array(
123             'parent' => array(
124                 'child' => 'foobar',
125             ),
126         ));
127         $config = SugarConfig::getInstance();
128
129         $this->assertEquals(
130             $this->_generateRandomValue(),
131             $config->get(
132                 'some-unknown-grandparent.parent.child',
133                 $this->_getLastRandomValue()
134             )
135         );
136         $this->assertEquals(
137             $this->_generateRandomValue(),
138             $config->get(
139                 'grandparent.some-unknown-parent.child',
140                 $this->_getLastRandomValue()
141             )
142         );
143         $this->assertEquals(
144             $this->_generateRandomValue(),
145             $config->get(
146                 'grandparent.parent.some-unknown-child',
147                 $this->_getLastRandomValue()
148             )
149         );
150     }
151
152     public function testStoresValuesInMemoryAfterFirstLookup() 
153     {
154         $this->_addKeyValueToConfig('foobar', 'barfoo');
155
156         $config = SugarConfig::getInstance();
157         $this->assertEquals($config->get('foobar'), 'barfoo');
158
159         $this->_addKeyValueToConfig('foobar', 'foobar');
160         $this->assertEquals($config->get('foobar'), 'barfoo', 'should still be equal "barfoo": got ' . $config->get('foobar'));
161     }
162
163     public function testCanClearsCachedValues() 
164     {
165         $this->_addKeyValueToConfig('foobar', 'barfoo');
166
167         $config = SugarConfig::getInstance();
168         $this->assertEquals($config->get('foobar'), 'barfoo', 'sanity check');
169         $this->_addKeyValueToConfig('foobar', 'foobar');
170         $this->assertEquals($config->get('foobar'), 'barfoo', 'sanity check');
171
172         $config->clearCache();
173         $this->assertEquals($config->get('foobar'), 'foobar', 'after clearCache() call, new value should be used');
174     }
175
176     public function testCanCherryPickKeyToClear() 
177     {
178         $this->_addKeyValueToConfig('foobar', 'barfoo');
179         $this->_addKeyValueToConfig('barfoo', 'barfoo');
180
181         $config = SugarConfig::getInstance();
182         $this->assertEquals($config->get('foobar'), 'barfoo', 'sanity check, got: ' . $config->get('foobar'));
183         $this->assertEquals($config->get('barfoo'), 'barfoo', 'sanity check');
184
185         $this->_addKeyValueToConfig('foobar', 'foobar');
186         $this->_addKeyValueToConfig('barfoo', 'foobar');
187         $this->assertEquals($config->get('foobar'), 'barfoo', 'should still be equal to "barfoo", got: ' . $config->get('barfoo'));
188         $this->assertEquals($config->get('barfoo'), 'barfoo', 'should still be equal to "barfoo", got: ' . $config->get('barfoo'));
189
190         $config->clearCache('barfoo');
191         $this->assertEquals($config->get('barfoo'), 'foobar', 'should be equal to "foobar" after cherry picked for clearing');
192         $this->assertEquals($config->get('foobar'), 'barfoo', 'should not be effected by cherry picked clearCache() call');
193     }
194
195     public function testDemonstrateGrabbingSiblingNodes() 
196     {
197         $this->_addKeyValueToConfig('foobar', array(
198             'foo' => array(
199                 array(
200                     'first' => 'one',
201                 ),
202                 array(
203                     'first' => 'uno',
204                 ),
205             ),
206         ));
207
208         $config = SugarConfig::getInstance();
209         $this->assertEquals($config->get('foobar.foo.0.first'), 'one');
210         $this->assertEquals($config->get('foobar.foo.1.first'), 'uno');
211     }
212 }
213