Skip to content

Commit a6fecad

Browse files
committed
Improve testing of the test command
1 parent cb8ef7c commit a6fecad

File tree

2 files changed

+100
-10
lines changed

2 files changed

+100
-10
lines changed

src/Command/SentryTestCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
1818
{
1919
$currentHub = Hub::getCurrent();
2020
$client = $currentHub->getClient();
21-
21+
2222
if (! $client) {
2323
$output->writeln('<error>No client found</error>');
2424
$output->writeln('<info>Your DSN is probably missing, check your configuration</info>');
@@ -27,13 +27,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2727
}
2828

2929
$dsn = $client->getOptions()->getDsn();
30-
30+
3131
if ($dsn) {
3232
$output->writeln('<info>DSN correctly configured in the current client</info>');
3333
} else {
3434
$output->writeln('<error>No DSN configured in the current client, please check your configuration</error>');
3535
$output->writeln('<info>To debug further, try bin/console debug:config sentry</info>');
36-
36+
3737
return 1;
3838
}
3939

@@ -46,7 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4646
} else {
4747
$output->writeln('<error>Message not sent!</error>');
4848
$output->writeln('<warning>Check your DSN or your before_send callback if used</warning>');
49-
49+
5050
return 1;
5151
}
5252

test/Command/SentryTestCommandTest.php

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,116 @@
33
namespace Sentry\SentryBundle\Test\Command;
44

55
use PHPUnit\Framework\TestCase;
6+
use Prophecy\Argument;
7+
use Sentry\ClientInterface;
8+
use Sentry\Options;
69
use Sentry\SentryBundle\Command\SentryTestCommand;
10+
use Sentry\State\Hub;
11+
use Sentry\State\HubInterface;
712
use Symfony\Component\Console\Application;
813
use Symfony\Component\Console\Tester\CommandTester;
914

1015
class SentryTestCommandTest extends TestCase
1116
{
12-
public function testExecuteFailsDueToMissingDsn(): void
17+
public function testExecuteSuccessfully(): void
18+
{
19+
$options = new Options(['dsn' => 'http://public:secret@example.com/sentry/1']);
20+
$client = $this->prophesize(ClientInterface::class);
21+
$client->getOptions()
22+
->willReturn($options);
23+
24+
$hub = $this->prophesize(HubInterface::class);
25+
$hub->getClient()
26+
->willReturn($client->reveal());
27+
$lastEventId = 'abcdef0123456';
28+
$hub->captureMessage(Argument::containingString('test'), Argument::cetera())
29+
->shouldBeCalled()
30+
->willReturn($lastEventId);
31+
32+
Hub::setCurrent($hub->reveal());
33+
34+
$commandTester = $this->executeCommand();
35+
36+
$output = $commandTester->getDisplay();
37+
$this->assertContains('DSN correctly configured', $output);
38+
$this->assertContains('Sending test message', $output);
39+
$this->assertContains('Message sent', $output);
40+
$this->assertContains($lastEventId, $output);
41+
$this->assertSame(0, $commandTester->getStatusCode());
42+
}
43+
44+
public function testExecuteFailsDueToMissingDSN(): void
45+
{
46+
$client = $this->prophesize(ClientInterface::class);
47+
$client->getOptions()
48+
->willReturn(new Options());
49+
50+
$hub = $this->prophesize(HubInterface::class);
51+
$hub->getClient()
52+
->willReturn($client->reveal());
53+
54+
Hub::setCurrent($hub->reveal());
55+
56+
$commandTester = $this->executeCommand();
57+
58+
$this->assertNotSame(0, $commandTester->getStatusCode());
59+
$output = $commandTester->getDisplay();
60+
$this->assertContains('No DSN configured', $output);
61+
$this->assertContains('try bin/console debug:config sentry', $output);
62+
}
63+
64+
public function testExecuteFailsDueToMessageNotSent(): void
65+
{
66+
$options = new Options(['dsn' => 'http://public:secret@example.com/sentry/1']);
67+
$client = $this->prophesize(ClientInterface::class);
68+
$client->getOptions()
69+
->willReturn($options);
70+
71+
$hub = $this->prophesize(HubInterface::class);
72+
$hub->getClient()
73+
->willReturn($client->reveal());
74+
$hub->captureMessage(Argument::containingString('test'), Argument::cetera())
75+
->shouldBeCalled()
76+
->willReturn(null);
77+
78+
Hub::setCurrent($hub->reveal());
79+
80+
$commandTester = $this->executeCommand();
81+
82+
$this->assertNotSame(0, $commandTester->getStatusCode());
83+
$output = $commandTester->getDisplay();
84+
$this->assertContains('DSN correctly configured', $output);
85+
$this->assertContains('Sending test message', $output);
86+
$this->assertContains('Message not sent', $output);
87+
}
88+
89+
public function testExecuteFailsDueToMissingClient(): void
90+
{
91+
$hub = $this->prophesize(HubInterface::class);
92+
$hub->getClient()
93+
->willReturn(null);
94+
95+
Hub::setCurrent($hub->reveal());
96+
97+
$commandTester = $this->executeCommand();
98+
99+
$this->assertNotSame(0, $commandTester->getStatusCode());
100+
$output = $commandTester->getDisplay();
101+
$this->assertContains('No client found', $output);
102+
$this->assertContains('DSN is probably missing', $output);
103+
}
104+
105+
private function executeCommand(): CommandTester
13106
{
14107
$application = new Application();
15108
$application->add(new SentryTestCommand());
16109

17110
$command = $application->find('sentry:test');
18111
$commandTester = new CommandTester($command);
19112
$commandTester->execute([
20-
'command' => $command->getName(),
113+
'command' => $command->getName(),
21114
]);
22115

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);
116+
return $commandTester;
27117
}
28118
}

0 commit comments

Comments
 (0)