Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ComponentTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Controller
15
 * @since         CakePHP(tm) v 1.2.0.5436
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Controller', 'Controller');
20
App::uses('Component', 'Controller');
21
 
22
/**
23
 * ParamTestComponent
24
 *
25
 * @package       Cake.Test.Case.Controller
26
 */
27
class ParamTestComponent extends Component {
28
 
29
/**
30
 * components property
31
 *
32
 * @var array
33
 */
34
	public $components = array('Banana' => array('config' => 'value'));
35
}
36
 
37
/**
38
 * ComponentTestController class
39
 *
40
 * @package       Cake.Test.Case.Controller
41
 */
42
class ComponentTestController extends Controller {
43
 
44
/**
45
 * uses property
46
 *
47
 * @var array
48
 */
49
	public $uses = array();
50
 
51
}
52
 
53
/**
54
 * AppleComponent class
55
 *
56
 * @package       Cake.Test.Case.Controller
57
 */
58
class AppleComponent extends Component {
59
 
60
/**
61
 * components property
62
 *
63
 * @var array
64
 */
65
	public $components = array('Orange');
66
 
67
/**
68
 * testName property
69
 *
70
 * @var mixed null
71
 */
72
	public $testName = null;
73
 
74
/**
75
 * startup method
76
 *
77
 * @param Controller $controller
78
 * @return void
79
 */
80
	public function startup(Controller $controller) {
81
		$this->testName = $controller->name;
82
	}
83
 
84
}
85
 
86
/**
87
 * OrangeComponent class
88
 *
89
 * @package       Cake.Test.Case.Controller
90
 */
91
class OrangeComponent extends Component {
92
 
93
/**
94
 * components property
95
 *
96
 * @var array
97
 */
98
	public $components = array('Banana');
99
 
100
/**
101
 * initialize method
102
 *
103
 * @param Controller $controller
104
 * @return void
105
 */
106
	public function initialize(Controller $controller) {
107
		$this->Controller = $controller;
108
		$this->Banana->testField = 'OrangeField';
109
	}
110
 
111
/**
112
 * startup method
113
 *
114
 * @param Controller $controller
115
 * @return string
116
 */
117
	public function startup(Controller $controller) {
118
		$controller->foo = 'pass';
119
	}
120
 
121
}
122
 
123
/**
124
 * BananaComponent class
125
 *
126
 * @package       Cake.Test.Case.Controller
127
 */
128
class BananaComponent extends Component {
129
 
130
/**
131
 * testField property
132
 *
133
 * @var string
134
 */
135
	public $testField = 'BananaField';
136
 
137
/**
138
 * startup method
139
 *
140
 * @param Controller $controller
141
 * @return string
142
 */
143
	public function startup(Controller $controller) {
144
		$controller->bar = 'fail';
145
	}
146
 
147
}
148
 
149
/**
150
 * MutuallyReferencingOneComponent class
151
 *
152
 * @package       Cake.Test.Case.Controller
153
 */
154
class MutuallyReferencingOneComponent extends Component {
155
 
156
/**
157
 * components property
158
 *
159
 * @var array
160
 */
161
	public $components = array('MutuallyReferencingTwo');
162
}
163
 
164
/**
165
 * MutuallyReferencingTwoComponent class
166
 *
167
 * @package       Cake.Test.Case.Controller
168
 */
169
class MutuallyReferencingTwoComponent extends Component {
170
 
171
/**
172
 * components property
173
 *
174
 * @var array
175
 */
176
	public $components = array('MutuallyReferencingOne');
177
}
178
 
179
/**
180
 * SomethingWithEmailComponent class
181
 *
182
 * @package       Cake.Test.Case.Controller
183
 */
184
class SomethingWithEmailComponent extends Component {
185
 
186
/**
187
 * components property
188
 *
189
 * @var array
190
 */
191
	public $components = array('Email');
192
}
193
 
194
/**
195
 * ComponentTest class
196
 *
197
 * @package       Cake.Test.Case.Controller
198
 */
199
class ComponentTest extends CakeTestCase {
200
 
201
/**
202
 * setUp method
203
 *
204
 * @return void
205
 */
206
	public function setUp() {
207
		parent::setUp();
208
		$this->_pluginPaths = App::path('plugins');
209
		App::build(array(
210
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
211
		));
212
	}
213
 
214
/**
215
 * test accessing inner components.
216
 *
217
 * @return void
218
 */
219
	public function testInnerComponentConstruction() {
220
		$Collection = new ComponentCollection();
221
		$Component = new AppleComponent($Collection);
222
 
223
		$this->assertInstanceOf('OrangeComponent', $Component->Orange, 'class is wrong');
224
	}
225
 
226
/**
227
 * test component loading
228
 *
229
 * @return void
230
 */
231
	public function testNestedComponentLoading() {
232
		$Collection = new ComponentCollection();
233
		$Apple = new AppleComponent($Collection);
234
 
235
		$this->assertInstanceOf('OrangeComponent', $Apple->Orange, 'class is wrong');
236
		$this->assertInstanceOf('BananaComponent', $Apple->Orange->Banana, 'class is wrong');
237
		$this->assertTrue(empty($Apple->Session));
238
		$this->assertTrue(empty($Apple->Orange->Session));
239
	}
240
 
241
/**
242
 * test that component components are not enabled in the collection.
243
 *
244
 * @return void
245
 */
246
	public function testInnerComponentsAreNotEnabled() {
247
		$Collection = new ComponentCollection();
248
		$Apple = $Collection->load('Apple');
249
 
250
		$this->assertInstanceOf('OrangeComponent', $Apple->Orange, 'class is wrong');
251
		$result = $Collection->enabled();
252
		$this->assertEquals(array('Apple'), $result, 'Too many components enabled.');
253
	}
254
 
255
/**
256
 * test a component being used more than once.
257
 *
258
 * @return void
259
 */
260
	public function testMultipleComponentInitialize() {
261
		$Collection = new ComponentCollection();
262
		$Banana = $Collection->load('Banana');
263
		$Orange = $Collection->load('Orange');
264
 
265
		$this->assertSame($Banana, $Orange->Banana, 'Should be references');
266
		$Banana->testField = 'OrangeField';
267
 
268
		$this->assertSame($Banana->testField, $Orange->Banana->testField, 'References are broken');
269
	}
270
 
271
/**
272
 * Test mutually referencing components.
273
 *
274
 * @return void
275
 */
276
	public function testSomethingReferencingEmailComponent() {
277
		$Controller = new ComponentTestController();
278
		$Controller->components = array('SomethingWithEmail');
279
		$Controller->uses = false;
280
		$Controller->constructClasses();
281
		$Controller->Components->trigger('initialize', array(&$Controller));
282
		$Controller->beforeFilter();
283
		$Controller->Components->trigger('startup', array(&$Controller));
284
 
285
		$this->assertInstanceOf('SomethingWithEmailComponent', $Controller->SomethingWithEmail);
286
		$this->assertInstanceOf('EmailComponent', $Controller->SomethingWithEmail->Email);
287
	}
288
 
289
}