|
4 | 4 |
|
5 | 5 | namespace Sentry\SentryBundle\Tests\Command;
|
6 | 6 |
|
7 |
| -use Prophecy\Argument; |
| 7 | +use PHPUnit\Framework\MockObject\MockObject; |
| 8 | +use PHPUnit\Framework\TestCase; |
8 | 9 | use Sentry\ClientInterface;
|
9 | 10 | use Sentry\EventId;
|
10 | 11 | use Sentry\Options;
|
11 | 12 | use Sentry\SentryBundle\Command\SentryTestCommand;
|
12 |
| -use Sentry\SentryBundle\Tests\BaseTestCase; |
13 |
| -use Sentry\SentrySdk; |
14 | 13 | use Sentry\State\HubInterface;
|
15 |
| -use Symfony\Component\Console\Application; |
| 14 | +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; |
16 | 15 | use Symfony\Component\Console\Tester\CommandTester;
|
17 | 16 |
|
18 |
| -class SentryTestCommandTest extends BaseTestCase |
| 17 | +final class SentryTestCommandTest extends TestCase |
19 | 18 | {
|
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 |
21 | 37 | {
|
22 |
| - parent::tearDown(); |
| 38 | + parent::setUp(); |
23 | 39 |
|
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)); |
26 | 43 | }
|
27 | 44 |
|
28 |
| - public function testExecuteSuccessfully(): void |
| 45 | + public function testExecute(): void |
29 | 46 | {
|
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()); |
38 | 47 | $lastEventId = EventId::generate();
|
39 |
| - $hub->captureMessage(Argument::containingString('test'), Argument::cetera()) |
40 |
| - ->shouldBeCalled() |
41 |
| - ->willReturn($lastEventId); |
42 | 48 |
|
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); |
44 | 61 |
|
45 |
| - $commandTester = $this->executeCommand(); |
| 62 | + $exitCode = $this->command->execute([]); |
| 63 | + $output = $this->command->getDisplay(); |
46 | 64 |
|
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); |
53 | 69 | }
|
54 | 70 |
|
55 | 71 | public function testExecuteFailsDueToMissingDSN(): void
|
56 | 72 | {
|
57 |
| - $client = $this->prophesize(ClientInterface::class); |
58 |
| - $client->getOptions() |
| 73 | + $this->client->expects($this->once()) |
| 74 | + ->method('getOptions') |
59 | 75 | ->willReturn(new Options());
|
60 | 76 |
|
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); |
64 | 80 |
|
65 |
| - SentrySdk::setCurrentHub($hub->reveal()); |
| 81 | + $exitCode = $this->command->execute([]); |
| 82 | + $output = $this->command->getDisplay(); |
66 | 83 |
|
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); |
73 | 87 | }
|
74 | 88 |
|
75 | 89 | public function testExecuteFailsDueToMessageNotSent(): void
|
76 | 90 | {
|
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'])); |
88 | 94 |
|
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); |
90 | 103 |
|
91 |
| - $commandTester = $this->executeCommand(); |
| 104 | + $exitCode = $this->command->execute([]); |
| 105 | + $output = $this->command->getDisplay(); |
92 | 106 |
|
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); |
98 | 112 | }
|
99 | 113 |
|
100 | 114 | public function testExecuteFailsDueToMissingClient(): void
|
101 | 115 | {
|
102 |
| - $hub = $this->prophesize(HubInterface::class); |
103 |
| - $hub->getClient() |
| 116 | + $this->hub->expects($this->once()) |
| 117 | + ->method('getClient') |
104 | 118 | ->willReturn(null);
|
105 | 119 |
|
106 |
| - SentrySdk::setCurrentHub($hub->reveal()); |
| 120 | + $exitCode = $this->command->execute([]); |
| 121 | + $output = $this->command->getDisplay(); |
107 | 122 |
|
108 |
| - $commandTester = $this->executeCommand(); |
109 |
| - |
110 |
| - $this->assertNotSame(0, $commandTester->getStatusCode()); |
111 |
| - $output = $commandTester->getDisplay(); |
| 123 | + $this->assertSame(1, $exitCode); |
112 | 124 | $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); |
114 | 126 | }
|
115 | 127 |
|
116 |
| - private function executeCommand(): CommandTester |
| 128 | + /** |
| 129 | + * @group legacy |
| 130 | + */ |
| 131 | + public function testConstructorTriggersDeprecationErrorIfHubIsNotPassedToConstructor(): void |
117 | 132 | {
|
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.'); |
129 | 134 |
|
130 |
| - return $commandTester; |
| 135 | + new SentryTestCommand(); |
131 | 136 | }
|
132 | 137 | }
|
0 commit comments