Tests - increase code coverage

This commit is contained in:
Meritoo
2018-01-17 21:37:07 +01:00
parent 8a4860088f
commit 57b411b59f
2 changed files with 180 additions and 23 deletions

View File

@@ -25,7 +25,7 @@ class Regex
* @var array * @var array
*/ */
private static $patterns = [ private static $patterns = [
'email' => '/[\w-]{2,}@[\w-]+\.[\w]{2,}+/', 'email' => '/^[\w-.]{2,}@[\w-]+\.[\w]{2,}+$/',
'phone' => '/^\+?[0-9 ]+$/', 'phone' => '/^\+?[0-9 ]+$/',
'camelCasePart' => '/([a-z]|[A-Z]){1}[a-z]*/', 'camelCasePart' => '/([a-z]|[A-Z]){1}[a-z]*/',
'urlProtocol' => '/^([a-z]+:\/\/)', 'urlProtocol' => '/^([a-z]+:\/\/)',
@@ -59,6 +59,10 @@ class Regex
*/ */
public static function isValidEmail($email) public static function isValidEmail($email)
{ {
if (!is_string($email)) {
return false;
}
$pattern = self::getEmailPattern(); $pattern = self::getEmailPattern();
return (bool)preg_match($pattern, $email); return (bool)preg_match($pattern, $email);
@@ -72,30 +76,56 @@ class Regex
*/ */
public static function isValidTaxId($taxIdString) public static function isValidTaxId($taxIdString)
{ {
if (!empty($taxIdString)) { /*
$weights = [ * Not a string?
6, * Nothing to do
5, */
7, if (!is_string($taxIdString)) {
2, return false;
3, }
4,
5,
6,
7,
];
$taxId = preg_replace('/[\s-]/', '', $taxIdString);
$sum = 0;
if (10 == strlen($taxId) && is_numeric($taxId)) { /*
for ($x = 0; $x <= 8; ++$x) { * Empty/Unknown value?
$sum += $taxId[$x] * $weights[$x]; * Nothing to do
} */
if (empty($taxIdString)) {
return false;
}
if ((($sum % 11) % 10) == $taxId[9]) { $taxId = preg_replace('/[\s-]/', '', $taxIdString);
return true;
} /*
} * 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; return false;

View File

@@ -355,6 +355,46 @@ class RegexTest extends BaseTestCase
self::assertEquals($expected, Regex::isBinaryValue($value)); 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 * 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} * {@inheritdoc}
*/ */