Add more rng tests

This commit is contained in:
Dennis Eichhorn 2018-10-03 16:38:47 +02:00
parent 52d8b17914
commit 527d6521ac
4 changed files with 34 additions and 31 deletions

View File

@ -48,27 +48,20 @@ class File
/** /**
* Get a random file extension. * Get a random file extension.
* *
* @param array<array<string>> $source Source array for possible extensions * @param array<array<string>> $source Source array for possible extensions
* @param DistributionType|int $distribution Distribution type for the extensions
* *
* @return false|array * @return string
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function generateExtension($source = null, $distribution = DistributionType::UNIFORM) public static function generateExtension(array $source = null) : string
{ {
if ($source === null) { if ($source === null) {
$source = self::$extensions; $source = self::$extensions;
} }
switch ($distribution) { $key = \rand(0, \count($source) - 1);
case DistributionType::UNIFORM:
$key = \rand(0, \count($source) - 1);
break;
default:
return false;
}
return $source[$key][0]; return $source[$key][\rand(0, \count($source[$key]) - 1)];
} }
} }

View File

@ -28,36 +28,46 @@ class Phone
/** /**
* Get a random phone number. * Get a random phone number.
* *
* @param bool $isInt This number uses a country code * @param bool $isInt This number uses a country code
* @param array $layout Number layout * @param string $struct Number layout
* @param array $countries Country codes * @param array<null|array> $size Digits per placeholder [min, max]
* @param array|null $countries Country codes
* *
* @return \DateTime * @return string
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function generatePhone( public static function generatePhone(
$isInt = true, bool $isInt = true,
$layout = [ string $struct = '+$1 ($2) $3-$4',
'struct' => '+$1 ($2) $3-$4', array $size = [null, [3, 4], [3, 5], [3, 8],],
'size' => [null, [3, 4], [3, 5], [3, 8],],], array $countries = null
$countries = null ) : string
) { {
$numberString = $layout['struct']; $numberString = $struct;
if ($isInt) { if ($isInt) {
if ($countries === null) { if ($countries === null) {
$countries = ['de' => 49, 'us' => 1]; $countries = ['de' => 49, 'us' => 1];
} }
$numberString = \str_replace('$1', $countries[array_keys($countries)[rand(0, \count($countries))]], $numberString); $numberString = \str_replace(
'$1',
$countries[\array_keys($countries)[\rand(0, \count($countries) - 1)]],
$numberString
);
} }
$numberParts = substr_count($layout['struct'], '$'); $numberParts = \substr_count($struct, '$');
for ($i = ($isInt ? 2 : 1); $i < $numberParts; ++$i) { for ($i = ($isInt ? 2 : 1); $i <= $numberParts; ++$i) {
$numberString = \str_replace( $numberString = \str_replace(
'$' . $i, StringUtils::generateString($layout['size'][$i - 1][0], $layout['size'][$i - 1][1], '0123456789'), '$' . $i,
StringUtils::generateString(
$size[$i - 1][0],
$size[$i - 1][1],
'0123456789'
),
$numberString $numberString
); );
} }

View File

@ -17,8 +17,8 @@ use phpOMS\Utils\RnG\File;
class FileTest extends \PHPUnit\Framework\TestCase class FileTest extends \PHPUnit\Framework\TestCase
{ {
public function testPlaceholder() public function testRnGExtension()
{ {
self::markTestIncomplete(); self::assertRegExp('/^[a-z]{2,5}$/', File::generateExtension());
} }
} }

View File

@ -17,8 +17,8 @@ use phpOMS\Utils\RnG\Phone;
class PhoneTest extends \PHPUnit\Framework\TestCase class PhoneTest extends \PHPUnit\Framework\TestCase
{ {
public function testPlaceholder() public function testRnG()
{ {
self::markTestIncomplete(); self::assertRegExp('/^\+\d{1,2} \(\d{3,4}\) \d{3,5}\-\d{3,8}$/', Phone::generatePhone());
} }
} }