Skip to content

Commit

Permalink
Format rule for required_without (#4196)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
rust17 and YouneselBarnoussi authored Feb 19, 2025
1 parent 203d0cd commit fccd234
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/Validators/RowValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ private function formatRule($rules)
return $rules;
}

if (Str::contains($rules, 'required_without') && preg_match('/(.*?):(.*)/', $rules, $matches)) {
$column = array_map(function ($match) {
return Str::startsWith($match, '*.') ? $match : '*.' . $match;
}, explode(',', $matches[2]));

return $matches[1] . ':' . implode(',', $column);
}

if (Str::contains($rules, 'required_') && preg_match('/(.*?):(.*),(.*)/', $rules, $matches)) {
$column = Str::startsWith($matches[2], '*.') ? $matches[2] : '*.' . $matches[2];

Expand Down
96 changes: 96 additions & 0 deletions tests/Validators/RowValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Maatwebsite\Excel\Tests\Validators;

use Illuminate\Contracts\Validation\Factory;
use Maatwebsite\Excel\Tests\TestCase;
use Maatwebsite\Excel\Validators\RowValidator;

class RowValidatorTest extends TestCase
{
/**
* The RowValidator instance.
*/
protected $validator;

/**
* Set up the test.
*/
public function setUp(): void
{
parent::setUp();

$this->validator = new RowValidator(app(Factory::class));
}

public function test_format_rule_with_array_input()
{
$rules = ['rule1', 'rule2'];

$result = $this->callPrivateMethod('formatRule', [$rules]);

$this->assertEquals($rules, $result);
}

public function test_format_rule_with_object_input()
{
$rule = new \stdClass();

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals($rule, $result);
}

public function test_format_rule_with_callable_input()
{
$rule = function () {
return 'callable';
};

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals($rule, $result);
}

public function test_format_rule_with_required_without_all()
{
$rule = 'required_without_all:first_name,last_name';

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals('required_without_all:*.first_name,*.last_name', $result);
}

public function test_format_rule_with_required_without()
{
$rule = 'required_without:first_name';

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals('required_without:*.first_name', $result);
}

public function test_format_rule_with_string_input_not_matching_pattern()
{
$rule = 'rule';

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals($rule, $result);
}

/**
* Call a private function.
*
* @param string $name
* @param array $args
* @return mixed
*/
public function callPrivateMethod(string $name, array $args)
{
$method = new \ReflectionMethod(RowValidator::class, $name);
$method->setAccessible(true);

return $method->invokeArgs($this->validator, $args);
}
}

0 comments on commit fccd234

Please sign in to comment.