Skip to content

Commit caa00de

Browse files
committed
move test commands into tests/ dir and add docs
1 parent f6001e4 commit caa00de

File tree

8 files changed

+322
-285
lines changed

8 files changed

+322
-285
lines changed

src/EventListener/BufferFlusher.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
use Symfony\Component\HttpKernel\Event\TerminateEvent;
1414
use Symfony\Component\HttpKernel\KernelEvents;
1515

16+
/**
17+
* Class that wraps Sentry monolog handlers to flush them for certain lifecycle events.
18+
* This is required to emit proper scope based information like tags and breadcrumbs.
19+
*
20+
* Without this class, buffered monolog messages are flushed when the request finishes at which
21+
* point breadcrumbs and tags are no longer present in the scope.
22+
*/
1623
class BufferFlusher implements EventSubscriberInterface
1724
{
1825
/**
@@ -30,9 +37,15 @@ public function __construct(array $bufferHandlers = [])
3037

3138
public static function getSubscribedEvents(): array
3239
{
40+
// Flush the Monolog buffer before any scope is destroyed so that events
41+
// get augmented with properly scoped data.
42+
// For ConsoleEvents::COMMAND, we have to flush before ConsoleListener::handleConsoleCommandEvent(..)
43+
// runs so that the proper tags get attached to the event.
44+
// Running with lower priority will make the ConsoleListener run before and create a new scope
45+
// with the new command name when running a symfony Command within another Command.
3346
return [
3447
KernelEvents::TERMINATE => ['onKernelTerminate', 10],
35-
ConsoleEvents::COMMAND => ['onConsoleCommand', 10],
48+
ConsoleEvents::COMMAND => ['onConsoleCommand', 150],
3649
ConsoleEvents::TERMINATE => ['onConsoleTerminate', 10],
3750
ConsoleEvents::ERROR => ['onConsoleError', 10],
3851
];

tests/Command/BreadcrumbTestCommandTest.php

Lines changed: 0 additions & 263 deletions
This file was deleted.

src/Command/SentryBreadcrumbTestCommand.php renamed to tests/End2End/App/Command/BreadcrumbTestCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace Sentry\SentryBundle\Command;
5+
namespace Sentry\SentryBundle\Tests\End2End\App\Command;
66

77
use Psr\Log\LoggerInterface;
88
use Symfony\Component\Console\Command\Command;
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Output\OutputInterface;
1111

12-
class SentryBreadcrumbTestCommand extends Command
12+
class BreadcrumbTestCommand extends Command
1313
{
1414
/**
1515
* @var LoggerInterface
@@ -18,13 +18,13 @@ class SentryBreadcrumbTestCommand extends Command
1818

1919
public function __construct(LoggerInterface $logger)
2020
{
21-
parent::__construct('sentry:breadcrumb:test');
21+
parent::__construct();
2222
$this->logger = $logger;
2323
}
2424

2525
protected function execute(InputInterface $input, OutputInterface $output): int
2626
{
27-
$this->logger->error('Breadcrumb error log line');
27+
$this->logger->error('breadcrumb 1 error');
2828

2929
throw new \RuntimeException('Breadcrumb error');
3030
}

src/Command/SentrySubcommandTestCommand.php renamed to tests/End2End/App/Command/CrashingSubcommandTestCommand.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Sentry\SentryBundle\Command;
5+
namespace Sentry\SentryBundle\Tests\End2End\App\Command;
66

77
use Psr\Log\LoggerInterface;
88
use Symfony\Component\Console\Command\Command;
@@ -11,34 +11,28 @@
1111
use Symfony\Component\Console\Output\NullOutput;
1212
use Symfony\Component\Console\Output\OutputInterface;
1313

14-
class SentrySubcommandTestCommand extends Command
14+
class CrashingSubcommandTestCommand extends Command
1515
{
1616
/**
1717
* @var LoggerInterface
1818
*/
1919
private $logger;
2020

21-
/**
22-
* @var Command
23-
*/
24-
private $subcommand;
25-
26-
public function __construct(LoggerInterface $logger, Command $subcommand)
21+
public function __construct(LoggerInterface $logger)
2722
{
28-
parent::__construct('sentry:subcommand:test');
23+
parent::__construct();
2924
$this->logger = $logger;
30-
$this->subcommand = $subcommand;
3125
}
3226

3327
protected function execute(InputInterface $input, OutputInterface $output): int
3428
{
35-
$this->logger->error('Subcommand will run now');
29+
$this->logger->error('subcommand crash 1 error');
3630

3731
if (null !== $this->getApplication()) {
38-
$this->getApplication()->doRun(new ArrayInput(['command' => $this->subcommand->getName()]), new NullOutput());
32+
$this->getApplication()->doRun(new ArrayInput(['command' => "sentry:breadcrumb:test"]), new NullOutput());
3933
}
4034

41-
$this->logger->error('Breadcrumb after subcommand');
35+
$this->logger->error('subcommand error 2 error');
4236

4337
return 0;
4438
}

0 commit comments

Comments
 (0)