File tree Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -536,6 +536,26 @@ public static function enableAuditing()
536
536
static ::$ auditingDisabled = false ;
537
537
}
538
538
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
+
539
559
/**
540
560
* Determine whether auditing is enabled.
541
561
*
Original file line number Diff line number Diff line change @@ -433,6 +433,29 @@ public function itDisablesAndEnablesAuditingBackAgainViaFacade()
433
433
$ this ->assertSame (1 , Audit::count ());
434
434
}
435
435
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
+
436
459
/**
437
460
* @test
438
461
* @return void
Original file line number Diff line number Diff line change @@ -36,6 +36,40 @@ public function setUp(): void
36
36
Relation::morphMap ([], false );
37
37
}
38
38
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
+
39
73
/**
40
74
* @group Auditable::isAuditingEnabled
41
75
* @test
You can’t perform that action at this time.
0 commit comments