diff --git a/composer.json b/composer.json index 4a34c204..23f257fe 100644 --- a/composer.json +++ b/composer.json @@ -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" : { diff --git a/src/Input/Password.php b/src/Input/Password.php index c4daa7c6..1c25d16d 100644 --- a/src/Input/Password.php +++ b/src/Input/Password.php @@ -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; @@ -84,7 +89,7 @@ public function getPlaceholderText() : string public function setValidator(callable $validator) : Input { $this->validator = $validator; - + return $this; } @@ -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 @@ -117,4 +122,9 @@ public function getStyle() : MenuStyle { return $this->style; } + + public function setPasswordLength(int $length) : int + { + return $this->passwordLength = $length; + } } diff --git a/test/Input/PasswordTest.php b/test/Input/PasswordTest.php index 08a13c80..c1b132e0 100644 --- a/test/Input/PasswordTest.php +++ b/test/Input/PasswordTest.php @@ -25,7 +25,7 @@ class PasswordTest extends TestCase private $inputIO; /** - * @var Text + * @var Password */ private $input; @@ -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))); + } }