Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,12 @@ protected static function bootLogsActivity(): void
->thenReturn();

// Actual logging
$logger = app(ActivityLogger::class)
app(ActivityLogger::class)
->useLog($logName)
->event($eventName)
->performedOn($model)
->withProperties($event->changes);

if (method_exists($model, 'tapActivity')) {
$logger->tap([$model, 'tapActivity'], $eventName);
}

$logger->log($description);
->withProperties($event->changes)
->log($description);

// Reset log options so the model can be serialized.
$model->activitylogOptions = null;
Expand Down Expand Up @@ -148,7 +143,7 @@ protected static function eventsToBeRecorded(): Collection
$reject = collect(static::$doNotRecordEvents ?? []);

if (isset(static::$recordEvents)) {
return collect(static::$recordEvents)->reject(fn(string $eventName) => $reject->contains($eventName));
return collect(static::$recordEvents)->reject(fn (string $eventName) => $reject->contains($eventName));
}

$events = collect([
Expand All @@ -161,7 +156,7 @@ protected static function eventsToBeRecorded(): Collection
$events->push('restored');
}

return $events->reject(fn(string $eventName) => $reject->contains($eventName));
return $events->reject(fn (string $eventName) => $reject->contains($eventName));
}

protected function shouldLogEvent(string $eventName): bool
Expand Down
26 changes: 26 additions & 0 deletions tests/LogsActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,32 @@ public function tapActivity(Activity $activity, string $eventName)
$this->assertEquals('my custom event', $firstActivity->event);
});

it('will only call tapActivity once', function () {
$callCount = 0;

$model = new class() extends Article {
use LogsActivity;

public static $tapCallCount = 0;

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}

public function tapActivity(Activity $activity, string $eventName)
{
static::$tapCallCount++;
}
};

$model::$tapCallCount = 0;
$entity = new $model();
$entity->save();

$this->assertEquals(1, $model::$tapCallCount);
});

it('will not submit log when there is no changes', function () {
$model = new class() extends Article {
use LogsActivity;
Expand Down