Skip to content

Commit dbebfef

Browse files
authored
Merge pull request #65 from getsentry/feature/test-command
Add sentry:test artisan command
2 parents d4fb483 + dc1bc8c commit dbebfef

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ return array(
125125
);
126126
```
127127

128+
## Testing with Artisan
129+
130+
You can test your configuration using the provided ``artisan`` command:
131+
132+
```bash
133+
$ php artisan sentry:test
134+
[sentry] Client configuration:
135+
-> server: https://app.getsentry.com/api/3235/store/
136+
-> project: 3235
137+
-> public_key: e9ebbd88548a441288393c457ec90441
138+
-> secret_key: 399aaee02d454e2ca91351f29bdc3a07
139+
[sentry] Generating test event
140+
[sentry] Sending test event with ID: 5256614438cf4e0798dc9688e9545d94
141+
```
142+
128143
## Adding Context
129144

130145
The mechanism to add context will vary depending on which version of Laravel you're using, but the general approach is the same. Find a good entry point to your application in which the context you want to add is available, ideally early in the process.

src/Sentry/SentryLaravel/SentryLaravelServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public function boot()
5050

5151
$this->bindEvents($app);
5252
}
53+
if ($this->app->runningInConsole()) {
54+
$this->registerArtisanCommands();
55+
}
56+
}
57+
58+
protected function registerArtisanCommands()
59+
{
60+
$this->commands([
61+
SentryTestCommand::class,
62+
]);
5363
}
5464

5565
protected function bindEvents($app)

src/Sentry/SentryLaravel/SentryLumenServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public function boot()
2222
{
2323
$this->app->configure('sentry');
2424
$this->bindEvents($this->app);
25+
if ($this->app->runningInConsole()) {
26+
$this->registerArtisanCommands();
27+
}
28+
}
29+
30+
protected function registerArtisanCommands()
31+
{
32+
$this->commands([
33+
SentryTestCommand::class,
34+
]);
2535
}
2636

2737
protected function bindEvents($app)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Sentry\SentryLaravel;
4+
5+
use Illuminate\Console\Command;
6+
7+
class SentryTestCommand extends Command
8+
{
9+
// XXX(dcramer): Laravel 4.x compatibility
10+
protected $name = 'sentry:test';
11+
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'sentry:test';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Generate a test event and send it to Sentry';
25+
26+
/**
27+
* Create a new command instance.
28+
*
29+
* @return void
30+
*/
31+
public function __construct()
32+
{
33+
parent::__construct();
34+
}
35+
36+
// XXX(dcramer): Laravel 4.x compatibility
37+
public function fire()
38+
{
39+
$this->handle();
40+
}
41+
42+
/**
43+
* Execute the console command.
44+
*
45+
* @return mixed
46+
*/
47+
public function handle()
48+
{
49+
// Maximize error reporting
50+
$old_error_reporting = error_reporting(E_ALL | E_STRICT);
51+
52+
try {
53+
$client = app('sentry');
54+
55+
$config = get_object_vars($client);
56+
$required_keys = array('server', 'project', 'public_key', 'secret_key');
57+
58+
$output = "";
59+
foreach ($required_keys as $key) {
60+
if (empty($config[$key])) {
61+
$this->error("[sentry] ERROR: Missing configuration for $key");
62+
}
63+
if (is_array($config[$key])) {
64+
$output .= "-> $key: [".implode(", ", $config[$key])."]\n";
65+
} else {
66+
$output .= "-> $key: $config[$key]\n";
67+
}
68+
}
69+
70+
$this->info("[sentry] Client configuration:\n" . trim($output));
71+
72+
$this->info('[sentry] Generating test event');
73+
74+
$ex = $this->generateTestException("command name", array("foo" => "bar"));
75+
76+
$event_id = $client->captureException($ex);
77+
78+
$this->info("[sentry] Sending test event with ID: $event_id");
79+
80+
$last_error = $client->getLastError();
81+
if (!empty($last_error)) {
82+
$this->error("[sentry] There was an error sending the test event:\n $last_error");
83+
}
84+
} finally {
85+
error_reporting($old_error_reporting);
86+
}
87+
}
88+
89+
protected function generateTestException($command, $arg)
90+
{
91+
// Do something silly
92+
try {
93+
throw new \Exception('This is a test exception sent from the Raven CLI.');
94+
} catch (\Exception $ex) {
95+
return $ex;
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)