From 57b411b59f09c364b77a948bc23d011e30208449 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Wed, 17 Jan 2018 21:37:07 +0100 Subject: [PATCH] Tests - increase code coverage --- src/Utilities/Regex.php | 76 ++++++++++++++------ tests/Utilities/RegexTest.php | 127 ++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 23 deletions(-) diff --git a/src/Utilities/Regex.php b/src/Utilities/Regex.php index 525720a..c0a5dd1 100644 --- a/src/Utilities/Regex.php +++ b/src/Utilities/Regex.php @@ -25,7 +25,7 @@ class Regex * @var array */ private static $patterns = [ - 'email' => '/[\w-]{2,}@[\w-]+\.[\w]{2,}+/', + 'email' => '/^[\w-.]{2,}@[\w-]+\.[\w]{2,}+$/', 'phone' => '/^\+?[0-9 ]+$/', 'camelCasePart' => '/([a-z]|[A-Z]){1}[a-z]*/', 'urlProtocol' => '/^([a-z]+:\/\/)', @@ -59,6 +59,10 @@ class Regex */ public static function isValidEmail($email) { + if (!is_string($email)) { + return false; + } + $pattern = self::getEmailPattern(); return (bool)preg_match($pattern, $email); @@ -72,30 +76,56 @@ class Regex */ public static function isValidTaxId($taxIdString) { - if (!empty($taxIdString)) { - $weights = [ - 6, - 5, - 7, - 2, - 3, - 4, - 5, - 6, - 7, - ]; - $taxId = preg_replace('/[\s-]/', '', $taxIdString); - $sum = 0; + /* + * Not a string? + * Nothing to do + */ + if (!is_string($taxIdString)) { + return false; + } - if (10 == strlen($taxId) && is_numeric($taxId)) { - for ($x = 0; $x <= 8; ++$x) { - $sum += $taxId[$x] * $weights[$x]; - } + /* + * Empty/Unknown value? + * Nothing to do + */ + if (empty($taxIdString)) { + return false; + } - if ((($sum % 11) % 10) == $taxId[9]) { - return true; - } - } + $taxId = preg_replace('/[\s-]/', '', $taxIdString); + + /* + * Tax ID is not 10 characters length OR is not numeric? + * Nothing to do + */ + if (10 !== strlen($taxId) || !is_numeric($taxId)) { + return false; + } + + $weights = [ + 6, + 5, + 7, + 2, + 3, + 4, + 5, + 6, + 7, + ]; + + $sum = 0; + + for ($x = 0; $x <= 8; ++$x) { + $sum += $taxId[$x] * $weights[$x]; + } + + /* + * Last number it's not a remainder from dividing per 11? + * Nothing to do + */ + if ($sum % 11 == $taxId[9]) { + return true; } return false; diff --git a/tests/Utilities/RegexTest.php b/tests/Utilities/RegexTest.php index f040190..d95b389 100644 --- a/tests/Utilities/RegexTest.php +++ b/tests/Utilities/RegexTest.php @@ -355,6 +355,46 @@ class RegexTest extends BaseTestCase self::assertEquals($expected, Regex::isBinaryValue($value)); } + /** + * @param mixed $emptyValue Empty value, e.g. "" + * @dataProvider provideEmptyValue + */ + public static function testIsValidEmailUsingEmptyValue($emptyValue) + { + self::assertFalse(Regex::isValidEmail($emptyValue)); + } + + /** + * @param string $email E-mail address to validate / verify + * @param bool $expected Information if e-mail is valid + * + * @dataProvider provideEmail + */ + public static function testIsValidEmail($email, $expected) + { + self::assertEquals($expected, Regex::isValidEmail($email)); + } + + /** + * @param mixed $emptyValue Empty value, e.g. "" + * @dataProvider provideEmptyValue + */ + public static function testIsValidTaxIdUsingEmptyValue($emptyValue) + { + self::assertFalse(Regex::isValidTaxId($emptyValue)); + } + + /** + * @param string $taxIdString Tax ID (NIP) string + * @param bool $expected Information if tax ID is valid + * + * @dataProvider provideTaxId + */ + public static function testIsValidTaxId($taxIdString, $expected) + { + self::assertEquals($expected, Regex::isValidTaxId($taxIdString)); + } + /** * Provides name of bundle and information if it's valid name * @@ -540,6 +580,93 @@ class RegexTest extends BaseTestCase ]; } + /** + * Provides e-mail and information if it's valid + * + * @return Generator + */ + public function provideEmail() + { + yield[ + '1', + false, + ]; + + yield[ + 1, + false, + ]; + + yield[ + 'a@a', + false, + ]; + + yield[ + 'a@a.com', + false, + ]; + + yield[ + 'aa@a.com', + true, + ]; + + yield[ + 'a.b@d.com', + true, + ]; + } + + /** + * Provides tax ID and information if it's valid + * + * @return Generator + */ + public function provideTaxId() + { + yield[ + '123', + false, + ]; + + yield[ + '12345', + false, + ]; + + yield[ + '1122334455', + false, + ]; + + yield[ + '1234567890', + false, + ]; + + yield[ + '0987654321', + false, + ]; + + /* + * Microsoft sp. z o.o. + */ + yield[ + '5270103391', + true, + ]; + + /* + * Onet S.A. + */ + yield[ + '7340009469', + true, + ]; + } + /** * {@inheritdoc} */