pictcode / lib / Cake / Test / Case / Utility / ClassRegistryTest.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (8.353 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* ClassRegistryTest 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.Utility
|
15 |
* @since CakePHP(tm) v 1.2.0.5432
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
17 |
*/
|
18 |
|
19 |
App::uses('ClassRegistry', 'Utility'); |
20 |
|
21 |
/**
|
22 |
* ClassRegisterModel class
|
23 |
*
|
24 |
* @package Cake.Test.Case.Utility
|
25 |
*/
|
26 |
class ClassRegisterModel extends CakeTestModel { |
27 |
|
28 |
/**
|
29 |
* useTable property
|
30 |
*
|
31 |
* @var bool
|
32 |
*/
|
33 |
public $useTable = false; |
34 |
} |
35 |
|
36 |
/**
|
37 |
* RegisterArticle class
|
38 |
*
|
39 |
* @package Cake.Test.Case.Utility
|
40 |
*/
|
41 |
class RegisterArticle extends ClassRegisterModel { |
42 |
} |
43 |
|
44 |
/**
|
45 |
* RegisterArticleFeatured class
|
46 |
*
|
47 |
* @package Cake.Test.Case.Utility
|
48 |
*/
|
49 |
class RegisterArticleFeatured extends ClassRegisterModel { |
50 |
} |
51 |
|
52 |
/**
|
53 |
* RegisterArticleTag class
|
54 |
*
|
55 |
* @package Cake.Test.Case.Utility
|
56 |
*/
|
57 |
class RegisterArticleTag extends ClassRegisterModel { |
58 |
} |
59 |
|
60 |
/**
|
61 |
* RegistryPluginAppModel class
|
62 |
*
|
63 |
* @package Cake.Test.Case.Utility
|
64 |
*/
|
65 |
class RegistryPluginAppModel extends ClassRegisterModel { |
66 |
|
67 |
/**
|
68 |
* tablePrefix property
|
69 |
*
|
70 |
* @var string
|
71 |
*/
|
72 |
public $tablePrefix = 'something_'; |
73 |
} |
74 |
|
75 |
/**
|
76 |
* TestRegistryPluginModel class
|
77 |
*
|
78 |
* @package Cake.Test.Case.Utility
|
79 |
*/
|
80 |
class TestRegistryPluginModel extends RegistryPluginAppModel { |
81 |
} |
82 |
|
83 |
/**
|
84 |
* RegisterCategory class
|
85 |
*
|
86 |
* @package Cake.Test.Case.Utility
|
87 |
*/
|
88 |
class RegisterCategory extends ClassRegisterModel { |
89 |
} |
90 |
/**
|
91 |
* RegisterPrefixedDs class
|
92 |
*
|
93 |
* @package Cake.Test.Case.Utility
|
94 |
*/
|
95 |
class RegisterPrefixedDs extends ClassRegisterModel { |
96 |
|
97 |
/**
|
98 |
* useDbConfig property
|
99 |
*
|
100 |
* @var string
|
101 |
*/
|
102 |
public $useDbConfig = 'doesnotexist'; |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Abstract class for testing ClassRegistry.
|
107 |
*/
|
108 |
abstract class ClassRegistryAbstractModel extends ClassRegisterModel { |
109 |
|
110 |
public abstract function doSomething(); |
111 |
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Interface for testing ClassRegistry
|
116 |
*/
|
117 |
interface ClassRegistryInterfaceTest { |
118 |
|
119 |
public function doSomething(); |
120 |
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* ClassRegistryTest class
|
125 |
*
|
126 |
* @package Cake.Test.Case.Utility
|
127 |
*/
|
128 |
class ClassRegistryTest extends CakeTestCase { |
129 |
|
130 |
/**
|
131 |
* testAddModel method
|
132 |
*
|
133 |
* @return void
|
134 |
*/
|
135 |
public function testAddModel() { |
136 |
$Tag = ClassRegistry::init('RegisterArticleTag'); |
137 |
$this->assertInstanceOf('RegisterArticleTag', $Tag); |
138 |
|
139 |
$TagCopy = ClassRegistry::isKeySet('RegisterArticleTag'); |
140 |
$this->assertTrue($TagCopy); |
141 |
|
142 |
$Tag->name = 'SomeNewName'; |
143 |
|
144 |
$TagCopy = ClassRegistry::getObject('RegisterArticleTag'); |
145 |
|
146 |
$this->assertInstanceOf('RegisterArticleTag', $TagCopy); |
147 |
$this->assertSame($Tag, $TagCopy); |
148 |
|
149 |
$NewTag = ClassRegistry::init(array('class' => 'RegisterArticleTag', 'alias' => 'NewTag')); |
150 |
$this->assertInstanceOf('RegisterArticleTag', $Tag); |
151 |
|
152 |
$NewTagCopy = ClassRegistry::init(array('class' => 'RegisterArticleTag', 'alias' => 'NewTag')); |
153 |
|
154 |
$this->assertNotSame($Tag, $NewTag); |
155 |
$this->assertSame($NewTag, $NewTagCopy); |
156 |
|
157 |
$NewTag->name = 'SomeOtherName'; |
158 |
$this->assertNotSame($Tag, $NewTag); |
159 |
$this->assertSame($NewTag, $NewTagCopy); |
160 |
|
161 |
$Tag->name = 'SomeOtherName'; |
162 |
$this->assertNotSame($Tag, $NewTag); |
163 |
|
164 |
$this->assertTrue($TagCopy->name === 'SomeOtherName'); |
165 |
|
166 |
$User = ClassRegistry::init(array('class' => 'RegisterUser', 'alias' => 'User', 'table' => false)); |
167 |
$this->assertInstanceOf('AppModel', $User); |
168 |
|
169 |
$UserCopy = ClassRegistry::init(array('class' => 'RegisterUser', 'alias' => 'User', 'table' => false)); |
170 |
$this->assertInstanceOf('AppModel', $UserCopy); |
171 |
$this->assertEquals($User, $UserCopy); |
172 |
|
173 |
$Category = ClassRegistry::init(array('class' => 'RegisterCategory')); |
174 |
$this->assertInstanceOf('RegisterCategory', $Category); |
175 |
|
176 |
$ParentCategory = ClassRegistry::init(array('class' => 'RegisterCategory', 'alias' => 'ParentCategory')); |
177 |
$this->assertInstanceOf('RegisterCategory', $ParentCategory); |
178 |
$this->assertNotSame($Category, $ParentCategory); |
179 |
|
180 |
$this->assertNotEquals($Category->alias, $ParentCategory->alias); |
181 |
$this->assertEquals('RegisterCategory', $Category->alias); |
182 |
$this->assertEquals('ParentCategory', $ParentCategory->alias); |
183 |
} |
184 |
|
185 |
/**
|
186 |
* testClassRegistryFlush method
|
187 |
*
|
188 |
* @return void
|
189 |
*/
|
190 |
public function testClassRegistryFlush() { |
191 |
ClassRegistry::init('RegisterArticleTag'); |
192 |
|
193 |
$ArticleTag = ClassRegistry::getObject('RegisterArticleTag'); |
194 |
$this->assertInstanceOf('RegisterArticleTag', $ArticleTag); |
195 |
ClassRegistry::flush(); |
196 |
|
197 |
$NoArticleTag = ClassRegistry::isKeySet('RegisterArticleTag'); |
198 |
$this->assertFalse($NoArticleTag); |
199 |
$this->assertInstanceOf('RegisterArticleTag', $ArticleTag); |
200 |
} |
201 |
|
202 |
/**
|
203 |
* testAddMultipleModels method
|
204 |
*
|
205 |
* @return void
|
206 |
*/
|
207 |
public function testAddMultipleModels() { |
208 |
$Article = ClassRegistry::isKeySet('Article'); |
209 |
$this->assertFalse($Article); |
210 |
|
211 |
$Featured = ClassRegistry::isKeySet('Featured'); |
212 |
$this->assertFalse($Featured); |
213 |
|
214 |
$Tag = ClassRegistry::isKeySet('Tag'); |
215 |
$this->assertFalse($Tag); |
216 |
|
217 |
$models = array(array('class' => 'RegisterArticle', 'alias' => 'Article'), |
218 |
array('class' => 'RegisterArticleFeatured', 'alias' => 'Featured'), |
219 |
array('class' => 'RegisterArticleTag', 'alias' => 'Tag')); |
220 |
|
221 |
$added = ClassRegistry::init($models); |
222 |
$this->assertTrue($added); |
223 |
|
224 |
$Article = ClassRegistry::isKeySet('Article'); |
225 |
$this->assertTrue($Article); |
226 |
|
227 |
$Featured = ClassRegistry::isKeySet('Featured'); |
228 |
$this->assertTrue($Featured); |
229 |
|
230 |
$Tag = ClassRegistry::isKeySet('Tag'); |
231 |
$this->assertTrue($Tag); |
232 |
|
233 |
$Article = ClassRegistry::getObject('Article'); |
234 |
$this->assertInstanceOf('RegisterArticle', $Article); |
235 |
|
236 |
$Featured = ClassRegistry::getObject('Featured'); |
237 |
$this->assertInstanceOf('RegisterArticleFeatured', $Featured); |
238 |
|
239 |
$Tag = ClassRegistry::getObject('Tag'); |
240 |
$this->assertInstanceOf('RegisterArticleTag', $Tag); |
241 |
} |
242 |
|
243 |
/**
|
244 |
* testPluginAppModel method
|
245 |
*
|
246 |
* @return void
|
247 |
*/
|
248 |
public function testPluginAppModel() { |
249 |
$TestRegistryPluginModel = ClassRegistry::isKeySet('TestRegistryPluginModel'); |
250 |
$this->assertFalse($TestRegistryPluginModel); |
251 |
|
252 |
//Faking a plugin
|
253 |
CakePlugin::load('RegistryPlugin', array('path' => '/fake/path')); |
254 |
$TestRegistryPluginModel = ClassRegistry::init('RegistryPlugin.TestRegistryPluginModel'); |
255 |
$this->assertInstanceOf('TestRegistryPluginModel', $TestRegistryPluginModel); |
256 |
|
257 |
$this->assertEquals('something_', $TestRegistryPluginModel->tablePrefix); |
258 |
|
259 |
$PluginUser = ClassRegistry::init(array('class' => 'RegistryPlugin.RegisterUser', 'alias' => 'RegistryPluginUser', 'table' => false)); |
260 |
$this->assertInstanceOf('RegistryPluginAppModel', $PluginUser); |
261 |
|
262 |
$PluginUserCopy = ClassRegistry::getObject('RegistryPluginUser'); |
263 |
$this->assertInstanceOf('RegistryPluginAppModel', $PluginUserCopy); |
264 |
$this->assertSame($PluginUser, $PluginUserCopy); |
265 |
CakePlugin::unload();
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* Tests prefixed datasource names for test purposes
|
270 |
*
|
271 |
* @return void
|
272 |
*/
|
273 |
public function testPrefixedTestDatasource() { |
274 |
ClassRegistry::config(array('testing' => true)); |
275 |
$Model = ClassRegistry::init('RegisterPrefixedDs'); |
276 |
$this->assertEquals('test', $Model->useDbConfig); |
277 |
ClassRegistry::removeObject('RegisterPrefixedDs'); |
278 |
|
279 |
$testConfig = ConnectionManager::getDataSource('test')->config; |
280 |
ConnectionManager::create('test_doesnotexist', $testConfig); |
281 |
|
282 |
$Model = ClassRegistry::init('RegisterArticle'); |
283 |
$this->assertEquals('test', $Model->useDbConfig); |
284 |
$Model = ClassRegistry::init('RegisterPrefixedDs'); |
285 |
$this->assertEquals('test_doesnotexist', $Model->useDbConfig); |
286 |
} |
287 |
|
288 |
/**
|
289 |
* Tests that passing the string parameter to init() will return false if the model does not exists
|
290 |
*
|
291 |
* @return void
|
292 |
*/
|
293 |
public function testInitStrict() { |
294 |
$this->assertFalse(ClassRegistry::init('NonExistent', true)); |
295 |
} |
296 |
|
297 |
/**
|
298 |
* Test that you cannot init() an abstract class. An exception will be raised.
|
299 |
*
|
300 |
* @expectedException CakeException
|
301 |
* @return void
|
302 |
*/
|
303 |
public function testInitAbstractClass() { |
304 |
ClassRegistry::init('ClassRegistryAbstractModel'); |
305 |
} |
306 |
|
307 |
/**
|
308 |
* Test that you cannot init() an abstract class. A exception will be raised.
|
309 |
*
|
310 |
* @expectedException CakeException
|
311 |
* @return void
|
312 |
*/
|
313 |
public function testInitInterface() { |
314 |
ClassRegistry::init('ClassRegistryInterfaceTest'); |
315 |
} |
316 |
} |