Skip to content

Commit cb8ef7c

Browse files
committed
Add command with basic testing
1 parent 2845df5 commit cb8ef7c

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/Command/SentryTestCommand.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Command;
4+
5+
use Sentry\State\Hub;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
class SentryTestCommand extends Command
11+
{
12+
public function __construct()
13+
{
14+
parent::__construct('sentry:test');
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output): int
18+
{
19+
$currentHub = Hub::getCurrent();
20+
$client = $currentHub->getClient();
21+
22+
if (! $client) {
23+
$output->writeln('<error>No client found</error>');
24+
$output->writeln('<info>Your DSN is probably missing, check your configuration</info>');
25+
26+
return 1;
27+
}
28+
29+
$dsn = $client->getOptions()->getDsn();
30+
31+
if ($dsn) {
32+
$output->writeln('<info>DSN correctly configured in the current client</info>');
33+
} else {
34+
$output->writeln('<error>No DSN configured in the current client, please check your configuration</error>');
35+
$output->writeln('<info>To debug further, try bin/console debug:config sentry</info>');
36+
37+
return 1;
38+
}
39+
40+
$output->writeln('Sending test message...');
41+
42+
$eventId = $currentHub->captureMessage('This is a test message from the Sentry bundle');
43+
44+
if ($eventId) {
45+
$output->writeln("<info>Message sent successfully with ID $eventId</info>");
46+
} else {
47+
$output->writeln('<error>Message not sent!</error>');
48+
$output->writeln('<warning>Check your DSN or your before_send callback if used</warning>');
49+
50+
return 1;
51+
}
52+
53+
return 0;
54+
}
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Test\Command;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Sentry\SentryBundle\Command\SentryTestCommand;
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Tester\CommandTester;
9+
10+
class SentryTestCommandTest extends TestCase
11+
{
12+
public function testExecuteFailsDueToMissingDsn(): void
13+
{
14+
$application = new Application();
15+
$application->add(new SentryTestCommand());
16+
17+
$command = $application->find('sentry:test');
18+
$commandTester = new CommandTester($command);
19+
$commandTester->execute([
20+
'command' => $command->getName(),
21+
]);
22+
23+
$this->assertNotSame(0, $commandTester->getStatusCode());
24+
$output = $commandTester->getDisplay();
25+
$this->assertContains('No client found', $output);
26+
$this->assertContains('DSN is probably missing', $output);
27+
}
28+
}

0 commit comments

Comments
 (0)