From e7a19b208093a2cb25e682957b481cf95badfce7 Mon Sep 17 00:00:00 2001 From: Filis Futsarov Date: Thu, 20 Feb 2020 23:44:59 +0100 Subject: [PATCH] Tests updated --- gump.class.php | 36 ++++------- tests/Validators/ExactLenValidatorTest.php | 71 ++++++++++++++++++++-- tests/Validators/MaxLenValidatorTest.php | 71 ++++++++++++++++++++-- tests/Validators/MinLenValidatorTest.php | 71 ++++++++++++++++++++-- tests/Validators/ValidCcValidatorTest.php | 49 ++++++++++++++- 5 files changed, 254 insertions(+), 44 deletions(-) diff --git a/gump.class.php b/gump.class.php index 766bf9d4..d098f3d3 100644 --- a/gump.class.php +++ b/gump.class.php @@ -399,11 +399,7 @@ public function validate(array $input, array $ruleset, $rules_delimiter='|', $pa if (count(array_intersect($look_for, $rules)) > 0 || (isset($input[$field]))) { if (isset($input[$field])) { - if (is_array($input[$field]) && in_array('required_file', $ruleset)) { - $input_array = array($input[$field]); - } else { - $input_array = array($input[$field]); - } + $input_array = array($input[$field]); } else { $input_array = array(''); } @@ -1135,14 +1131,12 @@ protected function validate_max_len($field, $input, $param = null) return; } - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { if (mb_strlen($input[$field]) <= (int) $param) { return; } - } else { - if (strlen($input[$field]) <= (int) $param) { - return; - } + } else if (strlen($input[$field]) <= (int) $param) { + return; } return array( @@ -1170,14 +1164,12 @@ protected function validate_min_len($field, $input, $param = null) return; } - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { if (mb_strlen($input[$field]) >= (int) $param) { return; } - } else { - if (strlen($input[$field]) >= (int) $param) { - return; - } + } else if (strlen($input[$field]) >= (int) $param) { + return; } return array( @@ -1205,14 +1197,12 @@ protected function validate_exact_len($field, $input, $param = null) return; } - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { if (mb_strlen($input[$field]) == (int) $param) { return; } - } else { - if (strlen($input[$field]) == (int) $param) { - return; - } + } else if (strlen($input[$field]) == (int) $param) { + return; } return array( @@ -1646,20 +1636,19 @@ protected function validate_valid_cc($field, $input, $param = null) $number = preg_replace('/\D/', '', $input[$field]); - if (function_exists('mb_strlen')) { + if (Helpers::functionExists('mb_strlen')) { $number_length = mb_strlen($number); } else { $number_length = strlen($number); } - /** * Bail out if $number_length is 0. * This can be the case if a user has entered only alphabets * * @since 1.5 */ - if( $number_length == 0 ) { + if ($number_length == 0 ) { return array( 'field' => $field, 'value' => $input[$field], @@ -1668,7 +1657,6 @@ protected function validate_valid_cc($field, $input, $param = null) ); } - $parity = $number_length % 2; $total = 0; diff --git a/tests/Validators/ExactLenValidatorTest.php b/tests/Validators/ExactLenValidatorTest.php index b2333bbb..c6222d73 100644 --- a/tests/Validators/ExactLenValidatorTest.php +++ b/tests/Validators/ExactLenValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class ExactLenValidatorTest @@ -13,18 +14,76 @@ */ class ExactLenValidatorTest extends BaseTestCase { - public function testSuccessWhenEqual() + public function testSuccessWhenEqualWithMbStrlen() { - $this->assertTrue($this->validate('exact_len,2', '12')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('exact_len,5', 'ñándú')); } - public function testErrorWhenMore() + public function testErrorWhenMoreWithMbStrlen() { - $this->assertNotTrue($this->validate('exact_len,2', '123')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('exact_len,2', 'ñán')); } - public function testErrorWhenLess() + public function testErrorWhenLessWithMbStrlen() { - $this->assertNotTrue($this->validate('exact_len,2', '1')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('exact_len,2', 'ñ')); + } + + public function testSuccessWhenEqualWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('exact_len,3', 'ña')); + $this->assertTrue($this->validate('exact_len,2', 'na')); + } + + public function testErrorWhenMoreWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('exact_len,2', 'nan')); + } + + public function testErrorWhenLessWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('exact_len,2', 'n')); } } \ No newline at end of file diff --git a/tests/Validators/MaxLenValidatorTest.php b/tests/Validators/MaxLenValidatorTest.php index 72a56856..b1ad86cd 100644 --- a/tests/Validators/MaxLenValidatorTest.php +++ b/tests/Validators/MaxLenValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class MaxLenValidatorTest @@ -13,18 +14,76 @@ */ class MaxLenValidatorTest extends BaseTestCase { - public function testSuccessWhenEqual() + public function testSuccessWhenEqualWithMbStrlen() { - $this->assertTrue($this->validate('max_len,2', '12')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('max_len,5', 'ñándú')); } - public function testSuccessWhenLess() + public function testSuccessWhenLessWithMbStrlen() { - $this->assertTrue($this->validate('max_len,2', '1')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('max_len,2', 'ñ')); } - public function testErrorWhenMore() + public function testErrorWhenMoreWithMbStrlen() { - $this->assertNotTrue($this->validate('max_len,2', '123')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('max_len,2', 'ñán')); + } + + public function testSuccessWhenEqualWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('max_len,3', 'ña')); + $this->assertTrue($this->validate('max_len,2', 'na')); + } + + public function testSuccessWhenLessWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('max_len,2', 'n')); + } + + public function testErrorWhenMoreWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('max_len,2', 'nan')); } } \ No newline at end of file diff --git a/tests/Validators/MinLenValidatorTest.php b/tests/Validators/MinLenValidatorTest.php index 4325fa95..f3ea76f1 100644 --- a/tests/Validators/MinLenValidatorTest.php +++ b/tests/Validators/MinLenValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class MinLenValidatorTest @@ -13,18 +14,76 @@ */ class MinLenValidatorTest extends BaseTestCase { - public function testSuccessWhenEqual() + public function testSuccessWhenEqualWithMbStrlen() { - $this->assertTrue($this->validate('min_len,2', '12')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('min_len,5', 'ñándú')); } - public function testSuccessWhenMore() + public function testSuccessWhenMoreWithMbStrlen() { - $this->assertTrue($this->validate('min_len,2', '123')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate('min_len,2', 'ñán')); } - public function testErrorWhenLess() + public function testErrorWhenLessWithMbStrlen() { - $this->assertNotTrue($this->validate('min_len,2', '1')); + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate('min_len,2', 'ñ')); + } + + public function testSuccessWhenEqualWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('min_len,3', 'ña')); + $this->assertTrue($this->validate('min_len,2', 'na')); + } + + public function testSuccessWhenMoreWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertTrue($this->validate('min_len,2', 'nan')); + } + + public function testErrorWhenLessWithStrlen() + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + + $this->assertNotTrue($this->validate('min_len,2', 'n')); } } \ No newline at end of file diff --git a/tests/Validators/ValidCcValidatorTest.php b/tests/Validators/ValidCcValidatorTest.php index 0b3066b0..6ac30f36 100644 --- a/tests/Validators/ValidCcValidatorTest.php +++ b/tests/Validators/ValidCcValidatorTest.php @@ -5,6 +5,7 @@ use GUMP; use Exception; use Tests\BaseTestCase; +use Mockery as m; /** * Class ValidCcValidatorTest @@ -18,8 +19,30 @@ class ValidCcValidatorTest extends BaseTestCase /** * @dataProvider successProvider */ - public function testSuccess($input) + public function testSuccessWithMbStrlen($input) { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertTrue($this->validate(self::RULE, $input)); + } + + /** + * @dataProvider successProvider + */ + public function testSuccessWithStrlen($input) + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + $this->assertTrue($this->validate(self::RULE, $input)); } @@ -33,8 +56,30 @@ public function successProvider() /** * @dataProvider errorProvider */ - public function testError($input) + public function testErrorWithMbStrlen($input) { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnTrue(); + + $this->assertNotTrue($this->validate(self::RULE, $input)); + } + + /** + * @dataProvider errorProvider + */ + public function testErrorWithStrlen($input) + { + $externalMock = m::mock('overload:GUMP\Helpers'); + + $externalMock->shouldReceive('functionExists') + ->once() + ->with('mb_strlen') + ->andReturnFalse(); + $this->assertNotTrue($this->validate(self::RULE, $input)); }