Skip to content

Commit

Permalink
🚨 audit should not have a factory
Browse files Browse the repository at this point in the history
  • Loading branch information
willpower232 committed Dec 24, 2024
1 parent 23ff144 commit 00395fa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 56 deletions.
32 changes: 23 additions & 9 deletions tests/Unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,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 @@ -456,7 +460,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 @@ -474,7 +483,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 All @@ -491,4 +505,4 @@ class itReturnsDecodedAuditableAttributesArticle extends Article
'title' => Base64Encoder::class,
'content' => LeftRedactor::class,
];
}
}
34 changes: 22 additions & 12 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,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 @@ -1047,7 +1047,7 @@ public function itFailsToTransitionWhenTheAuditAuditableTypeDoesNotMatchTheMorph
'articles' => Article::class,
]);

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

Expand Down Expand Up @@ -1085,7 +1085,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 @@ -1118,7 +1119,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 @@ -1141,8 +1143,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 @@ -1157,9 +1160,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 @@ -1168,19 +1171,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 @@ -1216,8 +1225,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 00395fa

Please sign in to comment.