Skip to content

Commit f1a468a

Browse files
authored
Stop requiring the phpspec/prophecy dev dependency (#770)
1 parent 3d7c8ae commit f1a468a

File tree

5 files changed

+115
-117
lines changed

5 files changed

+115
-117
lines changed

composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
"doctrine/dbal": "^2.13||^3.0",
4545
"doctrine/doctrine-bundle": "^1.12||^2.5",
4646
"friendsofphp/php-cs-fixer": "^2.19||<=3.16.0",
47-
"jangregor/phpstan-prophecy": "^1.0",
4847
"monolog/monolog": "^1.3||^2.0",
49-
"phpspec/prophecy": "!=1.11.0",
50-
"phpspec/prophecy-phpunit": "^1.1||^2.0",
5148
"phpstan/extension-installer": "^1.0",
5249
"phpstan/phpstan": "^1.3",
5350
"phpstan/phpstan-phpunit": "^1.0",

src/Command/SentryTestCommand.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,37 @@
55
namespace Sentry\SentryBundle\Command;
66

77
use Sentry\SentrySdk;
8+
use Sentry\State\HubInterface;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Input\InputInterface;
1011
use Symfony\Component\Console\Output\OutputInterface;
1112

13+
/**
14+
* @final since version 4.12
15+
*/
1216
class SentryTestCommand extends Command
1317
{
18+
/**
19+
* @var HubInterface
20+
*/
21+
private $hub;
22+
23+
public function __construct(?HubInterface $hub = null)
24+
{
25+
parent::__construct();
26+
27+
if (null === $hub) {
28+
@trigger_error(sprintf('Not passing an instance of the "%s" interface as argument of the constructor is deprecated since version 4.12 and will not work since version 5.0.', HubInterface::class), \E_USER_DEPRECATED);
29+
}
30+
31+
$this->hub = $hub ?? SentrySdk::getCurrentHub();
32+
}
33+
1434
protected function execute(InputInterface $input, OutputInterface $output): int
1535
{
16-
$currentHub = SentrySdk::getCurrentHub();
17-
$client = $currentHub->getClient();
36+
$client = $this->hub->getClient();
1837

19-
if (!$client) {
38+
if (null === $client) {
2039
$output->writeln('<error>No client found</error>');
2140
$output->writeln('<info>Your DSN is probably missing, check your configuration</info>');
2241

@@ -25,28 +44,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
2544

2645
$dsn = $client->getOptions()->getDsn();
2746

28-
if ($dsn) {
29-
$output->writeln('<info>DSN correctly configured in the current client</info>');
30-
} else {
47+
if (null === $dsn) {
3148
$output->writeln('<error>No DSN configured in the current client, please check your configuration</error>');
3249
$output->writeln('<info>To debug further, try bin/console debug:config sentry</info>');
3350

3451
return 1;
3552
}
3653

54+
$output->writeln('<info>DSN correctly configured in the current client</info>');
3755
$output->writeln('Sending test message...');
3856

39-
$eventId = $currentHub->captureMessage('This is a test message from the Sentry bundle');
57+
$eventId = $this->hub->captureMessage('This is a test message from the Sentry bundle');
4058

41-
if ($eventId) {
42-
$output->writeln("<info>Message sent successfully with ID $eventId</info>");
43-
} else {
59+
if (null === $eventId) {
4460
$output->writeln('<error>Message not sent!</error>');
4561
$output->writeln('<warning>Check your DSN or your before_send callback if used</warning>');
4662

4763
return 1;
4864
}
4965

66+
$output->writeln("<info>Message sent successfully with ID $eventId</info>");
67+
5068
return 0;
5169
}
5270
}

src/Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
</service>
9696

9797
<service id="Sentry\SentryBundle\Command\SentryTestCommand" class="Sentry\SentryBundle\Command\SentryTestCommand">
98+
<argument type="service" id="Sentry\State\HubInterface" />
99+
98100
<tag name="console.command" command="sentry:test" />
99101
</service>
100102

tests/BaseTestCase.php

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

tests/Command/SentryTestCommandTest.php

Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -4,129 +4,134 @@
44

55
namespace Sentry\SentryBundle\Tests\Command;
66

7-
use Prophecy\Argument;
7+
use PHPUnit\Framework\MockObject\MockObject;
8+
use PHPUnit\Framework\TestCase;
89
use Sentry\ClientInterface;
910
use Sentry\EventId;
1011
use Sentry\Options;
1112
use Sentry\SentryBundle\Command\SentryTestCommand;
12-
use Sentry\SentryBundle\Tests\BaseTestCase;
13-
use Sentry\SentrySdk;
1413
use Sentry\State\HubInterface;
15-
use Symfony\Component\Console\Application;
14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1615
use Symfony\Component\Console\Tester\CommandTester;
1716

18-
class SentryTestCommandTest extends BaseTestCase
17+
final class SentryTestCommandTest extends TestCase
1918
{
20-
protected function tearDown(): void
19+
use ExpectDeprecationTrait;
20+
21+
/**
22+
* @var HubInterface&MockObject
23+
*/
24+
private $hub;
25+
26+
/**
27+
* @var ClientInterface&MockObject
28+
*/
29+
private $client;
30+
31+
/**
32+
* @var CommandTester
33+
*/
34+
private $command;
35+
36+
protected function setUp(): void
2137
{
22-
parent::tearDown();
38+
parent::setUp();
2339

24-
// reset current Hub to avoid leaking the mock outside of this tests
25-
SentrySdk::init();
40+
$this->hub = $this->createMock(HubInterface::class);
41+
$this->client = $this->createMock(ClientInterface::class);
42+
$this->command = new CommandTester(new SentryTestCommand($this->hub));
2643
}
2744

28-
public function testExecuteSuccessfully(): void
45+
public function testExecute(): void
2946
{
30-
$options = new Options(['dsn' => 'http://public:[email protected]/sentry/1']);
31-
$client = $this->prophesize(ClientInterface::class);
32-
$client->getOptions()
33-
->willReturn($options);
34-
35-
$hub = $this->prophesize(HubInterface::class);
36-
$hub->getClient()
37-
->willReturn($client->reveal());
3847
$lastEventId = EventId::generate();
39-
$hub->captureMessage(Argument::containingString('test'), Argument::cetera())
40-
->shouldBeCalled()
41-
->willReturn($lastEventId);
4248

43-
SentrySdk::setCurrentHub($hub->reveal());
49+
$this->client->expects($this->once())
50+
->method('getOptions')
51+
->willReturn(new Options(['dsn' => 'https://public:[email protected]/sentry/1']));
52+
53+
$this->hub->expects($this->once())
54+
->method('getClient')
55+
->willReturn($this->client);
56+
57+
$this->hub->expects($this->once())
58+
->method('captureMessage')
59+
->with('This is a test message from the Sentry bundle')
60+
->willReturn($lastEventId);
4461

45-
$commandTester = $this->executeCommand();
62+
$exitCode = $this->command->execute([]);
63+
$output = $this->command->getDisplay();
4664

47-
$output = $commandTester->getDisplay();
48-
$this->assertStringContainsString('DSN correctly configured', $output);
49-
$this->assertStringContainsString('Sending test message', $output);
50-
$this->assertStringContainsString('Message sent', $output);
51-
$this->assertStringContainsString((string) $lastEventId, $output);
52-
$this->assertSame(0, $commandTester->getStatusCode());
65+
$this->assertSame(0, $exitCode);
66+
$this->assertStringContainsString('DSN correctly configured in the current client', $output);
67+
$this->assertStringContainsString('Sending test message...', $output);
68+
$this->assertStringContainsString("Message sent successfully with ID $lastEventId", $output);
5369
}
5470

5571
public function testExecuteFailsDueToMissingDSN(): void
5672
{
57-
$client = $this->prophesize(ClientInterface::class);
58-
$client->getOptions()
73+
$this->client->expects($this->once())
74+
->method('getOptions')
5975
->willReturn(new Options());
6076

61-
$hub = $this->prophesize(HubInterface::class);
62-
$hub->getClient()
63-
->willReturn($client->reveal());
77+
$this->hub->expects($this->once())
78+
->method('getClient')
79+
->willReturn($this->client);
6480

65-
SentrySdk::setCurrentHub($hub->reveal());
81+
$exitCode = $this->command->execute([]);
82+
$output = $this->command->getDisplay();
6683

67-
$commandTester = $this->executeCommand();
68-
69-
$this->assertNotSame(0, $commandTester->getStatusCode());
70-
$output = $commandTester->getDisplay();
71-
$this->assertStringContainsString('No DSN configured', $output);
72-
$this->assertStringContainsString('try bin/console debug:config sentry', $output);
84+
$this->assertSame(1, $exitCode);
85+
$this->assertStringContainsString('No DSN configured in the current client, please check your configuration', $output);
86+
$this->assertStringContainsString('To debug further, try bin/console debug:config sentry', $output);
7387
}
7488

7589
public function testExecuteFailsDueToMessageNotSent(): void
7690
{
77-
$options = new Options(['dsn' => 'http://public:[email protected]/sentry/1']);
78-
$client = $this->prophesize(ClientInterface::class);
79-
$client->getOptions()
80-
->willReturn($options);
81-
82-
$hub = $this->prophesize(HubInterface::class);
83-
$hub->getClient()
84-
->willReturn($client->reveal());
85-
$hub->captureMessage(Argument::containingString('test'), Argument::cetera())
86-
->shouldBeCalled()
87-
->willReturn(null);
91+
$this->client->expects($this->once())
92+
->method('getOptions')
93+
->willReturn(new Options(['dsn' => 'https://public:[email protected]/sentry/1']));
8894

89-
SentrySdk::setCurrentHub($hub->reveal());
95+
$this->hub->expects($this->once())
96+
->method('getClient')
97+
->willReturn($this->client);
98+
99+
$this->hub->expects($this->once())
100+
->method('captureMessage')
101+
->with('This is a test message from the Sentry bundle')
102+
->willReturn(null);
90103

91-
$commandTester = $this->executeCommand();
104+
$exitCode = $this->command->execute([]);
105+
$output = $this->command->getDisplay();
92106

93-
$this->assertNotSame(0, $commandTester->getStatusCode());
94-
$output = $commandTester->getDisplay();
95-
$this->assertStringContainsString('DSN correctly configured', $output);
96-
$this->assertStringContainsString('Sending test message', $output);
97-
$this->assertStringContainsString('Message not sent', $output);
107+
$this->assertSame(1, $exitCode);
108+
$this->assertStringContainsString('DSN correctly configured in the current client', $output);
109+
$this->assertStringContainsString('Sending test message...', $output);
110+
$this->assertStringContainsString('Message not sent!', $output);
111+
$this->assertStringContainsString('Check your DSN or your before_send callback if used', $output);
98112
}
99113

100114
public function testExecuteFailsDueToMissingClient(): void
101115
{
102-
$hub = $this->prophesize(HubInterface::class);
103-
$hub->getClient()
116+
$this->hub->expects($this->once())
117+
->method('getClient')
104118
->willReturn(null);
105119

106-
SentrySdk::setCurrentHub($hub->reveal());
120+
$exitCode = $this->command->execute([]);
121+
$output = $this->command->getDisplay();
107122

108-
$commandTester = $this->executeCommand();
109-
110-
$this->assertNotSame(0, $commandTester->getStatusCode());
111-
$output = $commandTester->getDisplay();
123+
$this->assertSame(1, $exitCode);
112124
$this->assertStringContainsString('No client found', $output);
113-
$this->assertStringContainsString('DSN is probably missing', $output);
125+
$this->assertStringContainsString('Your DSN is probably missing, check your configuration', $output);
114126
}
115127

116-
private function executeCommand(): CommandTester
128+
/**
129+
* @group legacy
130+
*/
131+
public function testConstructorTriggersDeprecationErrorIfHubIsNotPassedToConstructor(): void
117132
{
118-
$command = new SentryTestCommand();
119-
$command->setName('sentry:test');
120-
121-
$application = new Application();
122-
$application->add($command);
123-
124-
$command = $application->find('sentry:test');
125-
$commandTester = new CommandTester($command);
126-
$commandTester->execute([
127-
'command' => $command->getName(),
128-
]);
133+
$this->expectDeprecation('Not passing an instance of the "Sentry\State\HubInterface" interface as argument of the constructor is deprecated since version 4.12 and will not work since version 5.0.');
129134

130-
return $commandTester;
135+
new SentryTestCommand();
131136
}
132137
}

0 commit comments

Comments
 (0)