8889841cGeneratorInterface.php000064400000000705150524607730011037 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen; interface GeneratorInterface { /** * Outputs a generated key including the prefix and suffix if any. * May also return transformed keys. * * @return string */ public function generate(); } GeneratorFactory.php000064400000001500150524607730010540 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen; use InvalidArgumentException; class GeneratorFactory { /** * Create a generator instance from the specified type. * * @param string $type Generator type. * @return Keygen\Generator * * @throws \InvalidArgumentException */ public static function create($type) { $generator = sprintf("Keygen\Generators\%sGenerator", ucfirst($type)); if (class_exists($generator)) { $generator = new $generator; if ($generator instanceof Generator) { return $generator; } } throw new InvalidArgumentException('Cannot create unknown generator type.'); } } Traits/GeneratorMutation.php000064400000003543150524607730012210 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Traits; use InvalidArgumentException; use Keygen\AbstractGenerator; trait GeneratorMutation { use FlattenArguments; /** * A collection of mutable generators to receive property mutations. * * @var array */ protected $mutates = []; /** * Object difference comparator using array_diff callback. * * @param array $array1 * @param array $array2 * @return array */ private static function objectDiffInArrays($array1, $array2) { return array_udiff($array1, $array2, function($a, $b) { if ($a === $b) { return 0; } elseif ($a < $b) { return -1; } elseif ($a > $b) { return 1; } }); } /** * Add mutable generators to the mutates collection * * @param mixed $objects * @return $this * * @throws \InvalidArgumentException */ public function mutate($objects) { $objects = call_user_func_array(array($this, 'flattenArguments'), func_get_args()); $collect = []; foreach ($objects as $obj) { if ($obj instanceof AbstractGenerator) { array_push($collect, $obj); continue; } throw new InvalidArgumentException(sprintf('Mutable objects must be instances of %s.', AbstractGenerator::class)); } $this->mutates = array_merge(static::objectDiffInArrays($this->mutates, $collect), $collect); return $this; } /** * Remove generators from the mutates collection * * @param mixed $objects * @return $this */ public function dontMutate($objects) { $objects = call_user_func_array(array($this, 'flattenArguments'), func_get_args()); $this->mutates = static::objectDiffInArrays($this->mutates, $objects); return $this; } } Traits/FlattenArguments.php000064400000001242150524607730012016 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Traits; trait FlattenArguments { /** * Flattens its arguments array into a simple array. * * @return array */ protected function flattenArguments() { $args = func_get_args(); $flat = []; foreach ($args as $arg) { if (is_array($arg)) { $flat = call_user_func_array(array($this, 'flattenArguments'), array_merge($flat, $arg)); continue; } array_push($flat, $arg); } return $flat; } } Traits/IntegerCasting.php000064400000001341150524607730011441 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Traits; use InvalidArgumentException; trait IntegerCasting { /** * Casts the given value into an integer and returns the integer. * Throws an exception if value cannot be casted to an integer. * * @param mixed $value * @return int * * @throws \InvalidArgumentException */ protected function intCast($value) { if (is_numeric($value)) { return intval($value); } throw new InvalidArgumentException('The given value cannot be converted to an integer.'); } } Traits/MutableGenerator.php000064400000003062150524607730011775 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Traits; use InvalidArgumentException; use Keygen\AbstractGenerator; trait MutableGenerator { use FlattenArguments; /** * A collection of mutable attributes. * * @var array */ protected $mutables = []; /** * Add mutable attributes to the mutables collection * * @param mixed $props * @return $this * * @throws \InvalidArgumentException */ public function mutable($props) { $props = call_user_func_array(array($this, 'flattenArguments'), func_get_args()); $collect = $unknown = []; foreach ($props as $prop) { if (!property_exists(AbstractGenerator::class, $prop)) { array_push($unknown, $prop); continue; } array_push($collect, $prop); } if (!empty($unknown)) { throw new InvalidArgumentException(sprintf("Cannot add unknown %s to mutables collection ('%s').", (count($unknown) > 1) ? 'properties' : 'property', join("', '", $unknown))); } $this->mutables = array_merge(array_diff($this->mutables, $collect), $collect); return $this; } /** * Remove attributes from the mutables collection * * @param mixed $props * @return $this */ public function immutable($props) { $props = call_user_func_array(array($this, 'flattenArguments'), func_get_args()); $this->mutables = array_diff($this->mutables, $props); return $this; } } Traits/KeyManipulation.php000064400000006617150524607730011657 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Traits; use BadMethodCallException; use InvalidArgumentException; trait KeyManipulation { use IntegerCasting; /** * Length of keys to be generated by the generator. * * @var int */ protected $length; /** * Key prefix. * * @var string */ protected $prefix; /** * Key suffix. * * @var string */ protected $suffix; /** * Propagates property mutation to listed mutable generators. * * @param string $prop * @param bool $propagate * @return $this */ protected function propagateMutation($prop, $propagate) { $propagate = !is_bool($propagate) ? true : $propagate; if ($propagate && isset($this->mutates)) { foreach ($this->mutates as $obj) { if (in_array($prop, $obj->mutables)) { call_user_func(array($obj, $prop), $this->{$prop}); } } } return $this; } /** * Sets the length of keys to be generated by the generator. * * @param numeric $length * @param bool $propagate * @return $this * * @throws \InvalidArgumentException */ protected function length($length, $propagate = true) { $this->length = $this->intCast($length ?: $this->length); return $this->propagateMutation('length', $propagate); } /** * Affixes string to generated keys. * * @param string $affix Affix type (either 'prefix' or 'suffix') * @param string $value * @param bool $propagate * @return $this * * @throws \InvalidArgumentException */ protected function affix($affix, $value, $propagate = true) { $affixes = ['prefix', 'suffix']; if (in_array($affix, $affixes)) { if (is_scalar($value)) { $this->{$affix} = strval($value); return $this->propagateMutation($affix, $propagate); } throw new InvalidArgumentException("The given {$affix} cannot be converted to a string."); } } /** * Prepends string to generated keys. * * @param string $prefix * @param bool $propagate * @return $this * * @throws \InvalidArgumentException */ protected function prefix($prefix, $propagate = true) { return $this->affix('prefix', $prefix, $propagate); } /** * Appends string to generated keys. * * @param string $suffix * @param bool $propagate * @return $this * * @throws \InvalidArgumentException */ protected function suffix($suffix, $propagate = true) { return $this->affix('suffix', $suffix, $propagate); } /** * Gets the key length less the prefix length and suffix length. * * @return int */ protected function getAdjustedKeyLength() { return $this->length - intval(strlen($this->prefix) + strlen($this->suffix)); } /** * Overload helper for internal method calls. * * @param string $method * @param array $args * @return $this * * @throws \BadMethodCallException */ protected function __overloadMethods($method, $args) { $_method = strtolower($method); if (in_array($_method, ['prefix', 'suffix'])) { return call_user_func_array(array($this, 'affix'), array_merge([$_method], $args)); } if ($_method == 'length') { return call_user_func_array(array($this, 'length'), $args); } throw new BadMethodCallException(sprintf("Call to unknown method %s::%s()", get_called_class(), $method)); } } Keygen.php000064400000003570150524607730006515 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen; /** * @method static Generator token(int $length) * @method static Generator numeric(int $length) * @method static Generator alphanum(int $length) * @method static Generator bytes(int $length) */ class Keygen extends AbstractGenerator { /** * Creates a new generator instance of the given type. * * @param string $type Generator type * @param mixed $length * @return Keygen\Generator * * @throws \InvalidArgumentException */ protected function newGenerator($type, $length = null) { $generator = GeneratorFactory::create($type)->mutate($this)->length($length ?: $this->length); if (isset($this->prefix)) { $generator->prefix($this->prefix); } if (isset($this->suffix)) { $generator->suffix($this->suffix); } return $generator; } /** * Creates a new generator instance from the given alias. * * @param string $alias Generator type alias * @param mixed $length * @return Keygen\Generator | null * * @throws \InvalidArgumentException */ protected function newGeneratorFromAlias($alias, $length = null) { $generatorAliases = [ 'numeric' => 'numeric', 'alphanum' => 'alphaNumeric', 'token' => 'token', 'bytes' => 'randomByte' ]; if (array_key_exists($alias, $generatorAliases)) { return $this->newGenerator($generatorAliases[$alias], $length); } } /** * Overload the __call method */ public function __call($method, $args) { $_method = strtolower($method); $generator = $this->newGeneratorFromAlias($_method, isset($args[0]) ? $args[0] : null); if ($generator instanceof Generator) { return $generator; } return parent::__call($method, $args); } } Generator.php000064400000002257150524607730007222 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen; abstract class Generator extends AbstractGenerator implements GeneratorInterface { /** * Generates a random key. * * @param numeric $length * @return string */ abstract protected function keygen($length); /** * Outputs a generated key including the prefix and suffix if any. * May also return transformed keys. * * @return string */ public function generate() { $args = func_get_args(); $useKeyLength = array_shift($args); if (!is_bool($useKeyLength)) { array_unshift($args, $useKeyLength); $useKeyLength = false; } $callables = call_user_func_array(array($this, 'flattenArguments'), $args); $key = $this->keygen($useKeyLength ? $this->length : $this->getAdjustedKeyLength()); while ($callable = current($callables)) { if (is_callable($callable)) { $key = call_user_func($callable, $key); } next($callables); } return sprintf("%s%s%s", $this->prefix, $key, $this->suffix); } } Generators/NumericGenerator.php000064400000001264150524607730012653 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Generators; use Keygen\Generator; class NumericGenerator extends Generator { /** * Generates a random key. * * @param numeric $length * @return string */ protected function keygen($length) { $chars = str_shuffle('3759402687094152031368921'); $chars = str_shuffle(str_repeat($chars, ceil($length / strlen($chars)))); return strrev(str_shuffle(substr($chars, mt_rand(0, (strlen($chars) - $length - 1)), $length))); } } Generators/AlphaNumericGenerator.php000064400000002422150524607730013616 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Generators; use Keygen\Generator; class AlphaNumericGenerator extends Generator { /** * Generates a random key. * * @param numeric $length * @return string */ protected function keygen($length) { $key = ''; $chars = array_merge(range(0, 9), range('a', 'z'), range(0, 9), range('A', 'Z'), range(0, 9)); shuffle($chars); $chars = str_shuffle(str_rot13(join('', $chars))); $split = intval(ceil($length / 5)); $size = strlen($chars); $splitSize = ceil($size / $split); $chunkSize = 5 + $splitSize + mt_rand(1, 5); $chunkArray = array(); $chars = str_shuffle(str_repeat($chars, 2)); $size = strlen($chars); while ($split != 0) { $strip = substr($chars, mt_rand(0, $size - $chunkSize), $chunkSize); array_push($chunkArray, strrev($strip)); $split--; } foreach ($chunkArray as $set) { $modulus = ($length - strlen($key)) % 5; $adjusted = ($modulus > 0) ? $modulus : 5; $key .= substr($set, mt_rand(0, strlen($set) - $adjusted), $adjusted); } return str_rot13(str_shuffle($key)); } } Generators/TokenGenerator.php000064400000001250150524607730012324 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Generators; use Keygen\Generator; class TokenGenerator extends Generator { /** * Generates a random key. * * @param numeric $length * @return string */ protected function keygen($length) { $token = ''; $tokenlength = round($length * 4 / 3); for ($i = 0; $i < $tokenlength; ++$i) { $token .= chr(rand(32,1024)); } $token = base64_encode(str_shuffle($token)); return substr($token, 0, $length); } } Generators/RandomByteGenerator.php000064400000003163150524607730013315 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen\Generators; use RuntimeException; use Keygen\Generator; class RandomByteGenerator extends Generator { /** * Hexadecimal output enabled. * * @var bool */ protected $hex = false; /** * Enables hexadecimal output of byte string. * * @return $this */ public function hex() { $this->hex = true; return $this; } /** * Generates a random key. * * @param numeric $length * @return string * * @throws \RuntimeException */ protected function keygen($length) { $hex = !is_bool($this->hex) ?: $this->hex; $bytelength = $hex ? ceil($length / 2) : $length; if (function_exists('random_bytes')) { $bytes = random_bytes($bytelength); } elseif (function_exists('openssl_random_pseudo_bytes')) { $bytes = openssl_random_pseudo_bytes($bytelength); } elseif (@file_exists('/dev/urandom') && $bytelength < 100) { $bytes = file_get_contents('/dev/urandom', false, null, 0, $bytelength); } else { throw new RuntimeException('Cannot generate binary data.'); } return $hex ? substr(bin2hex($bytes), 0, $length) : $bytes; } /** * Outputs a generated key including the prefix and suffix if any. * May also return transformed keys. * * @return string */ public function generate() { $key = call_user_func_array('parent::generate', func_get_args()); if ($this->hex === true) { $this->hex = false; } return $key; } } AbstractGenerator.php000064400000002422150524607730010700 0ustar00 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Keygen; use Keygen\Traits\KeyManipulation; use Keygen\Traits\MutableGenerator; use Keygen\Traits\GeneratorMutation; abstract class AbstractGenerator { use KeyManipulation, MutableGenerator, GeneratorMutation { MutableGenerator::flattenArguments insteadof GeneratorMutation; } /** * Creates a new KeyGenerator instance. * * @param mixed $length * * @throws \InvalidArgumentException */ public function __construct($length = 16) { $this->length($length); } /** * Overload the __isset method */ public function __isset($prop) { return array_key_exists($prop, get_object_vars($this)); } /** * Overload the __get method */ public function __get($prop) { if (isset($this->{$prop})) { return $this->{$prop}; } } /** * Overload the __call method */ public function __call($method, $args) { return $this->__overloadMethods($method, $args); } /** * Overload the __callStatic method */ public static function __callStatic($method, $args) { return (new static)->__call($method, $args); } } AbstractGeneratorTest.php000064400000017245150524610370011543 0ustar00generator = new NumericGenerator; } /** * @covers ::__get * @covers ::__isset */ public function testGeneratorProperties() { $this->assertSame(16, $this->generator->length); $this->assertNull($this->generator->prefix); $this->assertNull($this->generator->suffix); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::length * @covers Keygen\Traits\IntegerCasting::intCast */ public function testLengthWithValidNumericArgument() { $this->generator->length(24); $this->assertSame(24, $this->generator->length); $this->generator->length('12'); $this->assertSame(12, $this->generator->length); $this->generator->length(32.0); $this->assertSame(32, $this->generator->length); $this->generator->length('40.0'); $this->assertSame(40, $this->generator->length); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::length * @covers Keygen\Traits\IntegerCasting::intCast */ public function testLengthWithFloatArgument() { $this->generator->length(24.4); $this->assertSame(24, $this->generator->length); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::length * @covers Keygen\Traits\IntegerCasting::intCast * @expectedException \InvalidArgumentException * @expectedExceptionMessage The given value cannot be converted to an integer. */ public function testLengthWithNonNumericArgument() { $this->generator->length([5]); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::length * @covers Keygen\Traits\IntegerCasting::intCast * @expectedException \InvalidArgumentException * @expectedExceptionMessage The given value cannot be converted to an integer. */ public function testLengthWithNonNumericStringArgument() { $this->generator->length('53.0bees'); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::prefix * @covers Keygen\Traits\IntegerCasting::affix */ public function testPrefixWithValidArgument() { $this->generator->prefix(123); $this->assertSame('123', $this->generator->prefix); $this->generator->prefix('TM-'); $this->assertSame('TM-', $this->generator->prefix); $this->generator->prefix(true); $this->assertSame('1', $this->generator->prefix); $this->generator->prefix(false); $this->assertSame('', $this->generator->prefix); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::prefix * @covers Keygen\Traits\IntegerCasting::affix * @expectedException \InvalidArgumentException * @expectedExceptionMessage The given prefix cannot be converted to a string. */ public function testPrefixWithNonScalarArgument() { $this->generator->prefix([123]); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::suffix * @covers Keygen\Traits\IntegerCasting::affix */ public function testSuffixWithValidArgument() { $this->generator->suffix(123); $this->assertSame('123', $this->generator->suffix); $this->generator->suffix('.img'); $this->assertSame('.img', $this->generator->suffix); $this->generator->suffix(true); $this->assertSame('1', $this->generator->suffix); $this->generator->suffix(false); $this->assertSame('', $this->generator->suffix); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @covers Keygen\Traits\KeyManipulation::suffix * @covers Keygen\Traits\IntegerCasting::affix * @expectedException \InvalidArgumentException * @expectedExceptionMessage The given suffix cannot be converted to a string. */ public function testSuffixWithNonScalarArgument() { $this->generator->suffix($this->generator); } /** * @covers Keygen\Traits\MutableGenerator::mutable * @covers Keygen\Traits\MutableGenerator::immutable * @covers Keygen\Traits\FlattenArguments::flattenArguments */ public function testMutableProperties() { $this->assertEquals([], $this->generator->mutables); $this->generator->mutable('prefix', 'length')->mutable('prefix'); $this->assertEquals(['length', 'prefix'], $this->generator->mutables); $this->generator->immutable('suffix', 'length')->immutable('suffix'); $this->assertNotContains('length', $this->generator->mutables); $this->assertContains('prefix', $this->generator->mutables); } /** * @covers Keygen\Traits\MutableGenerator::mutable * @covers Keygen\Traits\FlattenArguments::flattenArguments * @expectedException \InvalidArgumentException * @expectedExceptionMessage Cannot add unknown property to mutables collection ('mutate'). */ public function testInvalidMutableProperties() { $this->generator->mutable('prefix', 'mutate', 'length'); } /** * @covers Keygen\Traits\GeneratorMutation::mutate * @covers Keygen\Traits\GeneratorMutation::dontMutate * @covers Keygen\Traits\FlattenArguments::flattenArguments * @covers Keygen\Traits\KeyManipulation::propagateMutation */ public function testObjectMutations() { $generator1 = (new NumericGenerator(10))->mutable('length'); $generator2 = (new NumericGenerator(10))->mutable('length', 'prefix'); $this->generator->mutate($generator2, $generator1); $this->assertEquals([$generator2, $generator1], $this->generator->mutates); $this->assertEquals(16, $this->generator->length); $this->assertEquals(10, $generator1->length); $this->assertEquals(10, $generator2->length); $this->generator->length(20)->prefix('IMG-'); $this->assertEquals(20, $this->generator->length); $this->assertEquals(20, $generator1->length); $this->assertEquals(20, $generator2->length); $this->assertEquals('IMG-', $this->generator->prefix); $this->assertEquals(null, $generator1->prefix); $this->assertEquals('IMG-', $generator2->prefix); $this->generator->dontMutate($generator2); $this->assertContains($generator1, $this->generator->mutates); $this->assertNotContains($generator2, $this->generator->mutates); $generator1->length(10); $this->assertEquals(20, $this->generator->length); $this->assertEquals(10, $generator1->length); $this->assertEquals(20, $generator2->length); $this->generator->mutable('length'); $generator1->length(24); // Root generator not mutable by sub-generators $this->assertEquals(20, $this->generator->length); $this->assertEquals(24, $generator1->length); } /** * @covers Keygen\Traits\GeneratorMutation::mutate * @covers Keygen\Traits\FlattenArguments::flattenArguments * @expectedException \InvalidArgumentException * @expectedExceptionMessage Mutable objects must be instances of Keygen\AbstractGenerator. */ public function testInvalidMutationObject() { $generator1 = (new NumericGenerator(10))->mutable('length'); $generator2 = (new NumericGenerator(10))->mutable('length', 'prefix'); $this->generator->mutate($generator2, $generator1, new \stdClass); } /** * @covers ::__call * @covers Keygen\Traits\KeyManipulation::__overloadMethods * @expectedException \BadMethodCallException * @expectedExceptionMessage Call to unknown method Keygen\Generators\NumericGenerator::degenerate() */ public function testUnknownMethodCall() { $this->generator->degenerate(); } } GeneratorTest.php000064400000002574150524610370010056 0ustar00generator = new NumericGenerator; } /** * @covers ::generate * @covers Keygen\Traits\KeyManipulation::getAdjustedKeyLength */ public function testGenerateMethod() { $ga = $this->generator->generate(); $this->assertSame($this->generator->length, strlen($ga)); $gb = $this->generator->prefix('TM-')->generate(); $this->assertSame($this->generator->length, strlen($gb)); $this->assertRegExp('/^TM-/', $gb); $gc = $this->generator->prefix('token::')->suffix('::123')->generate(true); $this->assertSame($this->generator->length + strlen($this->generator->prefix) + strlen($this->generator->suffix), strlen($gc)); $this->assertRegExp('/^token::\d+?::123$/', $gc); $callable = function($s) { return strtoupper(substr($s, 8)); }; $gd = $this->generator->generate('md5', $callable); $this->assertSame(24 + strlen($this->generator->prefix) + strlen($this->generator->suffix), strlen($gd)); $this->assertRegExp('/^token::[A-F0-9]+?::123$/', $gd); $ge = $this->generator->prefix('')->suffix('')->generate(true, $callable); $this->assertSame($this->generator->length - 8, strlen($ge)); } } Generators/TokenGeneratorTest.php000064400000000743150524610370013164 0ustar00generator = new TokenGenerator; } /** * @covers ::keygen * @covers Keygen\Generator::generate */ public function testKeyGeneration() { $key = $this->generator->generate(); $this->assertRegExp('/^[a-zA-Z0-9+\/]{16}$/', $key); } } Generators/RandomByteGeneratorTest.php000064400000001322150524610370014142 0ustar00generator = new RandomByteGenerator; } /** * @covers ::hex * @covers ::keygen * @covers ::generate * @covers Keygen\Generator::generate */ public function testKeyGeneration() { $key = $this->generator->generate(); $this->assertEquals(16, strlen($key)); $this->assertFalse($this->generator->hex); $hexKey = $this->generator->hex()->generate(); $this->assertRegExp('/^[a-f0-9]{16}$/', $hexKey); $this->assertFalse($this->generator->hex); } } Generators/NumericGeneratorTest.php000064400000000737150524610370013511 0ustar00generator = new NumericGenerator; } /** * @covers ::keygen * @covers Keygen\Generator::generate */ public function testKeyGeneration() { $key = $this->generator->generate(); $this->assertRegExp('/^\d{16}$/', $key); } } Generators/AlphaNumericGeneratorTest.php000064400000000774150524610370014460 0ustar00generator = new AlphaNumericGenerator; } /** * @covers ::keygen * @covers Keygen\Generator::generate */ public function testKeyGeneration() { $key = $this->generator->generate(); $this->assertRegExp('/^[a-zA-Z0-9]{16}$/', $key); } } KeygenTest.php000064400000004627150524610370007353 0ustar00keygen = new Keygen; } /** * @covers Keygen\AbstractGenerator::__get * @covers Keygen\AbstractGenerator::__isset */ public function testGeneratorProperties() { $this->assertSame(16, $this->keygen->length); $this->assertNull($this->keygen->prefix); $this->assertNull($this->keygen->suffix); } /** * @covers ::__call * @covers ::newGenerator * @covers ::newGeneratorFromAlias * @covers Keygen\GeneratorFactory::create * @covers Keygen\AbstractGenerator::__call * @covers Keygen\AbstractGenerator::__callStatic */ public function testGeneratorMethods() { $this->assertInstanceOf(TokenGenerator::class, Keygen::token()); $this->assertInstanceOf(NumericGenerator::class, Keygen::numeric()); $this->assertInstanceOf(RandomByteGenerator::class, Keygen::bytes()); $this->assertInstanceOf(AlphaNumericGenerator::class, Keygen::alphanum()); $this->assertInstanceOf(TokenGenerator::class, $this->keygen->token()); $this->assertInstanceOf(NumericGenerator::class, $this->keygen->numeric()); $this->assertInstanceOf(RandomByteGenerator::class, $this->keygen->bytes()); $this->assertInstanceOf(AlphaNumericGenerator::class, $this->keygen->alphanum()); $this->assertSame(16, Keygen::token()->length); $this->assertSame(32, Keygen::bytes(32)->length); $this->assertSame(20, Keygen::numeric(20)->length); $this->assertSame(12, Keygen::alphanum(12)->length); $this->assertSame(16, $this->keygen->token()->length); $this->assertSame(32, $this->keygen->bytes(32)->length); $this->assertSame(20, $this->keygen->numeric(20)->length); $this->assertSame(12, $this->keygen->alphanum(12)->length); } /** * @covers ::__call * @covers ::newGenerator * @covers ::newGeneratorFromAlias * @covers Keygen\GeneratorFactory::create * @covers Keygen\AbstractGenerator::__call * @covers Keygen\AbstractGenerator::__callStatic * @expectedException \BadMethodCallException * @expectedExceptionMessage Call to unknown method Keygen\Keygen::unknown() */ public function testInvalidGeneratorMethods() { $this->keygen->unknown(); } } GeneratorFactoryTest.php000064400000001675150524610370011407 0ustar00assertInstanceOf(TokenGenerator::class, GeneratorFactory::create('token')); $this->assertInstanceOf(NumericGenerator::class, GeneratorFactory::create('numeric')); $this->assertInstanceOf(RandomByteGenerator::class, GeneratorFactory::create('randombyte')); $this->assertInstanceOf(AlphaNumericGenerator::class, GeneratorFactory::create('alphanumeric')); $generator = GeneratorFactory::create('bytes'); } }