From 96a0454e67a6966302be792e07a1b4a6778a7019 Mon Sep 17 00:00:00 2001 From: Davis Peixoto Date: Sun, 23 Jul 2017 23:57:49 -0300 Subject: [PATCH] Email validation rule Signed-off-by: Davis Peixoto --- .scrutinizer.yml | 1 - src/BlogCore/Entity/Author.php | 7 +++++-- tests/Entity/TestAuthor.php | 26 +++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 6b28b5a..52fc874 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -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: diff --git a/src/BlogCore/Entity/Author.php b/src/BlogCore/Entity/Author.php index 0846f89..f953576 100644 --- a/src/BlogCore/Entity/Author.php +++ b/src/BlogCore/Entity/Author.php @@ -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; + } } /** diff --git a/tests/Entity/TestAuthor.php b/tests/Entity/TestAuthor.php index 7da2c21..359c6ce 100644 --- a/tests/Entity/TestAuthor.php +++ b/tests/Entity/TestAuthor.php @@ -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'] + ]; + } }