|
10 | 10 | use Bugsnag\BugsnagLaravel\Tests\Stubs\InjecteeWithLogInterface; |
11 | 11 | use Bugsnag\Client; |
12 | 12 | use Bugsnag\FeatureFlag; |
| 13 | +use Bugsnag\Report; |
13 | 14 | use Bugsnag\PsrLogger\BugsnagLogger; |
14 | 15 | use Bugsnag\PsrLogger\MultiLogger as BaseMultiLogger; |
15 | 16 | use Illuminate\Contracts\Logging\Log; |
16 | 17 | use Illuminate\Foundation\Application; |
17 | 18 | use Illuminate\Support\ServiceProvider; |
| 19 | +use Illuminate\Support\Facades\Context; |
18 | 20 |
|
19 | 21 | class ServiceProviderTest extends AbstractTestCase |
20 | 22 | { |
@@ -531,4 +533,79 @@ public function testMaxBreadcrumbsCanBeSetFromConfig() |
531 | 533 | $this->assertInstanceOf(Client::class, $client); |
532 | 534 | $this->assertSame(73, $client->getMaxBreadcrumbs()); |
533 | 535 | } |
| 536 | + |
| 537 | + public function testItSetsContextAsMetadata() |
| 538 | + { |
| 539 | + if (!class_exists(Context::class)) { |
| 540 | + $this->markTestSkipped("Requires Laravel 11's 'Context' class"); |
| 541 | + } |
| 542 | + |
| 543 | + Context::add('a', 1234); |
| 544 | + Context::add('b', true); |
| 545 | + Context::addHidden('x', ':)'); |
| 546 | + |
| 547 | + /** @var Client $client */ |
| 548 | + $client = $this->app->make(Client::class); |
| 549 | + |
| 550 | + Context::add('c', 'hello'); |
| 551 | + Context::add('d', [1, 2, 3]); |
| 552 | + |
| 553 | + $expected = ['a' => 1234, 'b' => true, 'c' => 'hello', 'd' => [1, 2, 3]]; |
| 554 | + $actual = []; |
| 555 | + $hasHiddenContext = true; |
| 556 | + |
| 557 | + $client->notifyException( |
| 558 | + new \Exception('hello'), |
| 559 | + function (Report $report) use (&$actual, &$hasHiddenContext) { |
| 560 | + $metadata = $report->getMetadata(); |
| 561 | + |
| 562 | + $actual = $metadata['Laravel Context']; |
| 563 | + $hasHiddenContext = isset($metadata['Laravel Hidden Context']); |
| 564 | + } |
| 565 | + ); |
| 566 | + |
| 567 | + $this->assertSame($expected, $actual); |
| 568 | + $this->assertFalse($hasHiddenContext); |
| 569 | + } |
| 570 | + |
| 571 | + public function testItSetsHiddenContextAsMetadataIfConfiguredToDoSo() |
| 572 | + { |
| 573 | + if (!class_exists(Context::class)) { |
| 574 | + $this->markTestSkipped("Requires Laravel 11's 'Context' class"); |
| 575 | + } |
| 576 | + |
| 577 | + Context::addHidden('a', 1234); |
| 578 | + Context::add('b', true); |
| 579 | + |
| 580 | + /** @var \Illuminate\Config\Repository $laravelConfig */ |
| 581 | + $laravelConfig = $this->app->config; |
| 582 | + $bugsnagConfig = $laravelConfig->get('bugsnag'); |
| 583 | + $bugsnagConfig['attach_hidden_context'] = true; |
| 584 | + |
| 585 | + $laravelConfig->set('bugsnag', $bugsnagConfig); |
| 586 | + |
| 587 | + /** @var Client $client */ |
| 588 | + $client = $this->app->make(Client::class); |
| 589 | + |
| 590 | + Context::add('c', 'hello'); |
| 591 | + Context::addHidden('d', [1, 2, 3]); |
| 592 | + |
| 593 | + $expectedContext = ['b' => true, 'c' => 'hello']; |
| 594 | + $expectedHiddenContext = ['a' => 1234, 'd' => [1, 2, 3]]; |
| 595 | + $actualContext = []; |
| 596 | + $actualHiddenContext = []; |
| 597 | + |
| 598 | + $client->notifyException( |
| 599 | + new \Exception('hello'), |
| 600 | + function (Report $report) use (&$actualContext, &$actualHiddenContext) { |
| 601 | + $metadata = $report->getMetadata(); |
| 602 | + |
| 603 | + $actualContext = $metadata['Laravel Context']; |
| 604 | + $actualHiddenContext = $metadata['Laravel Hidden Context']; |
| 605 | + } |
| 606 | + ); |
| 607 | + |
| 608 | + $this->assertSame($expectedContext, $actualContext); |
| 609 | + $this->assertSame($expectedHiddenContext, $actualHiddenContext); |
| 610 | + } |
534 | 611 | } |
0 commit comments