Skip to content

Commit

Permalink
bug #61 Fix PasswordStrength doesn't work with named args (sstok)
Browse files Browse the repository at this point in the history
This PR was merged into the main branch.

Discussion
----------

| Q             | A
| ------------- | ---
| Branch?       | main
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Fixed tickets | Fix #60
| License       | MIT


Commits
-------

f3b8a51 Fix PasswordStrength doesn't work with named args
  • Loading branch information
sstok authored Nov 15, 2021
2 parents 09a1caa + f3b8a51 commit 469108f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Validator/Constraints/PasswordStrength.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ public function __construct(
string $message = null,
string $tooShortMessage = null
) {
parent::__construct($options ?? [], $groups, $payload);
$finalOptions = [];

if (is_array($options)) {
$finalOptions = $options;
} else {
$finalOptions['minStrength'] = $options;
}

// The minStrength option is required.
if ($minStrength !== null) {
$finalOptions['minStrength'] = $minStrength;
}

parent::__construct($finalOptions ?? [], $groups, $payload);

$this->minStrength = $minStrength ?? $this->minStrength;
$this->minLength = $minLength ?? $this->minLength;
$this->unicodeEquality = $unicodeEquality ?? $this->unicodeEquality;
$this->message = $message ?? $this->message;
Expand Down
16 changes: 16 additions & 0 deletions tests/Validator/PasswordStrengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ protected function createValidator()
return new PasswordStrengthValidator(new Translator('en'));
}

/** @test */
public function constraints_options_are_properly_resolved(): void
{
// Default option
$constraint = new PasswordStrength(3);
self::assertEquals(3, $constraint->minStrength);

// By option
$constraint = new PasswordStrength(['minStrength' => 3]);
self::assertEquals(3, $constraint->minStrength);

// Specific argument
$constraint = new PasswordStrength(null, null, null, 3);
self::assertEquals(3, $constraint->minStrength);
}

/**
* @test
*/
Expand Down

0 comments on commit 469108f

Please sign in to comment.