Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚨 first is nullable #985

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions tests/Functional/AuditingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public function itWillAuditTheRetrievedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

$this->assertEmpty($audit->new_values);
Expand All @@ -150,6 +152,8 @@ public function itWillAuditTheCreatedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

self::Assert()::assertArraySubset([
Expand Down Expand Up @@ -187,6 +191,8 @@ public function itWillAuditTheUpdatedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

self::Assert()::assertArraySubset([
'content' => 'N/A',
'published_at' => null,
Expand Down Expand Up @@ -220,6 +226,8 @@ public function itWillAuditTheDeletedEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

self::Assert()::assertArraySubset([
'title' => 'How To Audit Eloquent Models',
'content' => 'N/A',
Expand Down Expand Up @@ -252,6 +260,8 @@ public function itWillAuditTheRestoredEvent()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

self::Assert()::assertArraySubset([
Expand Down Expand Up @@ -359,6 +369,8 @@ public function itWillAuditUsingTheDefaultDriver()

$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->old_values);

self::Assert()::assertArraySubset([
Expand Down Expand Up @@ -476,10 +488,12 @@ public function itHandlesJsonColumnsCorrectly()
$article->config = ['articleIsGood' => false, 'authorsJob' => 'vampire'];
$article->save();

/** @var Audit $audit */
$audit = $article->audits()->skip(1)->first();
$this->assertSame(false, $audit->getModified()['config']['new']['articleIsGood']);
$this->assertSame(true, $audit->getModified()['config']['old']['articleIsGood']);

$this->assertNotNull($audit);

$this->assertFalse($audit->getModified()['config']['new']['articleIsGood']);
$this->assertTrue($audit->getModified()['config']['old']['articleIsGood']);
}

/**
Expand All @@ -493,8 +507,10 @@ public function canAddAdditionalResolver()

$article = factory(Article::class)->create();

$this->assertTrue(true);
$audit = $article->audits()->first();

$this->assertNotNull($audit);

$this->assertSame(1, (int)$audit->tenant_id);
}

Expand All @@ -510,6 +526,9 @@ public function canDisableResolver()
$article = factory(Article::class)->create();

$audit = $article->audits()->first();

$this->assertNotNull($audit);

$this->assertEmpty($audit->ip_address);
}

Expand Down
27 changes: 23 additions & 4 deletions tests/Unit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function itResolvesAuditData()
'published_at' => $now,
]);

/** @var Audit $audit */
$audit = $article->audits()->first();
$this->assertNotNull($audit);

$resolvedData = $audit->resolveData();
$this->assertCount(15, $resolvedData);

Expand Down Expand Up @@ -79,6 +80,8 @@ public function itResolvesAuditDataIncludingUserAttributes()

$audit = $article->audits()->first();

$this->assertNotNull($audit);

$this->assertCount(21, $resolvedData = $audit->resolveData());

self::Assert()::assertArraySubset([
Expand Down Expand Up @@ -127,6 +130,8 @@ public function itReturnsTheAppropriateAuditableDataValues()
'published_at' => Carbon::now(),
])->audits()->first();

$this->assertNotNull($audit);

// Resolve data, making it available to the getDataValue() method
$this->assertCount(21, $audit->resolveData());

Expand Down Expand Up @@ -179,6 +184,8 @@ public function itReturnsTheAppropriateAuditableDataValuesWithCustomCastValueObj

$lastAudit = $article->audits()->skip(1)->first();

$this->assertNotNull($lastAudit);

$this->assertEquals(new Money('24.68', 'USD'), $lastAudit->getModified()['price']['new']);
$this->assertEquals(new Money('12.45', 'USD'), $lastAudit->getModified()['price']['old']);
}
Expand All @@ -191,6 +198,8 @@ public function itReturnsAuditMetadataAsArray()
{
$audit = factory(Article::class)->create()->audits()->first();

$this->assertNotNull($audit);

$this->assertCount(10, $metadata = $audit->getMetadata());

self::Assert()::assertArraySubset([
Expand All @@ -217,6 +226,8 @@ public function itReturnsProperCommandLineInUrlAuditMetadata()
{
$audit = factory(Article::class)->create()->audits()->first();

$this->assertNotNull($audit);

self::Assert()::assertEquals($audit->getMetadata()['audit_url'], 'vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver');
}

Expand All @@ -235,9 +246,10 @@ public function itReturnsAuditMetadataIncludingUserAttributesAsArray()

$this->actingAs($user);

/** @var Audit $audit */
$audit = factory(Article::class)->create()->audits()->first();

$this->assertNotNull($audit);

$this->assertCount(16, $metadata = $audit->getMetadata());

self::Assert()::assertArraySubset([
Expand Down Expand Up @@ -268,6 +280,8 @@ public function itReturnsAuditMetadataAsJsonString()
{
$audit = factory(Article::class)->create()->audits()->first();

$this->assertNotNull($audit);

$metadata = $audit->getMetadata(true, JSON_PRETTY_PRINT);

$created_at = $audit->getSerializedDate($audit->created_at);
Expand Down Expand Up @@ -305,6 +319,8 @@ public function itReturnsAuditMetadataIncludingUserAttributesAsJsonString()

$audit = factory(Article::class)->create()->audits()->first();

$this->assertNotNull($audit);

$metadata = $audit->getMetadata(true, JSON_PRETTY_PRINT);

$created_at = $audit->getSerializedDate($audit->created_at);
Expand Down Expand Up @@ -348,6 +364,8 @@ public function itReturnsAuditableModifiedAttributesAsArray()
'published_at' => $now,
])->audits()->first();

$this->assertNotNull($audit);

$this->assertCount(5, $modified = $audit->getModified());

self::Assert()::assertArraySubset([
Expand Down Expand Up @@ -377,14 +395,15 @@ public function itReturnsAuditableModifiedAttributesAsJsonString()
{
$now = Carbon::now()->second(0)->microsecond(0);

/** @var Audit $audit */
$audit = factory(Article::class)->create([
'title' => 'How To Audit Eloquent Models',
'content' => 'First step: install the laravel-auditing package.',
'reviewed' => 1,
'published_at' => $now,
])->audits()->first();

$this->assertNotNull($audit);

$modified = $audit->getModified(true, JSON_PRETTY_PRINT);

$serializedDate = $audit->getSerializedDate($now);
Expand Down Expand Up @@ -491,4 +510,4 @@ class itReturnsDecodedAuditableAttributesArticle extends Article
'title' => Base64Encoder::class,
'content' => LeftRedactor::class,
];
}
}
10 changes: 8 additions & 2 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,10 @@ public function itExcludesAttributesFromExclude()
$model->reviewed = 1;
$model->save();

/** @var Audit $audit */
$audit = Audit::all()->first();
$audit = Audit::first();

$this->assertNotNull($audit);

$this->assertArrayNotHasKey('title', $audit->getModified());
}

Expand Down Expand Up @@ -1022,6 +1024,8 @@ public function itWorksOnTimesRestoredCorrectly()

$model = Article::first();

$this->assertNotNull($model);

$this->assertEquals($model->published_at, $originalStart);

$model->published_at = new Carbon('2022-01-01 12:30:00');
Expand Down Expand Up @@ -1067,6 +1071,8 @@ public function itFailsToTransitionWhenTheAuditAuditableIdDoesNotMatchTheModelId

$firstModel = factory(Article::class)->create();
$firstAudit = $firstModel->audits()->first();
$this->assertNotNull($firstAudit);

$firstAudit->auditable_id = $firstModel->id;

$secondModel = factory(Article::class)->create();
Expand Down
Loading