Skip to content

Commit

Permalink
Add first unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Jan 13, 2022
1 parent b99fd5f commit 3fb9d7f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Monolog/BreadcrumbHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bub
* channel: string,
* datetime: \DateTimeImmutable,
* message: string,
* formatted?: string,
* extra?: array<string, mixed>
* } $record {@see https://github.com/Seldaek/monolog/blob/main/doc/message-structure.md}
*/
Expand All @@ -54,7 +53,7 @@ protected function write(array $record): void
$this->getBreadcrumbLevel($record['level']),
$this->getBreadcrumbType($record['level']),
$record['channel'],
$record['formatted'] ?? $record['message'],
$record['message'],
$record['extra'] ?? [],
$record['datetime']->getTimestamp()
);
Expand Down
64 changes: 64 additions & 0 deletions tests/Monolog/BreadcrumbHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Sentry\Tests\Monolog;

use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Sentry\Breadcrumb;
use Sentry\Monolog\BreadcrumbHandler;
use Sentry\State\HubInterface;

final class BreadcrumbHandlerTest extends TestCase
{
/**
* @dataProvider handleDataProvider
*/
public function testHandle(array $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->assertSame($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);
}

public function handleDataProvider(): iterable
{
$defaultData = [
'message' => 'foo bar',
'level' => Logger::DEBUG,
'level_name' => Logger::getLevelName(Logger::DEBUG),
'channel' => 'channel.foo',
'context' => [],
'extra' => [],
'datetime' => new \DateTimeImmutable(),
];

$defaultBreadcrumb = new Breadcrumb(
Breadcrumb::LEVEL_DEBUG,
Breadcrumb::TYPE_DEFAULT,
'channel.foo',
'foo bar',
[],
$defaultData['datetime']->getTimestamp()
);

yield [
$defaultData,
$defaultBreadcrumb,
];
}
}

0 comments on commit 3fb9d7f

Please sign in to comment.