Skip to content

Commit

Permalink
Add more test cases and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
filisko committed Apr 29, 2024
1 parent f308ee6 commit 9d1230e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 101 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ GUMP::add_validator("equals_string", function($field, array $input, array $param
return $value === $params;
}, 'Field {field} does not equal to {param}.');

// You might want to check whether a validator exists first
GUMP::has_validator($rule);

/**
* @param string $value Value
* @param array $param Filter parameters (optional)
Expand All @@ -264,6 +267,9 @@ GUMP::add_validator("equals_string", function($field, array $input, array $param
GUMP::add_filter("upper", function($value, array $params = []) {
return strtoupper($value);
});

// You might want to check whether a filter first already
GUMP::has_filter($rule);
```

Alternately, you can simply create your own class that extends GUMP. You only have to have in mind:
Expand Down
49 changes: 26 additions & 23 deletions gump.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,32 @@ public static function add_filter(string $rule, callable $callback)
self::$filter_methods[$rule] = $callback;
}

/**
* Checks if a validator exists.
*
* @param string $rule
*
* @return bool
*/
public static function has_validator(string $rule)
{
return method_exists(__CLASS__, self::validator_to_method($rule)) || isset(self::$validation_methods[$rule]);
}

/**
* Checks if a filter exists.
*
* @param string $filter
*
* @return bool
*/
public static function has_filter(string $filter)
{
return method_exists(__CLASS__, self::filter_to_method($filter))
|| isset(self::$filter_methods[$filter])
|| function_exists($filter);
}

/**
* Helper method to extract an element from an array safely
*
Expand Down Expand Up @@ -1950,27 +1976,4 @@ protected function validate_valid_array_size_equal($field, array $input, array $
{
return !(!is_array($input[$field]) || count($input[$field]) != $params[0]);
}

/**
* Checks if a validator method exists for a given rule.
*
* @param string $rule
* @return bool
*/
public static function has_validator(string $rule): bool
{
return method_exists(__CLASS__, self::validator_to_method($rule)) || isset(self::$validation_methods[$rule]);
}

/**
* Checks if a filter method exists for a given filter.
* @param string $filter
* @return bool
*/
public static function has_filter(string $filter): bool
{
return method_exists(__CLASS__, self::filter_to_method($filter))
|| isset(self::$filter_methods[$filter])
|| function_exists($filter);
}
}
52 changes: 25 additions & 27 deletions tests/StaticHasFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,38 @@

class StaticHasFilterTest extends BaseTestCase
{
public function testHasFilterWhenExists(): void
public function dataOfExistingRules()
{
$filterRules = [
return [
// There are native filters
'noise_words',
'rmpunctuation',
'urlencode',
'htmlencode',
'sanitize_email',
'sanitize_numbers',
'sanitize_floats',
'sanitize_string',
'boolean',
'basic_tags',
'whole_number',
'ms_word_characters',
'lower_case',
'upper_case',
'slug',
['noise_words'],
['upper_case'],
['slug'],
// These are built-in functions
'trim',
'strtoupper',
'strtolower',
'intval',
'floatval',
['trim'],
['strtoupper'],
];
}

/**
* @dataProvider dataOfExistingRules
*/
public function testHasFilterWhenExists(string $rule)
{
$this->assertTrue(GUMP::has_filter($rule));
}

public function testHasFilterWithCustomRule(): void
{
GUMP::add_filter('test', function($value, array $params = []) {
return strtoupper($value);
});

foreach ($filterRules as $filterRule) {
$this->assertTrue(GUMP::has_filter($filterRule));
}
$this->assertTrue(GUMP::has_filter('test'));
}

public function testHasFilterWhenNotExists(): void
{
$this->assertFalse(GUMP::has_filter('custom_filter'));
$this->assertFalse(GUMP::has_filter('test'));
}
}
}
76 changes: 25 additions & 51 deletions tests/StaticHasValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,35 @@

class StaticHasValidatorTest extends BaseTestCase
{
public function testHasValidatorWhenExists(): void
public function dataOfExistingRules()
{
$validationRules = [
'required',
'contains',
'contains_list',
'doesnt_contain_list',
'boolean',
'valid_email',
'max_len',
'min_len',
'exact_len',
'between_len',
'alpha',
'alpha_numeric',
'alpha_dash',
'alpha_numeric_dash',
'alpha_numeric_space',
'alpha_space',
'numeric',
'integer',
'float',
'valid_url',
'url_exists',
'valid_ip',
'valid_ipv4',
'valid_ipv6',
'valid_cc',
'valid_name',
'street_address',
'iban',
'date',
'min_age',
'max_numeric',
'min_numeric',
'starts',
'required_file',
'extension',
'equalsfield',
'guidv4',
'phone_number',
'regex',
'valid_json_string',
'valid_array_size_greater',
'valid_array_size_lesser',
'valid_array_size_equal',
return [
['required'],
['contains'],
['regex'],
['valid_array_size_equal'],
];
}

/**
* @dataProvider dataOfExistingRules
*/
public function testHasValidatorWhenExists(string $rule)
{
$this->assertTrue(GUMP::has_validator($rule));
}

public function testHasValidatorWithCustomRule()
{
GUMP::add_validator("equals_string", function($field, array $input, array $params, $value) {
return $value === $params;
}, 'Field {field} does not equal to {param}.');

foreach ($validationRules as $rule) {
$this->assertTrue(GUMP::has_validator($rule));
}
$this->assertTrue(GUMP::has_validator('equals_string'));
}

public function testHasValidatorWhenNotExists(): void
public function testHasValidatorWhenDoesntExist()
{
$this->assertFalse(GUMP::has_validator('custom_rule'));
$this->assertFalse(GUMP::has_validator('equals_string'));
}
}
}

0 comments on commit 9d1230e

Please sign in to comment.