generated from nunomaduro/skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
168 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codelabmw\Testament\Contracts; | ||
|
||
use Codelabmw\Testament\Enums\CodeType; | ||
|
||
/** @internal */ | ||
interface Generator | ||
{ | ||
/** | ||
* Generate a code of the given type and length. | ||
*/ | ||
public function generate(CodeType $codeType, int $length): string; | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace Codelabmw\Testament\Enums; | ||
|
||
/** @internal */ | ||
enum CodeType | ||
{ | ||
case NUMERIC; | ||
|
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codelabmw\Testament\Generators; | ||
|
||
use Codelabmw\Testament\Contracts\Generator; | ||
use Codelabmw\Testament\Enums\CodeType; | ||
|
||
/** @internal */ | ||
final class DefaultGenerator implements Generator | ||
{ | ||
/** | ||
* Generate a code of the given type and length. | ||
*/ | ||
public function generate(CodeType $codeType, int $length): string | ||
{ | ||
$code = ''; | ||
|
||
$generator = function () use ($codeType, $length) { | ||
for ($i = 0; $i < $length; $i++) { | ||
yield match ($codeType) { | ||
CodeType::NUMERIC => random_int(0, 9), | ||
CodeType::ALPHA => chr(random_int(65, 90)), | ||
CodeType::ALPHANUMERIC => $this->getRandomAlphanumericCharAscii(), | ||
CodeType::PASSWORD => $this->getRandomStrongPasswordChar(), | ||
}; | ||
} | ||
}; | ||
|
||
foreach ($generator() as $char) { | ||
$code .= $char; | ||
} | ||
|
||
return $code; | ||
} | ||
|
||
/** | ||
* Get a charecter thats either a letter or number. | ||
*/ | ||
private function getRandomAlphanumericCharAscii(): string | ||
{ | ||
$asciiMin = 48; | ||
$asciiMax = 122; | ||
|
||
$randomAscii = random_int($asciiMin, $asciiMax); | ||
|
||
while (($randomAscii > 57 && $randomAscii < 65) || ($randomAscii > 90 && $randomAscii < 97)) { | ||
$randomAscii = random_int($asciiMin, $asciiMax); | ||
} | ||
|
||
return chr($randomAscii); | ||
} | ||
|
||
/** | ||
* Get a charecter thats either a letter, number or special charecter. | ||
*/ | ||
private function getRandomStrongPasswordChar(): string | ||
{ | ||
$lowerCaseRange = range(97, 122); // a-z | ||
$upperCaseRange = range(65, 90); // A-Z | ||
$numberRange = range(48, 57); // 0-9 | ||
$specialCharRange = range(33, 47) // !"#$%&'()*+,-./ | ||
+ range(58, 64) // :;<=>?@ | ||
+ range(91, 96); // [\]^_` | ||
|
||
$charTypeRanges = [$lowerCaseRange, $upperCaseRange, $numberRange, $specialCharRange]; | ||
$randomRangeIndex = random_int(0, count($charTypeRanges) - 1); | ||
$selectedRange = $charTypeRanges[$randomRangeIndex]; | ||
|
||
$randomAscii = $selectedRange[random_int(0, count($selectedRange) - 1)]; | ||
|
||
return chr($randomAscii); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,15 @@ | ||
<?php | ||
|
||
use Codelabmw\Testament\Contracts\Generator; | ||
|
||
arch()->preset()->php(); | ||
arch()->preset()->security(); | ||
arch()->preset()->strict(); | ||
|
||
arch('generators implements generator contract') | ||
->expect('Codelabmw\Testament\Generators') | ||
->toImplement(Generator::class); | ||
|
||
arch('it will not use debugging functions') | ||
->expect(['dd', 'dump', 'ray']) | ||
->expect(['dd', 'dump', 'ray', 'var_dump']) | ||
->each->not->toBeUsed(); |
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 |
---|---|---|
@@ -1,61 +1,39 @@ | ||
<?php | ||
|
||
use Codelabmw\Testament\Enums\CodeType; | ||
use Codelabmw\Testament\Testament; | ||
|
||
it('can creare a testament instance with default static method', function (): void { | ||
$testament = Testament::default(); | ||
|
||
expect($testament)->toBeInstanceOf(Testament::class); | ||
}); | ||
|
||
it('can generate a code with correct length', function (): void { | ||
$testament = Testament::default(); | ||
$code = $testament->generate(type: CodeType::NUMERIC, length: 6); | ||
it('can generate an alpha code', function (): void { | ||
// Act | ||
$code = Testament::alpha(); | ||
|
||
// Assert | ||
expect($code)->toHaveLength(6); | ||
expect($code)->toMatch('/^[a-zA-Z]+$/'); | ||
}); | ||
|
||
it('can generate a numeric code', function (): void { | ||
$testament = Testament::default(); | ||
$code = $testament->generate(type: CodeType::NUMERIC, length: 6); | ||
// Act | ||
$code = Testament::numeric(); | ||
|
||
// Assert | ||
expect($code)->toHaveLength(6); | ||
expect($code)->toMatch('/^\d+$/'); | ||
}); | ||
|
||
it('can generate an alphabetical code', function (): void { | ||
$testament = Testament::default(); | ||
$code = $testament->generate(type: CodeType::ALPHA, length: 6); | ||
|
||
expect($code)->toMatch('/^[a-zA-Z]+$/'); | ||
}); | ||
|
||
it('can generate an alphanumeric code', function (): void { | ||
$testament = Testament::default(); | ||
$code = $testament->generate(type: CodeType::ALPHANUMERIC, length: 6); | ||
it('can generate an alpha-numeric code', function (): void { | ||
// Act | ||
$code = Testament::alphaNumeric(20); | ||
|
||
// Assert | ||
expect($code)->toHaveLength(20); | ||
expect($code)->toMatch('/^[a-zA-Z0-9]+$/'); | ||
}); | ||
|
||
it('can generate a password code', function (): void { | ||
$testament = Testament::default(); | ||
$code = $testament->generate(type: CodeType::PASSWORD, length: 6); | ||
|
||
expect($code)->toMatch('/^[a-zA-Z0-9!@#$%^&*()]+$/'); | ||
}); | ||
|
||
it('can verify that two codes are equal', function (): void { | ||
$testament = Testament::default(); | ||
$expected = '123456'; | ||
$actual = '123456'; | ||
|
||
expect($testament->verify($expected, $actual))->toBeTrue(); | ||
}); | ||
|
||
it('can verify that two codes are not equal', function (): void { | ||
$testament = Testament::default(); | ||
$expected = '123456'; | ||
$actual = '654321'; | ||
// Act | ||
$code = Testament::password(); | ||
|
||
expect($testament->verify($expected, $actual))->toBeFalse(); | ||
// Assert | ||
expect($code)->toHaveLength(8); | ||
expect($code)->toMatch('/^.*$/'); | ||
}); |