-
-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathModulesIntegrationTest.php
More file actions
105 lines (86 loc) · 3.15 KB
/
Copy pathModulesIntegrationTest.php
File metadata and controls
105 lines (86 loc) · 3.15 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
<?php
declare(strict_types=1);
namespace Sentry\Tests\Integration;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\ClientBuilder;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\Integration\ModulesIntegration;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Transport\Result;
use Sentry\Transport\ResultStatus;
use Sentry\Transport\TransportInterface;
use function Sentry\withScope;
final class ModulesIntegrationTest extends TestCase
{
/**
* @dataProvider invokeDataProvider
*/
public function testInvoke(bool $isIntegrationEnabled, bool $expectedEmptyModules): void
{
$integration = new ModulesIntegration();
$integration->setupOnce();
/** @var ClientInterface&MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getIntegration')
->willReturn($isIntegrationEnabled ? $integration : null);
SentrySdk::getCurrentHub()->bindClient($client);
withScope(function (Scope $scope) use ($expectedEmptyModules): void {
$event = $scope->applyToEvent(Event::createEvent());
$this->assertNotNull($event);
if ($expectedEmptyModules) {
$this->assertEmpty($event->getModules());
} else {
$this->assertNotEmpty($event->getModules());
}
});
}
public static function invokeDataProvider(): \Generator
{
yield [
false,
true,
];
yield [
true,
false,
];
}
public function testModuleIntegration(): void
{
/** @var TransportInterface&MockObject $transport */
$transport = $this->createMock(TransportInterface::class);
$transport->expects($this->once())
->method('send')
->with($this->callback(function (Event $event): bool {
$modules = $event->getModules();
$this->assertIsArray($modules);
$this->assertNotEmpty($modules);
$this->assertArrayHasKey('psr/log', $modules);
$this->assertMatchesRegularExpression("/^\d+\.\d+\.\d+$/", $modules['psr/log']);
return true;
}))
->willReturnCallback(static function (Event $event): Result {
return new Result(ResultStatus::success(), $event);
});
$client = ClientBuilder::create()
->setTransport($transport)
->getClient();
SentrySdk::getCurrentHub()->bindClient($client);
$client->captureEvent(Event::createEvent(), null, new Scope());
}
/**
* Polyfill for older phpunit versions.
*/
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
if (method_exists(parent::class, 'assertMatchesRegularExpression')) {
parent::assertMatchesRegularExpression($pattern, $string, $message);
} else {
static::assertRegExp($pattern, $string, $message);
}
}
}