|
| 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 | + public function testLaravelContextIntegrationIsRegistered(): void |
| 15 | + { |
| 16 | + $integration = $this->getSentryHubFromContainer()->getIntegration(LaravelContextIntegration::class); |
| 17 | + |
| 18 | + $this->assertInstanceOf(LaravelContextIntegration::class, $integration); |
| 19 | + } |
| 20 | + |
| 21 | + public function testExceptionIsCapturedWithLaravelContext(): void |
| 22 | + { |
| 23 | + $this->setupTestContext(); |
| 24 | + |
| 25 | + captureException(new Exception('Context test')); |
| 26 | + |
| 27 | + $event = $this->getLastSentryEvent(); |
| 28 | + |
| 29 | + $this->assertNotNull($event); |
| 30 | + $this->assertEquals($event->getType(), EventType::event()); |
| 31 | + $this->assertContextIsCaptured($event->getContexts()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testTransactionIsCapturedWithLaravelContext(): void |
| 35 | + { |
| 36 | + $this->setupTestContext(); |
| 37 | + |
| 38 | + $transaction = $this->startTransaction(); |
| 39 | + $transaction->setSampled(true); |
| 40 | + $transaction->finish(); |
| 41 | + |
| 42 | + $event = $this->getLastSentryEvent(); |
| 43 | + |
| 44 | + $this->assertNotNull($event); |
| 45 | + $this->assertEquals($event->getType(), EventType::transaction()); |
| 46 | + $this->assertContextIsCaptured($event->getContexts()); |
| 47 | + } |
| 48 | + |
| 49 | + private function setupTestContext(): void |
| 50 | + { |
| 51 | + Context::flush(); |
| 52 | + Context::add('foo', 'bar'); |
| 53 | + Context::addHidden('hidden', 'value'); |
| 54 | + } |
| 55 | + |
| 56 | + private function assertContextIsCaptured(array $context): void |
| 57 | + { |
| 58 | + $this->assertArrayHasKey('laravel', $context); |
| 59 | + $this->assertArrayHasKey('foo', $context['laravel']); |
| 60 | + $this->assertArrayNotHasKey('hidden', $context['laravel']); |
| 61 | + $this->assertEquals('bar', $context['laravel']['foo']); |
| 62 | + } |
| 63 | +} |
0 commit comments