Skip to content

Commit aee9d12

Browse files
committed
feat(password): Make password having custom length
- 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
1 parent 1acf6fc commit aee9d12

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"php" : ">=7.1",
2323
"beberlei/assert": "^2.4 | ^3",
2424
"php-school/terminal": "^0.2.1",
25-
"ext-posix": "*"
25+
"ext-posix": "*",
26+
"ext-mbstring": "*"
2627
},
2728
"autoload" : {
2829
"psr-4" : {

src/Input/Input.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ public function getPlaceholderText() : string;
2828
public function filter(string $value) : string;
2929

3030
public function getStyle() : MenuStyle;
31+
32+
public function setValidator(callable $validator) : Input;
3133
}

src/Input/Password.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class Password implements Input
3939
*/
4040
private $style;
4141

42+
/**
43+
* @var int
44+
*/
45+
private $passwordLength = 16;
46+
4247
public function __construct(InputIO $inputIO, MenuStyle $style)
4348
{
4449
$this->inputIO = $inputIO;
@@ -84,7 +89,7 @@ public function getPlaceholderText() : string
8489
public function setValidator(callable $validator) : Input
8590
{
8691
$this->validator = $validator;
87-
92+
8893
return $this;
8994
}
9095

@@ -97,15 +102,15 @@ public function validate(string $input) : bool
97102
{
98103
if ($this->validator) {
99104
$validator = $this->validator;
100-
105+
101106
if ($validator instanceof \Closure) {
102107
$validator = $validator->bindTo($this);
103108
}
104-
109+
105110
return $validator($input);
106111
}
107112

108-
return mb_strlen($input) >= 16;
113+
return mb_strlen($input) >= $this->passwordLength;
109114
}
110115

111116
public function filter(string $value) : string
@@ -117,4 +122,9 @@ public function getStyle() : MenuStyle
117122
{
118123
return $this->style;
119124
}
125+
126+
public function setPasswordLength(int $length) : int
127+
{
128+
return $this->passwordLength = $length;
129+
}
120130
}

test/Input/PasswordTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PasswordTest extends TestCase
2525
private $inputIO;
2626

2727
/**
28-
* @var Text
28+
* @var Password
2929
*/
3030
private $input;
3131

@@ -131,9 +131,23 @@ public function testWithCustomValidatorAndCustomValidationMessage() : void
131131
};
132132

133133
$this->input->setValidator($customValidate);
134-
134+
135135
self::assertTrue($this->input->validate('superstrongpassword'));
136136
self::assertFalse($this->input->validate('mypassword'));
137137
self::assertEquals('Password too generic', $this->input->getValidationFailedText());
138138
}
139+
140+
public function testPasswordValidationWithDefaultLength() : void
141+
{
142+
self::assertFalse($this->input->validate(str_pad('a', 15)));
143+
self::assertTrue($this->input->validate(str_pad('a', 16)));
144+
}
145+
146+
public function testPasswordValidationWithDefinedLength() : void
147+
{
148+
$this->input->setPasswordLength(5);
149+
150+
self::assertFalse($this->input->validate(str_pad('a', 4)));
151+
self::assertTrue($this->input->validate(str_pad('a', 5)));
152+
}
139153
}

0 commit comments

Comments
 (0)