Skip to content

Commit 69125aa

Browse files
Add support for assertFailedWithValidationError() (#1)
1 parent ad4e6e7 commit 69125aa

18 files changed

+397
-301
lines changed

README.md

+270-261
Large diffs are not rendered by default.

composer.json

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
"pestphp/pest-plugin": true
5252
}
5353
},
54+
"extra": {
55+
"laravel": {
56+
"providers": [
57+
"PerryvanderMeer\\LaravelConsoleValidator\\LaravelConsoleValidatorServiceProvider"
58+
]
59+
}
60+
},
5461
"minimum-stability": "dev",
5562
"prefer-stable": true
5663
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PerryvanderMeer\LaravelConsoleValidator;
6+
7+
use Illuminate\Support\ServiceProvider;
8+
use Illuminate\Testing\PendingCommand;
9+
use PerryvanderMeer\LaravelConsoleValidator\Testing\PendingCommandMixin;
10+
11+
final class LaravelConsoleValidatorServiceProvider extends ServiceProvider
12+
{
13+
/**
14+
* Bootstrap any application services.
15+
*/
16+
public function boot() : void
17+
{
18+
// Mixin with PendingCommand is only available
19+
// from laravel/framework version 11.9.0...
20+
if (method_exists(PendingCommand::class, 'mixin'))
21+
{
22+
PendingCommand::mixin(new PendingCommandMixin);
23+
}
24+
}
25+
}

src/Testing/PendingCommandMixin.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PerryvanderMeer\LaravelConsoleValidator\Testing;
6+
7+
use Closure;
8+
use Symfony\Component\Console\Command\Command;
9+
10+
/** @mixin \Illuminate\Testing\PendingCommand */
11+
final class PendingCommandMixin
12+
{
13+
/**
14+
* Assert that the current command fails with a validation error.
15+
*/
16+
public function assertFailedWithValidationError() : Closure
17+
{
18+
return function () : self
19+
{
20+
$this->assertExitCode(Command::INVALID)
21+
->assertFailed();
22+
23+
return $this;
24+
};
25+
}
26+
}

tests/TestCase.php

+7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
namespace PerryvanderMeer\LaravelConsoleValidator\Tests;
66

77
use Orchestra\Testbench\TestCase as Orchestra;
8+
use PerryvanderMeer\LaravelConsoleValidator\LaravelConsoleValidatorServiceProvider;
89

910
class TestCase extends Orchestra
1011
{
12+
protected function getPackageProviders($app) : array
13+
{
14+
return [
15+
LaravelConsoleValidatorServiceProvider::class,
16+
];
17+
}
1118
}

tests/TestCommands/CanGetValidatedArguments/FakeCommandToCastASingleValidatedArgumentAsBool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function handle() : int
3535
{
3636
if ($this->bool('foo') !== $this->boolean('foo'))
3737
{
38-
throw new LogicException();
38+
throw new LogicException;
3939
}
4040

4141
$type = gettype($this->bool('foo'));

tests/Unit/CanGetValidatedArguments/CommandCanCastValidatedArguments.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
test('that the command can cast a validated argument as string', function () : void
1212
{
13-
Artisan::registerCommand(new FakeCommandToCastASingleValidatedArgumentAsString());
13+
Artisan::registerCommand(new FakeCommandToCastASingleValidatedArgumentAsString);
1414

1515
artisan(FakeCommandToCastASingleValidatedArgumentAsString::class, ['foo' => null])
1616
->expectsOutput("Type: 'string'")
@@ -35,7 +35,7 @@
3535

3636
test('that the command can cast a validated argument as boolean', function () : void
3737
{
38-
Artisan::registerCommand(new FakeCommandToCastASingleValidatedArgumentAsBool());
38+
Artisan::registerCommand(new FakeCommandToCastASingleValidatedArgumentAsBool);
3939

4040
artisan(FakeCommandToCastASingleValidatedArgumentAsBool::class, ['foo' => null])
4141
->expectsOutput("Type: 'boolean'")

tests/Unit/CanGetValidatedArguments/CommandCanGetValidatedArguments.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
test('that the command can get a single validated argument', function () : void
1515
{
16-
Artisan::registerCommand(new FakeCommandToGetASingleValidatedArgument());
16+
Artisan::registerCommand(new FakeCommandToGetASingleValidatedArgument);
1717

1818
artisan(FakeCommandToGetASingleValidatedArgument::class, ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'])
1919
->expectsOutput("Foo: 'foo'")
@@ -23,7 +23,7 @@
2323

2424
test('that the command can get a validated nullable argument', function () : void
2525
{
26-
Artisan::registerCommand(new FakeCommandToGetASingleValidatedArgument());
26+
Artisan::registerCommand(new FakeCommandToGetASingleValidatedArgument);
2727

2828
artisan(FakeCommandToGetASingleValidatedArgument::class, ['foo' => 'foo', 'bar' => null, 'baz' => 'baz'])
2929
->expectsOutput("Foo: 'foo'")
@@ -36,15 +36,15 @@
3636
$this->expectException(UnvalidatedArgumentException::class);
3737
$this->expectExceptionMessage('The requested argument [foo] is not validated.');
3838

39-
Artisan::registerCommand(new FakeCommandToGetAnUnvalidatedArgument());
39+
Artisan::registerCommand(new FakeCommandToGetAnUnvalidatedArgument);
4040

4141
artisan(FakeCommandToGetAnUnvalidatedArgument::class, ['foo' => 'foo'])
4242
->assertSuccessful();
4343
});
4444

4545
test('that the command can get all validated argument', function () : void
4646
{
47-
Artisan::registerCommand(new FakeCommandToGetAllValidatedArguments());
47+
Artisan::registerCommand(new FakeCommandToGetAllValidatedArguments);
4848

4949
artisan(FakeCommandToGetAllValidatedArguments::class, ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'])
5050
->expectsOutput('{"foo":"foo","bar":"bar"}')
@@ -53,7 +53,7 @@
5353

5454
test('that the command can collect all validated argument', function () : void
5555
{
56-
Artisan::registerCommand(new FakeCommandToCollectAllValidatedArguments());
56+
Artisan::registerCommand(new FakeCommandToCollectAllValidatedArguments);
5757

5858
artisan(FakeCommandToCollectAllValidatedArguments::class, ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'])
5959
->expectsOutput("Type: 'Illuminate\Support\Collection'")

tests/Unit/FakeCommandTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
test('that a normal command is working', function () : void
1212
{
13-
Artisan::registerCommand(new FakeCommandWithoutValidator());
13+
Artisan::registerCommand(new FakeCommandWithoutValidator);
1414

1515
artisan(FakeCommandWithoutValidator::class)
1616
->assertSuccessful();
1717
});
1818

1919
test('that a command with the validator is working', function () : void
2020
{
21-
Artisan::registerCommand(new FakeCommandWithValidator());
21+
Artisan::registerCommand(new FakeCommandWithValidator);
2222

2323
artisan(FakeCommandWithValidator::class)
2424
->assertSuccessful();

tests/Unit/PrepareArgumentsForValidation/PrepareArgumentsForValidationTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
test('that the validator prepares the argument for validation', function () : void
1212
{
13-
Artisan::registerCommand(new FakeCommandWithArgumentPreparation());
13+
Artisan::registerCommand(new FakeCommandWithArgumentPreparation);
1414

1515
artisan(FakeCommandWithArgumentPreparation::class, ['foo' => 'foo'])
1616
->expectsOutput('foo-bar')
@@ -21,7 +21,7 @@
2121

2222
test('that the validator prepares the arguments for validation', function () : void
2323
{
24-
Artisan::registerCommand(new FakeCommandWithArgumentsPreparation());
24+
Artisan::registerCommand(new FakeCommandWithArgumentsPreparation);
2525

2626
artisan(FakeCommandWithArgumentsPreparation::class, ['foo' => 'foo', 'bar' => 'bar'])
2727
->expectsOutput('foo-bar')

tests/Unit/ResolveValidationLogic/RegisterValidatorAttributesTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
test('that the validator registers attributes from the attributes property', function () : void
1313
{
14-
Artisan::registerCommand(new FakeCommandWithAttributesProperty());
14+
Artisan::registerCommand(new FakeCommandWithAttributesProperty);
1515

1616
artisan(FakeCommandWithAttributesProperty::class)
1717
->assertSuccessful();
1818

19-
expect((new FakeCommandWithAttributesProperty())->getExtractedValidationAttributesForCommand())
19+
expect((new FakeCommandWithAttributesProperty)->getExtractedValidationAttributesForCommand())
2020
->toBeArray()
2121
->toBe([
2222
'foo' => 'bar',
@@ -26,12 +26,12 @@
2626

2727
test('that the validator registers attributes from the attributes method', function () : void
2828
{
29-
Artisan::registerCommand(new FakeCommandWithAttributesMethod());
29+
Artisan::registerCommand(new FakeCommandWithAttributesMethod);
3030

3131
artisan(FakeCommandWithAttributesMethod::class)
3232
->assertSuccessful();
3333

34-
expect((new FakeCommandWithAttributesMethod())->getExtractedValidationAttributesForCommand())
34+
expect((new FakeCommandWithAttributesMethod)->getExtractedValidationAttributesForCommand())
3535
->toBeArray()
3636
->toEqual([
3737
'baz' => 'bax',
@@ -41,12 +41,12 @@
4141

4242
test('that the validator registers attributes from the both the attributes property and the attributes method', function () : void
4343
{
44-
Artisan::registerCommand(new FakeCommandWithAttributesPropertyAndMethod());
44+
Artisan::registerCommand(new FakeCommandWithAttributesPropertyAndMethod);
4545

4646
artisan(FakeCommandWithAttributesPropertyAndMethod::class)
4747
->assertSuccessful();
4848

49-
expect((new FakeCommandWithAttributesPropertyAndMethod())->getExtractedValidationAttributesForCommand())
49+
expect((new FakeCommandWithAttributesPropertyAndMethod)->getExtractedValidationAttributesForCommand())
5050
->toBeArray()
5151
->toEqual([
5252
'foo' => 'bar',

tests/Unit/ResolveValidationLogic/RegisterValidatorMessagesTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
test('that the validator registers messages from the messages property', function () : void
1313
{
14-
Artisan::registerCommand(new FakeCommandWithMessagesProperty());
14+
Artisan::registerCommand(new FakeCommandWithMessagesProperty);
1515

1616
artisan(FakeCommandWithMessagesProperty::class)
1717
->assertSuccessful();
1818

19-
expect((new FakeCommandWithMessagesProperty())->getExtractedValidationMessagesForCommand())
19+
expect((new FakeCommandWithMessagesProperty)->getExtractedValidationMessagesForCommand())
2020
->toBeArray()
2121
->toBe([
2222
'foo' => 'bar',
@@ -26,12 +26,12 @@
2626

2727
test('that the validator registers messages from the messages method', function () : void
2828
{
29-
Artisan::registerCommand(new FakeCommandWithMessagesMethod());
29+
Artisan::registerCommand(new FakeCommandWithMessagesMethod);
3030

3131
artisan(FakeCommandWithMessagesMethod::class)
3232
->assertSuccessful();
3333

34-
expect((new FakeCommandWithMessagesMethod())->getExtractedValidationMessagesForCommand())
34+
expect((new FakeCommandWithMessagesMethod)->getExtractedValidationMessagesForCommand())
3535
->toBeArray()
3636
->toEqual([
3737
'baz' => 'bax',
@@ -41,12 +41,12 @@
4141

4242
test('that the validator registers messages from the both the messages property and the messages method', function () : void
4343
{
44-
Artisan::registerCommand(new FakeCommandWithMessagesPropertyAndMethod());
44+
Artisan::registerCommand(new FakeCommandWithMessagesPropertyAndMethod);
4545

4646
artisan(FakeCommandWithMessagesPropertyAndMethod::class)
4747
->assertSuccessful();
4848

49-
expect((new FakeCommandWithMessagesPropertyAndMethod())->getExtractedValidationMessagesForCommand())
49+
expect((new FakeCommandWithMessagesPropertyAndMethod)->getExtractedValidationMessagesForCommand())
5050
->toBeArray()
5151
->toEqual([
5252
'foo' => 'bar',

tests/Unit/ResolveValidationLogic/RegisterValidatorRulesTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
test('that the validator registers rules from the rules property', function () : void
1414
{
15-
Artisan::registerCommand(new FakeCommandWithRulesProperty());
15+
Artisan::registerCommand(new FakeCommandWithRulesProperty);
1616

1717
artisan(FakeCommandWithRulesProperty::class)
1818
->assertSuccessful();
1919

20-
expect((new FakeCommandWithRulesProperty())->getExtractedValidationRulesForCommand())
20+
expect((new FakeCommandWithRulesProperty)->getExtractedValidationRulesForCommand())
2121
->toBeArray()
2222
->toBe([
2323
'foo' => ['bar'],
@@ -28,12 +28,12 @@
2828

2929
test('that the validator registers rules from the rules method', function () : void
3030
{
31-
Artisan::registerCommand(new FakeCommandWithRulesMethod());
31+
Artisan::registerCommand(new FakeCommandWithRulesMethod);
3232

3333
artisan(FakeCommandWithRulesMethod::class)
3434
->assertSuccessful();
3535

36-
expect((new FakeCommandWithRulesMethod())->getExtractedValidationRulesForCommand())
36+
expect((new FakeCommandWithRulesMethod)->getExtractedValidationRulesForCommand())
3737
->toBeArray()
3838
->toEqual([
3939
'foo' => [Rule::file()],
@@ -44,12 +44,12 @@
4444

4545
test('that the validator registers rules from the both the rules property and the rules method', function () : void
4646
{
47-
Artisan::registerCommand(new FakeCommandWithRulesPropertyAndMethod());
47+
Artisan::registerCommand(new FakeCommandWithRulesPropertyAndMethod);
4848

4949
artisan(FakeCommandWithRulesPropertyAndMethod::class)
5050
->assertSuccessful();
5151

52-
expect((new FakeCommandWithRulesPropertyAndMethod())->getExtractedValidationRulesForCommand())
52+
expect((new FakeCommandWithRulesPropertyAndMethod)->getExtractedValidationRulesForCommand())
5353
->toBeArray()
5454
->toEqual([
5555
'foo' => ['bar', Rule::file()],
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
use PerryvanderMeer\LaravelConsoleValidator\Tests\TestCommands\ValidatesArguments\FakeCommandWithRule;
7+
8+
use function Pest\Laravel\artisan;
9+
10+
test('that the assertFailedWithValidationError() assertion works', function () : void
11+
{
12+
Artisan::registerCommand(new FakeCommandWithRule);
13+
14+
artisan(FakeCommandWithRule::class, ['foo' => 'foo-bop'])
15+
->assertSuccessful();
16+
17+
artisan(FakeCommandWithRule::class, ['foo' => 'foo'])
18+
->assertFailedWithValidationError();
19+
})->skip(
20+
fn () : bool => version_compare(app()->version(), '11.9.0', '<'),
21+
'Mixin with PendingCommand is only available from 11.9.0',
22+
);

tests/Unit/ValidatesArguments/HandleMethodShouldNotBeCalledTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
Session::shouldReceive('put')->never();
1515

16-
Artisan::registerCommand(new FakeCommandToTestHandleMethod());
16+
Artisan::registerCommand(new FakeCommandToTestHandleMethod);
1717

1818
artisan(FakeCommandToTestHandleMethod::class, ['foo' => 'foo'])
1919
->assertExitCode(Command::INVALID)
@@ -24,7 +24,7 @@
2424
{
2525
Session::shouldReceive('put')->once();
2626

27-
Artisan::registerCommand(new FakeCommandToTestHandleMethod());
27+
Artisan::registerCommand(new FakeCommandToTestHandleMethod);
2828

2929
artisan(FakeCommandToTestHandleMethod::class, ['foo' => 'foo-bar'])
3030
->assertSuccessful();

tests/Unit/ValidatesArguments/ValidatorAttributesTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
test('that the validator supports custom attributes for an argument', function () : void
1212
{
13-
Artisan::registerCommand(new FakeCommandWithRulesAndAttributes());
13+
Artisan::registerCommand(new FakeCommandWithRulesAndAttributes);
1414

1515
artisan(FakeCommandWithRulesAndAttributes::class, ['foo' => 'foo', 'bar' => 'bar'])
1616
->expectsOutput('The first field must be at least 4 characters.')

0 commit comments

Comments
 (0)