|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\Tests\Monolog; |
| 6 | + |
| 7 | +use Monolog\Logger; |
| 8 | +use Monolog\LogRecord; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Sentry\Breadcrumb; |
| 11 | +use Sentry\Monolog\BreadcrumbHandler; |
| 12 | +use Sentry\State\HubInterface; |
| 13 | + |
| 14 | +final class BreadcrumbHandlerTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @dataProvider handleDataProvider |
| 18 | + */ |
| 19 | + public function testHandle($record, Breadcrumb $expectedBreadcrumb): void |
| 20 | + { |
| 21 | + $hub = $this->createMock(HubInterface::class); |
| 22 | + $hub->expects($this->once()) |
| 23 | + ->method('addBreadcrumb') |
| 24 | + ->with($this->callback(function (Breadcrumb $breadcrumb) use ($expectedBreadcrumb, $record): bool { |
| 25 | + $this->assertSame($expectedBreadcrumb->getMessage(), $breadcrumb->getMessage()); |
| 26 | + $this->assertSame($expectedBreadcrumb->getLevel(), $breadcrumb->getLevel()); |
| 27 | + $this->assertSame($expectedBreadcrumb->getType(), $breadcrumb->getType()); |
| 28 | + $this->assertEquals($record['datetime']->getTimestamp(), $breadcrumb->getTimestamp()); |
| 29 | + $this->assertSame($expectedBreadcrumb->getCategory(), $breadcrumb->getCategory()); |
| 30 | + $this->assertEquals($expectedBreadcrumb->getMetadata(), $breadcrumb->getMetadata()); |
| 31 | + |
| 32 | + return true; |
| 33 | + })); |
| 34 | + |
| 35 | + $handler = new BreadcrumbHandler($hub); |
| 36 | + $handler->handle($record); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return iterable<LogRecord|array{array<string, mixed>, Breadcrumb}> |
| 41 | + */ |
| 42 | + public function handleDataProvider(): iterable |
| 43 | + { |
| 44 | + $defaultBreadcrumb = new Breadcrumb( |
| 45 | + Breadcrumb::LEVEL_DEBUG, |
| 46 | + Breadcrumb::TYPE_DEFAULT, |
| 47 | + 'channel.foo', |
| 48 | + 'foo bar', |
| 49 | + [] |
| 50 | + ); |
| 51 | + |
| 52 | + $levelsToBeTested = [ |
| 53 | + Logger::DEBUG => Breadcrumb::LEVEL_DEBUG, |
| 54 | + Logger::INFO => Breadcrumb::LEVEL_INFO, |
| 55 | + Logger::NOTICE => Breadcrumb::LEVEL_INFO, |
| 56 | + Logger::WARNING => Breadcrumb::LEVEL_WARNING, |
| 57 | + ]; |
| 58 | + |
| 59 | + foreach ($levelsToBeTested as $loggerLevel => $breadcrumbLevel) { |
| 60 | + yield 'with level ' . Logger::getLevelName($loggerLevel) => [ |
| 61 | + RecordFactory::create('foo bar', $loggerLevel, 'channel.foo', [], []), |
| 62 | + $defaultBreadcrumb->withLevel($breadcrumbLevel), |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + yield 'with level ERROR' => [ |
| 67 | + RecordFactory::create('foo bar', Logger::ERROR, 'channel.foo', [], []), |
| 68 | + $defaultBreadcrumb->withLevel(Breadcrumb::LEVEL_ERROR) |
| 69 | + ->withType(Breadcrumb::TYPE_ERROR), |
| 70 | + ]; |
| 71 | + |
| 72 | + yield 'with level ALERT' => [ |
| 73 | + RecordFactory::create('foo bar', Logger::ALERT, 'channel.foo', [], []), |
| 74 | + $defaultBreadcrumb->withLevel(Breadcrumb::LEVEL_FATAL) |
| 75 | + ->withType(Breadcrumb::TYPE_ERROR), |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'with context' => [ |
| 79 | + RecordFactory::create('foo bar', Logger::DEBUG, 'channel.foo', ['context' => ['foo' => 'bar']], []), |
| 80 | + $defaultBreadcrumb->withMetadata('context', ['foo' => 'bar']), |
| 81 | + ]; |
| 82 | + |
| 83 | + yield 'with extra' => [ |
| 84 | + RecordFactory::create('foo bar', Logger::DEBUG, 'channel.foo', [], ['extra' => ['foo' => 'bar']]), |
| 85 | + $defaultBreadcrumb->withMetadata('extra', ['foo' => 'bar']), |
| 86 | + ]; |
| 87 | + } |
| 88 | +} |
0 commit comments