Skip to content

Commit 942ea16

Browse files
Merge pull request #10 from stefanzweifel/input-validation
Validate Input Options
2 parents b3dc485 + 5a5f484 commit 942ea16

File tree

4 files changed

+156
-141
lines changed

4 files changed

+156
-141
lines changed

app/Commands/UpdateCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use LaravelZero\Framework\Commands\Command;
99
use League\CommonMark\Output\RenderedContentInterface;
1010
use Throwable;
11+
use Webmozart\Assert\Assert;
1112

1213
class UpdateCommand extends Command
1314
{
@@ -26,6 +27,8 @@ class UpdateCommand extends Command
2627
*/
2728
public function handle(AddReleaseNotesToChangelog $addReleaseNotesToChangelog): void
2829
{
30+
$this->validateOptions();
31+
2932
$releaseNotes = $this->option('release-notes');
3033
$latestVersion = $this->option('latest-version');
3134
$releaseDate = $this->option('release-date');
@@ -61,4 +64,10 @@ protected function writeChangelogToFile(bool $shouldWriteToFile, string $pathToC
6164
file_put_contents($pathToChangelog, $updatedMarkdown->getContent());
6265
}
6366
}
67+
68+
private function validateOptions(): void
69+
{
70+
Assert::stringNotEmpty($this->option('release-notes'), 'No release-notes option provided. Abort.');
71+
Assert::stringNotEmpty($this->option('latest-version'), 'No latest-version option provided. Abort.');
72+
}
6473
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"require": {
1717
"php": "^8.0",
1818
"league/commonmark": "^2.0",
19+
"webmozart/assert": "^1.10",
1920
"wnx/commonmark-markdown-renderer": "^1.0"
2021
},
2122
"require-dev": {

composer.lock

Lines changed: 138 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Feature/UpdateCommandTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
});
2525

2626
it('throws error if release-notes are missing', function () {
27-
$this->markTestIncomplete('TODO: Add Validation to Update Command');
27+
$this->artisan('update', [])
28+
->assertExitCode(1);
29+
})->throws(InvalidArgumentException::class, 'No release-notes option provided. Abort.');
2830

29-
$this->artisan('update')
31+
it('throws error if latest-version is missing', function () {
32+
$this->artisan('update', [
33+
'--release-notes' => '::release-notes::',
34+
])
3035
->assertExitCode(1);
31-
});
36+
})->throws(InvalidArgumentException::class, 'No latest-version option provided. Abort.');
3237

3338
it('uses current date for release date if no option is provieded', function () {
3439
$expectedChangelog = file_get_contents(__DIR__ . '/../Stubs/expected-changelog.md');

0 commit comments

Comments
 (0)