-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from WendellAdriel/feature/lazy-validation
Add lazy validation feature
- Loading branch information
Showing
4 changed files
with
145 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\ValidatedDTO\Tests\Datasets; | ||
|
||
use WendellAdriel\ValidatedDTO\Casting\IntegerCast; | ||
use WendellAdriel\ValidatedDTO\Casting\StringCast; | ||
use WendellAdriel\ValidatedDTO\ValidatedDTO; | ||
|
||
class LazyDTO extends ValidatedDTO | ||
{ | ||
public bool $lazyValidation = true; | ||
|
||
public ?string $name; | ||
|
||
public ?int $age = null; | ||
|
||
protected function rules(): array | ||
{ | ||
return [ | ||
'name' => 'required', | ||
'age' => 'numeric', | ||
]; | ||
} | ||
|
||
protected function defaults(): array | ||
{ | ||
return []; | ||
} | ||
|
||
protected function casts(): array | ||
{ | ||
return [ | ||
'name' => new StringCast(), | ||
'age' => new IntegerCast(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Validation\ValidationException; | ||
use WendellAdriel\ValidatedDTO\Tests\Datasets\LazyDTO; | ||
use WendellAdriel\ValidatedDTO\ValidatedDTO; | ||
|
||
beforeEach(function () { | ||
$this->subject_name = fake()->name; | ||
}); | ||
|
||
it('instantiates a ValidatedDTO marked as lazy without validating its data', function () { | ||
$validatedDTO = new LazyDTO(['name' => $this->subject_name]); | ||
|
||
expect($validatedDTO)->toBeInstanceOf(ValidatedDTO::class) | ||
->and($validatedDTO->validatedData) | ||
->toBe(['name' => $this->subject_name]) | ||
->and($validatedDTO->lazyValidation) | ||
->toBeTrue(); | ||
}); | ||
|
||
it('does not fails a lazy validation with valid data', function () { | ||
$validatedDTO = new LazyDTO(['name' => $this->subject_name]); | ||
|
||
expect($validatedDTO)->toBeInstanceOf(ValidatedDTO::class) | ||
->and($validatedDTO->validatedData) | ||
->toBe(['name' => $this->subject_name]) | ||
->and($validatedDTO->lazyValidation) | ||
->toBeTrue(); | ||
|
||
$validatedDTO->validate(); | ||
}); | ||
|
||
it('fails a lazy validation with invalid data', function () { | ||
$validatedDTO = new LazyDTO(['name' => null]); | ||
|
||
expect($validatedDTO)->toBeInstanceOf(ValidatedDTO::class) | ||
->and($validatedDTO->validatedData) | ||
->toBe(['name' => null]) | ||
->and($validatedDTO->lazyValidation) | ||
->toBeTrue(); | ||
|
||
$validatedDTO->validate(); | ||
})->throws(ValidationException::class); |