Skip to content

Commit fe0e33a

Browse files
authored
Add withoutAuditing() for callbacks (#917)
Similar to Laravel's Model::withoutEvents() method
1 parent 0ec2762 commit fe0e33a

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/Auditable.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,26 @@ public static function enableAuditing()
536536
static::$auditingDisabled = false;
537537
}
538538

539+
/**
540+
* Execute a callback while auditing is disabled.
541+
*
542+
* @param callable $callback
543+
*
544+
* @return mixed
545+
*/
546+
public static function withoutAuditing(callable $callback)
547+
{
548+
$auditingDisabled = static::$auditingDisabled;
549+
550+
static::disableAuditing();
551+
552+
try {
553+
return $callback();
554+
} finally {
555+
static::$auditingDisabled = $auditingDisabled;
556+
}
557+
}
558+
539559
/**
540560
* Determine whether auditing is enabled.
541561
*

tests/Functional/AuditingTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,29 @@ public function itDisablesAndEnablesAuditingBackAgainViaFacade()
433433
$this->assertSame(1, Audit::count());
434434
}
435435

436+
/**
437+
* @test
438+
*/
439+
public function itDisablesAndEnablesAuditingBackAgainViaWithoutAuditingMethod()
440+
{
441+
// Auditing is enabled by default
442+
$this->assertFalse(Article::$auditingDisabled);
443+
444+
Article::withoutAuditing(function () {
445+
factory(Article::class)->create();
446+
});
447+
448+
$this->assertSame(1, Article::count());
449+
$this->assertSame(0, Audit::count());
450+
451+
$this->assertFalse(Article::$auditingDisabled);
452+
453+
factory(Article::class)->create();
454+
455+
$this->assertSame(2, Article::count());
456+
$this->assertSame(1, Audit::count());
457+
}
458+
436459
/**
437460
* @test
438461
* @return void

tests/Unit/AuditableTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,40 @@ public function setUp(): void
3636
Relation::morphMap([], false);
3737
}
3838

39+
/**
40+
* @group Auditable::withoutAuditing
41+
* @test
42+
*/
43+
public function itWillRunCallbackWithAuditingDisabled()
44+
{
45+
$this->assertFalse(Article::$auditingDisabled);
46+
47+
$result = Article::withoutAuditing(function () {
48+
$this->assertTrue(Article::$auditingDisabled);
49+
$this->assertFalse(ArticleExcludes::$auditingDisabled);
50+
51+
return 'result';
52+
});
53+
54+
$this->assertFalse(Article::$auditingDisabled);
55+
$this->assertSame('result', $result);
56+
}
57+
58+
/**
59+
* @group Auditable::withoutAuditing
60+
* @test
61+
*/
62+
public function itWillRunCallbackThenRestoreAuditingDisabled()
63+
{
64+
Article::$auditingDisabled = true;
65+
66+
Article::withoutAuditing(function () {
67+
$this->assertTrue(Article::$auditingDisabled);
68+
});
69+
70+
$this->assertTrue(Article::$auditingDisabled);
71+
}
72+
3973
/**
4074
* @group Auditable::isAuditingEnabled
4175
* @test

0 commit comments

Comments
 (0)