Skip to content

Commit c5caac2

Browse files
authored
Improve E2E tests for the Performance Monitoring feature (#517)
1 parent bcc8794 commit c5caac2

14 files changed

+217
-7
lines changed

.github/workflows/tests.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ jobs:
2323
- '7.3'
2424
- '7.2'
2525
sentry_constraint: [false]
26+
dbal_constraint: [false]
2627
symfony_constraint: ['']
2728
experimental: [false]
2829
include:
2930
# - description: 'sentry/sentry dev-develop'
3031
# php: '7.4'
3132
# sentry_constraint: 'dev-develop'
3233
# experimental: true
34+
- description: 'DBAL 2'
35+
php: '7.4'
36+
dbal_constraint: '^2.13'
3337
- description: 'Symfony 4.4'
3438
php: '7.3'
3539
symfony_constraint: 4.4.*
@@ -41,6 +45,7 @@ jobs:
4145
- description: 'prefer lowest'
4246
php: '7.2'
4347
composer_option: '--prefer-lowest'
48+
symfony_constraint: ^3.4.44
4449
env:
4550
SYMFONY_DEPRECATIONS_HELPER: disabled
4651

@@ -61,6 +66,8 @@ jobs:
6166
run: composer global require --no-progress --no-scripts --no-plugins symfony/flex
6267
- run: composer remove --dev symfony/messenger --no-update
6368
if: matrix.symfony_constraint == '3.4.*' || matrix.composer_option == '--prefer-lowest'
69+
- run: composer require --dev doctrine/dbal ${{ matrix.dbal_constraint }} --no-update
70+
if: matrix.dbal_constraint
6471
- run: composer update --no-progress --ansi ${{ matrix.composer_option }}
6572
- run: composer require sentry/sentry dev-develop
6673
if: matrix.sentry_constraint == 'dev-develop'

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,6 @@ parameters:
115115
count: 1
116116
path: src/aliases.php
117117

118-
-
119-
message: "#^Class Symfony\\\\Bundle\\\\FrameworkBundle\\\\Client not found\\.$#"
120-
count: 1
121-
path: tests/End2End/End2EndTest.php
122-
123118
-
124119
message: "#^Call to method getException\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#"
125120
count: 1

tests/Command/SentryTestCommandTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717

1818
class SentryTestCommandTest extends BaseTestCase
1919
{
20+
protected function tearDown(): void
21+
{
22+
parent::tearDown();
23+
24+
// reset current Hub to avoid leaking the mock outside of this tests
25+
SentrySdk::init();
26+
}
27+
2028
public function testExecuteSuccessfully(): void
2129
{
2230
$options = new Options(['dsn' => 'http://public:[email protected]/sentry/1']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App\Controller;
6+
7+
use Doctrine\DBAL\Connection;
8+
use Sentry\State\HubInterface;
9+
use Sentry\Tracing\SpanContext;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
class TracingController
13+
{
14+
/**
15+
* @var HubInterface
16+
*/
17+
private $hub;
18+
19+
/**
20+
* @var Connection|null
21+
*/
22+
private $connection;
23+
24+
public function __construct(HubInterface $hub, Connection $connection = null)
25+
{
26+
$this->hub = $hub;
27+
$this->connection = $connection;
28+
}
29+
30+
public function pingDatabase(): Response
31+
{
32+
$this->hub->setSpan(
33+
$this->hub->getSpan()
34+
->startChild($this->createSpan())
35+
);
36+
37+
if ($this->connection) {
38+
$this->connection->executeQuery('SELECT 1');
39+
}
40+
41+
return new Response('Success');
42+
}
43+
44+
private function createSpan(): SpanContext
45+
{
46+
$spanContext = new SpanContext();
47+
$spanContext->setOp('mock.span');
48+
$spanContext->setDescription('mocked subspan');
49+
50+
return $spanContext;
51+
}
52+
}

tests/End2End/App/Kernel.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Sentry\SentryBundle\Tests\End2End\App;
66

7+
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
78
use Symfony\Component\Config\Loader\LoaderInterface;
89
use Symfony\Component\DependencyInjection\ContainerBuilder;
910
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
@@ -12,25 +13,44 @@
1213

1314
class Kernel extends SymfonyKernel
1415
{
16+
/**
17+
* @var string
18+
*/
19+
private static $cacheDir = null;
20+
1521
/**
1622
* @return BundleInterface[]
1723
*/
1824
public function registerBundles(): array
1925
{
20-
return [
26+
$bundles = [
2127
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
2228
new \Symfony\Bundle\MonologBundle\MonologBundle(),
2329
new \Sentry\SentryBundle\SentryBundle(),
2430
];
31+
32+
if (class_exists(DoctrineBundle::class)) {
33+
$bundles[] = new DoctrineBundle();
34+
}
35+
36+
return $bundles;
2537
}
2638

2739
public function registerContainerConfiguration(LoaderInterface $loader): void
2840
{
2941
$loader->load(__DIR__ . '/config.yml');
3042

43+
if (self::VERSION_ID >= 50000) {
44+
$loader->load(__DIR__ . '/deprecations_for_5.yml');
45+
}
46+
3147
if (interface_exists(MessageBusInterface::class) && self::VERSION_ID >= 40300) {
3248
$loader->load(__DIR__ . '/messenger.yml');
3349
}
50+
51+
if (class_exists(DoctrineBundle::class)) {
52+
$loader->load(__DIR__ . '/doctrine.yml');
53+
}
3454
}
3555

3656
protected function build(ContainerBuilder $container): void
@@ -39,4 +59,13 @@ protected function build(ContainerBuilder $container): void
3959

4060
parent::build($container);
4161
}
62+
63+
public function getCacheDir(): string
64+
{
65+
if (null === self::$cacheDir) {
66+
self::$cacheDir = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('sentry-symfony-');
67+
}
68+
69+
return self::$cacheDir;
70+
}
4271
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App;
6+
7+
use Symfony\Component\Config\Loader\LoaderInterface;
8+
9+
class KernelWithTracing extends Kernel
10+
{
11+
public function registerContainerConfiguration(LoaderInterface $loader): void
12+
{
13+
parent::registerContainerConfiguration($loader);
14+
15+
$loader->load(__DIR__ . '/tracing.yml');
16+
}
17+
}

tests/End2End/App/config.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
sentry:
2+
tracing:
3+
enabled: true
24
options:
35
capture_silenced_errors: true
46
error_types: E_ALL & ~E_USER_DEPRECATED
7+
traces_sample_rate: 0
58

69
framework:
710
router: { resource: "%routing_config_dir%/routing.yml" }
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework:
2+
router:
3+
utf8: true

tests/End2End/App/doctrine.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
doctrine:
2+
dbal:
3+
url: 'sqlite:///%kernel.cache_dir%/data.db'

tests/End2End/App/routing.yml

+4
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ dispatch:
2929
dispatch_unrecoverable:
3030
path: /dispatch-unrecoverable-message
3131
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\MessengerController::dispatchUnrecoverableMessage' }
32+
33+
tracing_ping_database:
34+
path: /tracing/ping-database
35+
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\TracingController::pingDatabase' }

tests/End2End/App/tracing.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sentry:
2+
tracing:
3+
enabled: true
4+
options:
5+
traces_sample_rate: 1
6+
7+
services:
8+
Sentry\SentryBundle\Tests\End2End\App\Controller\TracingController:
9+
arguments:
10+
$hub: '@Sentry\State\HubInterface'
11+
$connection: '@?doctrine.dbal.default_connection'
12+
tags:
13+
- { name: controller.service_arguments }

tests/End2End/End2EndTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\HttpKernel\KernelInterface;
1919
use Symfony\Component\Messenger\MessageBusInterface;
2020

21-
if (!class_exists(KernelBrowser::class)) {
21+
if (!class_exists(KernelBrowser::class) && class_exists(Client::class)) {
2222
class_alias(Client::class, KernelBrowser::class);
2323
}
2424

tests/End2End/StubTransportFactory.php

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function send(Event $event): PromiseInterface
3434
$message = $event->getMessage();
3535
} elseif ($event->getExceptions()) {
3636
$message = $event->getExceptions()[0]->getValue();
37+
} elseif ($event->getTransaction()) {
38+
$message = 'TRACING: ' . $event->getTransaction();
39+
foreach ($event->getSpans() as $i => $span) {
40+
$message .= \PHP_EOL . $i . ') ' . $span->getDescription();
41+
}
3742
} else {
3843
$message = 'NO MESSAGE NOR EXCEPTIONS';
3944
}

tests/End2End/TracingEnd2EndTest.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)