]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/SugarArrayTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / utils / SugarArrayTest.php
1 <?php
2 require_once 'include/utils/array_utils.php';
3
4 class SugarArrayTest extends Sugar_PHPUnit_Framework_TestCase
5 {
6     public function testCanFindValueUsingDotNotation() 
7     {
8         $random = rand(100, 200);
9         $array = array(
10             'foo' => array(
11                 $random => array(
12                     'bar' => $random,
13                 ),
14             ),
15         );
16
17         $array = new SugarArray($array);
18         $this->assertEquals($array->get("foo.{$random}.bar"), $random);
19     }
20
21     public function testReturnsDefaultValueWhenDoesNotContainRequestedValue() 
22     {
23         $random = rand(100, 200);
24         $array = new SugarArray(array());
25         $this->assertEquals($array->get('unknown', $random), $random);
26     }
27     
28     public function testImplementsArrayAccess() 
29     {
30         $reflection = new ReflectionClass('SugarArray');
31         $this->assertTrue($reflection->implementsInterface('ArrayAccess'));
32     }
33
34     public function testImplementsCountable() 
35     {
36         $reflection = new ReflectionClass('SugarArray');
37         $this->assertTrue($reflection->implementsInterface('Countable'));
38     }
39
40     public function testStaticMethodCanTraverseProvidedArray() 
41     {
42         $random = rand(100, 200);
43         $array = array(
44             'foo' => array(
45                 $random => array(
46                     'bar' => $random,
47                 ),
48             ),
49         );
50
51         $this->assertEquals(SugarArray::staticGet($array, "foo.{$random}.bar"), $random);
52     }
53
54     public function testStaticMethodCanReturnDefaultOnUnknownValue() 
55     {
56         $random = rand(100, 200);
57         $this->assertEquals(SugarArray::staticGet(array(123, 321), 'unknown', $random), $random);
58     }
59     
60     public function testAdd_blank_option()
61     {
62         $options = 'noneArray';
63         $expectedArray = array(''=>'');
64         $result = add_blank_option($options);
65         $this->assertEquals($result[''], $expectedArray['']);
66         $options2 = array('mason'=>'unittest');
67         $expectedArray2 = array(''=>'','mason'=>'unittest');
68         $result2 = add_blank_option($options2);
69         $this->assertEquals($result2, $expectedArray2);
70     }
71 }
72