-
-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathFrameContextifierIntegrationTest.php
More file actions
169 lines (134 loc) · 5.17 KB
/
Copy pathFrameContextifierIntegrationTest.php
File metadata and controls
169 lines (134 loc) · 5.17 KB
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
<?php
declare(strict_types=1);
namespace Sentry\Tests\Integration;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\Frame;
use Sentry\Integration\FrameContextifierIntegration;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\Stacktrace;
use Sentry\State\Scope;
use function Sentry\withScope;
final class FrameContextifierIntegrationTest extends TestCase
{
/**
* @dataProvider invokeDataProvider
*/
public function testInvoke(string $fixtureFilePath, int $lineNumber, int $contextLines, int $preContextCount, int $postContextCount): void
{
$options = new Options(['context_lines' => $contextLines]);
$integration = new FrameContextifierIntegration();
$integration->setupOnce();
/** @var ClientInterface&MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getIntegration')
->with(FrameContextifierIntegration::class)
->willReturn($integration);
$client->expects($this->once())
->method('getOptions')
->willReturn($options);
SentrySdk::init($client);
$stacktrace = new Stacktrace([
new Frame('[unknown]', $fixtureFilePath, $lineNumber, null, $fixtureFilePath),
]);
$event = Event::createEvent();
$event->setStacktrace($stacktrace);
withScope(static function (Scope $scope) use (&$event): void {
$event = $scope->applyToEvent($event);
});
$this->assertNotNull($event);
$this->assertNotNull($event->getStacktrace());
$frames = $event->getStacktrace()->getFrames();
$this->assertCount(1, $frames);
$this->assertCount($preContextCount, $frames[0]->getPreContext());
$this->assertCount($postContextCount, $frames[0]->getPostContext());
$fileContent = explode("\n", $this->getFixtureFileContent($fixtureFilePath));
for ($i = 0; $i < $preContextCount; ++$i) {
$this->assertSame(rtrim($fileContent[$i + ($lineNumber - $preContextCount - 1)]), $frames[0]->getPreContext()[$i]);
}
$this->assertSame(rtrim($fileContent[$lineNumber - 1]), $frames[0]->getContextLine());
for ($i = 0; $i < $postContextCount; ++$i) {
$this->assertSame(rtrim($fileContent[$i + $lineNumber]), $frames[0]->getPostContext()[$i]);
}
}
public static function invokeDataProvider(): \Generator
{
yield 'short file' => [
realpath(__DIR__ . '/../Fixtures/code/ShortFile.php'),
3,
2,
2,
2,
];
yield 'long file with specified context' => [
realpath(__DIR__ . '/../Fixtures/code/LongFile.php'),
8,
2,
2,
2,
];
yield 'long file near end of file' => [
realpath(__DIR__ . '/../Fixtures/code/LongFile.php'),
11,
5,
5,
2,
];
yield 'long file near beginning of file' => [
realpath(__DIR__ . '/../Fixtures/code/LongFile.php'),
3,
5,
2,
5,
];
}
public function testInvokeLogsWarningMessageIfSourceCodeExcerptCannotBeRetrievedForFrame(): void
{
/** @var MockObject&LoggerInterface $logger */
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())
->method('warning')
->with('Failed to get the source code excerpt for the file "file.ext".');
$options = new Options();
$integration = new FrameContextifierIntegration($logger);
$integration->setupOnce();
/** @var ClientInterface&MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getIntegration')
->with(FrameContextifierIntegration::class)
->willReturn($integration);
$client->expects($this->once())
->method('getOptions')
->willReturn($options);
SentrySdk::init($client);
$stacktrace = new Stacktrace([
new Frame(null, '[internal]', 0),
new Frame(null, 'file.ext', 10, null, 'file.ext'),
]);
$event = Event::createEvent();
$event->setStacktrace($stacktrace);
withScope(static function (Scope $scope) use (&$event): void {
$event = $scope->applyToEvent($event);
});
$this->assertNotNull($event);
foreach ($stacktrace->getFrames() as $frame) {
$this->assertNull($frame->getContextLine());
$this->assertEmpty($frame->getPreContext());
$this->assertEmpty($frame->getPostContext());
}
}
private function getFixtureFileContent(string $file): string
{
$fileContent = file_get_contents($file);
if ($fileContent === false) {
throw new \RuntimeException(\sprintf('The fixture file at path "%s" could not be read.', $file));
}
return $fileContent;
}
}