Skip to content

Commit

Permalink
Email validation rule
Browse files Browse the repository at this point in the history
Signed-off-by: Davis Peixoto <davis.peixoto@gmail.com>
  • Loading branch information
davispeixoto committed Jul 24, 2017
1 parent 052020f commit 96a0454
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 0 additions & 1 deletion .scrutinizer.yml
Expand Up @@ -29,7 +29,6 @@ checks:
interface_name: '^[A-Z][a-zA-Z0-9]*Interface$'
type_name: '^[A-Z][a-zA-Z0-9]*$'
exception_name: '^[A-Z][a-zA-Z0-9]*Exception$'
isser_method_name: '^(?:is|has|should|may|supports)'
newline_at_end_of_file: true
no_goto: true
no_long_variable_names:
Expand Down
7 changes: 5 additions & 2 deletions src/BlogCore/Entity/Author.php
Expand Up @@ -107,12 +107,15 @@ public function getEmail(): string
}

/**
* @codeCoverageIgnore
* @param string $email
*/
public function setEmail(string $email)
{
$this->email = $email;
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if ($email !== false) {
$this->email = $email;
}
}

/**
Expand Down
26 changes: 25 additions & 1 deletion tests/Entity/TestAuthor.php
Expand Up @@ -25,16 +25,40 @@ class TestAuthor extends PHPUnit_Framework_TestCase
* @param $message
* @dataProvider authorConstructorProvider
*/
public function shouldConstructAuthor($uuid, $name, $email, $bio, $birthdate, $expected, $message)
public function testConstructor($uuid, $name, $email, $bio, $birthdate, $expected, $message)
{
$author = new Author($uuid, $name, $email, $bio, $birthdate);
$this->assertInstanceOf($expected, $author, $message);
}

/**
* @param Author $author
* @param $newEmailAddress
* @param $expected
* @param $message
* @dataProvider emailProvider
*/
public function testEmailShouldBeValid(Author $author, $newEmailAddress, $expected, $message)
{
$author->setEmail($newEmailAddress);
$email = $author->getEmail();
$this->assertEquals($expected, $email, $message);
}

public function authorConstructorProvider()
{
return [
[Uuid::uuid4(), 'Davis', 'email@example.org', 'Some string', new DateTime(), Author::class, 'Regular test'],
];
}

public function emailProvider()
{
$author = new Author(Uuid::uuid4(), 'Davis', 'email@example.org', 'Foo', new DateTime());

return [
[$author, 'example.org', 'email@example.org', 'Negative test'],
[$author, 'email2@example.org', 'email2@example.org', 'Positive test']
];
}
}

0 comments on commit 96a0454

Please sign in to comment.