Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from getsentry/add-test-command
Add test command
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Sentry\SentryBundle\Command; | ||
|
||
use Sentry\State\Hub; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class SentryTestCommand extends Command | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('sentry:test'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$currentHub = Hub::getCurrent(); | ||
$client = $currentHub->getClient(); | ||
|
||
if (! $client) { | ||
$output->writeln('<error>No client found</error>'); | ||
$output->writeln('<info>Your DSN is probably missing, check your configuration</info>'); | ||
|
||
return 1; | ||
} | ||
|
||
$dsn = $client->getOptions()->getDsn(); | ||
|
||
if ($dsn) { | ||
$output->writeln('<info>DSN correctly configured in the current client</info>'); | ||
} else { | ||
$output->writeln('<error>No DSN configured in the current client, please check your configuration</error>'); | ||
$output->writeln('<info>To debug further, try bin/console debug:config sentry</info>'); | ||
|
||
return 1; | ||
} | ||
|
||
$output->writeln('Sending test message...'); | ||
|
||
$eventId = $currentHub->captureMessage('This is a test message from the Sentry bundle'); | ||
|
||
if ($eventId) { | ||
$output->writeln("<info>Message sent successfully with ID $eventId</info>"); | ||
} else { | ||
$output->writeln('<error>Message not sent!</error>'); | ||
$output->writeln('<warning>Check your DSN or your before_send callback if used</warning>'); | ||
|
||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
namespace Sentry\SentryBundle\Test\Command; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Sentry\ClientInterface; | ||
use Sentry\Options; | ||
use Sentry\SentryBundle\Command\SentryTestCommand; | ||
use Sentry\State\Hub; | ||
use Sentry\State\HubInterface; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class SentryTestCommandTest extends TestCase | ||
{ | ||
public function testExecuteSuccessfully(): void | ||
{ | ||
$options = new Options(['dsn' => 'http://public:secret@example.com/sentry/1']); | ||
$client = $this->prophesize(ClientInterface::class); | ||
$client->getOptions() | ||
->willReturn($options); | ||
|
||
$hub = $this->prophesize(HubInterface::class); | ||
$hub->getClient() | ||
->willReturn($client->reveal()); | ||
$lastEventId = 'abcdef0123456'; | ||
$hub->captureMessage(Argument::containingString('test'), Argument::cetera()) | ||
->shouldBeCalled() | ||
->willReturn($lastEventId); | ||
|
||
Hub::setCurrent($hub->reveal()); | ||
|
||
$commandTester = $this->executeCommand(); | ||
|
||
$output = $commandTester->getDisplay(); | ||
$this->assertContains('DSN correctly configured', $output); | ||
$this->assertContains('Sending test message', $output); | ||
$this->assertContains('Message sent', $output); | ||
$this->assertContains($lastEventId, $output); | ||
$this->assertSame(0, $commandTester->getStatusCode()); | ||
} | ||
|
||
public function testExecuteFailsDueToMissingDSN(): void | ||
{ | ||
$client = $this->prophesize(ClientInterface::class); | ||
$client->getOptions() | ||
->willReturn(new Options()); | ||
|
||
$hub = $this->prophesize(HubInterface::class); | ||
$hub->getClient() | ||
->willReturn($client->reveal()); | ||
|
||
Hub::setCurrent($hub->reveal()); | ||
|
||
$commandTester = $this->executeCommand(); | ||
|
||
$this->assertNotSame(0, $commandTester->getStatusCode()); | ||
$output = $commandTester->getDisplay(); | ||
$this->assertContains('No DSN configured', $output); | ||
$this->assertContains('try bin/console debug:config sentry', $output); | ||
} | ||
|
||
public function testExecuteFailsDueToMessageNotSent(): void | ||
{ | ||
$options = new Options(['dsn' => 'http://public:secret@example.com/sentry/1']); | ||
$client = $this->prophesize(ClientInterface::class); | ||
$client->getOptions() | ||
->willReturn($options); | ||
|
||
$hub = $this->prophesize(HubInterface::class); | ||
$hub->getClient() | ||
->willReturn($client->reveal()); | ||
$hub->captureMessage(Argument::containingString('test'), Argument::cetera()) | ||
->shouldBeCalled() | ||
->willReturn(null); | ||
|
||
Hub::setCurrent($hub->reveal()); | ||
|
||
$commandTester = $this->executeCommand(); | ||
|
||
$this->assertNotSame(0, $commandTester->getStatusCode()); | ||
$output = $commandTester->getDisplay(); | ||
$this->assertContains('DSN correctly configured', $output); | ||
$this->assertContains('Sending test message', $output); | ||
$this->assertContains('Message not sent', $output); | ||
} | ||
|
||
public function testExecuteFailsDueToMissingClient(): void | ||
{ | ||
$hub = $this->prophesize(HubInterface::class); | ||
$hub->getClient() | ||
->willReturn(null); | ||
|
||
Hub::setCurrent($hub->reveal()); | ||
|
||
$commandTester = $this->executeCommand(); | ||
|
||
$this->assertNotSame(0, $commandTester->getStatusCode()); | ||
$output = $commandTester->getDisplay(); | ||
$this->assertContains('No client found', $output); | ||
$this->assertContains('DSN is probably missing', $output); | ||
} | ||
|
||
private function executeCommand(): CommandTester | ||
{ | ||
$application = new Application(); | ||
$application->add(new SentryTestCommand()); | ||
|
||
$command = $application->find('sentry:test'); | ||
$commandTester = new CommandTester($command); | ||
$commandTester->execute([ | ||
'command' => $command->getName(), | ||
]); | ||
|
||
return $commandTester; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters