Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withoutAuditing() callback method to temporarily disable auditing for one class #917

Merged
merged 1 commit into from
May 27, 2024
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
20 changes: 20 additions & 0 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,26 @@ public static function enableAuditing()
static::$auditingDisabled = false;
}

/**
* Execute a callback while auditing is disabled.
*
* @param callable $callback
*
* @return mixed
*/
public static function withoutAuditing(callable $callback)
{
$auditingDisabled = static::$auditingDisabled;

static::disableAuditing();

try {
return $callback();
} finally {
static::$auditingDisabled = $auditingDisabled;
}
}

/**
* Determine whether auditing is enabled.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,29 @@ public function itDisablesAndEnablesAuditingBackAgainViaFacade()
$this->assertSame(1, Audit::count());
}

/**
* @test
*/
public function itDisablesAndEnablesAuditingBackAgainViaWithoutAuditingMethod()
{
// Auditing is enabled by default
$this->assertFalse(Article::$auditingDisabled);

Article::withoutAuditing(function () {
factory(Article::class)->create();
});

$this->assertSame(1, Article::count());
$this->assertSame(0, Audit::count());

$this->assertFalse(Article::$auditingDisabled);

factory(Article::class)->create();

$this->assertSame(2, Article::count());
$this->assertSame(1, Audit::count());
}

/**
* @test
* @return void
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ public function setUp(): void
Relation::morphMap([], false);
}

/**
* @group Auditable::withoutAuditing
* @test
*/
public function itWillRunCallbackWithAuditingDisabled()
{
$this->assertFalse(Article::$auditingDisabled);

$result = Article::withoutAuditing(function () {
$this->assertTrue(Article::$auditingDisabled);
$this->assertFalse(ArticleExcludes::$auditingDisabled);

return 'result';
});

$this->assertFalse(Article::$auditingDisabled);
$this->assertSame('result', $result);
}

/**
* @group Auditable::withoutAuditing
* @test
*/
public function itWillRunCallbackThenRestoreAuditingDisabled()
{
Article::$auditingDisabled = true;

Article::withoutAuditing(function () {
$this->assertTrue(Article::$auditingDisabled);
});

$this->assertTrue(Article::$auditingDisabled);
}

/**
* @group Auditable::isAuditingEnabled
* @test
Expand Down