|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Maatwebsite\Excel\Tests\Validators; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Validation\Factory; |
| 6 | +use Maatwebsite\Excel\Tests\TestCase; |
| 7 | +use Maatwebsite\Excel\Validators\RowValidator; |
| 8 | + |
| 9 | +class RowValidatorTest extends TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The RowValidator instance. |
| 13 | + */ |
| 14 | + protected $validator; |
| 15 | + |
| 16 | + /** |
| 17 | + * Set up the test. |
| 18 | + */ |
| 19 | + public function setUp(): void |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + $this->validator = new RowValidator(app(Factory::class)); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_format_rule_with_array_input() |
| 27 | + { |
| 28 | + $rules = ['rule1', 'rule2']; |
| 29 | + |
| 30 | + $result = $this->callPrivateMethod('formatRule', [$rules]); |
| 31 | + |
| 32 | + $this->assertEquals($rules, $result); |
| 33 | + } |
| 34 | + |
| 35 | + public function test_format_rule_with_object_input() |
| 36 | + { |
| 37 | + $rule = new \stdClass(); |
| 38 | + |
| 39 | + $result = $this->callPrivateMethod('formatRule', [$rule]); |
| 40 | + |
| 41 | + $this->assertEquals($rule, $result); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_format_rule_with_callable_input() |
| 45 | + { |
| 46 | + $rule = function () { |
| 47 | + return 'callable'; |
| 48 | + }; |
| 49 | + |
| 50 | + $result = $this->callPrivateMethod('formatRule', [$rule]); |
| 51 | + |
| 52 | + $this->assertEquals($rule, $result); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_format_rule_with_required_without_all() |
| 56 | + { |
| 57 | + $rule = 'required_without_all:first_name,last_name'; |
| 58 | + |
| 59 | + $result = $this->callPrivateMethod('formatRule', [$rule]); |
| 60 | + |
| 61 | + $this->assertEquals('required_without_all:*.first_name,*.last_name', $result); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_format_rule_with_required_without() |
| 65 | + { |
| 66 | + $rule = 'required_without:first_name'; |
| 67 | + |
| 68 | + $result = $this->callPrivateMethod('formatRule', [$rule]); |
| 69 | + |
| 70 | + $this->assertEquals('required_without:*.first_name', $result); |
| 71 | + } |
| 72 | + |
| 73 | + public function test_format_rule_with_string_input_not_matching_pattern() |
| 74 | + { |
| 75 | + $rule = 'rule'; |
| 76 | + |
| 77 | + $result = $this->callPrivateMethod('formatRule', [$rule]); |
| 78 | + |
| 79 | + $this->assertEquals($rule, $result); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Call a private function. |
| 84 | + * |
| 85 | + * @param string $name |
| 86 | + * @param array $args |
| 87 | + * @return mixed |
| 88 | + */ |
| 89 | + public function callPrivateMethod(string $name, array $args) |
| 90 | + { |
| 91 | + $method = new \ReflectionMethod(RowValidator::class, $name); |
| 92 | + $method->setAccessible(true); |
| 93 | + |
| 94 | + return $method->invokeArgs($this->validator, $args); |
| 95 | + } |
| 96 | +} |
0 commit comments