diff --git a/tests/Models/Audit.php b/tests/Models/Audit.php deleted file mode 100644 index 88eacac0..00000000 --- a/tests/Models/Audit.php +++ /dev/null @@ -1,10 +0,0 @@ -create(); + + $this->assertTrue(itReturnsDecodedAuditableAttributesArticle::first()->is($model)); // Audit with redacted/encoded attributes - $audit = Audit::factory()->create([ - 'auditable_type' => get_class($article), + $audit = Audit::create([ + 'event' => 'updated', + 'auditable_id' => $model->getKey(), + 'auditable_type' => itReturnsDecodedAuditableAttributesArticle::class, 'old_values' => [ 'title' => 'SG93IFRvIEF1ZGl0IE1vZGVscw==', 'content' => '##A', @@ -489,7 +493,12 @@ public function itReturnsDecodedAuditableAttributes() */ public function itReturnsTags() { - $audit = Audit::factory()->create([ + $model = Article::factory()->create(); + + $audit = Audit::create([ + 'event' => 'updated', + 'auditable_id' => $model->getKey(), + 'auditable_type' => Article::class, 'tags' => 'foo,bar,baz', ]); @@ -508,7 +517,12 @@ public function itReturnsTags() */ public function itReturnsEmptyTags() { - $audit = Audit::factory()->create([ + $model = Article::factory()->create(); + + $audit = Audit::create([ + 'event' => 'updated', + 'auditable_id' => $model->getKey(), + 'auditable_type' => Article::class, 'tags' => null, ]); diff --git a/tests/Unit/AuditableTest.php b/tests/Unit/AuditableTest.php index 8e728dcb..6cb0052e 100644 --- a/tests/Unit/AuditableTest.php +++ b/tests/Unit/AuditableTest.php @@ -12,6 +12,7 @@ use OwenIt\Auditing\Encoders\Base64Encoder; use OwenIt\Auditing\Exceptions\AuditableTransitionException; use OwenIt\Auditing\Exceptions\AuditingException; +use OwenIt\Auditing\Models\Audit; use OwenIt\Auditing\Redactors\LeftRedactor; use OwenIt\Auditing\Redactors\RightRedactor; use OwenIt\Auditing\Resolvers\UrlResolver; @@ -19,7 +20,6 @@ use OwenIt\Auditing\Tests\Models\ApiModel; use OwenIt\Auditing\Tests\Models\Article; use OwenIt\Auditing\Tests\Models\ArticleExcludes; -use OwenIt\Auditing\Tests\Models\Audit; use OwenIt\Auditing\Tests\Models\User; use ReflectionClass; @@ -1034,7 +1034,7 @@ public function itFailsToTransitionWhenTheAuditAuditableTypeDoesNotMatchTheModel $this->expectException(AuditableTransitionException::class); $this->expectExceptionMessage('Expected Auditable type OwenIt\Auditing\Tests\Models\Article, got OwenIt\Auditing\Tests\Models\User instead'); - $audit = Audit::factory()->make([ + $audit = new Audit([ 'auditable_type' => User::class, ]); @@ -1091,7 +1091,7 @@ public function itFailsToTransitionWhenTheAuditAuditableTypeDoesNotMatchTheMorph 'articles' => Article::class, ]); - $audit = Audit::factory()->make([ + $audit = new Audit([ 'auditable_type' => 'users', ]); @@ -1134,7 +1134,8 @@ public function itFailsToTransitionWhenTheAuditAuditableIdTypeDoesNotMatchTheMod $model = Article::factory()->create(); - $audit = Audit::factory()->create([ + $audit = Audit::create([ + 'event' => 'updated', 'auditable_type' => Article::class, 'auditable_id' => (string) $model->id, ]); @@ -1168,7 +1169,8 @@ public function itTransitionsWhenTheAuditAuditableIdTypeDoesNotMatchTheModelIdTy $key = (int) $model->id; } - $audit = Audit::factory()->create([ + $audit = Audit::create([ + 'event' => 'updated', 'auditable_type' => Article::class, 'auditable_id' => $key, ]); @@ -1192,7 +1194,8 @@ public function itFailsToTransitionWhenAnAttributeRedactorIsSet() 'title' => RightRedactor::class, ]; - $audit = Audit::factory()->create([ + $audit = Audit::create([ + 'event' => 'created', 'auditable_id' => $model->getKey(), 'auditable_type' => Article::class, ]); @@ -1209,7 +1212,7 @@ public function itFailsToTransitionWhenTheAuditableAttributeCompatibilityIsNotMe { $model = Article::factory()->create(); - $incompatibleAudit = Audit::factory()->create([ + $incompatibleAudit = Audit::create([ 'event' => 'created', 'auditable_id' => $model->getKey(), 'auditable_type' => Article::class, @@ -1220,11 +1223,13 @@ public function itFailsToTransitionWhenTheAuditableAttributeCompatibilityIsNotMe ], ]); + $exceptionWasThrown = false; + try { $model->transitionTo($incompatibleAudit); } catch (AuditableTransitionException $e) { $this->assertSame( - 'Incompatibility between [OwenIt\Auditing\Tests\Models\Article:1] and [OwenIt\Auditing\Tests\Models\Audit:3]', + 'Incompatibility between [OwenIt\Auditing\Tests\Models\Article:1] and [OwenIt\Auditing\Models\Audit:2]', $e->getMessage() ); @@ -1232,7 +1237,11 @@ public function itFailsToTransitionWhenTheAuditableAttributeCompatibilityIsNotMe 'subject', 'text', ], $e->getIncompatibilities(), true); + + $exceptionWasThrown = true; } + + $this->assertTrue($exceptionWasThrown); } /** @@ -1263,7 +1272,8 @@ public function itTransitionsToAnotherModelState( $auditableType = $morphMap ? 'articles' : Article::class; $audits = $models->map(function (Article $model) use ($auditableType, $oldValues, $newValues) { - return Audit::factory()->create([ + return Audit::create([ + 'event' => 'updated', 'auditable_id' => $model->getKey(), 'auditable_type' => $auditableType, 'old_values' => $oldValues, diff --git a/tests/database/factories/AuditFactory.php b/tests/database/factories/AuditFactory.php deleted file mode 100644 index 4fea7e8c..00000000 --- a/tests/database/factories/AuditFactory.php +++ /dev/null @@ -1,39 +0,0 @@ - function () { - return User::factory()->create()->id; - }, - $morphPrefix.'_type' => User::class, - 'event' => 'updated', - 'auditable_id' => function () { - return Article::factory()->create()->id; - }, - 'auditable_type' => Article::class, - 'old_values' => [], - 'new_values' => [], - 'url' => fake()->url, - 'ip_address' => fake()->ipv4, - 'user_agent' => fake()->userAgent, - 'tags' => implode(',', fake()->words(4)), - ]; - } - - public function modelName() - { - return \OwenIt\Auditing\Tests\Models\Audit::class; - } -}