Skip to content

Commit

Permalink
Added the following validator: isEmail.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Nobrega committed Feb 21, 2018
1 parent 25662ae commit fc46cc1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Add the following validators: isEmail and isUrl.
- Add the following validator isUrl.
- Refactor unit tests to mock also PHP built-in methods.
- Add type casting.

## [1.0.4] - 2018-02-21
### Added
- Added the following validator: isEmail.

## [1.0.3] - 2018-02-20
### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lfbn/base-model",
"version": "1.0.3",
"version": "1.0.4",
"description": "This is a Base Model that can be extended to define Models. It helps handling data validation, and data conversion.",
"keywords": ["validation", "model"],
"homepage": "https://github.com/lfbn/base-model",
Expand Down
13 changes: 13 additions & 0 deletions src/Helpers/ValidatorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,17 @@ public function isArray($value)

return false;
}

/**
* @param string $value
* @return boolean
*/
public function isEmail($value)
{
if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
return true;
}

return false;
}
}
51 changes: 48 additions & 3 deletions tests/Helpers/ValidatorHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testIfIsNumericFails($input)

/**
* @param integer $input
* @dataProvider numericProvider
* @dataProvider numericProvider
*/
public function testIfIsNumericSucceeds($input)
{
Expand Down Expand Up @@ -256,7 +256,7 @@ public function testIfIsNotNullFails($input)
}

/**
* @param null $input
* @param mixed $input
* @dataProvider notNullProvider
*/
public function testIfIsNotNullSucceeds($input)
Expand All @@ -278,7 +278,7 @@ public function testIfIsArrayFails($input)
}

/**
* @param null $input
* @param array $input
* @dataProvider arrayProvider
*/
public function testIfIsArraySucceeds($input)
Expand All @@ -288,6 +288,32 @@ public function testIfIsArraySucceeds($input)
);
}

/**
* @param string $input
* @dataProvider notEmailProvider
*/
public function testIfIsEmailFails($input)
{
$this->assertFalse(
$this->validatorHelperMock->isEmail($input)
);
}

/**
* @param string $input
* @dataProvider emailProvider
*/
public function testIfIsEmailSucceeds($input)
{
$this->assertTrue(
$this->validatorHelperMock->isEmail($input)
);
}

/**
* PROVIDERS
*/

/**
* @return array
*/
Expand Down Expand Up @@ -471,4 +497,23 @@ public function notArrayProvider()
'Boolean' => [true]
];
}

public function emailProvider()
{
return [
'Valid e-mail' => ['test@test.com'],
'Valid e-mail with several dots' => ['test.test.test@test.com'],
'Valid e-mail with plus sign' => ['test+test@test.com'],
'Valid e-mail with underscore' => ['test_test@test.com']
];
}

public function notEmailProvider()
{
return [
'Invalid e-mail without domain' => ['test'],
'Invalid e-mail with only domain' => ['@test.com'],
'Invalid e-mail with two at' => ['test@test@.com']
];
}
}

0 comments on commit fc46cc1

Please sign in to comment.