Skip to content

Commit ec2f89a

Browse files
committed
add controller test
1 parent d9ad34c commit ec2f89a

File tree

5 files changed

+104
-1
lines changed

5 files changed

+104
-1
lines changed

src/SentryBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace Sentry\SentryBundle;
66

77
use Sentry\SentryBundle\DependencyInjection\Compiler\AddLoginListenerTagPass;
8+
use Sentry\SentryBundle\DependencyInjection\Compiler\BufferFlushPass;
89
use Sentry\SentryBundle\DependencyInjection\Compiler\CacheTracingPass;
910
use Sentry\SentryBundle\DependencyInjection\Compiler\DbalTracingPass;
1011
use Sentry\SentryBundle\DependencyInjection\Compiler\HttpClientTracingPass;
11-
use Sentry\SentryBundle\DependencyInjection\Compiler\BufferFlushPass;
1212
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1313
use Symfony\Component\DependencyInjection\ContainerBuilder;
1414
use Symfony\Component\HttpKernel\Bundle\Bundle;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End\App\Controller;
6+
7+
use Psr\Log\LoggerInterface;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class BufferFlushController
11+
{
12+
/**
13+
* @var LoggerInterface
14+
*/
15+
private $logger;
16+
17+
public function __construct(LoggerInterface $logger)
18+
{
19+
$this->logger = $logger;
20+
}
21+
22+
public function testBufferFlush(): Response
23+
{
24+
$this->logger->notice('Test notice message');
25+
$this->logger->warning('Test warning message');
26+
$this->logger->error('Test error message');
27+
28+
return new Response('OK');
29+
}
30+
}

tests/End2End/App/config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ services:
3333
tags:
3434
- controller.service_arguments
3535

36+
Sentry\SentryBundle\Tests\End2End\App\Controller\BufferFlushController:
37+
autowire: true
38+
tags:
39+
- controller.service_arguments
40+
3641
Sentry\SentryBundle\Tests\End2End\App\Command\MainCommand:
3742
tags: [{ name: 'console.command', command: 'main-command' }]
3843

@@ -42,3 +47,12 @@ monolog:
4247
type: stream
4348
path: "%kernel.logs_dir%/%kernel.environment%.log"
4449
level: debug
50+
sentry:
51+
type: sentry
52+
level: warning
53+
hub_id: 'Sentry\State\HubInterface'
54+
sentry_buffer:
55+
type: buffer
56+
handler: sentry
57+
level: notice
58+
buffer_size: 50

tests/End2End/App/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ tracing_ping_database:
4141
tracing_ignored_transaction:
4242
path: /tracing/ignored-transaction
4343
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\TracingController::ignoredTransaction' }
44+
45+
buffer_flush:
46+
path: /buffer-flush
47+
defaults: { _controller: 'Sentry\SentryBundle\Tests\End2End\App\Controller\BufferFlushController::testBufferFlush' }
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\End2End;
6+
7+
use Sentry\SentryBundle\Tests\End2End\App\Kernel;
8+
use Symfony\Bundle\FrameworkBundle\Client;
9+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
10+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
11+
12+
if (!class_exists(KernelBrowser::class) && class_exists(Client::class)) {
13+
class_alias(Client::class, KernelBrowser::class);
14+
}
15+
16+
/**
17+
* @runTestsInSeparateProcesses
18+
*/
19+
class BufferFlushEnd2EndTest extends WebTestCase
20+
{
21+
protected static function getKernelClass(): string
22+
{
23+
return Kernel::class;
24+
}
25+
26+
/**
27+
* Test that log messages from a controller action are properly flushed after kernel termination.
28+
*/
29+
public function testLogMessagesAvailableAfterKernelTermination(): void
30+
{
31+
StubTransport::$events = [];
32+
33+
$client = static::createClient(['debug' => false]);
34+
35+
$client->request('GET', '/buffer-flush');
36+
37+
$this->assertResponseIsSuccessful();
38+
$this->assertEquals('OK', $client->getResponse()->getContent());
39+
40+
$events = StubTransport::$events;
41+
42+
$this->assertCount(2, $events);
43+
44+
$foundMessages = [];
45+
46+
foreach ($events as $event) {
47+
if ($event->getMessage()) {
48+
$foundMessages[] = $event->getMessage();
49+
}
50+
}
51+
52+
$this->assertContains('Test warning message', $foundMessages);
53+
$this->assertContains('Test error message', $foundMessages);
54+
}
55+
}

0 commit comments

Comments
 (0)