Skip to content

Commit

Permalink
Merge pull request #73 from johanrosenson/event-ping-environments-arg…
Browse files Browse the repository at this point in the history
…ument

add $environments arguments to ping methods
  • Loading branch information
shalvah committed Feb 25, 2021
2 parents a33fdc9 + d1f6d4c commit 9afdf92
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/HoneybadgerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,21 @@ private function bindCommands()
*/
private function registerMacros()
{
Event::macro('thenPingHoneybadger', function ($id) {
return $this->then(function () use ($id) {
app(Reporter::class)->checkin($id);
/** @param string|array|null $environments */
Event::macro('thenPingHoneybadger', function (string $id, $environments = null) {
return $this->then(function () use ($id, $environments) {
if ($environments === null || app()->environment($environments)) {
app(Reporter::class)->checkin($id);
}
});
});

Event::macro('pingHoneybadgerOnSuccess', function ($id) {
return $this->onSuccess(function () use ($id) {
app(Reporter::class)->checkin($id);
/** @param string|array|null $environments */
Event::macro('pingHoneybadgerOnSuccess', function (string $id, $environments = null) {
return $this->onSuccess(function () use ($id, $environments) {
if ($environments === null || app()->environment($environments)) {
app(Reporter::class)->checkin($id);
}
});
});
}
Expand Down
127 changes: 127 additions & 0 deletions tests/HoneybadgerEventPingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,131 @@ public function scheduled_tasks_will_ping_honeybadger()

$this->artisan('schedule:run');
}

/** @test */
public function scheduled_tasks_will_ping_honeybadger_if_matching_environments()
{
$schedule = $this->app[Schedule::class];

$honeybadger = $this->createMock(Reporter::class);
$honeybadger->expects($this->exactly(2))
->method('checkin')
->with('1234');

$this->app->instance(Reporter::class, $honeybadger);

$schedule->call(function () {
return true;
})->thenPingHoneybadger('1234', 'testing');

$schedule->call(function () {
return true;
})->thenPingHoneybadger('1234', ['testing', 'production']);

$this->artisan('schedule:run');
}

/** @test */
public function scheduled_tasks_will_not_ping_honeybadger_if_non_matching_environments()
{
$schedule = $this->app[Schedule::class];

$honeybadger = $this->createMock(Reporter::class);
$honeybadger->expects($this->never())
->method('checkin');

$this->app->instance(Reporter::class, $honeybadger);

$schedule->call(function () {
return true;
})->thenPingHoneybadger('1234', 'development');

$schedule->call(function () {
return true;
})->thenPingHoneybadger('1234', ['development', 'production']);

$this->artisan('schedule:run');
}

/** @test */
public function successful_tasks_will_ping_honeybadger()
{
if (version_compare($this->app->version(), '8.6.0', '<')) {
$this->markTestSkipped("Laravel < 8.6 doesn't set proper return codes for callables.");

return;
}

$schedule = $this->app[Schedule::class];

$honeybadger = $this->createMock(Reporter::class);
$honeybadger->expects($this->once())
->method('checkin')
->with('1234');

$this->app->instance(Reporter::class, $honeybadger);

$schedule->call(function () {
return true;
})->pingHoneybadgerOnSuccess('1234');

$this->artisan('schedule:run');
}

/** @test */
public function successful_tasks_will_ping_honeybadger_if_matching_environments()
{
if (version_compare($this->app->version(), '8.6.0', '<')) {
$this->markTestSkipped("Laravel < 8.6 doesn't set proper return codes for callables.");

return;
}

$schedule = $this->app[Schedule::class];

$honeybadger = $this->createMock(Reporter::class);
$honeybadger->expects($this->exactly(2))
->method('checkin')
->with('1234');

$this->app->instance(Reporter::class, $honeybadger);

$schedule->call(function () {
return true;
})->pingHoneybadgerOnSuccess('1234', 'testing');

$schedule->call(function () {
return true;
})->pingHoneybadgerOnSuccess('1234', ['testing', 'production']);

$this->artisan('schedule:run');
}

/** @test */
public function successful_tasks_will_not_ping_honeybadger_if_non_matching_environments()
{
if (version_compare($this->app->version(), '8.6.0', '<')) {
$this->markTestSkipped("Laravel < 8.6 doesn't set proper return codes for callables.");

return;
}

$schedule = $this->app[Schedule::class];

$honeybadger = $this->createMock(Reporter::class);
$honeybadger->expects($this->never())
->method('checkin');

$this->app->instance(Reporter::class, $honeybadger);

$schedule->call(function () {
return true;
})->pingHoneybadgerOnSuccess('1234', 'development');

$schedule->call(function () {
return true;
})->pingHoneybadgerOnSuccess('1234', ['development', 'production']);

$this->artisan('schedule:run');
}
}

0 comments on commit 9afdf92

Please sign in to comment.