]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarObjects/SugarRegistryTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / SugarObjects / SugarRegistryTest.php
1 <?php
2 require_once 'include/SugarObjects/SugarRegistry.php';
3
4 class SugarRegistryTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     private $_old_reporting = null;
7     private $_old_globals = null;
8
9     public function setUp() 
10     {
11         $this->_old_reporting = error_reporting(E_ALL);
12         $this->_old_globals = $GLOBALS;
13         unset($GLOBALS);
14     }
15
16     public function tearDown() 
17     {
18         error_reporting($this->_old_reporting);
19         $GLOBALS = $this->_old_globals;
20         unset($this->_old_globals);
21     }
22
23     public function testGetInstanceReturnsAnInstanceOfSugarRegistry() 
24     {
25         $this->assertTrue(SugarRegistry::getInstance() instanceOf SugarRegistry,'Returned object is not a SugarRegistry instance');
26     }
27
28     public function testGetInstanceReturnsSameObject() 
29     {
30         $one = SugarRegistry::getInstance();
31         $two = SugarRegistry::getInstance();
32         $this->assertSame($one, $two);
33     }
34
35     public function testParameterPassedToGetInstanceSpecifiesInstanceName() 
36     {
37         $foo1 = SugarRegistry::getInstance('foo');
38         $foo2 = SugarRegistry::getInstance('foo');
39         $this->assertSame($foo1, $foo2);
40
41         $bar = SugarRegistry::getInstance('bar');
42         $this->assertNotSame($foo1, $bar);
43     }
44
45     public function testCanSetAndGetValues() 
46     {
47         $random = rand(100, 200);
48         $r = SugarRegistry::getInstance();
49         $r->integer = $random;
50         $this->assertEquals($random, $r->integer);
51         $this->assertEquals($random, SugarRegistry::getInstance()->integer);
52     }
53
54     public function testIssetReturnsTrueFalse() 
55     {
56         $r = SugarRegistry::getInstance();
57         $this->assertFalse(isset($r->foo));
58         $this->assertFalse(isset(SugarRegistry::getInstance()->foo));
59
60         $r->foo = 'bar';
61         $this->assertTrue(isset($r->foo));
62         $this->assertTrue(isset(SugarRegistry::getInstance()->foo));
63     }
64
65     public function testUnsetRemovesValueFromRegistry() 
66     {
67         $r = SugarRegistry::getInstance();
68         $r->foo = 'bar';
69         unset($r->foo);
70         $this->assertFalse(isset($r->foo));
71         $this->assertFalse(isset(SugarRegistry::getInstance()->foo));
72     }
73
74     public function testReturnsNullOnAnyUnknownValue() 
75     {
76         $r = SugarRegistry::getInstance();
77         $this->assertNull($r->unknown);
78         $this->assertNull(SugarRegistry::getInstance()->unknown);
79     }
80
81     public function testAddToGlobalsPutsRefsToAllRegistryObjectsInGlobalSpace() 
82     {
83         $r = SugarRegistry::getInstance();
84         $r->foo = 'bar';
85
86         $this->assertFalse(isset($GLOBALS['foo']), 'sanity check');
87         $r->addToGlobals();
88         $this->assertTrue(isset($GLOBALS['foo']));
89     }
90 }
91