Skip to content

Commit 6c887e1

Browse files
committed
Update post author name to fall back to username at construct time
Cherry picks commit 0dfb841 from #1782
1 parent d2c74bf commit 6c887e1

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
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
@@ -48,7 +48,7 @@ class PostAuthor implements Stringable
4848
public function __construct(string $username, ?string $name = null, ?string $website = null)
4949
{
5050
$this->username = $username;
51-
$this->name = $name;
51+
$this->name = $name ?? $this->username;
5252
$this->website = $website;
5353
}
5454

@@ -87,7 +87,7 @@ public function __toString(): string
8787

8888
public function getName(): string
8989
{
90-
return $this->name ?? $this->username;
90+
return $this->name;
9191
}
9292

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

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ public function testGetNameHelperReturnsUsernameIfNameIsNotSet()
134134
$this->assertEquals('username', $author->getName());
135135
}
136136

137+
public function testNameIsSetToUsernameIfNameIsNotSet()
138+
{
139+
$author = new PostAuthor('username');
140+
141+
$this->assertEquals('username', $author->name);
142+
}
143+
137144
public function testToStringHelperReturnsTheName()
138145
{
139146
$author = new PostAuthor('username', 'John Doe');

0 commit comments

Comments
 (0)