forked from troelskn/phpweaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
385 lines (371 loc) · 15.4 KB
/
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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
error_reporting(E_ALL | E_STRICT);
/**
* todo:
* xtrace -> resources ..
* use static typehints for parameter types
* use docblock comments for parameter types
* merge with existing docblock comments
*/
// You need to have simpletest in your include_path
require_once 'simpletest/unit_tester.php';
require_once 'simpletest/mock_objects.php';
if (realpath($_SERVER['PHP_SELF']) == __FILE__) {
require_once 'simpletest/autorun.php';
}
require_once 'signature.inc.php';
require_once 'xtrace.inc.php';
require_once 'scanner.inc.php';
require_once 'transform.inc.php';
require_once 'reflector.inc.php';
class TestOfTokenizer extends UnitTestCase {
function test_tokenize_php_without_errors() {
$tokenizer = new TokenStreamParser();
$tokenizer->scan('<'.'?php function foo($x) {} ?'.'>');
}
}
class TestOfClassScanner extends UnitTestCase {
function test_can_track_current_class() {
$scanner = new ClassScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php class Foo { function bar($x) {}');
$token_stream->iterate($scanner);
$this->assertEqual($scanner->getCurrentClass(), 'Foo');
}
function test_forgets_class_when_scope_ends() {
$scanner = new ClassScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php class Foo { function bar($x) {} }');
$token_stream->iterate($scanner);
$this->assertEqual($scanner->getCurrentClass(), null);
}
function test_forgets_class_when_scope_ends_within_nested_scopes() {
$scanner = new ClassScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php when (true) { class Foo { function bar($x) {} } }');
$token_stream->iterate($scanner);
$this->assertEqual($scanner->getCurrentClass(), null);
}
}
class test_callbackListener {
protected $results = array();
function results() {
return $this->results;
}
function one() {
if (count($this->results) !== 1) {
throw new Exception("Expected exactly one result");
}
return $this->results[0];
}
function call() {
$this->results[] = func_get_args();
}
}
class TestOfClassExtendsScanner extends UnitTestCase {
function test_can_track_single_extends() {
$scanner = new ScannerMultiplexer();
$class_scanner = $scanner->appendScanner(new ClassScanner());
$inheritance_scanner = $scanner->appendScanner(new ClassExtendsScanner($class_scanner));
$listener = new test_callbackListener();
$inheritance_scanner->notifyOnExtends(array($listener, 'call'));
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php class Foo extends Bar {}');
$token_stream->iterate($scanner);
$this->assertEqual($listener->one(), array('Foo', 'Bar'));
}
function test_can_track_single_implements() {
$scanner = new ScannerMultiplexer();
$class_scanner = $scanner->appendScanner(new ClassScanner());
$inheritance_scanner = $scanner->appendScanner(new ClassExtendsScanner($class_scanner));
$listener = new test_callbackListener();
$inheritance_scanner->notifyOnImplements(array($listener, 'call'));
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php class Foo implements Bar {}');
$token_stream->iterate($scanner);
$this->assertEqual($listener->one(), array('Foo', 'Bar'));
}
function test_can_track_multiple_implements() {
$scanner = new ScannerMultiplexer();
$class_scanner = $scanner->appendScanner(new ClassScanner());
$inheritance_scanner = $scanner->appendScanner(new ClassExtendsScanner($class_scanner));
$listener = new test_callbackListener();
$inheritance_scanner->notifyOnImplements(array($listener, 'call'));
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php class Foo implements Bar, Doink {}');
$token_stream->iterate($scanner);
$this->assertEqual($listener->results(), array(array('Foo', 'Bar'), array('Foo', 'Doink')));
}
}
class TestOfStaticReflector extends UnitTestCase {
function test_can_scan_sources() {
$reflector = new StaticReflector();
$reflector->scanString('<'.'?php class Foo implements Bar, Doink {}');
$reflector->scanString('<'.'?php class Zip implements Bar {}');
$this->assertEqual($reflector->ancestors('Foo'), array('Bar', 'Doink'));
$this->assertEqual($reflector->ancestors('Zip'), array('Bar'));
}
function test_can_collate_same() {
$reflector = new StaticReflector();
$reflector->scanString('<'.'?php class Foo extends Bar {}');
$reflector->scanString('<'.'?php class Zip extends Bar {}');
$this->assertEqual($reflector->collate('Foo', 'Foo'), 'Foo');
}
function test_can_collate_direct_inheritance() {
$reflector = new StaticReflector();
$reflector->scanString('<'.'?php class Foo extends Bar {}');
$reflector->scanString('<'.'?php class Zip extends Bar {}');
$this->assertEqual($reflector->collate('Foo', 'Zip'), 'Bar');
}
function test_can_collate_child_to_parent() {
$reflector = new StaticReflector();
$reflector->scanString('<'.'?php class Foo {}');
$reflector->scanString('<'.'?php class Bar extends Foo {}');
$this->assertEqual($reflector->collate('Foo', 'Bar'), 'Foo');
}
function test_can_collate_parent_to_child() {
$reflector = new StaticReflector();
$reflector->scanString('<'.'?php class Foo {}');
$reflector->scanString('<'.'?php class Bar extends Foo {}');
$this->assertEqual($reflector->collate('Bar', 'Foo'), 'Foo');
}
}
class TestOfFunctionParametersScanner extends UnitTestCase {
function test_can_track_current_signature_for_function() {
$scanner = new FunctionParametersScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php function bar($x) {}');
$token_stream->iterate($scanner);
$this->assertEqual($scanner->getCurrentSignatureAsString(), '($x)');
}
function test_scanner_is_active_after_first_opening_paren() {
$scanner = new FunctionParametersScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php function bar(');
$this->assertFalse($scanner->isActive());
$token_stream->iterate($scanner);
$this->assertTrue($scanner->isActive());
}
}
class TestOfModifiersScanner extends UnitTestCase {
function test_can_track_modifiers() {
$scanner = new ModifiersScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php final static protected');
$this->assertFalse($scanner->isActive());
$token_stream->iterate($scanner);
$this->assertTrue($scanner->isActive());
}
function test_ends_on_function() {
$scanner = new ModifiersScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php final static protected function foo() {} ');
$token_stream->iterate($scanner);
$this->assertFalse($scanner->isActive());
}
}
class TestOfFunctionBodyScanner extends UnitTestCase {
function test_can_track_function_body() {
$scanner = new FunctionBodyScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php function bar() {');
$this->assertFalse($scanner->isActive());
$token_stream->iterate($scanner);
$this->assertTrue($scanner->isActive());
}
function test_can_track_end_of_function_body() {
$scanner = new FunctionBodyScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php function bar() { if (true) {} }');
$token_stream->iterate($scanner);
$this->assertFalse($scanner->isActive());
}
function test_can_track_function_name() {
$scanner = new FunctionBodyScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php function bar() { print 42;');
$token_stream->iterate($scanner);
$this->assertEqual($scanner->getName(), 'bar');
}
function test_can_track_end_of_scoped_function_body() {
$scanner = new FunctionBodyScanner();
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan('<'.'?php class Fizz { function buzz() { if (true) {} }');
$token_stream->iterate($scanner);
$this->assertFalse($scanner->isActive());
}
}
class MockPassthruBufferEditor extends PassthruBufferEditor {
public $buffer;
function editBuffer(TokenBuffer $buffer) {
$this->buffer = clone $buffer;
}
}
class TestOfDocCommentEditorTransformer extends UnitTestCase {
function scan($source, $editor = null) {
$editor = $editor ? $editor : new PassthruBufferEditor();
$scanner = new ScannerMultiplexer();
$parameters_scanner = $scanner->appendScanner(new FunctionParametersScanner());
$function_body_scanner = $scanner->appendScanner(new FunctionBodyScanner());
$modifiers_scanner = $scanner->appendScanner(new ModifiersScanner());
$transformer = $scanner->appendScanner(new DocCommentEditorTransformer($function_body_scanner, $modifiers_scanner, $parameters_scanner, $editor));
$tokenizer = new TokenStreamParser();
$token_stream = $tokenizer->scan($source);
$token_stream->iterate($scanner);
return $transformer;
}
function test_input_returns_output() {
$source = '<'.'?php /** Lorem Ipsum */' . "\n" . 'function bar($x) {}' . "\n" . 'function zim($y) {}';
$transformer = $this->scan($source);
$this->assertEqual($transformer->getOutput(), $source);
}
function test_invokes_editor_on_function() {
$source = '<'.'?php' . "\n" . 'function bar($x) {}';
$mock_editor = new MockPassthruBufferEditor();
$transformer = $this->scan($source, $mock_editor);
$this->assertIsA($mock_editor->buffer, 'TokenBuffer');
$this->assertEqual($mock_editor->buffer->toText(), 'function bar($x) ');
}
function test_invokes_editor_on_function_modifiers() {
$source = '<'.'?php' . "\n" . 'abstract function bar($x) {}';
$mock_editor = new MockPassthruBufferEditor();
$transformer = $this->scan($source, $mock_editor);
$this->assertIsA($mock_editor->buffer, 'TokenBuffer');
$this->assertEqual($mock_editor->buffer->toText(), 'abstract function bar($x) ');
}
function test_doesnt_invoke_editor_on_class_modifiers() {
$source = '<'.'?php' . "\n" . 'abstract class Foo {}';
$mock_editor = new MockPassthruBufferEditor();
$transformer = $this->scan($source, $mock_editor);
$this->assertNull($mock_editor->buffer);
}
function test_invokes_editor_on_docblock() {
$source = '<'.'?php' . "\n" . '/** Lorem Ipsum */' . "\n" . 'function bar($x) {}';
$mock_editor = new MockPassthruBufferEditor();
$transformer = $this->scan($source, $mock_editor);
$this->assertIsA($mock_editor->buffer, 'TokenBuffer');
$this->assertTrue($mock_editor->buffer->getFirstToken()->isA(T_DOC_COMMENT));
$this->assertEqual($mock_editor->buffer->toText(), '/** Lorem Ipsum */' . "\n" . 'function bar($x) ');
}
}
class TestOfTracer extends UnitTestCase {
function bindir() {
return dirname(__FILE__);
}
function sandbox() {
return dirname(__FILE__) . '/sandbox';
}
function setUp() {
$this->curdir = getcwd();
$dir_sandbox = $this->sandbox();
mkdir($dir_sandbox);
$source_include = '<'.'?php' . "\n" .
'function callit($foo) {' . "\n" .
' return new Bar();' . "\n" .
'}'. "\n" .
'class Foo {' . "\n" .
' protected $val;' . "\n" .
' function __construct($val) {' . "\n" .
' $this->val = $val;' . "\n" .
' }'. "\n" .
'}'. "\n" .
'class Bar {' . "\n" .
'}';
file_put_contents($dir_sandbox . '/include.php', $source_include);
$source_main = '<'.'?php' . "\n" .
'require_once "include.php";' . "\n" .
'callit(new Foo(42));' . "\n" .
'echo "(completed)\n";';
file_put_contents($dir_sandbox . '/main.php', $source_main);
}
function tearDown() {
chdir($this->curdir);
$dir_sandbox = $this->sandbox();
unlink($dir_sandbox . '/include.php');
unlink($dir_sandbox . '/main.php');
if (is_file($dir_sandbox . '/dumpfile.xt')) {
unlink($dir_sandbox . '/dumpfile.xt');
}
rmdir($dir_sandbox);
}
function test_can_execute_sandbox_code() {
chdir($this->sandbox());
$output = shell_exec('php ' . escapeshellarg($this->sandbox() . '/main.php'));
$this->assertEqual("(completed)\n", $output);
}
function test_can_execute_sandbox_code_with_instrumentation() {
chdir($this->sandbox());
$output = shell_exec(escapeshellcmd($this->bindir() . '/trace.sh') . " " . escapeshellarg($this->sandbox() . '/main.php'));
//$this->dump("\n----\n" . $output . "\n----\n");
$this->assertPattern('~\(completed\)~', $output);
$this->assertPattern('~TRACE COMPLETE\n$~', $output);
}
function test_instrumentation_creates_tracefile() {
chdir($this->sandbox());
$output = shell_exec(escapeshellcmd($this->bindir() . '/trace.sh') . " " . escapeshellarg($this->sandbox() . '/main.php'));
$this->assertTrue(is_file($this->sandbox() . '/dumpfile.xt'));
}
function test_can_parse_tracefile() {
chdir($this->sandbox());
shell_exec(escapeshellcmd($this->bindir() . '/trace.sh') . " " . escapeshellarg($this->sandbox() . '/main.php'));
$sigs = new Signatures(new DummyClassCollator());
$this->assertFalse($sigs->has('callit'));
$trace = new xtrace_TraceReader(new SplFileObject($this->sandbox() . '/dumpfile.xt'));
$collector = new xtrace_TraceSignatureLogger($sigs);
$trace->process(new xtrace_FunctionTracer($collector));
$this->assertTrue($sigs->has('callit'));
}
function test_can_parse_class_arg() {
chdir($this->sandbox());
shell_exec(escapeshellcmd($this->bindir() . '/trace.sh') . " " . escapeshellarg($this->sandbox() . '/main.php'));
$sigs = new Signatures(new DummyClassCollator());
$trace = new xtrace_TraceReader(new SplFileObject($this->sandbox() . '/dumpfile.xt'));
$collector = new xtrace_TraceSignatureLogger($sigs);
$trace->process(new xtrace_FunctionTracer($collector));
$this->assertEqual('Foo', $sigs->get('callit')->getArgumentById(0)->getType());
}
}
class TestOfCollation extends UnitTestCase {
function bindir() {
return dirname(__FILE__);
}
function sandbox() {
return dirname(__FILE__) . '/sandbox';
}
function setUp() {
$this->curdir = getcwd();
$dir_sandbox = $this->sandbox();
mkdir($dir_sandbox);
$source_main = '<'.'?php' . "\n" .
'class Foo {' . "\n" .
'}'. "\n" .
'class Bar extends Foo {' . "\n" .
'}'. "\n" .
'class Cuux extends Foo {' . "\n" .
'}'. "\n" .
'function do_stuff($x) {}'. "\n" .
'do_stuff(new Bar());'. "\n" .
'do_stuff(new Cuux());'
;
file_put_contents($dir_sandbox . '/main.php', $source_main);
}
function tearDown() {
chdir($this->curdir);
$dir_sandbox = $this->sandbox();
unlink($dir_sandbox . '/main.php');
if (is_file($dir_sandbox . '/dumpfile.xt')) {
unlink($dir_sandbox . '/dumpfile.xt');
}
rmdir($dir_sandbox);
}
function test_can_collate_classes() {
chdir($this->sandbox());
shell_exec(escapeshellcmd($this->bindir() . '/trace.sh') . " " . escapeshellarg($this->sandbox() . '/main.php'));
$reflector = new StaticReflector();
$sigs = new Signatures($reflector);
$trace = new xtrace_TraceReader(new SplFileObject($this->sandbox() . '/dumpfile.xt'));
$collector = new xtrace_TraceSignatureLogger($sigs, $reflector);
$trace->process(new xtrace_FunctionTracer($collector));
$this->assertEqual('Foo', $sigs->get('do_stuff')->getArgumentById(0)->getType());
}
}