-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add lazy validation feature * test: add mock for validator in lazy validation tests --------- Co-authored-by: Deeka Wong <[email protected]>
- Loading branch information
1 parent
f952621
commit 440a354
Showing
4 changed files
with
170 additions
and
18 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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
|
||
namespace FriendsOfHyperf\Tests\ValidatedDTO\Datasets; | ||
|
||
use FriendsOfHyperf\ValidatedDTO\Casting\IntegerCast; | ||
use FriendsOfHyperf\ValidatedDTO\Casting\StringCast; | ||
use FriendsOfHyperf\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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of friendsofhyperf/components. | ||
* | ||
* @link https://github.com/friendsofhyperf/components | ||
* @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
* @contact [email protected] | ||
*/ | ||
use FriendsOfHyperf\Tests\ValidatedDTO\Datasets\LazyDTO; | ||
use FriendsOfHyperf\ValidatedDTO\ValidatedDTO; | ||
use Hyperf\Contract\ValidatorInterface; | ||
use Hyperf\Validation\Contract\ValidatorFactoryInterface; | ||
use Hyperf\Validation\ValidationException; | ||
|
||
beforeEach(function () { | ||
$this->subject_name = faker()->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 () { | ||
$this->mock(ValidatorFactoryInterface::class, function ($mock) { | ||
$mock->shouldReceive('make')->andReturn(Mockery::mock(ValidatorInterface::class, function ($mock) { | ||
$mock->shouldReceive('fails')->andReturn(true) | ||
->shouldReceive('passes')->andReturn(false) | ||
->shouldReceive('after')->andReturn(null); | ||
})); | ||
}); | ||
|
||
$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); |