Skip to content

Commit

Permalink
Added Size Test
Browse files Browse the repository at this point in the history
  • Loading branch information
v1p3r75 committed Nov 17, 2023
1 parent 0121f02 commit 4d8bf7a
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/LangManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class LangManager
'validation.json' => "Le champ :attribute doit être un json valide.",
'validation.url' => "Le champ :attribute doit être un url valide.",
'validation.valid_ip' => "Le champ :attribute doit être une addresse ip valide.",
'validation.size' => "Le champ :attribute doit avoir la longeur réquise :value.",
],
'en' => [
// English translations
Expand Down Expand Up @@ -76,6 +77,7 @@ class LangManager
'validation.json' => "The :attribute field must be a valid json.",
'validation.url' => "The :attribute field must be a valid url.",
'validation.valid_ip' => "The :attribute field must be a valid IP address.",
'validation.size' => "The :attribute field must have the required length :value.",
],
];

Expand Down
95 changes: 95 additions & 0 deletions src/Rules/SizeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/**
* InRule - A validation rule implementation for checking value size.
*
* @package BlakvGhost\PHPValidator\Rules
* @author Fortunatus KIDJE (v1p3r75)
* @github https://github.com/v1p3r75
*/

namespace BlakvGhost\PHPValidator\Rules;

use BlakvGhost\PHPValidator\LangManager;

class SizeRule implements RuleInterface
{
/**
* Name of the field being validated.
*
* @var string
*/
protected $field;

/**
* Constructor of the InRule class.
*
* @param array $parameters Parameters for the rule, specifying the list of valid values.
*/
public function __construct(protected array $parameters)
{
// No specific logic needed in the constructor for this rule.
}

/**
* Check value size.
*
* @param string $field Name of the field being validated.
* @param mixed $value Value of the field being validated.
* @param array $data All validation data.
* @return bool True if the value have a valid size, false otherwise.
*/
public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->field = $field;

$total = $this->parameters[0] ?? 0;

if (is_string($value)) {

return strlen($value) == $total;
}

if (is_int($value)) {

return $value == $total;
}

if (is_array($value)) {

return count($value) == $total;
}

if (is_file($value)) {

if (isset($_FILES[$value]) && $_FILES[$value]["error"] == 0) {
// Get the file size in bytes
$size = $_FILES[$value]["size"];

// Convert bytes to kilobytes
$size_kb = $size / 1024; // kilobytes

return $size_kb == $total;
}

return false;
}

return false;
}

/**
* Get the validation error message for this rule.
*
* @return string Validation error message.
*/
public function message(): string
{
// Use LangManager to get a translated validation error message.
return LangManager::getTranslation('validation.size', [
'attribute' => $this->field,
'value' => $this->parameters[0]
]);
}
}
57 changes: 57 additions & 0 deletions tests/Feature/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use BlakvGhost\PHPValidator\Rules\JsonRule;
use BlakvGhost\PHPValidator\Rules\LowerRule;
use BlakvGhost\PHPValidator\Rules\RequiredWithRule;
use BlakvGhost\PHPValidator\Rules\SizeRule;
use BlakvGhost\PHPValidator\Rules\StringRule;
use BlakvGhost\PHPValidator\Rules\RequiredRule;
use BlakvGhost\PHPValidator\Rules\MaxLengthRule;
Expand Down Expand Up @@ -378,4 +379,60 @@
'attribute' => 'field',
])
);
});

it('validates size rule (string) successfully', function () {
$validator = new SizeRule([4]);

expect($validator->passes('field', "azerty", []))->toBeFalse();
expect($validator->passes('field', 'azer', []))->toBeTrue();

expect($validator->message())->toBe(
LangManager::getTranslation('validation.size', [
'attribute' => 'field',
'value' => 4,
])
);
});

it('validates size rule (integer) successfully', function () {
$validator = new SizeRule([3]);

expect($validator->passes('field', 6, []))->toBeFalse();
expect($validator->passes('field', 3, []))->toBeTrue();

expect($validator->message())->toBe(
LangManager::getTranslation('validation.size', [
'attribute' => 'field',
'value' => 3,
])
);
});

it('validates size rule (array) successfully', function () {
$validator = new SizeRule([2]);

expect($validator->passes('field', ['key1', 'key2', 'key3'], []))->toBeFalse();
expect($validator->passes('field', ['key1', 'key2'], []))->toBeTrue();

expect($validator->message())->toBe(
LangManager::getTranslation('validation.size', [
'attribute' => 'field',
'value' => 2,
])
);
});

it('validates size rule (file) successfully', function () {
$validator = new SizeRule([512]);

expect($validator->passes('field', __FILE__, []))->toBeFalse();
// expect($validator->passes('field', __FILE__, []))->toBeTrue();

expect($validator->message())->toBe(
LangManager::getTranslation('validation.size', [
'attribute' => 'field',
'value' => 512,
])
);
});

0 comments on commit 4d8bf7a

Please sign in to comment.