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 5d43c4a commit d59d7fb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 64 deletions.
10 changes: 0 additions & 10 deletions tests/Models/Audit.php

This file was deleted.

26 changes: 20 additions & 6 deletions tests/Unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use DateTimeInterface;
use Illuminate\Testing\Assert;
use OwenIt\Auditing\Encoders\Base64Encoder;
use OwenIt\Auditing\Models\Audit;
use OwenIt\Auditing\Redactors\LeftRedactor;
use OwenIt\Auditing\Resolvers\UrlResolver;
use OwenIt\Auditing\Tests\AuditingTestCase;
use OwenIt\Auditing\Tests\Models\Article;
use OwenIt\Auditing\Tests\Models\Audit;
use OwenIt\Auditing\Tests\Models\Money;
use OwenIt\Auditing\Tests\Models\User;

Expand Down Expand Up @@ -447,11 +447,15 @@ public function itReturnsAuditableModifiedAttributesAsJsonString()
*/
public function itReturnsDecodedAuditableAttributes()
{
$article = new itReturnsDecodedAuditableAttributesArticle();
$model = Article::factory()->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',
Expand Down Expand Up @@ -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',
]);

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

Expand Down
28 changes: 19 additions & 9 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
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;
use OwenIt\Auditing\Tests\AuditingTestCase;
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;

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

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

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

Expand Down Expand Up @@ -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,
]);
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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,
]);
Expand All @@ -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,
Expand All @@ -1220,19 +1223,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\Tests\Models\Audit:3]',
'Incompatibility between [OwenIt\Auditing\Tests\Models\Article:1] and [OwenIt\Auditing\Models\Audit:2]',
$e->getMessage()
);

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

$exceptionWasThrown = true;
}

$this->assertTrue($exceptionWasThrown);
}

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

This file was deleted.

0 comments on commit d59d7fb

Please sign in to comment.