-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
helper_collection.test.php
180 lines (156 loc) · 4.81 KB
/
helper_collection.test.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
<?php
/**
* HelperCollectionTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake
* @subpackage cake.tests.cases.libs
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', 'HelperCollection');
App::import('View', 'View');
class HelperCollectionTest extends CakeTestCase {
/**
* setup
*
* @return void
*/
function setup() {
$this->View = $this->getMock('View', array(), array(null));
$this->Helpers = new HelperCollection($this->View);
}
/**
* teardown
*
* @return void
*/
function teardown() {
unset($this->Helpers, $this->View);
}
/**
* test triggering callbacks on loaded helpers
*
* @return void
*/
function testLoad() {
$result = $this->Helpers->load('Html');
$this->assertType('HtmlHelper', $result);
$this->assertType('HtmlHelper', $this->Helpers->Html);
$result = $this->Helpers->attached();
$this->assertEquals(array('Html'), $result, 'attached() results are wrong.');
$this->assertTrue($this->Helpers->enabled('Html'));
}
/**
* test load and enable = false
*
* @return void
*/
function testLoadWithEnableFalse() {
$result = $this->Helpers->load('Html', array(), false);
$this->assertType('HtmlHelper', $result);
$this->assertType('HtmlHelper', $this->Helpers->Html);
$this->assertFalse($this->Helpers->enabled('Html'), 'Html should be disabled');
}
/**
* test missinghelper exception
*
* @expectedException MissingHelperFileException
* @return void
*/
function testLoadMissingHelperFile() {
$result = $this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
}
/**
* test loading a plugin helper.
*
* @return void
*/
function testLoadPluginHelper() {
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
));
$result = $this->Helpers->load('TestPlugin.OtherHelper');
$this->assertType('OtherHelperHelper', $result, 'Helper class is wrong.');
$this->assertType('OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
App::build();
}
/**
* test unload()
*
* @return void
*/
function testUnload() {
$this->Helpers->load('Form');
$this->Helpers->load('Html');
$result = $this->Helpers->attached();
$this->assertEquals(array('Form', 'Html'), $result, 'loaded helpers is wrong');
$this->Helpers->unload('Html');
$this->assertFalse(isset($this->Helpers->Html));
$this->assertTrue(isset($this->Helpers->Form));
$result = $this->Helpers->attached();
$this->assertEquals(array('Form'), $result, 'loaded helpers is wrong');
}
/**
* test triggering callbacks.
*
* @return void
*/
function testTrigger() {
$this->Helpers->load('Form');
$this->Helpers->load('Html');
$this->Helpers->Html = $this->getMock('HtmlHelper', array(), array($this->View));
$this->Helpers->Form = $this->getMock('FormHelper', array(), array($this->View));
$this->Helpers->Html->expects($this->once())->method('beforeRender')
->with('one', 'two');
$this->Helpers->Form->expects($this->once())->method('beforeRender')
->with('one', 'two');
$this->Helpers->trigger('beforeRender', array('one', 'two'));
}
/**
* test trigger and disabled helpers.
*
* @return void
*/
function testTriggerWithDisabledHelpers() {
$this->Helpers->load('Form');
$this->Helpers->load('Html');
$this->Helpers->Html = $this->getMock('HtmlHelper', array(), array($this->View));
$this->Helpers->Form = $this->getMock('FormHelper', array(), array($this->View));
$this->Helpers->Html->expects($this->once())->method('beforeRender')
->with('one', 'two');
$this->Helpers->Form->expects($this->never())->method('beforeRender');
$this->Helpers->disable('Form');
$this->Helpers->trigger('beforeRender', array('one', 'two'));
}
/**
* test normalizeObjectArray
*
* @return void
*/
function testnormalizeObjectArray() {
$helpers = array(
'Html',
'Foo.Bar' => array('one', 'two'),
'Something',
'Banana.Apple' => array('foo' => 'bar')
);
$result = ObjectCollection::normalizeObjectArray($helpers);
$expected = array(
'Html' => array('class' => 'Html', 'settings' => array()),
'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),
'Something' => array('class' => 'Something', 'settings' => array()),
'Apple' => array('class' => 'Banana.Apple', 'settings' => array('foo' => 'bar')),
);
$this->assertEquals($expected, $result);
}
}