|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\SentryBundle\Tests\End2End; |
| 6 | + |
| 7 | +use Sentry\SentryBundle\Tests\End2End\App\KernelWithTracing; |
| 8 | +use Sentry\State\HubInterface; |
| 9 | +use Symfony\Bundle\FrameworkBundle\Client; |
| 10 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 11 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | + |
| 14 | +if (!class_exists(KernelBrowser::class)) { |
| 15 | + /** @phpstan-ignore-next-line */ |
| 16 | + class_alias(Client::class, KernelBrowser::class); |
| 17 | +} |
| 18 | + |
| 19 | +class TracingEnd2EndTest extends WebTestCase |
| 20 | +{ |
| 21 | + public const SENT_EVENTS_LOG = '/tmp/sentry_e2e_test_sent_events.log'; |
| 22 | + |
| 23 | + protected static function getKernelClass(): string |
| 24 | + { |
| 25 | + return KernelWithTracing::class; |
| 26 | + } |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + file_put_contents(self::SENT_EVENTS_LOG, ''); |
| 33 | + } |
| 34 | + |
| 35 | + public function testTracingWithDoctrineConnectionPing(): void |
| 36 | + { |
| 37 | + $client = static::createClient(['debug' => false]); |
| 38 | + |
| 39 | + $client->request('GET', '/tracing/ping-database'); |
| 40 | + |
| 41 | + $response = $client->getResponse(); |
| 42 | + |
| 43 | + $this->assertInstanceOf(Response::class, $response); |
| 44 | + $this->assertSame(200, $response->getStatusCode()); |
| 45 | + |
| 46 | + $this->assertLastEventIdIsNotNull($client); |
| 47 | + $this->assertTracingEventCount(1); |
| 48 | + } |
| 49 | + |
| 50 | + private function assertLastEventIdIsNotNull(KernelBrowser $client): void |
| 51 | + { |
| 52 | + $container = $client->getContainer(); |
| 53 | + $this->assertNotNull($container); |
| 54 | + |
| 55 | + $hub = $container->get('test.hub'); |
| 56 | + $this->assertInstanceOf(HubInterface::class, $hub); |
| 57 | + |
| 58 | + $this->assertNotNull($hub->getLastEventId(), 'Last error not captured'); |
| 59 | + } |
| 60 | + |
| 61 | + private function assertTracingEventCount(int $expectedCount): void |
| 62 | + { |
| 63 | + $events = file_get_contents(self::SENT_EVENTS_LOG); |
| 64 | + $this->assertNotFalse($events, 'Cannot read sent events log'); |
| 65 | + $listOfTracingEvents = array_filter(explode(StubTransportFactory::SEPARATOR, trim($events)), static function (string $elem) { |
| 66 | + return str_contains('TRACING', $elem); |
| 67 | + }); |
| 68 | + |
| 69 | + $this->assertCount($expectedCount, $listOfTracingEvents, 'Wrong number of tracing events sent: ' . \PHP_EOL . $events); |
| 70 | + } |
| 71 | +} |
0 commit comments