forked from getsentry/sentry-php
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StacktraceTest.php
248 lines (217 loc) · 8.1 KB
/
StacktraceTest.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
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
function raven_test_recurse($times, $callback)
{
$times -= 1;
if ($times > 0) {
return call_user_func('raven_test_recurse', $times, $callback);
}
return call_user_func($callback);
}
function raven_test_create_stacktrace($args=null, $times=3)
{
return raven_test_recurse($times, 'debug_backtrace');
}
class Raven_Tests_StacktraceTest extends PHPUnit_Framework_TestCase
{
public function testCanTraceParamContext()
{
$stack = raven_test_create_stacktrace(array('biz', 'baz'), 0);
if (isset($stack[0]['function']) and ($stack[0]['function'] == 'call_user_func')) {
$offset = 2;
} else {
$offset = 1;
}
$frame = $stack[$offset];
$params = Raven_Stacktrace::get_frame_context($frame);
$this->assertEquals($params['args'], array('biz', 'baz'));
$this->assertEquals($params['times'], 0);
}
public function testSimpleTrace()
{
$stack = array(
array(
'file' => dirname(__FILE__).'/resources/a.php',
'line' => 9,
'function' => 'a_test',
'args' => array('friend'),
),
array(
'file' => dirname(__FILE__).'/resources/b.php',
'line' => 2,
'args' => array(
dirname(__FILE__).'/resources/a.php',
),
'function' => 'include_once',
)
);
$frames = Raven_Stacktrace::get_stack_info($stack, true);
$frame = $frames[0];
$this->assertEquals(2, $frame['lineno']);
$this->assertNull($frame['function']);
$this->assertEquals("include_once 'a.php';", $frame['context_line']);
$this->assertFalse(isset($frame['vars']));
$frame = $frames[1];
$this->assertEquals(9, $frame['lineno']);
$this->assertEquals('include_once', $frame['function']);
$this->assertEquals('a_test($foo);', $frame['context_line']);
$this->assertEquals(dirname(__FILE__) . '/resources/a.php', $frame['vars']['param1']);
}
public function testDoesNotModifyCaptureVars()
{
// PHP's errcontext as passed to the error handler contains REFERENCES to any vars that were in the global scope.
// Modification of these would be really bad, since if control is returned (non-fatal error) we'll have altered the state of things!
$originalFoo = 'bloopblarp';
$newFoo = $originalFoo;
$nestedArray = array(
'key' => 'xxxxxxxxxx',
);
$frame = array(
"file" => dirname(__FILE__) . "/resources/a.php",
"line" => 9,
"args"=> array(
&$newFoo,
&$nestedArray,
),
"function" => "a_test",
);
$result = Raven_Stacktrace::get_frame_context($frame, 5);
// Check we haven't modified our vars.
$this->assertEquals($originalFoo, 'bloopblarp');
$this->assertEquals($nestedArray['key'], 'xxxxxxxxxx');
// Check that we did truncate the variable in our output
$this->assertEquals($result['param1'], 'bloop');
$this->assertEquals($result['param2']['key'], 'xxxxx');
}
public function testDoesFixFrameInfo()
{
if (isset($_ENV['HHVM']) and ($_ENV['HHVM'] == 1)) {
$this->markTestSkipped('HHVM stacktrace behaviour');
return;
}
/**
* PHP's way of storing backstacks seems bass-ackwards to me
* 'function' is not the function you're in; it's any function being
* called, so we have to shift 'function' down by 1. Ugh.
*/
$stack = raven_test_create_stacktrace();
$frames = Raven_Stacktrace::get_stack_info($stack, true);
// just grab the last few frames
$frames = array_slice($frames, -6);
$skip_call_user_func_fix = false;
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$skip_call_user_func_fix = true;
foreach ($frames as &$frame) {
if (isset($frame['function']) and ($frame['function'] == 'call_user_func')) {
$skip_call_user_func_fix = false;
break;
}
}
unset($frame);
}
if ($skip_call_user_func_fix) {
$frame = $frames[3];
$this->assertEquals('raven_test_create_stacktrace', $frame['function']);
$frame = $frames[4];
$this->assertEquals('raven_test_recurse', $frame['function']);
$frame = $frames[5];
$this->assertEquals('raven_test_recurse', $frame['function']);
} else {
$frame = $frames[0];
$this->assertEquals('raven_test_create_stacktrace', $frame['function']);
$frame = $frames[1];
$this->assertEquals('raven_test_recurse', $frame['function']);
$frame = $frames[2];
$this->assertEquals('call_user_func', $frame['function']);
$frame = $frames[3];
$this->assertEquals('raven_test_recurse', $frame['function']);
$frame = $frames[4];
$this->assertEquals('call_user_func', $frame['function']);
$frame = $frames[5];
$this->assertEquals('raven_test_recurse', $frame['function']);
}
}
public function testInApp()
{
$stack = array(
array(
"file" => dirname(__FILE__) . "/resources/a.php",
"line" => 11,
"function" => "a_test",
),
array(
"file" => dirname(__FILE__) . "/resources/b.php",
"line" => 3,
"function" => "include_once",
),
);
$frames = Raven_Stacktrace::get_stack_info($stack, true, null, 0, null, dirname(__FILE__));
$this->assertEquals($frames[0]['in_app'], true);
$this->assertEquals($frames[1]['in_app'], true);
}
public function testInAppWithExclusion()
{
$stack = array(
array(
"file" => dirname(__FILE__) . '/resources/foo/a.php',
"line" => 11,
"function" => "a_test",
),
array(
"file" => dirname(__FILE__) . '/resources/bar/b.php',
"line" => 3,
"function" => "include_once",
),
);
$frames = Raven_Stacktrace::get_stack_info(
$stack, true, null, 0, null, dirname(__FILE__) . '/',
array(dirname(__FILE__) . '/resources/bar/'));
// stack gets reversed
$this->assertEquals($frames[0]['in_app'], false);
$this->assertEquals($frames[1]['in_app'], true);
}
public function testBasePath()
{
$stack = array(
array(
"file" => dirname(__FILE__) . "/resources/a.php",
"line" => 11,
"function" => "a_test",
),
);
$frames = Raven_Stacktrace::get_stack_info($stack, true, null, 0, array(dirname(__FILE__) . '/'));
$this->assertEquals($frames[0]['filename'], 'resources/a.php');
}
public function testNoBasePath()
{
$stack = array(
array(
"file" => dirname(__FILE__) . "/resources/a.php",
"line" => 11,
"function" => "a_test",
),
);
$frames = Raven_Stacktrace::get_stack_info($stack);
$this->assertEquals($frames[0]['filename'], dirname(__FILE__) . '/resources/a.php');
}
public function testWithEvaldCode()
{
try {
eval("throw new Exception('foobar');");
} catch (Exception $ex) {
$trace = $ex->getTrace();
$frames = Raven_Stacktrace::get_stack_info($trace);
}
/**
* @var array $frames
*/
$this->assertEquals($frames[count($frames) -1]['filename'], __FILE__);
}
}