Skip to content

Commit 8014167

Browse files
committed
Add failing tests
1 parent 77359d0 commit 8014167

3 files changed

Lines changed: 75 additions & 5 deletions

File tree

src/Logs/LogsAggregator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ public function flush(): ?EventId
124124
return $hub->captureEvent($event);
125125
}
126126

127+
/**
128+
* @return Log[]
129+
*/
130+
public function all(): array
131+
{
132+
return $this->logs;
133+
}
134+
127135
private function getTraceId(HubInterface $hub): string
128136
{
129137
$span = $hub->getSpan();

tests/Logs/LogTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
namespace Sentry\Tests\Logs;
66

77
use PHPUnit\Framework\TestCase;
8-
use Sentry\Attributes\Attribute;
98
use Sentry\Logs\Log;
109
use Sentry\Logs\LogLevel;
1110

12-
/**
13-
* @phpstan-import-type AttributeValue from Attribute
14-
* @phpstan-import-type AttributeSerialized from Attribute
15-
*/
1611
final class LogTest extends TestCase
1712
{
1813
public function testGettersAndSetters(): void

tests/Logs/LogsAggregatorTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\Tests\Logs;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sentry\ClientBuilder;
9+
use Sentry\Logs\LogLevel;
10+
use Sentry\Logs\LogsAggregator;
11+
use Sentry\SentrySdk;
12+
use Sentry\State\Hub;
13+
14+
final class LogsAggregatorTest extends TestCase
15+
{
16+
/**
17+
* @dataProvider messageFormattingDataProvider
18+
*/
19+
public function testMessageFormatting(string $message, array $values, string $expected): void
20+
{
21+
$client = ClientBuilder::create([
22+
'enable_logs' => true,
23+
])->getClient();
24+
25+
$hub = new Hub($client);
26+
SentrySdk::setCurrentHub($hub);
27+
28+
$aggregator = new LogsAggregator();
29+
30+
$aggregator->add(LogLevel::info(), $message, $values);
31+
32+
$logs = $aggregator->all();
33+
34+
$this->assertCount(1, $logs);
35+
36+
$log = $logs[0];
37+
38+
$this->assertEquals($expected, $log->getBody());
39+
}
40+
41+
public static function messageFormattingDataProvider(): \Generator
42+
{
43+
yield [
44+
'Simple message without values',
45+
[],
46+
'Simple message without values',
47+
];
48+
49+
yield [
50+
'Message with a value: %s',
51+
['value'],
52+
'Message with a value: value',
53+
];
54+
55+
yield [
56+
'Message with placeholders but no values: %s',
57+
[],
58+
'Message with placeholders but no values: %s',
59+
];
60+
61+
yield [
62+
'Message with placeholders but incorrect number of values: %s, %s',
63+
['value'],
64+
'Message with placeholders but incorrect number of values: %s, %s',
65+
];
66+
}
67+
}

0 commit comments

Comments
 (0)