Skip to content

Commit 5d6ddab

Browse files
committed
Support setting all arguments through the array constructor
1 parent d29744c commit 5d6ddab

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Hyde\Support\Contracts\SerializableContract;
1313

1414
use function is_string;
15+
use function array_merge;
1516
use function array_filter;
1617

1718
/**
@@ -94,7 +95,9 @@ public static function getOrCreate(string|array $data): static
9495
return static::get($data);
9596
}
9697

97-
return Author::create(static::findUsername($data), $data['name'] ?? null, $data['website'] ?? null);
98+
return new static(...array_merge([
99+
'username' => static::findUsername($data),
100+
], $data));
98101
}
99102

100103
/** Get an Author from the config, or create it with the username. */

packages/framework/tests/Unit/PostAuthorTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,65 @@ public function testCanCreateAuthorModelWithFullDetailsFromArgumentUnpacking()
7676
$this->assertSame($data['socials'], $author->socials);
7777
}
7878

79+
public function testCanCreateAuthorModelWithFullDetailsFromArrayUsingGetOrCreate()
80+
{
81+
$data = $this->exampleData();
82+
83+
$author = PostAuthor::getOrCreate($data);
84+
85+
$this->assertSame($data['username'], $author->username);
86+
$this->assertSame($data['name'], $author->name);
87+
$this->assertSame($data['website'], $author->website);
88+
$this->assertSame($data['bio'], $author->bio);
89+
$this->assertSame($data['avatar'], $author->avatar);
90+
$this->assertSame($data['socials'], $author->socials);
91+
}
92+
93+
public function testCanCreateAuthorModelWithSomeDetailsFromArrayUsingGetOrCreate()
94+
{
95+
$data = $this->exampleData();
96+
97+
$author = PostAuthor::getOrCreate([
98+
'username' => $data['username'],
99+
'name' => $data['name'],
100+
'website' => $data['website'],
101+
]);
102+
103+
$this->assertSame($data['username'], $author->username);
104+
$this->assertSame($data['name'], $author->name);
105+
$this->assertSame($data['website'], $author->website);
106+
$this->assertNull($author->bio);
107+
$this->assertNull($author->avatar);
108+
$this->assertEmpty($author->socials);
109+
}
110+
111+
public function testCanCreateAuthorModelWithSomeDetailsFromArrayUsingGetOrCreateWithoutUsername()
112+
{
113+
$data = $this->exampleData();
114+
115+
$author = PostAuthor::getOrCreate([
116+
'name' => $data['name'],
117+
'website' => $data['website'],
118+
]);
119+
120+
$this->assertSame($data['name'], $author->username);
121+
$this->assertSame($data['name'], $author->name);
122+
$this->assertSame($data['website'], $author->website);
123+
}
124+
125+
public function testCanCreateAuthorModelWithSomeDetailsFromArrayUsingGetOrCreateWithoutAnyNames()
126+
{
127+
$data = $this->exampleData();
128+
129+
$author = PostAuthor::getOrCreate([
130+
'website' => $data['website'],
131+
]);
132+
133+
$this->assertSame('Guest', $author->username);
134+
$this->assertSame('Guest', $author->name);
135+
$this->assertSame($data['website'], $author->website);
136+
}
137+
79138
public function testNameIsSetToUsernameIfNoNameIsProvided()
80139
{
81140
$author = new PostAuthor('foo');

0 commit comments

Comments
 (0)