/
ControllerMergeVarsTest.php
252 lines (219 loc) · 5.9 KB
/
ControllerMergeVarsTest.php
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Controller Merge vars Test file
*
* Isolated from the Controller and Component test as to not pollute their AppController class
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.Case.Controller
* @since CakePHP(tm) v 1.2.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Controller', 'Controller');
/**
* Test case AppController
*
* @package Cake.Test.Case.Controller
* @package Cake.Test.Case.Controller
*/
class MergeVarsAppController extends Controller {
/**
* components
*
* @var array
*/
public $components = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => false));
/**
* helpers
*
* @var array
*/
public $helpers = array('MergeVar' => array('format' => 'html', 'terse'));
}
/**
* MergeVar Component
*
* @package Cake.Test.Case.Controller
*/
class MergeVarComponent extends Object {
}
/**
* Additional controller for testing
*
* @package Cake.Test.Case.Controller
*/
class MergeVariablesController extends MergeVarsAppController {
/**
* name
*
* @var string
*/
public $name = 'MergeVariables';
/**
* uses
*
* @var arrays
*/
public $uses = array();
/**
* parent for mergeVars
*
* @var string
*/
protected $_mergeParent = 'MergeVarsAppController';
}
/**
* MergeVarPlugin App Controller
*
* @package Cake.Test.Case.Controller
*/
class MergeVarPluginAppController extends MergeVarsAppController {
/**
* components
*
* @var array
*/
public $components = array('Auth' => array('setting' => 'val', 'otherVal'));
/**
* helpers
*
* @var array
*/
public $helpers = array('Javascript');
/**
* parent for mergeVars
*
* @var string
*/
protected $_mergeParent = 'MergeVarsAppController';
}
/**
* MergePostsController
*
* @package Cake.Test.Case.Controller
*/
class MergePostsController extends MergeVarPluginAppController {
/**
* name
*
* @var string
*/
public $name = 'MergePosts';
/**
* uses
*
* @var array
*/
public $uses = array();
}
/**
* Test Case for Controller Merging of Vars.
*
* @package Cake.Test.Case.Controller
*/
class ControllerMergeVarsTest extends CakeTestCase {
/**
* test that component settings are not duplicated when merging component settings
*
* @return void
*/
public function testComponentParamMergingNoDuplication() {
$Controller = new MergeVariablesController();
$Controller->constructClasses();
$expected = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => false));
$this->assertEquals($Controller->components, $expected, 'Duplication of settings occurred. %s');
}
/**
* test component merges with redeclared components
*
* @return void
*/
public function testComponentMergingWithRedeclarations() {
$Controller = new MergeVariablesController();
$Controller->components['MergeVar'] = array('remote', 'redirect' => true);
$Controller->constructClasses();
$expected = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => true, 'remote'));
$this->assertEquals($Controller->components, $expected, 'Merging of settings is wrong. %s');
}
/**
* test merging of helpers array, ensure no duplication occurs
*
* @return void
*/
public function testHelperSettingMergingNoDuplication() {
$Controller = new MergeVariablesController();
$Controller->constructClasses();
$expected = array('MergeVar' => array('format' => 'html', 'terse'));
$this->assertEquals($Controller->helpers, $expected, 'Duplication of settings occurred. %s');
}
/**
* Test that helpers declared in appcontroller come before those in the subclass
* orderwise
*
* @return void
*/
public function testHelperOrderPrecedence() {
$Controller = new MergeVariablesController();
$Controller->helpers = array('Custom', 'Foo' => array('something'));
$Controller->constructClasses();
$expected = array(
'MergeVar' => array('format' => 'html', 'terse'),
'Custom' => null,
'Foo' => array('something')
);
$this->assertSame($Controller->helpers, $expected, 'Order is incorrect.');
}
/**
* test merging of vars with plugin
*
* @return void
*/
public function testMergeVarsWithPlugin() {
$Controller = new MergePostsController();
$Controller->components = array('Email' => array('ports' => 'open'));
$Controller->plugin = 'MergeVarPlugin';
$Controller->constructClasses();
$expected = array(
'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
'Auth' => array('setting' => 'val', 'otherVal'),
'Email' => array('ports' => 'open')
);
$this->assertEquals($expected, $Controller->components, 'Components are unexpected.');
$expected = array(
'MergeVar' => array('format' => 'html', 'terse'),
'Javascript' => null
);
$this->assertEquals($expected, $Controller->helpers, 'Helpers are unexpected.');
$Controller = new MergePostsController();
$Controller->components = array();
$Controller->plugin = 'MergeVarPlugin';
$Controller->constructClasses();
$expected = array(
'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
'Auth' => array('setting' => 'val', 'otherVal'),
);
$this->assertEquals($expected, $Controller->components, 'Components are unexpected.');
}
/**
* Ensure that _mergeControllerVars is not being greedy and merging with
* AppController when you make an instance of Controller
*
* @return void
*/
public function testMergeVarsNotGreedy() {
$Controller = new Controller();
$Controller->components = array();
$Controller->uses = array();
$Controller->constructClasses();
$this->assertFalse(isset($Controller->Session));
}
}