Skip to content

Commit

Permalink
feat(password): Make password having custom length (#235)
Browse files Browse the repository at this point in the history
- fix phpdoc type error in password unit test
- add new internal property about password length with default value int "16"
- replace hard coded password length comparison with that internal property
- add a new setter for a custom password length
- add missing ext-mbstring to composer.json
- add setValidator to Input interface
- add tests in PasswordTest
  • Loading branch information
AydinHassan committed Mar 5, 2020
1 parent 1acf6fc commit aab3b01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -22,7 +22,8 @@
"php" : ">=7.1",
"beberlei/assert": "^2.4 | ^3",
"php-school/terminal": "^0.2.1",
"ext-posix": "*"
"ext-posix": "*",
"ext-mbstring": "*"
},
"autoload" : {
"psr-4" : {
Expand Down
18 changes: 14 additions & 4 deletions src/Input/Password.php
Expand Up @@ -39,6 +39,11 @@ class Password implements Input
*/
private $style;

/**
* @var int
*/
private $passwordLength = 16;

public function __construct(InputIO $inputIO, MenuStyle $style)
{
$this->inputIO = $inputIO;
Expand Down Expand Up @@ -84,7 +89,7 @@ public function getPlaceholderText() : string
public function setValidator(callable $validator) : Input
{
$this->validator = $validator;

return $this;
}

Expand All @@ -97,15 +102,15 @@ public function validate(string $input) : bool
{
if ($this->validator) {
$validator = $this->validator;

if ($validator instanceof \Closure) {
$validator = $validator->bindTo($this);
}

return $validator($input);
}

return mb_strlen($input) >= 16;
return mb_strlen($input) >= $this->passwordLength;
}

public function filter(string $value) : string
Expand All @@ -117,4 +122,9 @@ public function getStyle() : MenuStyle
{
return $this->style;
}

public function setPasswordLength(int $length) : int
{
return $this->passwordLength = $length;
}
}
18 changes: 16 additions & 2 deletions test/Input/PasswordTest.php
Expand Up @@ -25,7 +25,7 @@ class PasswordTest extends TestCase
private $inputIO;

/**
* @var Text
* @var Password
*/
private $input;

Expand Down Expand Up @@ -131,9 +131,23 @@ public function testWithCustomValidatorAndCustomValidationMessage() : void
};

$this->input->setValidator($customValidate);

self::assertTrue($this->input->validate('superstrongpassword'));
self::assertFalse($this->input->validate('mypassword'));
self::assertEquals('Password too generic', $this->input->getValidationFailedText());
}

public function testPasswordValidationWithDefaultLength() : void
{
self::assertFalse($this->input->validate(str_pad('a', 15)));
self::assertTrue($this->input->validate(str_pad('a', 16)));
}

public function testPasswordValidationWithDefinedLength() : void
{
$this->input->setPasswordLength(5);

self::assertFalse($this->input->validate(str_pad('a', 4)));
self::assertTrue($this->input->validate(str_pad('a', 5)));
}
}

0 comments on commit aab3b01

Please sign in to comment.