Skip to content

Commit fb12b6b

Browse files
committed
Throw an InvalidArgumentException when missing the array key
Enforces the new state. Consider adding a BadConfigurationException?
1 parent 26be37d commit fb12b6b

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/framework/src/Foundation/Concerns/HasKernelData.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hyde\Foundation\Concerns;
66

77
use Hyde\Facades\Config;
8+
use InvalidArgumentException;
89
use Illuminate\Support\Collection;
910
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
1011

@@ -50,6 +51,10 @@ public function authors(): Collection
5051
protected function parseConfigurationAuthors(Collection $authors): Collection
5152
{
5253
return $authors->mapWithKeys(function (PostAuthor $author, string $username): array {
54+
if (! $username) {
55+
throw new InvalidArgumentException('Author username cannot be empty. Did you forget to set the author\'s array key?');
56+
}
57+
5358
return [$username => tap($author, fn (PostAuthor $author) => $author->username = $username)];
5459
});
5560
}

packages/framework/tests/Feature/HydeKernelTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Hyde\Facades\Author;
88
use Hyde\Facades\Features;
9+
use InvalidArgumentException;
910
use Hyde\Foundation\Facades\Pages;
1011
use Illuminate\Support\Collection;
1112
use Hyde\Foundation\Facades\Routes;
@@ -591,6 +592,16 @@ public function testAuthorsUseTheConfigArrayKeyAsTheUsername()
591592
],
592593
], Hyde::authors()->toArray());
593594
}
595+
596+
public function testAuthorsThrowsExceptionWhenUsernameIsNotSet()
597+
{
598+
$this->expectException(InvalidArgumentException::class);
599+
$this->expectExceptionMessage('Author username cannot be empty. Did you forget to set the author\'s array key?');
600+
601+
Config::set('hyde.authors', ['' => Author::create('foo')]);
602+
603+
Hyde::authors();
604+
}
594605
}
595606

596607
class CallableClass

0 commit comments

Comments
 (0)