diff --git a/tests/Unit/AuditTest.php b/tests/Unit/AuditTest.php index b60f891e..203863c3 100644 --- a/tests/Unit/AuditTest.php +++ b/tests/Unit/AuditTest.php @@ -435,14 +435,18 @@ public function itReturnsAuditableModifiedAttributesAsJsonString() */ public function itReturnsDecodedAuditableAttributes() { - $article = new itReturnsDecodedAuditableAttributesArticle(); + $model = factory(Article::class)->create(); + + $this->assertTrue(itReturnsDecodedAuditableAttributesArticle::first()->is($model)); // Audit with redacted/encoded attributes - $audit = factory(Audit::class)->create([ - 'auditable_type' => get_class($article), - 'old_values' => [ - 'title' => 'SG93IFRvIEF1ZGl0IE1vZGVscw==', - 'content' => '##A', + $audit = Audit::create([ + 'event' => 'updated', + 'auditable_id' => $model->getKey(), + 'auditable_type' => itReturnsDecodedAuditableAttributesArticle::class, + 'old_values' => [ + 'title' => 'SG93IFRvIEF1ZGl0IE1vZGVscw==', + 'content' => '##A', 'reviewed' => 0, ], 'new_values' => [ @@ -476,7 +480,12 @@ public function itReturnsDecodedAuditableAttributes() */ public function itReturnsTags() { - $audit = factory(Audit::class)->create([ + $model = factory(Article::class)->create(); + + $audit = Audit::create([ + 'event' => 'updated', + 'auditable_id' => $model->getKey(), + 'auditable_type' => Article::class, 'tags' => 'foo,bar,baz', ]); @@ -494,7 +503,12 @@ public function itReturnsTags() */ public function itReturnsEmptyTags() { - $audit = factory(Audit::class)->create([ + $model = factory(Article::class)->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 13330b6f..611c0194 100644 --- a/tests/Unit/AuditableTest.php +++ b/tests/Unit/AuditableTest.php @@ -996,7 +996,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 = factory(Audit::class)->make([ + $audit = new Audit([ 'auditable_type' => User::class, ]); @@ -1052,7 +1052,7 @@ public function itFailsToTransitionWhenTheAuditAuditableTypeDoesNotMatchTheMorph 'articles' => Article::class, ]); - $audit = factory(Audit::class)->make([ + $audit = new Audit([ 'auditable_type' => 'users', ]); @@ -1092,7 +1092,8 @@ public function itFailsToTransitionWhenTheAuditAuditableIdTypeDoesNotMatchTheMod $model = factory(Article::class)->create(); - $audit = factory(Audit::class)->create([ + $audit = Audit::create([ + 'event' => 'updated', 'auditable_type' => Article::class, 'auditable_id' => (string)$model->id, ]); @@ -1125,7 +1126,8 @@ public function itTransitionsWhenTheAuditAuditableIdTypeDoesNotMatchTheModelIdTy $key = (int)$model->id; } - $audit = factory(Audit::class)->create([ + $audit = Audit::create([ + 'event' => 'updated', 'auditable_type' => Article::class, 'auditable_id' => $key, ]); @@ -1148,8 +1150,9 @@ public function itFailsToTransitionWhenAnAttributeRedactorIsSet() 'title' => RightRedactor::class, ]; - $audit = factory(Audit::class)->create([ - 'auditable_id' => $model->getKey(), + $audit = Audit::create([ + 'event' => 'created', + 'auditable_id' => $model->getKey(), 'auditable_type' => Article::class, ]); @@ -1164,9 +1167,9 @@ public function itFailsToTransitionWhenTheAuditableAttributeCompatibilityIsNotMe { $model = factory(Article::class)->create(); - $incompatibleAudit = factory(Audit::class)->create([ - 'event' => 'created', - 'auditable_id' => $model->getKey(), + $incompatibleAudit = Audit::create([ + 'event' => 'created', + 'auditable_id' => $model->getKey(), 'auditable_type' => Article::class, 'old_values' => [], 'new_values' => [ @@ -1175,11 +1178,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\Models\Audit:3]', + 'Incompatibility between [OwenIt\Auditing\Tests\Models\Article:1] and [OwenIt\Auditing\Models\Audit:2]', $e->getMessage() ); @@ -1187,7 +1192,11 @@ public function itFailsToTransitionWhenTheAuditableAttributeCompatibilityIsNotMe 'subject', 'text', ], $e->getIncompatibilities(), true); + + $exceptionWasThrown = true; } + + $this->assertTrue($exceptionWasThrown); } /** @@ -1223,8 +1232,9 @@ public function itTransitionsToAnotherModelState( $auditableType = $morphMap ? 'articles' : Article::class; $audits = $models->map(function (Article $model) use ($auditableType, $oldValues, $newValues) { - return factory(Audit::class)->create([ - 'auditable_id' => $model->getKey(), + return Audit::create([ + 'event' => 'updated', + 'auditable_id' => $model->getKey(), 'auditable_type' => $auditableType, 'old_values' => $oldValues, 'new_values' => $newValues, diff --git a/tests/database/factories/AuditFactory.php b/tests/database/factories/AuditFactory.php deleted file mode 100644 index 1725c5fa..00000000 --- a/tests/database/factories/AuditFactory.php +++ /dev/null @@ -1,35 +0,0 @@ -define(Audit::class, function (Faker $faker) { - $morphPrefix = Config::get('audit.user.morph_prefix', 'user'); - - return [ - $morphPrefix . '_id' => function () { - return factory(User::class)->create()->id; - }, - $morphPrefix . '_type' => User::class, - 'event' => 'updated', - 'auditable_id' => function () { - return factory(Article::class)->create()->id; - }, - 'auditable_type' => Article::class, - 'old_values' => [], - 'new_values' => [], - 'url' => $faker->url, - 'ip_address' => $faker->ipv4, - 'user_agent' => $faker->userAgent, - 'tags' => implode(',', $faker->words(4)), - ]; -});