1
|
|
|
<?php |
2
|
|
|
require_once 'Intraface/Setting.php'; |
3
|
|
|
|
4
|
|
|
class SettingTest extends PHPUnit_Framework_TestCase |
5
|
|
|
{ |
6
|
|
|
protected $backupGlobals = false; |
7
|
|
|
|
8
|
|
|
function createSetting() |
9
|
|
|
{ |
10
|
|
|
$intranet_id = rand(1, 1000000); |
11
|
|
|
$user_id = rand(1, 10000); |
12
|
|
|
return new Intraface_Setting($intranet_id, $user_id); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
View Code Duplication |
function testConstructionOfUser() |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$intranet_id = rand(1, 1000000); |
18
|
|
|
$user_id = rand(1, 1000000); |
19
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
20
|
|
|
$this->assertTrue(is_object($setting)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
function testThatSetCanSetSettingsWhichAreDifferentFromUserAndIntranetAndSystem() |
24
|
|
|
{ |
25
|
|
|
$intranet_id = rand(1, 1000000); |
26
|
|
|
$user_id = rand(1, 10000); |
27
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
28
|
|
|
|
29
|
|
|
$setting->set('user', 'rows_pr_page', 10); |
30
|
|
|
$setting->set('intranet', 'rows_pr_page', 15); |
31
|
|
|
$this->assertEquals(20, $setting->get('system', 'rows_pr_page')); |
32
|
|
|
$this->assertEquals(15, $setting->get('intranet', 'rows_pr_page')); |
33
|
|
|
$this->assertEquals(10, $setting->get('user', 'rows_pr_page')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function testSetAreAbleToRepeatinglySetUserSettingToANewSetting() |
37
|
|
|
{ |
38
|
|
|
$intranet_id = rand(1, 1000000); |
39
|
|
|
$user_id = rand(1, 10000); |
40
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
41
|
|
|
$setting->set('user', 'rows_pr_page', 10); |
42
|
|
|
$setting->set('intranet', 'rows_pr_page', 15); |
43
|
|
|
$this->assertEquals(20, $setting->get('system', 'rows_pr_page')); |
44
|
|
|
$this->assertEquals(15, $setting->get('intranet', 'rows_pr_page')); |
45
|
|
|
$this->assertEquals(10, $setting->get('user', 'rows_pr_page')); |
46
|
|
|
$setting->set('user', 'rows_pr_page', 30); |
47
|
|
|
$setting->set('intranet', 'rows_pr_page', 50); |
48
|
|
|
$this->assertEquals(50, $setting->get('intranet', 'rows_pr_page')); |
49
|
|
|
$this->assertEquals(30, $setting->get('user', 'rows_pr_page')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
function testSettingsHaveBeenPersistedSoWeCanRetrieveThemAgainOnTheNextSettingInvocation() |
53
|
|
|
{ |
54
|
|
|
$intranet_id = rand(1, 1000000); |
55
|
|
|
$user_id = rand(1, 10000); |
56
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
57
|
|
|
$setting->set('user', 'rows_pr_page', 30); |
58
|
|
|
$setting->set('intranet', 'rows_pr_page', 50); |
59
|
|
|
// making sure that it still works next time we get the Setting object |
60
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
61
|
|
|
$this->assertEquals(50, $setting->get('intranet', 'rows_pr_page')); |
62
|
|
|
$this->assertEquals(30, $setting->get('user', 'rows_pr_page')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
function testThatYouCanAccessNewlySetSettingsWithGetRightAway() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$intranet_id = rand(1, 1000000); |
68
|
|
|
$user_id = rand(1, 10000); |
69
|
|
|
$setting1 = new Intraface_Setting($intranet_id, $user_id); |
70
|
|
|
$first_user_setting = 10; |
71
|
|
|
$setting1->set('user', 'rows_pr_page', $first_user_setting); |
72
|
|
|
$this->assertEquals($first_user_setting, $setting1->get('user', 'rows_pr_page')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function testSettingIsDifferentWhenSetForDifferntUsers() |
76
|
|
|
{ |
77
|
|
|
$intranet_id = rand(1, 1000000); |
78
|
|
|
$user_id = rand(1, 10000); |
79
|
|
|
$setting1 = new Intraface_Setting($intranet_id, $user_id); |
80
|
|
|
$first_user_setting = 10; |
81
|
|
|
$setting1->set('user', 'rows_pr_page', $first_user_setting); |
82
|
|
|
|
83
|
|
|
$another_user_id = $user_id = rand(10001, 100000); |
|
|
|
|
84
|
|
|
$setting2 = new Intraface_Setting($intranet_id, $another_user_id); |
85
|
|
|
$second_user_setting = 15; |
86
|
|
|
$setting2->set('user', 'rows_pr_page', $second_user_setting); |
87
|
|
|
|
88
|
|
|
$this->assertEquals($first_user_setting, $setting1->get('user', 'rows_pr_page')); |
89
|
|
|
$this->assertEquals($second_user_setting, $setting2->get('user', 'rows_pr_page')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
function testSetReturnsTrueIfItCanSetASetting() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$intranet_id = rand(1, 1000000); |
95
|
|
|
$user_id = rand(1, 10000); |
96
|
|
|
$setting1 = new Intraface_Setting($intranet_id, $user_id); |
97
|
|
|
$first_user_setting = 10; |
98
|
|
|
$this->assertTrue($setting1->set('user', 'rows_pr_page', $first_user_setting)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
function testThrowsExceptionIfAnIvalidSettingIsBeingSet() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$intranet_id = rand(1, 1000000); |
104
|
|
|
$user_id = rand(1, 10000); |
105
|
|
|
$setting1 = new Intraface_Setting($intranet_id, $user_id); |
106
|
|
|
$first_user_setting = 10; |
107
|
|
|
try { |
108
|
|
|
$setting1->set('user', 'somereallystrangesettingwhichwillneverbevalid', $first_user_setting); |
109
|
|
|
$this->assertTrue(false, 'An exception should be thrown'); |
110
|
|
|
} catch (Exception $e) { |
111
|
|
|
$this->assertTrue(true); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
function testThrowsExceptionIfTryingToSetASystemSetting() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$intranet_id = rand(1, 1000000); |
118
|
|
|
$user_id = rand(1, 10000); |
119
|
|
|
$setting1 = new Intraface_Setting($intranet_id, $user_id); |
120
|
|
|
$first_user_setting = 10; |
121
|
|
|
try { |
122
|
|
|
$setting1->set('system', 'rows_pr_page', $first_user_setting); |
123
|
|
|
$this->assertTrue(false, 'An exception should be thrown'); |
124
|
|
|
} catch (Exception $e) { |
125
|
|
|
$this->assertTrue(true); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
function testGetSettingForUserWillReturnIntranetSettingIfUserSettingIsNotSet() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$intranet_id = rand(1, 1000000); |
132
|
|
|
$user_id = rand(1, 10000); |
133
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
134
|
|
|
$intranet_setting = 10; |
135
|
|
|
$setting->set('intranet', 'rows_pr_page', $intranet_setting); |
136
|
|
|
$this->assertEquals($intranet_setting, $setting->get('user', 'rows_pr_page')); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function testGetSettingsReturnsAnArray() |
140
|
|
|
{ |
141
|
|
|
$intranet_id = rand(1, 1000000); |
142
|
|
|
$user_id = rand(1, 10000); |
143
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
144
|
|
|
$this->assertEquals(0, count($setting->getSettings())); |
145
|
|
|
$setting->set('user', 'rows_pr_page', 10); |
146
|
|
|
$this->assertTrue(is_array($setting->getSettings())); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
function testIsSettingSetReturnsFalseIfSettingIsNotSet() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$intranet_id = rand(1, 1000000); |
152
|
|
|
$user_id = rand(1, 10000); |
153
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
154
|
|
|
$this->assertFalse($setting->isSettingSet('user', 'rows_pr_page')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
View Code Duplication |
function testIsSettingSetReturnsTrueIfSettingIsSet() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$intranet_id = rand(1, 1000000); |
160
|
|
|
$user_id = rand(1, 10000); |
161
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
162
|
|
|
$setting->set('user', 'rows_pr_page', 10); |
163
|
|
|
$this->assertTrue($setting->isSettingSet('user', 'rows_pr_page')); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
function testDeleteReturnsTrueIfItCanDeleteASettingAndSettingIsNotSetAnymoreAndCannotBeRetrieved() |
167
|
|
|
{ |
168
|
|
|
$intranet_id = rand(1, 1000000); |
169
|
|
|
$user_id = rand(1, 10000); |
170
|
|
|
$setting = new Intraface_Setting($intranet_id, $user_id); |
171
|
|
|
$user_setting = 10; |
172
|
|
|
$setting->set('user', 'rows_pr_page', $user_setting); |
173
|
|
|
$this->assertTrue($setting->delete('user', 'rows_pr_page')); |
174
|
|
|
$this->assertFalse($setting->isSettingSet('user', 'rows_pr_page')); |
175
|
|
|
$this->assertNotEquals($user_setting, $setting->get('user', 'rows_pr_page')); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.