Skip to content

Commit 0dfb841

Browse files
committed
Update post author name to fall back to username at construct time
1 parent 4d5a936 commit 0dfb841

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

packages/framework/src/Framework/Features/Blogging/Models/PostAuthor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PostAuthor implements Stringable, SerializableContract
5353
public function __construct(string $username, ?string $name = null, ?string $website = null)
5454
{
5555
$this->username = $username;
56-
$this->name = $name;
56+
$this->name = $name ?? $username;
5757
$this->website = $website;
5858
}
5959

@@ -99,7 +99,7 @@ public function toArray(): array
9999

100100
public function getName(): string
101101
{
102-
return $this->name ?? $this->username;
102+
return $this->name;
103103
}
104104

105105
/** @param array{username?: string, name?: string, website?: string} $data */

packages/framework/tests/Feature/HydeKernelTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ public function testAuthorsUseTheConfigArrayKey()
586586
$this->assertSame([
587587
'foo' => [
588588
'username' => 'bar',
589+
'name' => 'bar',
589590
],
590591
], Hyde::authors()->toArray());
591592
}

packages/framework/tests/Feature/MarkdownPostTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testConstructorCanCreateANewAuthorInstanceFromUsernameString()
2525

2626
$this->assertInstanceOf(PostAuthor::class, $post->author);
2727
$this->assertSame('John Doe', $post->author->username);
28-
$this->assertNull($post->author->name);
28+
$this->assertSame('John Doe', $post->author->name);
2929
$this->assertNull($post->author->website);
3030
}
3131

packages/framework/tests/Unit/PostAuthorTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public function testCanCreateAuthorModelWithDetails()
4141
$this->assertSame('https://example.com', $author->website);
4242
}
4343

44+
public function testNameIsSetToUsernameIfNoNameIsProvided()
45+
{
46+
$author = new PostAuthor('foo');
47+
48+
$this->assertSame('foo', $author->name);
49+
}
50+
4451
public function testCreateMethodCreatesNewAuthorModel()
4552
{
4653
$author = Author::create('foo');
@@ -218,6 +225,6 @@ public function testEmptyFieldsAreRemovedFromSerializedModel()
218225
{
219226
$author = new PostAuthor('username', null, null);
220227

221-
$this->assertSame('{"username":"username"}', $author->toJson());
228+
$this->assertSame('{"username":"username","name":"username"}', $author->toJson());
222229
}
223230
}

0 commit comments

Comments
 (0)