From 3ac348b6a7191a68a7fe7ca713d644488798c709 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 21 Sep 2022 16:13:58 +0900 Subject: [PATCH] test: add tests for required_without --- tests/system/Validation/RulesTest.php | 93 +++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index 419070714599..924d8d3d334c 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -639,4 +639,97 @@ public function requiredWithoutProvider(): Generator ], ]; } + + /** + * @dataProvider requiredWithoutMultipleProvider + */ + public function testRequiredWithoutMultiple(string $foo, string $bar, string $baz, bool $result): void + { + $this->validation->setRules(['foo' => 'required_without[bar,baz]']); + + $data = [ + 'foo' => $foo, + 'bar' => $bar, + 'baz' => $baz, + ]; + $this->assertSame($result, $this->validation->run($data)); + } + + public function requiredWithoutMultipleProvider(): Generator + { + yield from [ + 'all empty' => [ + '', + '', + '', + false, + ], + 'foo is not empty' => [ + 'a', + '', + '', + true, + ], + 'bar is not empty' => [ + '', + 'b', + '', + false, + ], + 'baz is not empty' => [ + '', + '', + 'c', + false, + ], + 'bar,baz are not empty' => [ + '', + 'b', + 'c', + true, + ], + ]; + } + + /** + * @dataProvider requiredWithoutMultipleWithoutFieldsProvider + */ + public function testRequiredWithoutMultipleWithoutFields(array $data, bool $result): void + { + $this->validation->setRules(['foo' => 'required_without[bar,baz]']); + + $this->assertSame($result, $this->validation->run($data)); + } + + public function requiredWithoutMultipleWithoutFieldsProvider(): Generator + { + yield from [ + 'baz is missing' => [ + [ + 'foo' => '', + 'bar' => '', + ], + false, + ], + 'bar,baz are missing' => [ + [ + 'foo' => '', + ], + false, + ], + 'bar is not empty' => [ + [ + 'foo' => '', + 'bar' => 'b', + ], + false, + ], + 'foo is not empty' => [ + [ + 'foo' => 'a', + ], + true, + ], + ]; + } }