Skip to content

Commit

Permalink
Update post author name to fall back to username at construct time
Browse files Browse the repository at this point in the history
Cherry picks commit 0dfb841 from #1782
  • Loading branch information
caendesilva committed Jul 6, 2024
1 parent d2c74bf commit 6c887e1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PostAuthor implements Stringable
public function __construct(string $username, ?string $name = null, ?string $website = null)
{
$this->username = $username;
$this->name = $name;
$this->name = $name ?? $this->username;
$this->website = $website;
}

Expand Down Expand Up @@ -87,7 +87,7 @@ public function __toString(): string

public function getName(): string
{
return $this->name ?? $this->username;
return $this->name;
}

/** @param array{username?: string, name?: string, website?: string} $data */
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/tests/Feature/MarkdownPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testConstructorCanCreateANewAuthorInstanceFromUsernameString()

$this->assertInstanceOf(PostAuthor::class, $post->author);
$this->assertSame('John Doe', $post->author->username);
$this->assertNull($post->author->name);
$this->assertSame('John Doe', $post->author->name);
$this->assertNull($post->author->website);
}

Expand Down
7 changes: 7 additions & 0 deletions packages/framework/tests/Unit/PostAuthorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ public function testGetNameHelperReturnsUsernameIfNameIsNotSet()
$this->assertEquals('username', $author->getName());
}

public function testNameIsSetToUsernameIfNameIsNotSet()
{
$author = new PostAuthor('username');

$this->assertEquals('username', $author->name);
}

public function testToStringHelperReturnsTheName()
{
$author = new PostAuthor('username', 'John Doe');
Expand Down

0 comments on commit 6c887e1

Please sign in to comment.