Skip to content

Commit

Permalink
Merge pull request #2 from v1p3r75/main
Browse files Browse the repository at this point in the history
Added Email Rule
  • Loading branch information
BlakvGhost authored Nov 16, 2023
2 parents 53a1d7d + 8599098 commit b9a8d78
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
2 changes: 2 additions & 0 deletions src/LangManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LangManager
'validation.string_rule' => "Le champ :attribute doit être une chaîne de caractères.",
'validation.required_rule' => "Le champ :attribute est requis.",
'validation.max_length_rule' => "Le champ :attribute ne doit pas dépasser :max caractères.",
'validation.email_rule' => "Le champ :attribute doit être un e-mail."
],
'en' => [
// English translations
Expand All @@ -36,6 +37,7 @@ class LangManager
'validation.string_rule' => "The :attribute field must be a string.",
'validation.required_rule' => "The :attribute field is required.",
'validation.max_length_rule' => "The :attribute field must not exceed :max characters.",
'validation.email_rule' => "The :attribute field must be a email.",
],
];

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

/**
* Email - A validation rule implementation for checking if a field is a valid email
*
* @package BlakvGhost\PHPValidator\Rules
* @author Fortunatus KIDJE (v1p3r75)
* @github https://github.com/v1p3r75
*/

namespace BlakvGhost\PHPValidator\Rules;

use BlakvGhost\PHPValidator\LangManager;

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

/**
* Constructor of the RequiredRule class.
*
* @param array $parameters Parameters for the rule, if any.
*/
public function __construct(protected array $parameters)
{
// No specific logic needed in the constructor for this rule.
}

/**
* Check if the given field is a valid email.
*
* @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 field is required and not empty, false otherwise.
*/
public function passes(string $field, $value, array $data): bool
{
// Set the field property for use in the message method.
$this->field = $field;

// Check if the field is set in the data and not empty.
return (bool)filter_var($value, FILTER_VALIDATE_EMAIL);
}

/**
* Get the validation error message for the required rule.
*
* @return string Validation error message.
*/
public function message(): string
{

// Use LangManager to get a translated validation error message.
return LangManager::getTranslation('validation.email_rule', [
'attribute' => $this->field,
]);
}
}
8 changes: 8 additions & 0 deletions tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use BlakvGhost\PHPValidator\Rules\EmailRule;
use BlakvGhost\PHPValidator\Rules\StringRule;

it('validates string rule successfully')
->does(fn () => (new EmailRule([]))->passes('name', 'John', []))
->assertTrue();
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}
14 changes: 14 additions & 0 deletions tests/Unit/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
use BlakvGhost\PHPValidator\Rules\EmailRule;
use BlakvGhost\PHPValidator\Rules\RequiredRule;
use function PHPUnit\Framework\assertTrue;

it('should validate email successfully', function() {

$result = new EmailRule([]);

//($result);

assertTrue(true);

});

0 comments on commit b9a8d78

Please sign in to comment.