-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from JanDC/master
Allow the validator to be used as a property validator as well
- Loading branch information
Showing
8 changed files
with
157 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vendor | ||
.idea | ||
composer.lock | ||
composer.lock | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
$finder = PhpCsFixer\Finder::create() | ||
->in(__DIR__) | ||
; | ||
|
||
return PhpCsFixer\Config::create() | ||
->setRules([ | ||
'@Symfony' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
]) | ||
->setFinder($finder) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PasswordValidator\Constraints\Password; | ||
use PasswordValidator\Constraints\PasswordValidator; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\Exception\MissingOptionsException; | ||
|
||
class PasswordValidatorTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase | ||
{ | ||
|
@@ -38,7 +39,6 @@ public function testLength() | |
|
||
$this->assertSame(1, $this->context->getViolations()->count()); | ||
$this->assertSame($passwordConstraint->minMessage, $this->context->getViolations()->get(0)->getMessageTemplate()); | ||
|
||
} | ||
|
||
public function testNumber() | ||
|
@@ -61,7 +61,6 @@ public function testUpperCase() | |
|
||
$this->assertSame(1, $this->context->getViolations()->count()); | ||
$this->assertSame($passwordConstraint->upperCaseCharacterMissingMessage, $this->context->getViolations()->get(0)->getMessageTemplate()); | ||
|
||
} | ||
|
||
public function testLowerCase() | ||
|
@@ -73,7 +72,6 @@ public function testLowerCase() | |
|
||
$this->assertSame(1, $this->context->getViolations()->count()); | ||
$this->assertSame($passwordConstraint->lowerCaseCharacterMissingMessage, $this->context->getViolations()->get(0)->getMessageTemplate()); | ||
|
||
} | ||
|
||
public function testProperPassword() | ||
|
@@ -85,6 +83,16 @@ public function testProperPassword() | |
$this->assertNoViolation(); | ||
} | ||
|
||
// Check whether class and property options are mixed | ||
public function testInvalidUserConfiguration() | ||
{ | ||
$user = new Symfony\Component\Security\Core\User\User('[email protected]', '[email protected]'); | ||
$passwordConstraint = new Password(); | ||
|
||
$this->expectException(MissingOptionsException::class); | ||
$this->validator->validate($user, $passwordConstraint); | ||
} | ||
|
||
protected function createValidator() | ||
{ | ||
return new PasswordValidator(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PasswordValidator\Constraints\Password; | ||
use PasswordValidator\Constraints\PasswordValidator; | ||
|
||
class PropertyConstraintTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase | ||
{ | ||
public function testLength() | ||
{ | ||
$password = 'Foo1'; | ||
$passwordConstraint = new Password(); | ||
|
||
$this->validator->validate($password, $passwordConstraint); | ||
|
||
$this->assertSame(1, $this->context->getViolations()->count()); | ||
$this->assertSame($passwordConstraint->minMessage, $this->context->getViolations()->get(0)->getMessageTemplate()); | ||
} | ||
|
||
public function testNumber() | ||
{ | ||
$password = '[email protected]'; | ||
$passwordConstraint = new Password(); | ||
$this->validator->validate($password, $passwordConstraint); | ||
|
||
$this->assertSame(1, $this->context->getViolations()->count()); | ||
$this->assertSame($passwordConstraint->numberMissingMessage, $this->context->getViolations()->get(0)->getMessageTemplate()); | ||
} | ||
|
||
public function testUpperCase() | ||
{ | ||
$password = '[email protected]'; | ||
$passwordConstraint = new Password(); | ||
|
||
$this->validator->validate($password, $passwordConstraint); | ||
|
||
$this->assertSame(1, $this->context->getViolations()->count()); | ||
$this->assertSame($passwordConstraint->upperCaseCharacterMissingMessage, $this->context->getViolations()->get(0)->getMessageTemplate()); | ||
} | ||
|
||
public function testLowerCase() | ||
{ | ||
$password = 'FOOBARBAZ1'; | ||
$passwordConstraint = new Password(); | ||
|
||
$this->validator->validate($password, $passwordConstraint); | ||
|
||
$this->assertSame(1, $this->context->getViolations()->count()); | ||
$this->assertSame($passwordConstraint->lowerCaseCharacterMissingMessage, $this->context->getViolations()->get(0)->getMessageTemplate()); | ||
} | ||
|
||
public function testProperPassword() | ||
{ | ||
$password = '[email protected]'; | ||
$passwordConstraint = new Password(); | ||
|
||
$this->validator->validate($password, $passwordConstraint); | ||
$this->assertNoViolation(); | ||
} | ||
|
||
// Check whether class and property options are mixed | ||
public function testInvalidPropertyConfiguration() | ||
{ | ||
$password = '[email protected]'; | ||
$passwordConstraint = new Password(['plainPasswordAccessor' => 'getPassword', 'plainPasswordProperty' => 'password']); | ||
|
||
$this->validator->validate($password, $passwordConstraint); | ||
$this->assertNoViolation(); | ||
} | ||
|
||
protected function createValidator() | ||
{ | ||
return new PasswordValidator(); | ||
} | ||
} |