/
HelperRegistryTest.php
210 lines (182 loc) · 5.66 KB
/
HelperRegistryTest.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\View;
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\TestSuite\TestCase;
use Cake\View\HelperRegistry;
use Cake\View\Helper\HtmlHelper;
use Cake\View\View;
/**
* Extended HtmlHelper
*/
class HtmlAliasHelper extends HtmlHelper {
public function afterRender($viewFile) {
}
}
/**
* Class HelperRegistryTest
*
*/
class HelperRegistryTest extends TestCase {
/**
* setUp
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->View = new View(null);
$this->Events = $this->View->getEventManager();
$this->Helpers = new HelperRegistry($this->View);
}
/**
* tearDown
*
* @return void
*/
public function tearDown() {
Plugin::unload();
unset($this->Helpers, $this->View);
parent::tearDown();
}
/**
* test triggering callbacks on loaded helpers
*
* @return void
*/
public function testLoad() {
$result = $this->Helpers->load('Html');
$this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
$this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
$result = $this->Helpers->loaded();
$this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
}
/**
* test lazy loading of helpers
*
* @return void
*/
public function testLazyLoad() {
$result = $this->Helpers->Html;
$this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
$result = $this->Helpers->Form;
$this->assertInstanceOf('Cake\View\Helper\FormHelper', $result);
App::build(array('Plugin' => array(CAKE . 'Test/TestApp/Plugin/')));
$this->View->plugin = 'TestPlugin';
Plugin::load(array('TestPlugin'));
$result = $this->Helpers->OtherHelper;
$this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
}
/**
* test lazy loading of helpers
*
* @expectedException Cake\Error\MissingHelperException
* @return void
*/
public function testLazyLoadException() {
$this->Helpers->NotAHelper;
}
/**
* Test that loading helpers subscribes to events.
*
* @return void
*/
public function testLoadSubscribeEvents() {
$this->Helpers->load('Html', array('className' => __NAMESPACE__ . '\HtmlAliasHelper'));
$result = $this->Events->listeners('View.afterRender');
$this->assertCount(1, $result);
}
/**
* Tests loading as an alias
*
* @return void
*/
public function testLoadWithAlias() {
$result = $this->Helpers->load('Html', array('className' => __NAMESPACE__ . '\HtmlAliasHelper'));
$this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
$this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $this->Helpers->Html);
$result = $this->Helpers->loaded();
$this->assertEquals(array('Html'), $result, 'loaded() results are wrong.');
$result = $this->Helpers->load('Html');
$this->assertInstanceOf(__NAMESPACE__ . '\HtmlAliasHelper', $result);
}
/**
* Test loading helpers with aliases and plugins.
*
* @return void
*/
public function testLoadWithAliasAndPlugin() {
App::build(['Plugin' => [CAKE . 'Test/TestApp/Plugin/']]);
Plugin::load('TestPlugin');
$result = $this->Helpers->load('SomeOther', array('className' => 'TestPlugin.OtherHelper'));
$this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);
$this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->SomeOther);
$result = $this->Helpers->loaded();
$this->assertEquals(['SomeOther'], $result, 'loaded() results are wrong.');
}
/**
* test that the enabled setting disables the helper.
*
* @return void
*/
public function testLoadWithEnabledFalse() {
$result = $this->Helpers->load('Html', array('enabled' => false));
$this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $result);
$this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $this->Helpers->Html);
$this->assertEmpty($this->Events->listeners('View.beforeRender'));
}
/**
* test missinghelper exception
*
* @expectedException Cake\Error\MissingHelperException
* @return void
*/
public function testLoadMissingHelper() {
$this->Helpers->load('ThisHelperShouldAlwaysBeMissing');
}
/**
* test loading a plugin helper.
*
* @return void
*/
public function testLoadPluginHelper() {
App::build(array(
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/'),
));
Plugin::load(array('TestPlugin'));
$result = $this->Helpers->load('TestPlugin.OtherHelper');
$this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result, 'Helper class is wrong.');
$this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $this->Helpers->OtherHelper, 'Class is wrong');
App::build();
}
/**
* Test reset.
*
* @return void
*/
public function testReset() {
$instance = $this->Helpers->load('Paginator');
$this->assertSame(
$instance,
$this->Helpers->Paginator,
'Instance in registry should be the same as previously loaded'
);
$this->assertCount(1, $this->Events->listeners('View.beforeRender'));
$this->assertNull($this->Helpers->reset(), 'No return expected');
$this->assertCount(0, $this->Events->listeners('View.beforeRender'));
$this->assertNotSame($instance, $this->Helpers->load('Paginator'));
}
}