Skip to content

Commit ef7e3db

Browse files
xepozzStyleCIBotarogachev
authored
Fix rules serialization (#344)
* Fix rules serialization * Apply fixes from StyleCI * Add test for iterable rules * Fix error message * Apply fixes from StyleCI * Improve callable rule (#343) * Add ability to run non-static closures * Apply fixes from StyleCI * Fix tests * Apply fixes from StyleCI * Improve method check * Apply fixes from StyleCI * Check property before the use * Collapse test * Apply fixes from StyleCI * Add test for static method * Apply CR suggestions * Update tests/Rule/CallbackTest.php * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Alexey Rogachev <[email protected]> * Fix code coverage Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Alexey Rogachev <[email protected]>
1 parent ea4c47f commit ef7e3db

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

src/RulesDumper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Yiisoft\Validator;
66

77
use InvalidArgumentException;
8-
use function is_array;
98

109
/**
1110
* RulesDumper allows to get an array of rule names and corresponding settings from a set of rules.
@@ -54,16 +53,17 @@ private function fetchOptions(iterable $rules): array
5453
{
5554
$result = [];
5655
foreach ($rules as $attribute => $rule) {
57-
if (is_array($rule)) {
56+
if (is_iterable($rule)) {
5857
$result[$attribute] = $this->fetchOptions($rule);
5958
} elseif ($rule instanceof SerializableRuleInterface) {
6059
$result[$attribute] = array_merge([$rule->getName()], $rule->getOptions());
6160
} elseif ($rule instanceof RuleInterface) {
6261
$result[$attribute] = [$rule->getName()];
6362
} else {
6463
throw new InvalidArgumentException(sprintf(
65-
'Rules should be a rule or an array of rules that implements %s.',
64+
'Each rule must implement "%s". Type "%s" given.',
6665
RuleInterface::class,
66+
get_debug_type($rule),
6767
));
6868
}
6969
}

tests/RulesDumperTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Validator\Tests;
66

7+
use InvalidArgumentException;
78
use PHPUnit\Framework\TestCase;
89
use Yiisoft\Validator\Rule\Number;
910
use Yiisoft\Validator\RulesDumper;
@@ -16,7 +17,7 @@ public function asArrayDataProvider(): array
1617
[
1718
[
1819
'attributeName' => [
19-
new Number(
20+
$rule = new Number(
2021
asInteger: true,
2122
min: 10,
2223
max: 100,
@@ -25,11 +26,12 @@ public function asArrayDataProvider(): array
2526
skipOnEmpty: true,
2627
skipOnError: true
2728
),
29+
(fn () => yield from [$rule, $rule])(),
2830
],
2931
],
3032
[
3133
'attributeName' => [
32-
[
34+
$dump = [
3335
'number',
3436
'asInteger' => true,
3537
'min' => 10,
@@ -50,6 +52,10 @@ public function asArrayDataProvider(): array
5052
'integerPattern' => '/^\s*[+-]?\d+\s*$/',
5153
'numberPattern' => '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
5254
],
55+
[
56+
$dump,
57+
$dump,
58+
],
5359
],
5460
],
5561
],
@@ -66,4 +72,16 @@ public function testAsArray($rules, array $expected): void
6672

6773
$this->assertEquals($expected, $result);
6874
}
75+
76+
public function testException(): void
77+
{
78+
$dumper = new RulesDumper();
79+
80+
$this->expectException(InvalidArgumentException::class);
81+
82+
$message = 'Each rule must implement "Yiisoft\Validator\RuleInterface". Type "string" given.';
83+
$this->expectExceptionMessage($message);
84+
85+
$dumper->asArray(['not a rule']);
86+
}
6987
}

0 commit comments

Comments
 (0)