-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UniqueFactory for creating random string or int from given range
- Loading branch information
Showing
32 changed files
with
266 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL; | ||
|
||
class UniqueFactory | ||
{ | ||
public static function int(int $min, int $max) : int | ||
{ | ||
return \random_int($min, $max); | ||
} | ||
|
||
/** @param int<1, max> $int */ | ||
public static function string(int $int) : string | ||
{ | ||
$bytes = (int) \ceil($int / 2); | ||
$bytes >= 1 ?: $bytes = 1; | ||
|
||
return \mb_substr(\bin2hex(\random_bytes($bytes)), 0, \max(0, $int)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/core/etl/tests/Flow/ETL/Tests/Unit/UniqueFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Unit; | ||
|
||
use Flow\ETL\UniqueFactory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class UniqueFactoryTest extends TestCase | ||
{ | ||
public static function integers_provider() : \Generator | ||
{ | ||
foreach (range(1, 10) as $i) { | ||
yield [$i]; | ||
} | ||
} | ||
|
||
public static function invalid_range_provider() : array | ||
{ | ||
return [ | ||
'min greater than max' => [2, 1], | ||
]; | ||
} | ||
|
||
public static function valid_range_provider() : array | ||
{ | ||
return [ | ||
'min equal to max' => [1, 1], | ||
'min less than max' => [1, 2], | ||
'min less than zero' => [-1, 1], | ||
'max and min less than zero' => [-1, -1], | ||
]; | ||
} | ||
|
||
public function test_can_create_random_int_from_given_range() : void | ||
{ | ||
self::assertSame(1, (UniqueFactory::int(1, 1))); | ||
self::assertThat( | ||
UniqueFactory::int(1, 2), | ||
self::logicalOr( | ||
self::equalTo(1), | ||
self::equalTo(2) | ||
) | ||
); | ||
} | ||
|
||
/** @dataProvider integers_provider */ | ||
public function test_can_create_random_string_with_given_length(int $expectedLength) : void | ||
{ | ||
self::assertSame($expectedLength, mb_strlen(UniqueFactory::string($expectedLength))); | ||
} | ||
|
||
/** @dataProvider invalid_range_provider */ | ||
public function test_fail_on_invalid_range(int $min, int $max) : void | ||
{ | ||
self::expectException(\ValueError::class); | ||
UniqueFactory::int($min, $max); | ||
} | ||
|
||
/** @dataProvider valid_range_provider */ | ||
public function test_return_random_int_on_valid_range(int $min, int $max) : void | ||
{ | ||
self::assertThat( | ||
UniqueFactory::int($min, $max), | ||
self::logicalOr( | ||
self::greaterThanOrEqual($min), | ||
self::lessThanOrEqual($max) | ||
) | ||
); | ||
} | ||
|
||
public function test_empty_string_on_length_below_1() | ||
{ | ||
self::assertSame( | ||
'', | ||
UniqueFactory::string(0) | ||
); | ||
self::assertSame( | ||
'', | ||
UniqueFactory::string(-1) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.