@@ -481,4 +481,95 @@ public function testWrappingHandlerInFingersCrossedWhenActionLevelIsUsed()
481
481
$ this ->assertInstanceOf (StreamHandler::class, $ expectedStreamHandler );
482
482
$ this ->assertEquals (Monolog::DEBUG , $ expectedStreamHandler ->getLevel ());
483
483
}
484
+
485
+ public function testItSharesContextWithAlreadyResolvedChannels ()
486
+ {
487
+ $ manager = new LogManager ($ this ->app );
488
+ $ channel = $ manager ->channel ('single ' );
489
+ $ context = null ;
490
+
491
+ $ channel ->listen (function ($ message ) use (&$ context ) {
492
+ $ context = $ message ->context ;
493
+ });
494
+ $ manager ->shareContext ([
495
+ 'invocation-id ' => 'expected-id ' ,
496
+ ]);
497
+ $ channel ->info ('xxxx ' );
498
+
499
+ $ this ->assertSame (['invocation-id ' => 'expected-id ' ], $ context );
500
+ }
501
+
502
+ public function testItSharesContextWithFreshlyResolvedChannels ()
503
+ {
504
+ $ manager = new LogManager ($ this ->app );
505
+ $ context = null ;
506
+
507
+ $ manager ->shareContext ([
508
+ 'invocation-id ' => 'expected-id ' ,
509
+ ]);
510
+ $ manager ->channel ('single ' )->listen (function ($ message ) use (&$ context ) {
511
+ $ context = $ message ->context ;
512
+ });
513
+ $ manager ->channel ('single ' )->info ('xxxx ' );
514
+
515
+ $ this ->assertSame (['invocation-id ' => 'expected-id ' ], $ context );
516
+ }
517
+
518
+ public function testContextCanBePublicallyAccessedByOtherLoggingSystems ()
519
+ {
520
+ $ manager = new LogManager ($ this ->app );
521
+ $ context = null ;
522
+
523
+ $ manager ->shareContext ([
524
+ 'invocation-id ' => 'expected-id ' ,
525
+ ]);
526
+
527
+ $ this ->assertSame ($ manager ->sharedContext (), ['invocation-id ' => 'expected-id ' ]);
528
+ }
529
+
530
+ public function testItSharesContextWithStacksWhenTheyAreResolved ()
531
+ {
532
+ $ manager = new LogManager ($ this ->app );
533
+ $ context = null ;
534
+
535
+ $ manager ->shareContext ([
536
+ 'invocation-id ' => 'expected-id ' ,
537
+ ]);
538
+ $ stack = $ manager ->stack (['single ' ]);
539
+ $ stack ->listen (function ($ message ) use (&$ context ) {
540
+ $ context = $ message ->context ;
541
+ });
542
+ $ stack ->info ('xxxx ' );
543
+
544
+ $ this ->assertSame (['invocation-id ' => 'expected-id ' ], $ context );
545
+ }
546
+
547
+ public function testItMergesSharedContextRatherThanReplacing ()
548
+ {
549
+ $ manager = new LogManager ($ this ->app );
550
+ $ context = null ;
551
+
552
+ $ manager ->shareContext ([
553
+ 'invocation-id ' => 'expected-id ' ,
554
+ ]);
555
+ $ manager ->shareContext ([
556
+ 'invocation-start ' => 1651800456 ,
557
+ ]);
558
+ $ manager ->channel ('single ' )->listen (function ($ message ) use (&$ context ) {
559
+ $ context = $ message ->context ;
560
+ });
561
+ $ manager ->channel ('single ' )->info ('xxxx ' , [
562
+ 'logged ' => 'context ' ,
563
+ ]);
564
+
565
+ $ this ->assertSame ([
566
+ 'invocation-id ' => 'expected-id ' ,
567
+ 'invocation-start ' => 1651800456 ,
568
+ 'logged ' => 'context ' ,
569
+ ], $ context );
570
+ $ this ->assertSame ([
571
+ 'invocation-id ' => 'expected-id ' ,
572
+ 'invocation-start ' => 1651800456 ,
573
+ ], $ manager ->sharedContext ());
574
+ }
484
575
}
0 commit comments