From 770502337ac586321b0e078ef94d97ef5d53749d Mon Sep 17 00:00:00 2001 From: Propaganistas Date: Fri, 26 May 2017 12:56:23 +0200 Subject: [PATCH] Implement dependent validation (#51) --- src/LaravelPhoneServiceProvider.php | 7 +- tests/PhoneValidatorTest.php | 115 ++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/src/LaravelPhoneServiceProvider.php b/src/LaravelPhoneServiceProvider.php index b49ada1..3f5e3ec 100644 --- a/src/LaravelPhoneServiceProvider.php +++ b/src/LaravelPhoneServiceProvider.php @@ -1,5 +1,6 @@ app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone'); + $extend = version_compare(Application::VERSION, '5.4.18', '>=') + ? 'extendDependent' + : 'extend'; + + $this->app['validator']->{$extend}('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone'); } /** diff --git a/tests/PhoneValidatorTest.php b/tests/PhoneValidatorTest.php index 5c9eeaa..aabc3bd 100644 --- a/tests/PhoneValidatorTest.php +++ b/tests/PhoneValidatorTest.php @@ -334,6 +334,121 @@ public function testValidatePhoneFaultyParameters() )->passes(); } + public function testValidatePhoneWithArrayInput() + { + if (version_compare(Application::VERSION, '5.4.18', '>=')) { + // Validator with correct country value. + $this->assertTrue($this->validator->make( + [ + 'container' => [ + ['field' => '016123456'], + ['field' => '0499123456'] + ] + ], + ['container.*.field' => 'phone:BE'])->passes() + ); + + // Validator with wrong country value. + $this->assertFalse($this->validator->make( + [ + 'container' => [ + ['field' => '016123456'], + ['field' => '0499123456'] + ] + ], + ['container.*.field' => 'phone:NL'])->passes() + ); + + // Validator with correct country value, one wrong input. + $this->assertFalse($this->validator->make( + [ + 'container' => [ + ['field' => '01612'], + ['field' => '0499123456'] + ] + ], + ['container.*.field' => 'phone:BE'])->passes() + ); + + // Validator with correct country value, one wrong input. + $this->assertFalse($this->validator->make( + [ + 'container' => [ + ['field' => '016123456'], + ['field' => '049912'] + ] + ], + ['container.*.field' => 'phone:BE'])->passes() + ); + + // Validator with correct country value. + $this->assertTrue($this->validator->make( + [ + 'container' => [ + ['field' => '0477123456'], + ['field' => '0499123456'] + ] + ], + ['container.*.field' => 'phone:BE,mobile'])->passes() + ); + + // Validator with correct country value, one input wrong type. + $this->assertFalse($this->validator->make( + [ + 'container' => [ + ['field' => '016123456'], + ['field' => '0499123456'] + ] + ], + ['container.*.field' => 'phone:BE,mobile'])->passes() + ); + + // Validator with correct country fields. + $this->assertTrue($this->validator->make( + [ + 'container' => [ + ['field' => '016123456', 'field_country' => 'BE'], + ['field' => '6502530000', 'field_country' => 'US'] + ] + ], + ['container.*.field' => 'phone'])->passes() + ); + + // Validator with correct country fields. + $this->assertFalse($this->validator->make( + [ + 'container' => [ + ['field' => '016123456', 'field_country' => 'BE'], + ['field' => '6502530000', 'field_country' => 'BE'] + ] + ], + ['container.*.field' => 'phone'])->passes() + ); + + // Validator with correct custom country fields. + $this->assertTrue($this->validator->make( + [ + 'container' => [ + ['field' => '016123456', 'country_code' => 'BE'], + ['field' => '6502530000', 'country_code' => 'US'] + ] + ], + ['container.*.field' => 'phone:container.*.country_code'])->passes() + ); + + // Validator with wrong custom country fields. + $this->assertFalse($this->validator->make( + [ + 'container' => [ + ['field' => '016123456', 'country_code' => 'BE'], + ['field' => '6502530000', 'country_code' => 'BE'] + ] + ], + ['container.*.field' => 'phone:container.*.country_code'])->passes() + ); + } + } + public function testHelperFunction() { // Test landline number without format parameter.