|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Integration; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Support\Facades\Context; |
| 7 | +use Sentry\EventType; |
| 8 | +use Sentry\Laravel\Integration\LaravelContextIntegration; |
| 9 | +use Sentry\Laravel\Tests\TestCase; |
| 10 | +use function Sentry\captureException; |
| 11 | + |
| 12 | +class LaravelContextIntegrationTest extends TestCase |
| 13 | +{ |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + if (!class_exists(Context::class)) { |
| 17 | + $this->markTestSkipped('Laravel introduced contexts in version 11.'); |
| 18 | + } |
| 19 | + |
| 20 | + parent::setUp(); |
| 21 | + } |
| 22 | + |
| 23 | + public function testLaravelContextIntegrationIsRegistered(): void |
| 24 | + { |
| 25 | + $integration = $this->getSentryHubFromContainer()->getIntegration(LaravelContextIntegration::class); |
| 26 | + |
| 27 | + $this->assertInstanceOf(LaravelContextIntegration::class, $integration); |
| 28 | + } |
| 29 | + |
| 30 | + public function testExceptionIsCapturedWithLaravelContext(): void |
| 31 | + { |
| 32 | + $this->setupTestContext(); |
| 33 | + |
| 34 | + captureException(new Exception('Context test')); |
| 35 | + |
| 36 | + $event = $this->getLastSentryEvent(); |
| 37 | + |
| 38 | + $this->assertNotNull($event); |
| 39 | + $this->assertEquals($event->getType(), EventType::event()); |
| 40 | + $this->assertContextIsCaptured($event->getContexts()); |
| 41 | + } |
| 42 | + |
| 43 | + public function testExceptionIsCapturedWithoutLaravelContextIfEmpty(): void |
| 44 | + { |
| 45 | + captureException(new Exception('Context test')); |
| 46 | + |
| 47 | + $event = $this->getLastSentryEvent(); |
| 48 | + |
| 49 | + $this->assertNotNull($event); |
| 50 | + $this->assertEquals($event->getType(), EventType::event()); |
| 51 | + $this->assertArrayNotHasKey('laravel', $event->getContexts()); |
| 52 | + } |
| 53 | + |
| 54 | + public function testExceptionIsCapturedWithoutLaravelContextIfOnlyHidden(): void |
| 55 | + { |
| 56 | + Context::addHidden('hidden', 'value'); |
| 57 | + |
| 58 | + captureException(new Exception('Context test')); |
| 59 | + |
| 60 | + $event = $this->getLastSentryEvent(); |
| 61 | + |
| 62 | + $this->assertNotNull($event); |
| 63 | + $this->assertEquals($event->getType(), EventType::event()); |
| 64 | + $this->assertArrayNotHasKey('laravel', $event->getContexts()); |
| 65 | + } |
| 66 | + |
| 67 | + public function testTransactionIsCapturedWithLaravelContext(): void |
| 68 | + { |
| 69 | + $this->setupTestContext(); |
| 70 | + |
| 71 | + $transaction = $this->startTransaction(); |
| 72 | + $transaction->setSampled(true); |
| 73 | + $transaction->finish(); |
| 74 | + |
| 75 | + $event = $this->getLastSentryEvent(); |
| 76 | + |
| 77 | + $this->assertNotNull($event); |
| 78 | + $this->assertEquals($event->getType(), EventType::transaction()); |
| 79 | + $this->assertContextIsCaptured($event->getContexts()); |
| 80 | + } |
| 81 | + |
| 82 | + private function setupTestContext(): void |
| 83 | + { |
| 84 | + Context::flush(); |
| 85 | + Context::add('foo', 'bar'); |
| 86 | + Context::addHidden('hidden', 'value'); |
| 87 | + } |
| 88 | + |
| 89 | + private function assertContextIsCaptured(array $context): void |
| 90 | + { |
| 91 | + $this->assertArrayHasKey('laravel', $context); |
| 92 | + $this->assertArrayHasKey('foo', $context['laravel']); |
| 93 | + $this->assertArrayNotHasKey('hidden', $context['laravel']); |
| 94 | + $this->assertEquals('bar', $context['laravel']['foo']); |
| 95 | + } |
| 96 | +} |
0 commit comments