77use Monolog \Logger ;
88use PHPUnit \Framework \TestCase ;
99use Sentry \ClientBuilder ;
10+ use Sentry \Event ;
1011use Sentry \Logs \Log ;
1112use Sentry \Logs \LogLevel ;
1213use Sentry \Logs \Logs ;
1314use Sentry \Monolog \LogsHandler ;
1415use Sentry \SentrySdk ;
1516use Sentry \State \Hub ;
17+ use Sentry \Transport \Result ;
18+ use Sentry \Transport \ResultStatus ;
19+ use Sentry \Transport \TransportInterface ;
1620
1721final class LogsHandlerTest extends TestCase
1822{
1923 protected function setUp (): void
2024 {
2125 Logs::getInstance ()->flush ();
22- }
23-
24- /**
25- * @dataProvider handleDataProvider
26- */
27- public function testHandle ($ record , Log $ expectedLog ): void
28- {
2926 $ client = ClientBuilder::create ([
3027 'enable_logs ' => true ,
3128 'before_send ' => static function () {
@@ -35,7 +32,13 @@ public function testHandle($record, Log $expectedLog): void
3532
3633 $ hub = new Hub ($ client );
3734 SentrySdk::setCurrentHub ($ hub );
35+ }
3836
37+ /**
38+ * @dataProvider handleDataProvider
39+ */
40+ public function testHandle ($ record , Log $ expectedLog ): void
41+ {
3942 $ handler = new LogsHandler ();
4043 $ handler ->handle ($ record );
4144
@@ -65,21 +68,79 @@ static function (string $key) {
6568 */
6669 public function testLogLevels ($ record , int $ countLogs ): void
6770 {
71+ $ handler = new LogsHandler (LogLevel::warn ());
72+ $ handler ->handle ($ record );
73+
74+ $ logs = Logs::getInstance ()->aggregator ()->all ();
75+ $ this ->assertCount ($ countLogs , $ logs );
76+ }
77+
78+ public function testLogsHandlerDestructor ()
79+ {
80+ $ transport = new class implements TransportInterface {
81+ private $ events = [];
82+
83+ public function send (Event $ event ): Result
84+ {
85+ $ this ->events [] = $ event ;
86+
87+ return new Result (ResultStatus::success ());
88+ }
89+
90+ public function close (?int $ timeout = null ): Result
91+ {
92+ return new Result (ResultStatus::success ());
93+ }
94+
95+ /**
96+ * @return Event[]
97+ */
98+ public function getEvents (): array
99+ {
100+ return $ this ->events ;
101+ }
102+ };
68103 $ client = ClientBuilder::create ([
69104 'enable_logs ' => true ,
70- 'before_send ' => static function () {
71- return null ;
72- },
73- ])->getClient ();
105+ ])->setTransport ($ transport )
106+ ->getClient ();
74107
75108 $ hub = new Hub ($ client );
76109 SentrySdk::setCurrentHub ($ hub );
77110
111+ $ this ->handleLogAndDrop ();
112+
113+ $ this ->assertCount (1 , $ transport ->getEvents ());
114+ $ this ->assertSame ('I was dropped :( ' , $ transport ->getEvents ()[0 ]->getLogs ()[0 ]->getBody ());
115+ }
116+
117+ private function handleLogAndDrop (): void
118+ {
119+ $ handler = new LogsHandler ();
120+ $ handler ->handle (RecordFactory::create ('I was dropped :( ' , Logger::INFO , 'chanel.foo ' , [], []));
121+ }
122+
123+ public function testOriginTagAppliedWithHandler (): void
124+ {
78125 $ handler = new LogsHandler (LogLevel::warn ());
79- $ handler ->handle ($ record );
126+ $ handler ->handle (RecordFactory:: create ( ' with origin ' , Logger:: WARNING , ' channel.foo ' , [], []) );
80127
81128 $ logs = Logs::getInstance ()->aggregator ()->all ();
82- $ this ->assertCount ($ countLogs , $ logs );
129+ $ this ->assertCount (1 , $ logs );
130+ $ log = $ logs [0 ];
131+ $ this ->assertArrayHasKey ('sentry.origin ' , $ log ->attributes ()->toSimpleArray ());
132+ $ this ->assertSame ('auto.logger.monolog ' , $ log ->attributes ()->toSimpleArray ()['sentry.origin ' ]);
133+ }
134+
135+ public function testOriginTagNotAppliedWhenUsingDirectly ()
136+ {
137+ \Sentry \logger ()->info ('No origin attribute ' );
138+
139+ $ logs = Logs::getInstance ()->aggregator ()->all ();
140+ $ this ->assertCount (1 , $ logs );
141+ $ log = $ logs [0 ];
142+ $ this ->assertSame ('No origin attribute ' , $ log ->getBody ());
143+ $ this ->assertArrayNotHasKey ('sentry.origin ' , $ log ->attributes ()->toSimpleArray ());
83144 }
84145
85146 public static function handleDataProvider (): iterable
0 commit comments