@@ -23,7 +23,7 @@ class AuditingTest extends AuditingTestCase
2323{
2424 use WithFaker;
2525
26-
26+
2727 /**
2828 * @test
2929 */
@@ -576,6 +576,112 @@ public function itWillAuditCustomEventData()
576576 );
577577 }
578578
579+ /**
580+ * @test
581+ * @return void
582+ */
583+ public function itWillAuditSync ()
584+ {
585+ $ firstCategory = factory (Category::class)->create ();
586+ $ secondCategory = factory (Category::class)->create ();
587+ $ article = factory (Article::class)->create ();
588+
589+ $ article ->categories ()->attach ($ firstCategory );
590+
591+ $ no_of_audits_before = Audit::where ('auditable_type ' , Article::class)->count ();
592+ $ categoryBefore = $ article ->categories ()->first ()->getKey ();
593+
594+ $ article ->auditSync ('categories ' , [$ secondCategory ->getKey ()]);
595+
596+ $ no_of_audits_after = Audit::where ('auditable_type ' , Article::class)->count ();
597+ $ categoryAfter = $ article ->categories ()->first ()->getKey ();
598+
599+ $ this ->assertSame ($ firstCategory ->getKey (), $ categoryBefore );
600+ $ this ->assertSame ($ secondCategory ->getKey (), $ categoryAfter );
601+ $ this ->assertNotSame ($ categoryBefore , $ categoryAfter );
602+ $ this ->assertGreaterThan ($ no_of_audits_before , $ no_of_audits_after );
603+ }
604+
605+ /**
606+ * @test
607+ * @return void
608+ */
609+ public function itWillAuditSyncWithoutChanges ()
610+ {
611+ $ firstCategory = factory (Category::class)->create ();
612+ $ article = factory (Article::class)->create ();
613+
614+ $ article ->categories ()->attach ($ firstCategory );
615+
616+ $ no_of_audits_before = Audit::where ('auditable_type ' , Article::class)->count ();
617+ $ categoryBefore = $ article ->categories ()->first ()->getKey ();
618+
619+ $ article ->auditSync ('categories ' , [$ firstCategory ->getKey ()]);
620+
621+ $ no_of_audits_after = Audit::where ('auditable_type ' , Article::class)->count ();
622+ $ categoryAfter = $ article ->categories ()->first ()->getKey ();
623+
624+ $ this ->assertSame ($ firstCategory ->getKey (), $ categoryBefore );
625+ $ this ->assertSame ($ firstCategory ->getKey (), $ categoryAfter );
626+ $ this ->assertSame ($ categoryBefore , $ categoryAfter );
627+ $ this ->assertGreaterThan ($ no_of_audits_before , $ no_of_audits_after );
628+ }
629+
630+ /**
631+ * @test
632+ * @return void
633+ */
634+ public function itWillAuditSyncWhenSkippingEmptyValues ()
635+ {
636+ $ this ->app ['config ' ]->set ('audit.empty_values ' , false );
637+
638+ $ firstCategory = factory (Category::class)->create ();
639+ $ secondCategory = factory (Category::class)->create ();
640+ $ article = factory (Article::class)->create ();
641+
642+ $ article ->categories ()->attach ($ firstCategory );
643+
644+ $ no_of_audits_before = Audit::where ('auditable_type ' , Article::class)->count ();
645+ $ categoryBefore = $ article ->categories ()->first ()->getKey ();
646+
647+ $ article ->auditSync ('categories ' , [$ secondCategory ->getKey ()]);
648+
649+ $ no_of_audits_after = Audit::where ('auditable_type ' , Article::class)->count ();
650+ $ categoryAfter = $ article ->categories ()->first ()->getKey ();
651+
652+ $ this ->assertSame ($ firstCategory ->getKey (), $ categoryBefore );
653+ $ this ->assertSame ($ secondCategory ->getKey (), $ categoryAfter );
654+ $ this ->assertNotSame ($ categoryBefore , $ categoryAfter );
655+ $ this ->assertGreaterThan ($ no_of_audits_before , $ no_of_audits_after );
656+ }
657+
658+ /**
659+ * @test
660+ * @return void
661+ */
662+ public function itWillNotAuditSyncWhenSkippingEmptyValuesAndNoChangesMade ()
663+ {
664+ $ this ->app ['config ' ]->set ('audit.empty_values ' , false );
665+
666+ $ firstCategory = factory (Category::class)->create ();
667+ $ article = factory (Article::class)->create ();
668+
669+ $ article ->categories ()->attach ($ firstCategory );
670+
671+ $ no_of_audits_before = Audit::where ('auditable_type ' , Article::class)->count ();
672+ $ categoryBefore = $ article ->categories ()->first ()->getKey ();
673+
674+ $ article ->auditSync ('categories ' , [$ firstCategory ->getKey ()]);
675+
676+ $ no_of_audits_after = Audit::where ('auditable_type ' , Article::class)->count ();
677+ $ categoryAfter = $ article ->categories ()->first ()->getKey ();
678+
679+ $ this ->assertSame ($ firstCategory ->getKey (), $ categoryBefore );
680+ $ this ->assertSame ($ firstCategory ->getKey (), $ categoryAfter );
681+ $ this ->assertSame ($ categoryBefore , $ categoryAfter );
682+ $ this ->assertSame ($ no_of_audits_before , $ no_of_audits_after );
683+ }
684+
579685 /**
580686 * @test
581687 * @return void
0 commit comments