diff --git a/src/Drivers/Database.php b/src/Drivers/Database.php index 3aa7a3f1..e92d16d2 100644 --- a/src/Drivers/Database.php +++ b/src/Drivers/Database.php @@ -14,9 +14,7 @@ class Database implements AuditDriver */ public function audit(Auditable $model): ?Audit { - $implementation = Config::get('audit.implementation', \OwenIt\Auditing\Models\Audit::class); - - return call_user_func([$implementation, 'create'], $model->toAudit()); + return call_user_func([get_class($model->audits()->getModel()), 'create'], $model->toAudit()); } /** diff --git a/tests/Functional/AuditingTest.php b/tests/Functional/AuditingTest.php index dec1e70b..6f55abc2 100644 --- a/tests/Functional/AuditingTest.php +++ b/tests/Functional/AuditingTest.php @@ -9,12 +9,14 @@ use Illuminate\Support\Facades\Event; use InvalidArgumentException; use OwenIt\Auditing\Events\AuditCustom; +use OwenIt\Auditing\Events\Audited; use OwenIt\Auditing\Events\Auditing; use OwenIt\Auditing\Exceptions\AuditingException; use OwenIt\Auditing\Models\Audit; use OwenIt\Auditing\Tests\AuditingTestCase; use OwenIt\Auditing\Tests\fixtures\TenantResolver; use OwenIt\Auditing\Tests\Models\Article; +use OwenIt\Auditing\Tests\Models\ArticleCustomAuditMorph; use OwenIt\Auditing\Tests\Models\ArticleExcludes; use OwenIt\Auditing\Tests\Models\Category; use OwenIt\Auditing\Tests\Models\User; @@ -814,4 +816,25 @@ public function canAuditAnyCustomEvent() 'old_values' => '{"customExample":"Anakin Skywalker"}' ]); } + + /** + * @test + * @return void + */ + public function canAuditCustomAuditModelImplementation() + { + $audit = null; + Event::listen(Audited::class, function ($event) use (&$audit) { + $audit = $event->audit; + }); + + $article = new ArticleCustomAuditMorph(); + $article->title = $this->faker->unique()->sentence; + $article->content = $this->faker->unique()->paragraph(6); + $article->reviewed = 0; + $article->save(); + + $this->assertNotEmpty($audit); + $this->assertSame(get_class($audit), \OwenIt\Auditing\Tests\Models\CustomAudit::class); + } } diff --git a/tests/Models/ArticleCustomAuditMorph.php b/tests/Models/ArticleCustomAuditMorph.php new file mode 100644 index 00000000..2669d6e0 --- /dev/null +++ b/tests/Models/ArticleCustomAuditMorph.php @@ -0,0 +1,18 @@ + + */ + public function audits(): MorphMany + { + return $this->morphMany(CustomAudit::class, 'auditable'); + } +} diff --git a/tests/Models/ArticleExcludes.php b/tests/Models/ArticleExcludes.php index 0f616a2f..a1b0e091 100644 --- a/tests/Models/ArticleExcludes.php +++ b/tests/Models/ArticleExcludes.php @@ -2,62 +2,9 @@ namespace OwenIt\Auditing\Tests\Models; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\SoftDeletes; -use OwenIt\Auditing\Contracts\Auditable; - -class ArticleExcludes extends Model implements Auditable +class ArticleExcludes extends Article { - use \OwenIt\Auditing\Auditable; - use SoftDeletes; - protected $table = 'articles'; - /** - * {@inheritdoc} - */ - protected $casts = [ - 'reviewed' => 'bool', - 'config' => 'json' - ]; - public $auditExclude = ['title']; - - /** - * {@inheritdoc} - */ - protected $dates = [ - 'published_at', - ]; - - /** - * {@inheritdoc} - */ - protected $fillable = [ - 'title', - 'content', - 'published_at', - 'reviewed', - ]; - - - public function __construct(array $attributes = []) - { - if (class_exists(\Illuminate\Database\Eloquent\Casts\AsArrayObject::class)) { - $this->casts['config'] = \Illuminate\Database\Eloquent\Casts\AsArrayObject::class; - } - parent::__construct($attributes); - } - - /** - * Uppercase Title accessor. - * - * @param string $value - * - * @return string - */ - public function getTitleAttribute(string $value): string - { - return strtoupper($value); - } } diff --git a/tests/Models/CustomAudit.php b/tests/Models/CustomAudit.php new file mode 100644 index 00000000..4019d1b2 --- /dev/null +++ b/tests/Models/CustomAudit.php @@ -0,0 +1,10 @@ +