Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(password): Make password having custom length #235

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)));
}
}