-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from v1p3r75/main
Added Email Rule
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// .. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}); |