diff --git a/src/HoneybadgerServiceProvider.php b/src/HoneybadgerServiceProvider.php index a398ac2..ed0c044 100644 --- a/src/HoneybadgerServiceProvider.php +++ b/src/HoneybadgerServiceProvider.php @@ -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); + } }); }); } diff --git a/tests/HoneybadgerEventPingTest.php b/tests/HoneybadgerEventPingTest.php index 7c31378..fa92156 100644 --- a/tests/HoneybadgerEventPingTest.php +++ b/tests/HoneybadgerEventPingTest.php @@ -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'); + } }