Skip to content

Commit

Permalink
fix Attempt to read property "name" on null error
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix committed Jun 10, 2024
1 parent 2342c34 commit 304352f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Livewire/Table/BaseUrlTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('author', function (Url $url) {
$author = $url->author->name ?? 'Guest';
$author = $url->author->name;

return view('components.table.author', ['name' => $author])
->render();
Expand Down
5 changes: 4 additions & 1 deletion app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ protected function casts(): array
*/
public function author()
{
return $this->belongsTo(User::class, 'user_id');
return $this->belongsTo(User::class, 'user_id')
->withDefault([
'name' => 'Guest Author',
]);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tests/Feature/AuthPage/UrlListGuestUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
#[PHPUnit\Group('link-page')]
class UrlListGuestUserTest extends TestCase
{
private $url;

protected function setUp(): void
{
parent::setUp();

// This must be created, to ensure `$url->author->name` does not raise
// an error.
Url::factory()->create(['user_id' => Url::GUEST_ID]);
$this->url = Url::factory()->create(['user_id' => Url::GUEST_ID]);
}

#[PHPUnit\Test]
Expand All @@ -37,4 +39,13 @@ public function basicUsersCantAccessPage(): void
->get(route('dashboard.allurl.u-guest'));
$response->assertForbidden();
}

#[PHPUnit\Test]
public function adminUserCanAccessEditUrlPage(): void
{
$response = $this->actingAs($this->adminUser())
->get(route('dboard.url.edit.show', $this->url->keyword));

$response->assertOk();
}
}

0 comments on commit 304352f

Please sign in to comment.