Skip to content

Commit

Permalink
Merge pull request #7 from staysafe/develop
Browse files Browse the repository at this point in the history
Access policy from password policy builder
  • Loading branch information
pavlakis committed Oct 29, 2019
2 parents 565d18b + a3c92f1 commit 11e8410
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/PasswordPolicyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public function isValid(string $password): bool

return true;
}

public function getPolicy(): PolicyInterface
{
return $this->policy;
}
}
4 changes: 4 additions & 0 deletions src/PasswordPolicyBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace StaySafe\Password\Policy;

use StaySafe\Password\Policy\Policy\PolicyInterface;

interface PasswordPolicyBuilderInterface
{
public function isValid(string $password): bool;

public function getPolicy(): PolicyInterface;
}
18 changes: 18 additions & 0 deletions tests/phpunit/Policy/JsonPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use StaySafe\Password\Policy\Policy\JsonPolicy;
use StaySafe\Password\Policy\PasswordPolicyBuilder;
use StaySafe\Password\Policy\Format\HumanReadablePolicy;
use StaySafe\Password\Policy\Rule\Exception\InvalidRuleTypeException;
use StaySafe\Password\Policy\Rule\Exception\InvalidConstraintException;

Expand Down Expand Up @@ -71,4 +72,21 @@ public function test_rule_does_not_exist_throws_exception(): void
$this->expectException(InvalidRuleTypeException::class);
new JsonPolicy($jsonConstraints);
}

/**
* @throws InvalidConstraintException
* @throws InvalidRuleTypeException
*/
public function test_can_view_human_readable_policy(): void
{
$jsonConstraints = file_get_contents(dirname(__DIR__) . '/fixtures/policy.json');
$policy = new JsonPolicy($jsonConstraints);

$passwordPolicyBuilder = new PasswordPolicyBuilder($policy);

$this->assertSame(
'Password should be at least 8 characters long, have at least 2 special characters, have at least 3 numbers, have at least 1 upper case letter, and have at least 1 lower case letter.',
(new HumanReadablePolicy($passwordPolicyBuilder->getPolicy()))->getHumanReadablePolicySentence()
);
}
}

0 comments on commit 11e8410

Please sign in to comment.