Skip to content

Commit

Permalink
🚨 audit should not have a factory (#986 tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
willpower232 authored Dec 24, 2024
1 parent 4581ec7 commit 2d5f2b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
30 changes: 22 additions & 8 deletions tests/Unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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',
]);

Expand All @@ -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,
]);

Expand Down
34 changes: 22 additions & 12 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand Down Expand Up @@ -1052,7 +1052,7 @@ public function itFailsToTransitionWhenTheAuditAuditableTypeDoesNotMatchTheMorph
'articles' => Article::class,
]);

$audit = factory(Audit::class)->make([
$audit = new Audit([
'auditable_type' => 'users',
]);

Expand Down Expand Up @@ -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,
]);
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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,
]);

Expand All @@ -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' => [
Expand All @@ -1175,19 +1178,25 @@ 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()
);

self::Assert()::assertArraySubset([
'subject',
'text',
], $e->getIncompatibilities(), true);

$exceptionWasThrown = true;
}

$this->assertTrue($exceptionWasThrown);
}

/**
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 0 additions & 35 deletions tests/database/factories/AuditFactory.php

This file was deleted.

0 comments on commit 2d5f2b1

Please sign in to comment.