aboutsummaryrefslogtreecommitdiff
path: root/vendors/simpletest/test/reflection_php5_test.php
blob: 3bfc6e85b580847932b85859198e6fbb0b416c24 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
// $Id: reflection_php5_test.php 1541 2007-06-10 02:27:59Z tswicegood $
require_once(dirname(__FILE__) . '/../autorun.php');
require_once(dirname(__FILE__) . '/../reflection_php5.php');

class AnyOldLeafClass {
	function aMethod() { }
}

abstract class AnyOldClass {
	function aMethod() { }
}

class AnyOldLeafClassWithAFinal {
	final function aMethod() { }
}

interface AnyOldInterface {
	function aMethod();
}

interface AnyOldArgumentInterface {
	function aMethod(AnyOldInterface $argument);
}

interface AnyDescendentInterface extends AnyOldInterface {
}

class AnyOldImplementation implements AnyOldInterface {
	function aMethod() { }
	function extraMethod() { }
}

abstract class AnyAbstractImplementation implements AnyOldInterface {
}

abstract class AnotherOldAbstractClass {
    protected abstract function aMethod(AnyOldInterface $argument);
}

class AnyOldSubclass extends AnyOldImplementation { }

class AnyOldArgumentClass {
	function aMethod($argument) { }
}

class AnyOldArgumentImplementation implements AnyOldArgumentInterface {
	function aMethod(AnyOldInterface $argument) { }
}

class AnyOldTypeHintedClass implements AnyOldArgumentInterface {
	function aMethod(AnyOldInterface $argument) { }
}

class AnyDescendentImplementation implements AnyDescendentInterface {
	function aMethod() { }
}

class AnyOldOverloadedClass {
	function __isset($key) { }
	function __unset($key) { }
}

class AnyOldClassWithStaticMethods {
	static function aStatic() { }
	static function aStaticWithParameters($arg1, $arg2) { }
}

abstract class AnyOldAbstractClassWithAbstractMethods {
    abstract function anAbstract();
    abstract function anAbstractWithParameter($foo);
    abstract function anAbstractWithMultipleParameters($foo, $bar);
}

class TestOfReflection extends UnitTestCase {

	function testClassExistence() {
		$reflection = new SimpleReflection('AnyOldLeafClass');
		$this->assertTrue($reflection->classOrInterfaceExists());
		$this->assertTrue($reflection->classOrInterfaceExistsSansAutoload());
		$this->assertFalse($reflection->isAbstract());
		$this->assertFalse($reflection->isInterface());
	}

	function testClassNonExistence() {
		$reflection = new SimpleReflection('UnknownThing');
		$this->assertFalse($reflection->classOrInterfaceExists());
		$this->assertFalse($reflection->classOrInterfaceExistsSansAutoload());
	}

	function testDetectionOfAbstractClass() {
		$reflection = new SimpleReflection('AnyOldClass');
		$this->assertTrue($reflection->isAbstract());
	}

	function testDetectionOfFinalMethods() {
		$reflection = new SimpleReflection('AnyOldClass');
		$this->assertFalse($reflection->hasFinal());
		$reflection = new SimpleReflection('AnyOldLeafClassWithAFinal');
		$this->assertTrue($reflection->hasFinal());
	}

	function testFindingParentClass() {
		$reflection = new SimpleReflection('AnyOldSubclass');
		$this->assertEqual($reflection->getParent(), 'AnyOldImplementation');
	}

	function testInterfaceExistence() {
		$reflection = new SimpleReflection('AnyOldInterface');
		$this->assertTrue($reflection->classOrInterfaceExists());
		$this->assertTrue($reflection->classOrInterfaceExistsSansAutoload());
		$this->assertTrue($reflection->isInterface());
	}

	function testMethodsListFromClass() {
		$reflection = new SimpleReflection('AnyOldClass');
		$this->assertIdentical($reflection->getMethods(), array('aMethod'));
	}

	function testMethodsListFromInterface() {
		$reflection = new SimpleReflection('AnyOldInterface');
		$this->assertIdentical($reflection->getMethods(), array('aMethod'));
		$this->assertIdentical($reflection->getInterfaceMethods(), array('aMethod'));
	}

	function testMethodsComeFromDescendentInterfacesASWell() {
		$reflection = new SimpleReflection('AnyDescendentInterface');
		$this->assertIdentical($reflection->getMethods(), array('aMethod'));
	}
	
	function testCanSeparateInterfaceMethodsFromOthers() {
		$reflection = new SimpleReflection('AnyOldImplementation');
		$this->assertIdentical($reflection->getMethods(), array('aMethod', 'extraMethod'));
		$this->assertIdentical($reflection->getInterfaceMethods(), array('aMethod'));
	}

	function testMethodsComeFromDescendentInterfacesInAbstractClass() {
		$reflection = new SimpleReflection('AnyAbstractImplementation');
		$this->assertIdentical($reflection->getMethods(), array('aMethod'));
	}

	function testInterfaceHasOnlyItselfToImplement() {
		$reflection = new SimpleReflection('AnyOldInterface');
		$this->assertEqual(
				$reflection->getInterfaces(),
				array('AnyOldInterface'));
	}

	function testInterfacesListedForClass() {
		$reflection = new SimpleReflection('AnyOldImplementation');
		$this->assertEqual(
				$reflection->getInterfaces(),
				array('AnyOldInterface'));
	}

	function testInterfacesListedForSubclass() {
		$reflection = new SimpleReflection('AnyOldSubclass');
		$this->assertEqual(
				$reflection->getInterfaces(),
				array('AnyOldInterface'));
	}

	function testNoParameterCreationWhenNoInterface() {
		$reflection = new SimpleReflection('AnyOldArgumentClass');
		$function = $reflection->getSignature('aMethod');
		if (version_compare(phpversion(), '5.0.2', '<=')) {
			$this->assertEqual('function amethod()', strtolower($function));
		} else {
			$this->assertEqual('function aMethod()', $function);
		}
	}

	function testParameterCreationWithoutTypeHinting() {
		$reflection = new SimpleReflection('AnyOldArgumentImplementation');
		$function = $reflection->getSignature('aMethod');
		if (version_compare(phpversion(), '5.0.2', '<=')) {
			$this->assertEqual('function amethod(AnyOldInterface $argument)', $function);
		} else {
			$this->assertEqual('function aMethod(AnyOldInterface $argument)', $function);
		}
	}

	function testParameterCreationForTypeHinting() {
		$reflection = new SimpleReflection('AnyOldTypeHintedClass');
		$function = $reflection->getSignature('aMethod');
		if (version_compare(phpversion(), '5.0.2', '<=')) {
			$this->assertEqual('function amethod(AnyOldInterface $argument)', $function);
		} else {
			$this->assertEqual('function aMethod(AnyOldInterface $argument)', $function);
		}
	}

	function testIssetFunctionSignature() {
		$reflection = new SimpleReflection('AnyOldOverloadedClass');
		$function = $reflection->getSignature('__isset');
		if (version_compare(phpversion(), '5.1.0', '>=')) {
			$this->assertEqual('function __isset($key)', $function);
		} else {
			$this->assertEqual('function __isset()', $function);
		}
	}
	
	function testUnsetFunctionSignature() {
		$reflection = new SimpleReflection('AnyOldOverloadedClass');
		$function = $reflection->getSignature('__unset');
		if (version_compare(phpversion(), '5.1.0', '>=')) {
			$this->assertEqual('function __unset($key)', $function);
		} else {
			$this->assertEqual('function __unset()', $function);
		}
	}

	function testProperlyReflectsTheFinalInterfaceWhenObjectImplementsAnExtendedInterface() {
		$reflection = new SimpleReflection('AnyDescendentImplementation');
		$interfaces = $reflection->getInterfaces();
		$this->assertEqual(1, count($interfaces));
		$this->assertEqual('AnyDescendentInterface', array_shift($interfaces));
	}
	
	function testCreatingSignatureForAbstractMethod() {
	    $reflection = new SimpleReflection('AnotherOldAbstractClass');
	    $this->assertEqual($reflection->getSignature('aMethod'), 'function aMethod(AnyOldInterface $argument)');
	}
	
	function testCanProperlyGenerateStaticMethodSignatures() {
		$reflection = new SimpleReflection('AnyOldClassWithStaticMethods');
		$this->assertEqual('static function aStatic()', $reflection->getSignature('aStatic'));
		$this->assertEqual(
			'static function aStaticWithParameters($arg1, $arg2)',
			$reflection->getSignature('aStaticWithParameters')
		);
	}
}

class TestOfReflectionWithTypeHints extends UnitTestCase {
	function skip() {
		$this->skipIf(version_compare(phpversion(), '5.1.0', '<'), 'Reflection with type hints only tested for PHP 5.1.0 and above');
	}

	function testParameterCreationForTypeHintingWithArray() {
		eval('interface AnyOldArrayTypeHintedInterface {
				  function amethod(array $argument);
			  } 
			  class AnyOldArrayTypeHintedClass implements AnyOldArrayTypeHintedInterface {
				  function amethod(array $argument) {}
			  }');
		$reflection = new SimpleReflection('AnyOldArrayTypeHintedClass');
		$function = $reflection->getSignature('amethod');
		$this->assertEqual('function amethod(array $argument)', $function);
	}
}

class TestOfAbstractsWithAbstractMethods extends UnitTestCase {
    function testCanProperlyGenerateAbstractMethods() {
        $reflection = new SimpleReflection('AnyOldAbstractClassWithAbstractMethods');
        $this->assertEqual(
            'function anAbstract()',
            $reflection->getSignature('anAbstract')
        );
        $this->assertEqual(
            'function anAbstractWithParameter($foo)',
            $reflection->getSignature('anAbstractWithParameter')
        );
        $this->assertEqual(
            'function anAbstractWithMultipleParameters($foo, $bar)',
            $reflection->getSignature('anAbstractWithMultipleParameters')
        );
    }
}

?>