|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\SentryBundle\Test\Command; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Prophecy\Argument; |
| 7 | +use Sentry\ClientInterface; |
| 8 | +use Sentry\Options; |
| 9 | +use Sentry\SentryBundle\Command\SentryTestCommand; |
| 10 | +use Sentry\State\Hub; |
| 11 | +use Sentry\State\HubInterface; |
| 12 | +use Symfony\Component\Console\Application; |
| 13 | +use Symfony\Component\Console\Tester\CommandTester; |
| 14 | + |
| 15 | +class SentryTestCommandTest extends TestCase |
| 16 | +{ |
| 17 | + public function testExecuteSuccessfully(): void |
| 18 | + { |
| 19 | + $options = new Options([ 'dsn' => 'http://public:[email protected]/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:[email protected]/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 |
| 106 | + { |
| 107 | + $application = new Application(); |
| 108 | + $application->add(new SentryTestCommand()); |
| 109 | + |
| 110 | + $command = $application->find('sentry:test'); |
| 111 | + $commandTester = new CommandTester($command); |
| 112 | + $commandTester->execute([ |
| 113 | + 'command' => $command->getName(), |
| 114 | + ]); |
| 115 | + |
| 116 | + return $commandTester; |
| 117 | + } |
| 118 | +} |
0 commit comments