1 | <?php |
||
10 | class ObjectTest extends SapphireTest { |
||
11 | |||
12 | public function setUp() { |
||
16 | |||
17 | public function testHasmethodBehaviour() { |
||
18 | $obj = new ObjectTest_ExtendTest(); |
||
19 | |||
20 | $this->assertTrue($obj->hasMethod('extendableMethod'), "Extension method found in original spelling"); |
||
21 | $this->assertTrue($obj->hasMethod('ExTendableMethod'), "Extension method found case-insensitive"); |
||
22 | |||
23 | $objs = array(); |
||
24 | $objs[] = new ObjectTest_T2(); |
||
25 | $objs[] = new ObjectTest_T2(); |
||
26 | $objs[] = new ObjectTest_T2(); |
||
27 | |||
28 | // All these methods should exist and return true |
||
29 | $trueMethods = array('testMethod','otherMethod','someMethod','t1cMethod','normalMethod'); |
||
30 | |||
31 | foreach($objs as $i => $obj) { |
||
32 | foreach($trueMethods as $method) { |
||
33 | $methodU = strtoupper($method); |
||
34 | $methodL = strtoupper($method); |
||
35 | $this->assertTrue($obj->hasMethod($method), "Test that obj#$i has method $method ($obj->class)"); |
||
36 | $this->assertTrue($obj->hasMethod($methodU), "Test that obj#$i has method $methodU"); |
||
37 | $this->assertTrue($obj->hasMethod($methodL), "Test that obj#$i has method $methodL"); |
||
38 | |||
39 | $this->assertTrue($obj->$method(), "Test that obj#$i can call method $method"); |
||
40 | $this->assertTrue($obj->$methodU(), "Test that obj#$i can call method $methodU"); |
||
41 | $this->assertTrue($obj->$methodL(), "Test that obj#$i can call method $methodL"); |
||
42 | } |
||
43 | |||
44 | $this->assertTrue($obj->hasMethod('Wrapping'), "Test that obj#$i has method Wrapping"); |
||
45 | $this->assertTrue($obj->hasMethod('WRAPPING'), "Test that obj#$i has method WRAPPING"); |
||
46 | $this->assertTrue($obj->hasMethod('wrapping'), "Test that obj#$i has method wrapping"); |
||
47 | |||
48 | $this->assertEquals("Wrapping", $obj->Wrapping(), "Test that obj#$i can call method Wrapping"); |
||
49 | $this->assertEquals("Wrapping", $obj->WRAPPING(), "Test that obj#$i can call method WRAPPIGN"); |
||
50 | $this->assertEquals("Wrapping", $obj->wrapping(), "Test that obj#$i can call method wrapping"); |
||
51 | } |
||
52 | |||
53 | } |
||
54 | |||
55 | public function testSingletonCreation() { |
||
56 | $myObject = singleton('ObjectTest_MyObject'); |
||
57 | $this->assertEquals($myObject->class, 'ObjectTest_MyObject', |
||
58 | 'singletons are creating a correct class instance'); |
||
59 | $this->assertEquals(get_class($myObject), 'ObjectTest_MyObject', |
||
60 | 'singletons are creating a correct class instance'); |
||
61 | |||
62 | $mySubObject = singleton('ObjectTest_MySubObject'); |
||
63 | $this->assertEquals($mySubObject->class, 'ObjectTest_MySubObject', |
||
64 | 'singletons are creating a correct subclass instance'); |
||
65 | $this->assertEquals(get_class($mySubObject), 'ObjectTest_MySubObject', |
||
66 | 'singletons are creating a correct subclass instance'); |
||
67 | |||
68 | $myFirstObject = singleton('ObjectTest_MyObject'); |
||
69 | $mySecondObject = singleton('ObjectTest_MyObject'); |
||
70 | $this->assertTrue($myFirstObject === $mySecondObject, |
||
71 | 'singletons are using the same object on subsequent calls'); |
||
72 | } |
||
73 | |||
74 | public function testStaticGetterMethod() { |
||
75 | $obj = singleton('ObjectTest_MyObject'); |
||
76 | $this->assertEquals( |
||
77 | 'MyObject', |
||
78 | $obj->stat('mystaticProperty'), |
||
79 | 'Uninherited statics through stat() on a singleton behave the same as built-in PHP statics' |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | public function testStaticInheritanceGetters() { |
||
84 | $obj = singleton('ObjectTest_MyObject'); |
||
85 | $subObj = singleton('ObjectTest_MyObject'); |
||
86 | $this->assertEquals( |
||
87 | $subObj->stat('mystaticProperty'), |
||
88 | 'MyObject', |
||
89 | 'Statics defined on a parent class are available through stat() on a subclass' |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | public function testStaticSettingOnSingletons() { |
||
94 | $singleton1 = singleton('ObjectTest_MyObject'); |
||
95 | $singleton2 = singleton('ObjectTest_MyObject'); |
||
96 | $singleton1->set_stat('mystaticProperty', 'changed'); |
||
97 | $this->assertEquals( |
||
98 | $singleton2->stat('mystaticProperty'), |
||
99 | 'changed', |
||
100 | 'Statics setting is populated throughout singletons without explicitly clearing cache' |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | public function testStaticSettingOnInstances() { |
||
105 | $instance1 = new ObjectTest_MyObject(); |
||
106 | $instance2 = new ObjectTest_MyObject(); |
||
107 | $instance1->set_stat('mystaticProperty', 'changed'); |
||
108 | $this->assertEquals( |
||
109 | $instance2->stat('mystaticProperty'), |
||
110 | 'changed', |
||
111 | 'Statics setting through set_stat() is populated throughout instances without explicitly clearing cache' |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Tests that {@link Object::create()} correctly passes all arguments to the new object |
||
117 | */ |
||
118 | public function testCreateWithArgs() { |
||
119 | $createdObj = ObjectTest_CreateTest::create('arg1', 'arg2', array(), null, 'arg5'); |
||
120 | $this->assertEquals($createdObj->constructArguments, array('arg1', 'arg2', array(), null, 'arg5')); |
||
121 | |||
122 | $strongObj = Object::strong_create('ObjectTest_CreateTest', 'arg1', 'arg2', array(), null, 'arg5'); |
||
123 | $this->assertEquals($strongObj->constructArguments, array('arg1', 'arg2', array(), null, 'arg5')); |
||
|
|||
124 | } |
||
125 | |||
126 | public function testCreateLateStaticBinding() { |
||
130 | |||
131 | /** |
||
132 | * Tests that {@link Object::useCustomClass()} correnctly replaces normal and strong objects |
||
133 | */ |
||
134 | public function testUseCustomClass() { |
||
135 | $obj1 = ObjectTest_CreateTest::create(); |
||
136 | $this->assertTrue($obj1 instanceof ObjectTest_CreateTest); |
||
137 | |||
138 | Object::useCustomClass('ObjectTest_CreateTest', 'ObjectTest_CreateTest2'); |
||
139 | $obj2 = ObjectTest_CreateTest::create(); |
||
140 | $this->assertTrue($obj2 instanceof ObjectTest_CreateTest2); |
||
141 | |||
142 | $obj2_2 = Object::strong_create('ObjectTest_CreateTest'); |
||
143 | $this->assertTrue($obj2_2 instanceof ObjectTest_CreateTest); |
||
144 | |||
145 | Object::useCustomClass('ObjectTest_CreateTest', 'ObjectTest_CreateTest3', true); |
||
146 | $obj3 = ObjectTest_CreateTest::create(); |
||
147 | $this->assertTrue($obj3 instanceof ObjectTest_CreateTest3); |
||
148 | |||
149 | $obj3_2 = Object::strong_create('ObjectTest_CreateTest'); |
||
150 | $this->assertTrue($obj3_2 instanceof ObjectTest_CreateTest3); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Tests {@link Object::singleton()} |
||
155 | */ |
||
156 | public function testSingleton() { |
||
157 | $inst = Controller::singleton(); |
||
158 | $this->assertInstanceOf('Controller', $inst); |
||
159 | $inst2 = Controller::singleton(); |
||
160 | $this->assertSame($inst2, $inst); |
||
161 | } |
||
162 | |||
163 | public function testGetExtensions() { |
||
164 | $this->assertEquals( |
||
165 | Object::get_extensions('ObjectTest_ExtensionTest'), |
||
166 | array( |
||
167 | 'oBjEcTTEST_ExtendTest1', |
||
168 | "ObjectTest_ExtendTest2", |
||
169 | ) |
||
170 | ); |
||
171 | $this->assertEquals( |
||
172 | Object::get_extensions('ObjectTest_ExtensionTest', true), |
||
173 | array( |
||
174 | 'oBjEcTTEST_ExtendTest1', |
||
175 | "ObjectTest_ExtendTest2('FOO', 'BAR')", |
||
176 | ) |
||
177 | ); |
||
178 | $inst = new ObjectTest_ExtensionTest(); |
||
179 | $extensions = $inst->getExtensionInstances(); |
||
180 | $this->assertEquals(count($extensions), 2); |
||
181 | $this->assertInstanceOf( |
||
182 | 'ObjectTest_ExtendTest1', |
||
183 | $extensions['ObjectTest_ExtendTest1'] |
||
184 | ); |
||
185 | $this->assertInstanceOf( |
||
186 | 'ObjectTest_ExtendTest2', |
||
187 | $extensions['ObjectTest_ExtendTest2'] |
||
188 | ); |
||
189 | $this->assertInstanceOf( |
||
190 | 'ObjectTest_ExtendTest1', |
||
191 | $inst->getExtensionInstance('ObjectTest_ExtendTest1') |
||
192 | ); |
||
193 | $this->assertInstanceOf( |
||
194 | 'ObjectTest_ExtendTest2', |
||
195 | $inst->getExtensionInstance('ObjectTest_ExtendTest2') |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Tests {@link Object::has_extension()}, {@link Object::add_extension()} |
||
201 | */ |
||
202 | public function testHasAndAddExtension() { |
||
203 | // ObjectTest_ExtendTest1 is built in via $extensions |
||
204 | $this->assertTrue( |
||
205 | ObjectTest_ExtensionTest::has_extension('OBJECTTEST_ExtendTest1'), |
||
206 | "Extensions are detected when set on Object::\$extensions on has_extension() without case-sensitivity" |
||
207 | ); |
||
208 | $this->assertTrue( |
||
209 | ObjectTest_ExtensionTest::has_extension('ObjectTest_ExtendTest1'), |
||
210 | "Extensions are detected when set on Object::\$extensions on has_extension() without case-sensitivity" |
||
211 | ); |
||
212 | $this->assertTrue( |
||
213 | singleton('ObjectTest_ExtensionTest')->hasExtension('ObjectTest_ExtendTest1'), |
||
214 | "Extensions are detected when set on Object::\$extensions on instance hasExtension() without" |
||
215 | . " case-sensitivity" |
||
216 | ); |
||
217 | |||
218 | // ObjectTest_ExtendTest2 is built in via $extensions (with parameters) |
||
219 | $this->assertTrue( |
||
220 | ObjectTest_ExtensionTest::has_extension('ObjectTest_ExtendTest2'), |
||
221 | "Extensions are detected with static has_extension() when set on Object::\$extensions with" |
||
222 | . " additional parameters" |
||
223 | ); |
||
224 | $this->assertTrue( |
||
225 | singleton('ObjectTest_ExtensionTest')->hasExtension('ObjectTest_ExtendTest2'), |
||
226 | "Extensions are detected with instance hasExtension() when set on Object::\$extensions with" |
||
227 | . " additional parameters" |
||
228 | ); |
||
229 | $this->assertFalse( |
||
230 | ObjectTest_ExtensionTest::has_extension('ObjectTest_ExtendTest3'), |
||
231 | "Other extensions available in the system are not present unless explicitly added to this object" |
||
232 | . " when checking through has_extension()" |
||
233 | ); |
||
234 | $this->assertFalse( |
||
235 | singleton('ObjectTest_ExtensionTest')->hasExtension('ObjectTest_ExtendTest3'), |
||
236 | "Other extensions available in the system are not present unless explicitly added to this object" |
||
237 | . " when checking through instance hasExtension()" |
||
238 | ); |
||
239 | |||
240 | // ObjectTest_ExtendTest3 is added manually |
||
241 | ObjectTest_ExtensionTest::add_extension('ObjectTest_ExtendTest3("Param")'); |
||
242 | $this->assertTrue( |
||
243 | ObjectTest_ExtensionTest::has_extension('ObjectTest_ExtendTest3'), |
||
244 | "Extensions are detected with static has_extension() when added through add_extension()" |
||
245 | ); |
||
246 | // ObjectTest_ExtendTest4 is added manually |
||
247 | ObjectTest_ExtensionTest3::add_extension('ObjectTest_ExtendTest4("Param")'); |
||
248 | // test against ObjectTest_ExtendTest3, not ObjectTest_ExtendTest3 |
||
249 | $this->assertTrue( |
||
250 | ObjectTest_ExtensionTest3::has_extension('ObjectTest_ExtendTest4'), |
||
251 | "Extensions are detected with static has_extension() when added through add_extension()" |
||
252 | ); |
||
253 | // test against ObjectTest_ExtendTest3, not ObjectTest_ExtendTest4 to test if it picks up |
||
254 | // the sub classes of ObjectTest_ExtendTest3 |
||
255 | $this->assertTrue( |
||
256 | ObjectTest_ExtensionTest3::has_extension('ObjectTest_ExtendTest3'), |
||
257 | "Sub-Extensions are detected with static has_extension() when added through add_extension()" |
||
258 | ); |
||
259 | // strictly test against ObjectTest_ExtendTest3, not ObjectTest_ExtendTest4 to test if it picks up |
||
260 | // the sub classes of ObjectTest_ExtendTest3 |
||
261 | $this->assertFalse( |
||
262 | ObjectTest_ExtensionTest3::has_extension('ObjectTest_ExtendTest3', null, true), |
||
263 | "Sub-Extensions are detected with static has_extension() when added through add_extension()" |
||
264 | ); |
||
265 | // a singleton() wouldn't work as its already initialized |
||
266 | $objectTest_ExtensionTest = new ObjectTest_ExtensionTest(); |
||
267 | $this->assertTrue( |
||
268 | $objectTest_ExtensionTest->hasExtension('ObjectTest_ExtendTest3'), |
||
269 | "Extensions are detected with instance hasExtension() when added through add_extension()" |
||
270 | ); |
||
271 | |||
272 | // @todo At the moment, this does NOT remove the extension due to parameterized naming, |
||
273 | // meaning the extension will remain added in further test cases |
||
274 | ObjectTest_ExtensionTest::remove_extension('ObjectTest_ExtendTest3'); |
||
275 | } |
||
276 | |||
277 | public function testRemoveExtension() { |
||
278 | // manually add ObjectTest_ExtendTest2 |
||
279 | ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2'); |
||
280 | $this->assertTrue( |
||
281 | ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest2'), |
||
282 | "Extension added through \$add_extension() are added correctly" |
||
283 | ); |
||
284 | |||
285 | ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest2'); |
||
286 | $this->assertFalse( |
||
287 | ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest2'), |
||
288 | "Extension added through \$add_extension() are detected as removed in has_extension()" |
||
289 | ); |
||
290 | $this->assertFalse( |
||
291 | singleton('ObjectTest_ExtensionRemoveTest')->hasExtension('ObjectTest_ExtendTest2'), |
||
292 | "Extensions added through \$add_extension() are detected as removed in instances through hasExtension()" |
||
293 | ); |
||
294 | |||
295 | // ObjectTest_ExtendTest1 is already present in $extensions |
||
296 | ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest1'); |
||
297 | |||
298 | $this->assertFalse( |
||
299 | ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest1'), |
||
300 | "Extension added through \$extensions are detected as removed in has_extension()" |
||
301 | ); |
||
302 | |||
303 | $objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest(); |
||
304 | $this->assertFalse( |
||
305 | $objectTest_ExtensionRemoveTest->hasExtension('ObjectTest_ExtendTest1'), |
||
306 | "Extensions added through \$extensions are detected as removed in instances through hasExtension()" |
||
307 | ); |
||
308 | } |
||
309 | |||
310 | public function testRemoveExtensionWithParameters() { |
||
311 | ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2("MyParam")'); |
||
312 | |||
313 | $this->assertTrue( |
||
314 | ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest2'), |
||
315 | "Extension added through \$add_extension() are added correctly" |
||
316 | ); |
||
317 | |||
318 | ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest2'); |
||
319 | $this->assertFalse( |
||
320 | Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'), |
||
321 | "Extension added through \$add_extension() are detected as removed in has_extension()" |
||
322 | ); |
||
323 | |||
324 | $objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest(); |
||
325 | $this->assertFalse( |
||
326 | $objectTest_ExtensionRemoveTest->hasExtension('ObjectTest_ExtendTest2'), |
||
327 | "Extensions added through \$extensions are detected as removed in instances through hasExtension()" |
||
328 | ); |
||
329 | } |
||
330 | |||
331 | public function testParentClass() { |
||
334 | |||
335 | public function testIsA() { |
||
339 | |||
340 | /** |
||
341 | * Tests {@link Object::hasExtension() and Object::getExtensionInstance()} |
||
342 | */ |
||
343 | public function testExtInstance() { |
||
344 | $obj = new ObjectTest_ExtensionTest2(); |
||
345 | |||
346 | $this->assertTrue($obj->hasExtension('ObjectTest_Extension')); |
||
347 | $this->assertTrue($obj->getExtensionInstance('ObjectTest_Extension') instanceof ObjectTest_Extension); |
||
348 | } |
||
349 | |||
350 | public function testCacheToFile() { |
||
372 | |||
373 | public function testExtend() { |
||
374 | $object = new ObjectTest_ExtendTest(); |
||
375 | $argument = 'test'; |
||
376 | |||
377 | $this->assertEquals($object->extend('extendableMethod'), array('ExtendTest2()')); |
||
378 | $this->assertEquals($object->extend('extendableMethod', $argument), array('ExtendTest2(modified)')); |
||
379 | $this->assertEquals($argument, 'modified'); |
||
380 | |||
381 | $this->assertEquals($object->invokeWithExtensions('extendableMethod'), array('ExtendTest()', 'ExtendTest2()')); |
||
382 | $this->assertEquals ( |
||
383 | $object->invokeWithExtensions('extendableMethod', 'test'), |
||
384 | array('ExtendTest(test)', 'ExtendTest2(modified)') |
||
385 | ); |
||
386 | |||
400 | |||
401 | public function testParseClassSpec() { |
||
463 | } |
||
464 | |||
665 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.