|
4 | 4 |
|
5 | 5 | use DateTime; |
6 | 6 | use PHPUnit\Framework\TestCase; |
| 7 | +use Rakit\Validation\Rule; |
7 | 8 | use Rakit\Validation\Rules\UploadedFile; |
8 | 9 | use Rakit\Validation\Validator; |
9 | 10 |
|
10 | 11 | class ValidatorTest extends TestCase |
11 | 12 | { |
12 | | - |
| 13 | + /** @var Validator */ |
13 | 14 | protected $validator; |
14 | 15 |
|
15 | 16 | protected function setUp() |
@@ -803,6 +804,71 @@ public function testArrayAssocValidation() |
803 | 804 | $this->assertNull($errors->first('user.name:required')); |
804 | 805 | } |
805 | 806 |
|
| 807 | + /** |
| 808 | + * Test root asterisk validation. |
| 809 | + * |
| 810 | + * @dataProvider rootAsteriskProvider |
| 811 | + */ |
| 812 | + public function testRootAsteriskValidation(array $data, array $rules, $errors = null) |
| 813 | + { |
| 814 | + $validation = $this->validator->validate($data, $rules); |
| 815 | + $this->assertSame(empty($errors), $validation->passes()); |
| 816 | + $errorBag = $validation->errors(); |
| 817 | + if (!empty($errors)) { |
| 818 | + foreach ($errors as $error) { |
| 819 | + $field = $error[0]; |
| 820 | + $rule = $error[1] ?? null; |
| 821 | + $error = $errorBag->get($field); |
| 822 | + $this->assertNotEmpty($error); |
| 823 | + if ($rule !== null) { |
| 824 | + $this->assertArrayHasKey($rule, $error); |
| 825 | + } |
| 826 | + } |
| 827 | + } |
| 828 | + } |
| 829 | + |
| 830 | + public function rootAsteriskProvider(): array |
| 831 | + { |
| 832 | + return [ |
| 833 | + 'control sample success' => [ |
| 834 | + ['Body' => ['a' => 1, 'b' => 2]], |
| 835 | + ['Body.*' => 'integer|min:0'], |
| 836 | + ], |
| 837 | + 'control sample failure' => [ |
| 838 | + ['Body' => ['a' => 1, 'b' => -2]], |
| 839 | + ['Body.*' => 'integer|min:0'], |
| 840 | + [['Body.b', 'min']], |
| 841 | + ], |
| 842 | + 'root field success' => [ |
| 843 | + ['a' => 1, 'b' => 2], |
| 844 | + ['*' => 'integer|min:0'], |
| 845 | + ], |
| 846 | + 'root field failure' => [ |
| 847 | + ['a' => 1, 'b' => -2], |
| 848 | + ['*' => 'integer|min:0'], |
| 849 | + [['b', 'min']], |
| 850 | + ], |
| 851 | + 'root array success' => [ |
| 852 | + [[1], [2]], |
| 853 | + ['*.*' => 'integer|min:0'], |
| 854 | + ], |
| 855 | + 'root array failure' => [ |
| 856 | + [[1], [-2]], |
| 857 | + ['*.*' => 'integer|min:0'], |
| 858 | + [['1.0', 'min']], |
| 859 | + ], |
| 860 | + 'root dict success' => [ |
| 861 | + ['a' => ['c' => 1, 'd' => 4], 'b' => ['c' => 'e', 'd' => 8]], |
| 862 | + ['*.c' => 'required'], |
| 863 | + ], |
| 864 | + 'root dict failure' => [ |
| 865 | + ['a' => ['c' => 1, 'd' => 4], 'b' => ['d' => 8]], |
| 866 | + ['*.c' => 'required'], |
| 867 | + [['b.c', 'required']], |
| 868 | + ], |
| 869 | + ]; |
| 870 | + } |
| 871 | + |
806 | 872 | public function testArrayValidation() |
807 | 873 | { |
808 | 874 | $validation = $this->validator->validate([ |
|
0 commit comments