Skip to content

Commit

Permalink
Merge eda1ea6 into 077fd60
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlakis committed Jun 1, 2023
2 parents 077fd60 + eda1ea6 commit 1ba8623
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 40 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
name: CI
on: push
on: [push, pull_request]
jobs:
security:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '7.2', '7.3' ]
php-versions: [ '8.0', '8.1', '8.2' ]
steps:
- name: checkout project
uses: actions/checkout@v2
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
- name: Roave Security advisories
run: composer require --dev roave/security-advisories:dev-latest
- name: The PHP Security Checker
uses: symfonycorp/security-checker-action@v2
- name: Composer Security check
run: composer audit
tests:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '7.2', '7.3' ]
php-versions: ['8.0', '8.1', '8.2' ]
steps:
- name: checkout project
uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":[],"times":{"HumanReadablePolicyTest::test_get_human_readable_policy_sentence":0.006,"HumanReadablePolicyTest::test_get_human_readable_constraints":0.001,"HumanReadablePolicyTest::test_get_human_readable_policy":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_can_validate_password_from_json_policy with data set #0":0.001,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_can_validate_password_from_json_policy with data set #1":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_can_validate_password_from_json_policy with data set #2":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_can_validate_password_from_json_policy with data set #3":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_an_invalid_or_empty_policy_throws_exception with data set #0":0.001,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_an_invalid_or_empty_policy_throws_exception with data set #1":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_an_invalid_or_empty_policy_throws_exception with data set #2":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_rule_does_not_exist_throws_exception":0.001,"StaySafe\\Password\\Policy\\Unit\\Rule\\JsonPolicyTest::test_can_view_human_readable_policy":0.001,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #0":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #1":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #2":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #3":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #4":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #5":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #6":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #7":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #8":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #9":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #10":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #11":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #12":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #13":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #14":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #15":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #16":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #17":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #18":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #19":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #20":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #21":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #22":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_rule with data set #23":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_negative_occurrences_throw_invalid_argument_exception with data set #0":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_negative_occurrences_throw_invalid_argument_exception with data set #1":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_negative_occurrences_throw_invalid_argument_exception with data set #2":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\AllRulesTest::test_negative_occurrences_throw_invalid_argument_exception with data set #3":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\DigitRuleTest::testIsValidMatchesNumberOfDigitsInString":0.001,"StaySafe\\Password\\Policy\\Unit\\Rule\\DigitRuleTest::testIsValidDoesNotMatchNumberOfDigitsInString":0,"StaySafe\\Password\\Policy\\Test\\Rule\\MaximumLengthRuleTest::testPasswordDoesNotExceedMaximumLength with data set #0":0,"StaySafe\\Password\\Policy\\Test\\Rule\\MaximumLengthRuleTest::testPasswordDoesNotExceedMaximumLength with data set #1":0,"StaySafe\\Password\\Policy\\Test\\Rule\\MaximumLengthRuleTest::testPasswordDoesNotExceedMaximumLength with data set #2":0,"StaySafe\\Password\\Policy\\Test\\Rule\\MaximumLengthRuleTest::testPasswordDoesNotExceedMaximumLength with data set #3":0,"StaySafe\\Password\\Policy\\Test\\Rule\\MaximumLengthRuleTest::testPasswordDoesMaximumLength with data set #0":0,"StaySafe\\Password\\Policy\\Test\\Rule\\MaximumLengthRuleTest::testPasswordDoesMaximumLength with data set #1":0,"StaySafe\\Password\\Policy\\Test\\Rule\\MaximumLengthRuleTest::testPasswordDoesMaximumLength with data set #2":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\MinimumLengthRuleTest::test_below_min_allowed_length_throws_exception":0.001,"StaySafe\\Password\\Policy\\Unit\\Rule\\SpecialCharacterRuleTest::testPasswordContainsSpecialCharacters":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\SpecialCharacterRuleTest::testPasswordDoesNotContainSpecialCharacters":0,"StaySafe\\Password\\Policy\\Unit\\Rule\\UpperCaseCharacterRuleTest::testIsValidCountsNumberOfUpperCaseLetters":0}}
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
}
},
"require": {
"php": "^7.2 || ^8.0",
"php": "^8.0",
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8.4"
"friendsofphp/php-cs-fixer": "^3",
"phpstan/phpstan": "^1",
"phpunit/phpunit": "^9"
},
"config": {
"bin-dir": "bin",
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
parameters:
inferPrivatePropertyTypeFromConstructor: true
inferPrivatePropertyTypeFromConstructor: true
level: 7
29 changes: 11 additions & 18 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.4/phpunit.xsd"
backupGlobals="false"
colors="true"
>

<testsuites>
<testsuite name="unit">
<directory>tests/phpunit/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true">
<coverage>
<include>
<directory>src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory>tests/phpunit/</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/Format/HumanReadablePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getHumanReadablePolicySentence(): string
}

/**
* @return array
* @return array<int, string>
*/
public function getHumanReadableConstraints(): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Format/HumanReadablePolicyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ interface HumanReadablePolicyInterface
{
public function getHumanReadablePolicy(): string;

/**
* @return array<int, string>
*/
public function getHumanReadableConstraints(): array;

public function getHumanReadablePolicySentence(): string;
Expand Down
9 changes: 6 additions & 3 deletions src/Policy/JsonPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class JsonPolicy implements PolicyInterface
private const RULE_CLASS_PREFIX = 'StaySafe\\Password\\Policy\\Rule\\';

/**
* @var array
* @var array<string, RuleInterface>
*/
private $constraints = [];

Expand Down Expand Up @@ -41,7 +41,7 @@ private function loadConstraints(string $constraints): void
}

foreach ($policy as $constraintClass => $number) {
$this->constraints[$constraintClass] = $this->get($constraintClass, $number);
$this->constraints[(string)$constraintClass] = $this->get((string)$constraintClass, $number);
}
}

Expand All @@ -60,7 +60,10 @@ private function get(string $ruleClassName, int $value): RuleInterface
throw new InvalidRuleTypeException(sprintf('Rule class %s does not exist', $ruleClassName));
}

return new $ruleClassName($value);
/** @var RuleInterface $rule */
$rule = new $ruleClassName($value);

return $rule;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/DigitRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function isValid(string $value): bool
* Returns the rule as an associative array
* self::class => rule.
*
* @return array
* @return array<class-string,int>
*/
public function getRule(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/LowerCaseCharacterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function isValid(string $value): bool
* Returns the rule as an associative array
* self::class => rule.
*
* @return array
* @return array<class-string, int>
*/
public function getRule(): array
{
Expand Down
37 changes: 37 additions & 0 deletions src/Rule/MaximumLengthRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace StaySafe\Password\Policy\Rule;

final class MaximumLengthRule implements RuleInterface
{
/**
* @var int
*/
private $maxLength;

public function __construct(int $maxLength)
{
$this->maxLength = $maxLength;
}

public function __toString(): string
{
return sprintf(
'Password should be at most %d characters long',
$this->maxLength
);
}

public function isValid(string $value): bool
{
return $this->maxLength >= strlen($value);
}

/**
* @return array<class-string, int>
*/
public function getRule(): array
{
return [self::class => $this->maxLength];
}
}
2 changes: 1 addition & 1 deletion src/Rule/MinimumLengthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function isValid(string $value): bool
* Returns the rule as an associative array
* self::class => rule.
*
* @return array
* @return array<class-string, int>
*/
public function getRule(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/RuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function isValid(string $value): bool;
* Returns the rule as an associative array
* self::class => rule.
*
* @return array
* @return array<class-string, int>
*/
public function getRule(): array;

Expand Down
2 changes: 1 addition & 1 deletion src/Rule/SpecialCharacterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function isValid(string $value): bool
* Returns the rule as an associative array
* self::class => rule.
*
* @return array
* @return array<class-string, int>
*/
public function getRule(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/UpperCaseCharacterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function isValid(string $value): bool
* Returns the rule as an associative array
* self::class => rule.
*
* @return array
* @return array<class-string, int>
*/
public function getRule(): array
{
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/Rule/AllRulesTest.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\Rule\DigitRule;
use StaySafe\Password\Policy\Rule\RuleInterface;
use StaySafe\Password\Policy\Rule\MaximumLengthRule;
use StaySafe\Password\Policy\Rule\MinimumLengthRule;
use StaySafe\Password\Policy\Rule\SpecialCharacterRule;
use StaySafe\Password\Policy\Rule\LowerCaseCharacterRule;
Expand Down Expand Up @@ -51,6 +52,11 @@ public function rulesDataProvider(): array
[MinimumLengthRule::class, 8, '12345678', true, 'Password should be at least 8 characters long'],
[MinimumLengthRule::class, 12, 'aaaaaaaa1234', true, 'Password should be at least 12 characters long'],

[MaximumLengthRule::class, 8, 'AAAAAAAA', true, 'Password should be at most 8 characters long'],
[MaximumLengthRule::class, 8, 'aaaaaaaa', true, 'Password should be at most 8 characters long'],
[MaximumLengthRule::class, 8, '12345678', true, 'Password should be at most 8 characters long'],
[MaximumLengthRule::class, 5, 'aaAA12', false, 'Password should be at most 5 characters long'],

[SpecialCharacterRule::class, 0, 'a', true, 'Password should have at least 0 special characters'],
[SpecialCharacterRule::class, 1, '£10', true, 'Password should have at least 1 special character'],
[SpecialCharacterRule::class, 2, 'One!#', true, 'Password should have at least 2 special characters'],
Expand Down
50 changes: 50 additions & 0 deletions tests/phpunit/Rule/MaximumLengthRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace StaySafe\Password\Policy\Test\Rule;

use PHPUnit\Framework\TestCase;
use StaySafe\Password\Policy\Rule\MaximumLengthRule;

class MaximumLengthRuleTest extends TestCase
{
/**
* @dataProvider passwordDoesNotExceedMaximumLengthDataProvider
*/
public function testPasswordDoesNotExceedMaximumLength(int $maxLength, string $password): void
{
$rule = new MaximumLengthRule($maxLength);

static::assertTrue($rule->isValid($password));
}

public function passwordDoesNotExceedMaximumLengthDataProvider(): array
{
return [
[16, 'abcdef'],
[26, 'abcdef'],
[26, 'abcdefghijklmnopqrstuvwxyz'],
[27, 'abcdefghijklmnopqrstuvwxyz'],
];
}

/**
* @dataProvider passwordDoesExceedMaximumLengthDataProvider
*/
public function testPasswordDoesMaximumLength(int $maxLength, string $password): void
{
$rule = new MaximumLengthRule($maxLength);

static::assertFalse($rule->isValid($password));
}

public function passwordDoesExceedMaximumLengthDataProvider()
{
return [
[15, '01234567890123456789'],
[19, '01234567890123456789'],
[20, 'abcdefghijklmnopqrstuvwxyz'],
];
}
}

0 comments on commit 1ba8623

Please sign in to comment.