Skip to content

Commit fccd234

Browse files
Format rule for required_without (#4196)
* test: 🧪 add failing tests for row validator * format rule for required_without * fix: phpunit error when run in php7.4 * fix: style ci * fix: style ci --------- Co-authored-by: Younes el Barnoussi <younes_barnoussi@live.nl>
1 parent 203d0cd commit fccd234

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

.phpunit.cache/test-results

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/Validators/RowValidator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ private function formatRule($rules)
134134
return $rules;
135135
}
136136

137+
if (Str::contains($rules, 'required_without') && preg_match('/(.*?):(.*)/', $rules, $matches)) {
138+
$column = array_map(function ($match) {
139+
return Str::startsWith($match, '*.') ? $match : '*.' . $match;
140+
}, explode(',', $matches[2]));
141+
142+
return $matches[1] . ':' . implode(',', $column);
143+
}
144+
137145
if (Str::contains($rules, 'required_') && preg_match('/(.*?):(.*),(.*)/', $rules, $matches)) {
138146
$column = Str::startsWith($matches[2], '*.') ? $matches[2] : '*.' . $matches[2];
139147

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)