From 46b127a69d11ea856ca15fd524d2062f9562337a Mon Sep 17 00:00:00 2001 From: David Cox Date: Thu, 12 Mar 2020 17:23:09 -0400 Subject: [PATCH 1/2] Add withoutLogs function to be able to more fluently disable logging for a section of code --- src/ActivityLogger.php | 15 +++++++++ tests/ActivityLoggerTest.php | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/ActivityLogger.php b/src/ActivityLogger.php index dd5a7be7..ef3ed0f6 100644 --- a/src/ActivityLogger.php +++ b/src/ActivityLogger.php @@ -165,6 +165,21 @@ public function log(string $description) return $activity; } + public function withoutLogs(callable $callback) + { + if ($this->logStatus->disabled()) { + return $callback(); + } + + $this->logStatus->disable(); + + try { + return $callback(); + } finally { + $this->logStatus->enable(); + } + } + protected function normalizeCauser($modelOrId): Model { if ($modelOrId instanceof Model) { diff --git a/tests/ActivityLoggerTest.php b/tests/ActivityLoggerTest.php index dd4bcf01..8ecb2904 100644 --- a/tests/ActivityLoggerTest.php +++ b/tests/ActivityLoggerTest.php @@ -4,6 +4,7 @@ use Auth; use Carbon\Carbon; +use Exception; use Illuminate\Support\Collection; use Spatie\Activitylog\Exceptions\CouldNotLogActivity; use Spatie\Activitylog\Models\Activity; @@ -337,4 +338,67 @@ public function it_will_log_a_custom_created_at_date_time() $this->assertEquals($activityDateTime->toAtomString(), $firstActivity->created_at->toAtomString()); } + + /** @test */ + public function it_will_disable_logs_for_a_callback() + { + $result = activity()->withoutLogs(function() { + activity()->log('created'); + return 'hello'; + }); + + $this->assertNull($this->getLastActivity()); + $this->assertEquals('hello', $result); + } + + /** @test */ + public function it_will_disable_logs_for_a_callback_without_affecting_previous_state() + { + activity()->withoutLogs(function() { + activity()->log('created'); + }); + + $this->assertNull($this->getLastActivity()); + + activity()->log('outer'); + + $this->assertEquals('outer', $this->getLastActivity()->description); + } + + /** @test */ + public function it_will_disable_logs_for_a_callback_without_affecting_previous_state_even_when_already_disabled() + { + activity()->disableLogging(); + + activity()->withoutLogs(function() { + activity()->log('created'); + }); + + $this->assertNull($this->getLastActivity()); + + activity()->log('outer'); + + $this->assertNull($this->getLastActivity()); + } + + /** @test */ + public function it_will_disable_logs_for_a_callback_without_affecting_previous_state_even_with_exception() + { + activity()->disableLogging(); + + try { + activity()->withoutLogs(function() { + activity()->log('created'); + throw new Exception('OH NO'); + }); + } catch (Exception $ex) { + // + } + + $this->assertNull($this->getLastActivity()); + + activity()->log('outer'); + + $this->assertNull($this->getLastActivity()); + } } From f7a1d0d77a9ed040ce2db70638220a4071552b1f Mon Sep 17 00:00:00 2001 From: David Cox Date: Thu, 12 Mar 2020 17:29:04 -0400 Subject: [PATCH 2/2] styleci fixes --- tests/ActivityLoggerTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/ActivityLoggerTest.php b/tests/ActivityLoggerTest.php index 8ecb2904..1112e242 100644 --- a/tests/ActivityLoggerTest.php +++ b/tests/ActivityLoggerTest.php @@ -342,8 +342,9 @@ public function it_will_log_a_custom_created_at_date_time() /** @test */ public function it_will_disable_logs_for_a_callback() { - $result = activity()->withoutLogs(function() { + $result = activity()->withoutLogs(function () { activity()->log('created'); + return 'hello'; }); @@ -354,7 +355,7 @@ public function it_will_disable_logs_for_a_callback() /** @test */ public function it_will_disable_logs_for_a_callback_without_affecting_previous_state() { - activity()->withoutLogs(function() { + activity()->withoutLogs(function () { activity()->log('created'); }); @@ -370,7 +371,7 @@ public function it_will_disable_logs_for_a_callback_without_affecting_previous_s { activity()->disableLogging(); - activity()->withoutLogs(function() { + activity()->withoutLogs(function () { activity()->log('created'); }); @@ -387,7 +388,7 @@ public function it_will_disable_logs_for_a_callback_without_affecting_previous_s activity()->disableLogging(); try { - activity()->withoutLogs(function() { + activity()->withoutLogs(function () { activity()->log('created'); throw new Exception('OH NO'); });