From b25924e868cdf80944e56a76fa6eed4509d9af7b Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 16 Jul 2019 01:39:54 +0200 Subject: Allow nested rules (not and optional) --- tests/Unit/Http/Validation/ValidatorTest.php | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'tests/Unit') diff --git a/tests/Unit/Http/Validation/ValidatorTest.php b/tests/Unit/Http/Validation/ValidatorTest.php index 0790b7a8..450e5d4e 100644 --- a/tests/Unit/Http/Validation/ValidatorTest.php +++ b/tests/Unit/Http/Validation/ValidatorTest.php @@ -16,6 +16,7 @@ class ValidatorTest extends TestCase public function testValidate() { $val = new Validator(); + $this->assertTrue($val->validate( ['foo' => 'bar', 'lorem' => 'on', 'dolor' => 'bla'], ['lorem' => 'accepted'] @@ -32,12 +33,39 @@ class ValidatorTest extends TestCase ); } + /** + * @covers \Engelsystem\Http\Validation\Validator::validate + */ + public function testValidateChaining() + { + $val = new Validator(); + + $this->assertTrue($val->validate( + ['lorem' => 10], + ['lorem' => 'required|min:3|max:10'] + )); + $this->assertTrue($val->validate( + ['lorem' => 3], + ['lorem' => 'required|min:3|max:10'] + )); + + $this->assertFalse($val->validate( + ['lorem' => 2], + ['lorem' => 'required|min:3|max:10'] + )); + $this->assertFalse($val->validate( + ['lorem' => 42], + ['lorem' => 'required|min:3|max:10'] + )); + } + /** * @covers \Engelsystem\Http\Validation\Validator::validate */ public function testValidateNotImplemented() { $val = new Validator(); + $this->expectException(InvalidArgumentException::class); $val->validate( @@ -53,6 +81,7 @@ class ValidatorTest extends TestCase public function testValidateMapping() { $val = new Validator(); + $this->assertTrue($val->validate( ['foo' => 'bar'], ['foo' => 'required'] @@ -75,4 +104,39 @@ class ValidatorTest extends TestCase $val->getErrors() ); } + + /** + * @covers \Engelsystem\Http\Validation\Validator::validate + */ + public function testValidateNesting() + { + $val = new Validator(); + + $this->assertTrue($val->validate( + [], + ['foo' => 'not|required'] + )); + + $this->assertTrue($val->validate( + ['foo' => 'foo'], + ['foo' => 'not|int'] + )); + $this->assertFalse($val->validate( + ['foo' => 1], + ['foo' => 'not|int'] + )); + + $this->assertTrue($val->validate( + [], + ['foo' => 'optional|int'] + )); + $this->assertTrue($val->validate( + ['foo' => '33'], + ['foo' => 'optional|int'] + )); + $this->assertFalse($val->validate( + ['foo' => 'T'], + ['foo' => 'optional|int'] + )); + } } -- cgit v1.2.3-54-g00ecf