|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Command; |
| 6 | + |
| 7 | +use Monolog\Logger; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Psr\Log\LoggerInterface; |
| 10 | +use Sentry\Client; |
| 11 | +use Sentry\Event; |
| 12 | +use Sentry\Monolog\BreadcrumbHandler; |
| 13 | +use Sentry\Options; |
| 14 | +use Sentry\SentryBundle\Command\SentryBreadcrumbTestCommand; |
| 15 | +use Sentry\SentryBundle\Command\SentryDummyTestCommand; |
| 16 | +use Sentry\SentryBundle\Command\SentrySubcommandTestCommand; |
| 17 | +use Sentry\SentryBundle\EventListener\ConsoleListener; |
| 18 | +use Sentry\SentryBundle\Tests\End2End\StubTransport; |
| 19 | +use Sentry\State\Hub; |
| 20 | +use Sentry\State\HubInterface; |
| 21 | +use Sentry\State\Scope; |
| 22 | +use Symfony\Component\Console\Application; |
| 23 | +use Symfony\Component\Console\ConsoleEvents; |
| 24 | +use Symfony\Component\Console\Input\ArgvInput; |
| 25 | +use Symfony\Component\Console\Output\NullOutput; |
| 26 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 27 | + |
| 28 | +class BreadcrumbTestCommandTest extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var HubInterface |
| 32 | + */ |
| 33 | + private $hub; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var LoggerInterface |
| 37 | + */ |
| 38 | + private $logger; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Application |
| 42 | + */ |
| 43 | + private $application; |
| 44 | + |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + parent::setUp(); |
| 48 | + $client = new Client(new Options(), new StubTransport()); |
| 49 | + $hub = new Hub($client); |
| 50 | + |
| 51 | + $consoleListener = new ConsoleListener($hub); |
| 52 | + |
| 53 | + $dispatcher = new EventDispatcher(); |
| 54 | + $dispatcher->addListener(ConsoleEvents::COMMAND, [$consoleListener, 'handleConsoleCommandEvent']); |
| 55 | + $dispatcher->addListener(ConsoleEvents::TERMINATE, [$consoleListener, 'handleConsoleTerminateEvent']); |
| 56 | + $dispatcher->addListener(ConsoleEvents::ERROR, [$consoleListener, 'handleConsoleErrorEvent']); |
| 57 | + |
| 58 | + $this->application = new Application(); |
| 59 | + $this->application->setDispatcher($dispatcher); |
| 60 | + |
| 61 | + $breadcrumbHandler = new BreadcrumbHandler($hub); |
| 62 | + $this->hub = $hub; |
| 63 | + $this->logger = new Logger('test', [$breadcrumbHandler]); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests that breadcrumbs are properly captured within a console command and not lost |
| 68 | + * on command termination. |
| 69 | + * |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + public function testBreadcrumbWithConsoleListener() |
| 73 | + { |
| 74 | + $command = new SentryBreadcrumbTestCommand($this->logger); |
| 75 | + $this->application->add($command); |
| 76 | + |
| 77 | + try { |
| 78 | + // We need to run this by the application directly because the CommandTester doesn't produce proper events. |
| 79 | + $this->application->doRun(new ArgvInput(['bin/console', 'sentry:breadcrumb:test']), new NullOutput()); |
| 80 | + $this->fail(); |
| 81 | + } catch (\Throwable $e) { |
| 82 | + $this->assertSame($e->getMessage(), 'Breadcrumb error'); |
| 83 | + } |
| 84 | + |
| 85 | + $event = Event::createEvent(); |
| 86 | + $modifiedEvent = null; |
| 87 | + |
| 88 | + $this->hub->configureScope(function (Scope $scope) use ($event, &$modifiedEvent) { |
| 89 | + $modifiedEvent = $scope->applyToEvent($event); |
| 90 | + }); |
| 91 | + |
| 92 | + $this->assertNotNull($modifiedEvent); |
| 93 | + $this->assertCount(1, $modifiedEvent->getBreadcrumbs()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Tests that the scope is reset after the command finished without any errors. |
| 98 | + * |
| 99 | + * @return void |
| 100 | + * |
| 101 | + * @throws \Throwable |
| 102 | + */ |
| 103 | + public function testSubCommandBreadcrumbs() |
| 104 | + { |
| 105 | + $subcommand = new SentryDummyTestCommand($this->logger); |
| 106 | + $this->application->add($subcommand); |
| 107 | + |
| 108 | + $command = new SentrySubcommandTestCommand($this->logger, $subcommand); |
| 109 | + $this->application->add($command); |
| 110 | + |
| 111 | + // We need to run this by the application directly because the CommandTester doesn't produce proper events. |
| 112 | + $this->application->doRun(new ArgvInput(['bin/console', 'sentry:subcommand:test']), new NullOutput()); |
| 113 | + |
| 114 | + $event = Event::createEvent(); |
| 115 | + $modifiedEvent = null; |
| 116 | + |
| 117 | + $this->hub->configureScope(function (Scope $scope) use ($event, &$modifiedEvent) { |
| 118 | + $modifiedEvent = $scope->applyToEvent($event); |
| 119 | + }); |
| 120 | + |
| 121 | + $this->assertNotNull($modifiedEvent); |
| 122 | + // We do not have breadcrumbs here because no error happened and the scope was popped. |
| 123 | + $this->assertCount(0, $modifiedEvent->getBreadcrumbs()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Tests that the command that caused the crash is reported as `console.command` tag. |
| 128 | + * |
| 129 | + * @return void |
| 130 | + */ |
| 131 | + public function testCrashingSubcommand() |
| 132 | + { |
| 133 | + $subcommand = new SentryBreadcrumbTestCommand($this->logger); |
| 134 | + $this->application->add($subcommand); |
| 135 | + |
| 136 | + $command = new SentrySubcommandTestCommand($this->logger, $subcommand); |
| 137 | + $this->application->add($command); |
| 138 | + |
| 139 | + try { |
| 140 | + $this->application->doRun(new ArgvInput(['bin/console', 'sentry:subcommand:test']), new NullOutput()); |
| 141 | + $this->fail(); |
| 142 | + } catch (\Throwable $e) { |
| 143 | + $this->assertSame($e->getMessage(), 'Breadcrumb error'); |
| 144 | + } |
| 145 | + |
| 146 | + $event = Event::createEvent(); |
| 147 | + $modifiedEvent = null; |
| 148 | + |
| 149 | + $this->hub->configureScope(function (Scope $scope) use ($event, &$modifiedEvent) { |
| 150 | + $modifiedEvent = $scope->applyToEvent($event); |
| 151 | + }); |
| 152 | + |
| 153 | + $this->assertNotNull($modifiedEvent); |
| 154 | + $this->assertCount(2, $modifiedEvent->getBreadcrumbs()); |
| 155 | + $this->assertSame($modifiedEvent->getTags()['console.command'], 'sentry:breadcrumb:test'); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Tests that we have the correct `console.command` tag if one command throws an unhandled exception |
| 160 | + * even if we had commands that threw exceptions but were handled. |
| 161 | + * |
| 162 | + * @return void |
| 163 | + * |
| 164 | + * @throws \Throwable |
| 165 | + */ |
| 166 | + public function testRunSecondCommandAfterCrashingCommand() |
| 167 | + { |
| 168 | + $subcommand = new SentryBreadcrumbTestCommand($this->logger); |
| 169 | + $this->application->add($subcommand); |
| 170 | + |
| 171 | + $command = new SentrySubcommandTestCommand($this->logger, $subcommand); |
| 172 | + $this->application->add($command); |
| 173 | + |
| 174 | + try { |
| 175 | + // Run first command which crashes but is handled |
| 176 | + $this->application->doRun(new ArgvInput(['bin/console', 'sentry:subcommand:test']), new NullOutput()); |
| 177 | + $this->fail(); |
| 178 | + } catch (\Throwable $e) { |
| 179 | + $this->assertSame($e->getMessage(), 'Breadcrumb error'); |
| 180 | + } |
| 181 | + |
| 182 | + $command = new SentryDummyTestCommand($this->logger); |
| 183 | + $this->application->add($command); |
| 184 | + |
| 185 | + // Run the second command which crashes unhandled. |
| 186 | + $this->application->doRun(new ArgvInput(['bin/console', 'sentry:dummy:test']), new NullOutput()); |
| 187 | + |
| 188 | + $event = Event::createEvent(); |
| 189 | + $modifiedEvent = null; |
| 190 | + $this->hub->configureScope(function (Scope $scope) use ($event, &$modifiedEvent) { |
| 191 | + $modifiedEvent = $scope->applyToEvent($event); |
| 192 | + }); |
| 193 | + |
| 194 | + $this->assertNotNull($modifiedEvent); |
| 195 | + // Breadcrumbs contain all log entries until the sentry:dummy:test command crash. |
| 196 | + $this->assertCount(3, $modifiedEvent->getBreadcrumbs()); |
| 197 | + // console.command tag is properly set to the last command |
| 198 | + $this->assertSame($modifiedEvent->getTags()['console.command'], 'sentry:dummy:test'); |
| 199 | + } |
| 200 | +} |
0 commit comments