-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathBreadcrumbHandlerTest.php
More file actions
108 lines (91 loc) · 4.11 KB
/
BreadcrumbHandlerTest.php
File metadata and controls
108 lines (91 loc) · 4.11 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
<?php
declare(strict_types=1);
namespace Sentry\Tests\Monolog;
use Monolog\Logger;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use Sentry\Breadcrumb;
use Sentry\Monolog\BreadcrumbHandler;
use Sentry\State\HubInterface;
final class BreadcrumbHandlerTest extends TestCase
{
/**
* @dataProvider handleDataProvider
*
* @param LogRecord|array<string, mixed> $record
*/
public function testHandle($record, Breadcrumb $expectedBreadcrumb): void
{
$hub = $this->createMock(HubInterface::class);
$hub->expects($this->once())
->method('addBreadcrumb')
->with($this->callback(function (Breadcrumb $breadcrumb) use ($expectedBreadcrumb): bool {
$this->assertSame($expectedBreadcrumb->getMessage(), $breadcrumb->getMessage());
$this->assertSame($expectedBreadcrumb->getLevel(), $breadcrumb->getLevel());
$this->assertSame($expectedBreadcrumb->getType(), $breadcrumb->getType());
$this->assertEquals($expectedBreadcrumb->getTimestamp(), $breadcrumb->getTimestamp());
$this->assertSame($expectedBreadcrumb->getCategory(), $breadcrumb->getCategory());
$this->assertEquals($expectedBreadcrumb->getMetadata(), $breadcrumb->getMetadata());
return true;
}));
$handler = new BreadcrumbHandler($hub);
$handler->handle($record);
}
/**
* @return iterable<array{LogRecord|array<string, mixed>, Breadcrumb}>
*/
public static function handleDataProvider(): iterable
{
$now = new \DateTimeImmutable();
$defaultBreadcrumb = new Breadcrumb(
Breadcrumb::LEVEL_DEBUG,
Breadcrumb::TYPE_DEFAULT,
'channel.foo',
'foo bar',
[],
(float) $now->format('U.u')
);
$levelsToBeTested = [
Logger::DEBUG => Breadcrumb::LEVEL_DEBUG,
Logger::INFO => Breadcrumb::LEVEL_INFO,
Logger::NOTICE => Breadcrumb::LEVEL_INFO,
Logger::WARNING => Breadcrumb::LEVEL_WARNING,
];
foreach ($levelsToBeTested as $loggerLevel => $breadcrumbLevel) {
yield 'with level ' . Logger::getLevelName($loggerLevel) => [
RecordFactory::create('foo bar', $loggerLevel, 'channel.foo', [], [], $now),
$defaultBreadcrumb->withLevel($breadcrumbLevel),
];
}
yield 'with level ERROR' => [
RecordFactory::create('foo bar', Logger::ERROR, 'channel.foo', [], [], $now),
$defaultBreadcrumb->withLevel(Breadcrumb::LEVEL_ERROR)
->withType(Breadcrumb::TYPE_ERROR),
];
yield 'with level ALERT' => [
RecordFactory::create('foo bar', Logger::ALERT, 'channel.foo', [], [], $now),
$defaultBreadcrumb->withLevel(Breadcrumb::LEVEL_FATAL)
->withType(Breadcrumb::TYPE_ERROR),
];
yield 'with context' => [
RecordFactory::create('foo bar', Logger::DEBUG, 'channel.foo', ['context' => ['foo' => 'bar']], [], $now),
$defaultBreadcrumb->withMetadata('context', ['foo' => 'bar']),
];
yield 'with extra' => [
RecordFactory::create('foo bar', Logger::DEBUG, 'channel.foo', [], ['extra' => ['foo' => 'bar']], $now),
$defaultBreadcrumb->withMetadata('extra', ['foo' => 'bar']),
];
yield 'with timestamp' => [
RecordFactory::create('foo bar', Logger::DEBUG, 'channel.foo', [], [], new \DateTimeImmutable('1970-01-01 00:00:42.1337 UTC')),
$defaultBreadcrumb->withTimestamp(42.1337),
];
yield 'with zero timestamp' => [
RecordFactory::create('foo bar', Logger::DEBUG, 'channel.foo', [], [], new \DateTimeImmutable('1970-01-01 00:00:00.000 UTC')),
$defaultBreadcrumb->withTimestamp(0.0),
];
yield 'with negative timestamp' => [
RecordFactory::create('foo bar', Logger::DEBUG, 'channel.foo', [], [], new \DateTimeImmutable('1969-12-31 23:59:56.859 UTC')),
$defaultBreadcrumb->withTimestamp(-3.141),
];
}
}