-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Laravel about command registration (#768)
Co-authored-by: Alex Bouma <alex@bouma.me>
- Loading branch information
1 parent
ba87236
commit bf155b8
Showing
3 changed files
with
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Console; | ||
|
||
use Sentry\Client; | ||
use Sentry\Laravel\Version; | ||
use Sentry\State\HubInterface; | ||
|
||
class AboutCommandIntegration | ||
{ | ||
public function __invoke(HubInterface $hub): array | ||
{ | ||
$client = $hub->getClient(); | ||
|
||
if ($client === null) { | ||
return [ | ||
'Enabled' => '<fg=red;options=bold>NOT CONFIGURED</>', | ||
'Laravel SDK Version' => Version::SDK_VERSION, | ||
'PHP SDK Version' => Client::SDK_VERSION, | ||
]; | ||
} | ||
|
||
$options = $client->getOptions(); | ||
|
||
// Note: order is not important since Laravel orders these alphabetically | ||
return [ | ||
'Enabled' => $options->getDsn() ? '<fg=green;options=bold>YES</>' : '<fg=red;options=bold>MISSING DSN</>', | ||
'Environment' => $options->getEnvironment() ?: '<fg=yellow;options=bold>NOT SET</>', | ||
'Laravel SDK Version' => Version::SDK_VERSION, | ||
'PHP SDK Version' => Client::SDK_VERSION, | ||
'Release' => $options->getRelease() ?: '<fg=yellow;options=bold>NOT SET</>', | ||
'Sample Rate Errors' => $this->formatSampleRate($options->getSampleRate()), | ||
'Sample Rate Performance Monitoring' => $this->formatSampleRate($options->getTracesSampleRate(), $options->getTracesSampler() !== null), | ||
'Sample Rate Profiling' => $this->formatSampleRate($options->getProfilesSampleRate()), | ||
'Send Default PII' => $options->shouldSendDefaultPii() ? '<fg=green;options=bold>ENABLED</>' : '<fg=yellow;options=bold>DISABLED</>', | ||
]; | ||
} | ||
|
||
private function formatSampleRate(?float $sampleRate, bool $hasSamplerCallback = false): string | ||
{ | ||
if ($hasSamplerCallback) { | ||
return '<fg=green;options=bold>CUSTOM SAMPLER</>'; | ||
} | ||
|
||
if ($sampleRate === null) { | ||
return '<fg=yellow;options=bold>NOT SET</>'; | ||
} | ||
|
||
return number_format($sampleRate * 100) . '%'; | ||
} | ||
} |
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,88 @@ | ||
<?php | ||
|
||
namespace Sentry\Console; | ||
|
||
use Illuminate\Foundation\Console\AboutCommand; | ||
use Illuminate\Support\Facades\Artisan; | ||
use Sentry\Client; | ||
use Sentry\Laravel\Tests\TestCase; | ||
use Sentry\Laravel\Version; | ||
use Sentry\State\Hub; | ||
use Sentry\State\HubInterface; | ||
|
||
class AboutCommandIntegrationTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!class_exists(AboutCommand::class)) { | ||
$this->markTestSkipped('The about command is only available in Laravel 9.0+'); | ||
} | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testAboutCommandContainsExpectedData(): void | ||
{ | ||
$this->resetApplicationWithConfig([ | ||
'sentry.release' => '1.2.3', | ||
'sentry.environment' => 'testing', | ||
'sentry.traces_sample_rate' => 0.95, | ||
]); | ||
|
||
$expectedData = [ | ||
'environment' => 'testing', | ||
'release' => '1.2.3', | ||
'sample_rate_errors' => '100%', | ||
'sample_rate_profiling' => 'NOT SET', | ||
'sample_rate_performance_monitoring' => '95%', | ||
'send_default_pii' => 'DISABLED', | ||
'php_sdk_version' => Client::SDK_VERSION, | ||
'laravel_sdk_version' => Version::SDK_VERSION, | ||
]; | ||
|
||
$actualData = $this->runArtisanAboutAndReturnSentryData(); | ||
|
||
foreach ($expectedData as $key => $value) { | ||
$this->assertArrayHasKey($key, $actualData); | ||
$this->assertEquals($value, $actualData[$key]); | ||
} | ||
} | ||
|
||
public function testAboutCommandContainsExpectedDataWithoutHubClient(): void | ||
{ | ||
$this->app->bind(HubInterface::class, static function () { | ||
return new Hub(null); | ||
}); | ||
|
||
$expectedData = [ | ||
'enabled' => 'NOT CONFIGURED', | ||
'php_sdk_version' => Client::SDK_VERSION, | ||
'laravel_sdk_version' => Version::SDK_VERSION, | ||
]; | ||
|
||
$actualData = $this->runArtisanAboutAndReturnSentryData(); | ||
|
||
foreach ($expectedData as $key => $value) { | ||
$this->assertArrayHasKey($key, $actualData); | ||
$this->assertEquals($value, $actualData[$key]); | ||
} | ||
} | ||
|
||
private function runArtisanAboutAndReturnSentryData(): array | ||
{ | ||
$this->withoutMockingConsoleOutput(); | ||
|
||
$this->artisan(AboutCommand::class, ['--json' => null]); | ||
|
||
$output = Artisan::output(); | ||
|
||
// This might seem like a weird thing to do, but it's necessary to make sure that that the command didn't have any side effects on the container | ||
$this->refreshApplication(); | ||
|
||
$aboutOutput = json_decode($output, true); | ||
|
||
$this->assertArrayHasKey('sentry', $aboutOutput); | ||
|
||
return $aboutOutput['sentry']; | ||
} | ||
} |