Skip to content

Commit

Permalink
Add PasswordRequirementsValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
fre5h committed Apr 28, 2020
1 parent bdd5967 commit 7d1c778
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Tests/Annotation/DummyDto.php
Expand Up @@ -17,6 +17,6 @@
/**
* DummyDto.
*/
class DummyDto implements DtoInterface
final class DummyDto implements DtoInterface
{
}
2 changes: 1 addition & 1 deletion Tests/Annotation/DummyDtoClass.php
Expand Up @@ -17,6 +17,6 @@
/**
* DummyDtoClass.
*/
class DummyDtoClass implements DtoInterface
final class DummyDtoClass implements DtoInterface
{
}
2 changes: 1 addition & 1 deletion Tests/Traits/DummyClass.php
Expand Up @@ -20,7 +20,7 @@
/**
* DummyClass.
*/
class DummyClass
final class DummyClass
{
use DtoExtractorTrait;
use SerializerTrait;
Expand Down
77 changes: 77 additions & 0 deletions Tests/Util/PasswordRequirementsValidatorTest.php
@@ -0,0 +1,77 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Tests\Util;

use PHPUnit\Framework\TestCase;
use StfalconStudio\ApiBundle\Util\PasswordRequirementsValidator;

/**
* PasswordRequirementsValidatorTest.
*/
final class PasswordRequirementsValidatorTest extends TestCase
{
/** @var PasswordRequirementsValidator */
private $validator;

protected function setUp(): void
{
$this->validator = new PasswordRequirementsValidator();
}

protected function tearDown(): void
{
unset(
$this->validator,
);
}

/**
* @param string $password
*
* @dataProvider dataProviderForTestPasswordIsValid
*/
public function testPasswordIsValid(string $password): void
{
self::assertTrue($this->validator->isValid($password));
}

public static function dataProviderForTestPasswordIsValid(): iterable
{
yield ['qwertY1'];
yield ['QWERTy1'];
yield ['QWERTy1'];
yield ['Пароль1'];
yield ['ПАРОЛь1'];
}

/**
* @param string $password
*
* @dataProvider dataProviderForTestPasswordIsNotValid
*/
public function testPasswordIsNotValid(string $password): void
{
self::assertFalse($this->validator->isValid($password));
}

public static function dataProviderForTestPasswordIsNotValid(): iterable
{
yield ['PASSWORD'];
yield ['password'];
yield ['passWORD'];
yield ['password1'];
yield ['PASSWORD1'];
yield ['парольчик'];
yield ['ПАРОЛЬЧИК'];
}
}
66 changes: 66 additions & 0 deletions Util/PasswordRequirementsValidator.php
@@ -0,0 +1,66 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Util;

/**
* PasswordRequirementsValidator.
*/
class PasswordRequirementsValidator
{
private const NUMBER_REGEXP = '/[\d]{1,}/';
private const LOWERCASE_LETTER_REGEXP = '/\p{Ll}{1,}/u';
private const UPPERCASE_LETTER_REGEXP = '/\p{Lu}{1,}/u';

/**
* @param string $password
*
* @return bool
*/
public function isValid(string $password): bool
{
return $this->hasAtLeastOneNumber($password)
&& $this->hasAtLeastOneUnicodeUpperCaseLetter($password)
&& $this->hasAtLeastOneUnicodeLowerCaseLetter($password)
;
}

/**
* @param string $password
*
* @return bool
*/
private function hasAtLeastOneNumber(string $password): bool
{
return (bool) \preg_match(self::NUMBER_REGEXP, $password);
}

/**
* @param string $password
*
* @return bool
*/
private function hasAtLeastOneUnicodeUpperCaseLetter(string $password): bool
{
return (bool) \preg_match(self::UPPERCASE_LETTER_REGEXP, $password);
}

/**
* @param string $password
*
* @return bool
*/
private function hasAtLeastOneUnicodeLowerCaseLetter(string $password): bool
{
return (bool) \preg_match(self::LOWERCASE_LETTER_REGEXP, $password);
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -8,6 +8,7 @@ parameters:
level: 8
checkMissingIterableValueType: false
excludes_analyse:
- '%rootDir%/../../../DependencyInjection/Configuration'
- '%rootDir%/../../../Tests/*'
- '%rootDir%/../../../tests/*'
- '%rootDir%/../../../vendor/*'
Expand Down

0 comments on commit 7d1c778

Please sign in to comment.