|
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() |
@@ -812,6 +813,71 @@ public function testEmptyArrayAssocValidation() |
812 | 813 |
|
813 | 814 | $this->assertFalse($validation->passes()); |
814 | 815 | } |
| 816 | + |
| 817 | + /** |
| 818 | + * Test root asterisk validation. |
| 819 | + * |
| 820 | + * @dataProvider rootAsteriskProvider |
| 821 | + */ |
| 822 | + public function testRootAsteriskValidation(array $data, array $rules, $errors = null) |
| 823 | + { |
| 824 | + $validation = $this->validator->validate($data, $rules); |
| 825 | + $this->assertSame(empty($errors), $validation->passes()); |
| 826 | + $errorBag = $validation->errors(); |
| 827 | + if (!empty($errors)) { |
| 828 | + foreach ($errors as $error) { |
| 829 | + $field = $error[0]; |
| 830 | + $rule = $error[1] ?? null; |
| 831 | + $error = $errorBag->get($field); |
| 832 | + $this->assertNotEmpty($error); |
| 833 | + if ($rule !== null) { |
| 834 | + $this->assertArrayHasKey($rule, $error); |
| 835 | + } |
| 836 | + } |
| 837 | + } |
| 838 | + } |
| 839 | + |
| 840 | + public function rootAsteriskProvider(): array |
| 841 | + { |
| 842 | + return [ |
| 843 | + 'control sample success' => [ |
| 844 | + ['Body' => ['a' => 1, 'b' => 2]], |
| 845 | + ['Body.*' => 'integer|min:0'], |
| 846 | + ], |
| 847 | + 'control sample failure' => [ |
| 848 | + ['Body' => ['a' => 1, 'b' => -2]], |
| 849 | + ['Body.*' => 'integer|min:0'], |
| 850 | + [['Body.b', 'min']], |
| 851 | + ], |
| 852 | + 'root field success' => [ |
| 853 | + ['a' => 1, 'b' => 2], |
| 854 | + ['*' => 'integer|min:0'], |
| 855 | + ], |
| 856 | + 'root field failure' => [ |
| 857 | + ['a' => 1, 'b' => -2], |
| 858 | + ['*' => 'integer|min:0'], |
| 859 | + [['b', 'min']], |
| 860 | + ], |
| 861 | + 'root array success' => [ |
| 862 | + [[1], [2]], |
| 863 | + ['*.*' => 'integer|min:0'], |
| 864 | + ], |
| 865 | + 'root array failure' => [ |
| 866 | + [[1], [-2]], |
| 867 | + ['*.*' => 'integer|min:0'], |
| 868 | + [['1.0', 'min']], |
| 869 | + ], |
| 870 | + 'root dict success' => [ |
| 871 | + ['a' => ['c' => 1, 'd' => 4], 'b' => ['c' => 'e', 'd' => 8]], |
| 872 | + ['*.c' => 'required'], |
| 873 | + ], |
| 874 | + 'root dict failure' => [ |
| 875 | + ['a' => ['c' => 1, 'd' => 4], 'b' => ['d' => 8]], |
| 876 | + ['*.c' => 'required'], |
| 877 | + [['b.c', 'required']], |
| 878 | + ], |
| 879 | + ]; |
| 880 | + } |
815 | 881 |
|
816 | 882 | public function testArrayValidation() |
817 | 883 | { |
|
0 commit comments