Skip to content

Commit

Permalink
Merge pull request #58 from programmatordev/YAPV-65-escape-test-regul…
Browse files Browse the repository at this point in the history
…ar-expressions-for-exception-messages

Properly escape exception messages asserts
  • Loading branch information
andrepimpao authored Mar 27, 2024
2 parents 7683b6f + a76d144 commit 1c2acad
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 61 deletions.
12 changes: 6 additions & 6 deletions tests/ChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public static function provideRuleUnexpectedValueData(): \Generator
{
$constraints = [1, 2, 3, 4, 5];

$unexpectedMultipleMessage = '/Expected value of type "array", "(.*)" given/';
$unexpectedMinMaxMessage = '/Maximum value must be greater than or equal to minimum value./';
$unexpectedMultipleMessage = '/Expected value of type "array", "(.*)" given\./';
$unexpectedMinMaxMessage = '/Maximum value must be greater than or equal to minimum value\./';

yield 'multiple not array' => [new Choice($constraints, true), 1, $unexpectedMultipleMessage];
yield 'min greater than max constraint' => [new Choice($constraints, true, 3, 2), [1, 2], $unexpectedMinMaxMessage];
Expand All @@ -32,10 +32,10 @@ public static function provideRuleFailureConditionData(): \Generator
$constraints = [1, 2, 3, 4, 5];
$exception = ChoiceException::class;

$message = '/The (.*) value is not a valid choice, (.*) given. Accepted values are: (.*)./';
$multipleMessage = '/The (.*) value has one or more invalid choices, (.*) given. Accepted values are: (.*)./';
$maxMessage = '/The (.*) value must have at most (.*) choices, (.*) choices given./';
$minMessage = '/The (.*) value must have at least (.*) choices, (.*) choices given./';
$message = '/The (.*) value is not a valid choice, (.*) given\. Accepted values are\: (.*)\./';
$multipleMessage = '/The (.*) value has one or more invalid choices, (.*) given\. Accepted values are\: (.*)\./';
$maxMessage = '/The (.*) value must have at most (.*) choices, (.*) choices given\./';
$minMessage = '/The (.*) value must have at least (.*) choices, (.*) choices given\./';

yield 'invalid choice' => [new Choice($constraints), 10, $exception, $message];
yield 'invalid choice type' => [new Choice($constraints), '1', $exception, $message];
Expand Down
12 changes: 6 additions & 6 deletions tests/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class CountTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedMissingMinMaxMessage = '/At least one of the options "min" or "max" must be given./';
$unexpectedMinMaxMessage = '/Maximum value must be greater than or equal to minimum value./';
$unexpectedTypeMessage = '/Expected value of type "array|\Countable", "(.*)" given./';
$unexpectedMissingMinMaxMessage = '/At least one of the options "min" or "max" must be given\./';
$unexpectedMinMaxMessage = '/Maximum value must be greater than or equal to minimum value\./';
$unexpectedTypeMessage = '/Expected value of type "array\|\\\Countable", "(.*)" given\./';

yield 'missing min max' => [new Count(), [1, 2, 3], $unexpectedMissingMinMaxMessage];
yield 'min greater than max constraint' => [new Count(min: 3, max: 2), [1, 2, 3], $unexpectedMinMaxMessage];
Expand All @@ -32,9 +32,9 @@ public static function provideRuleFailureConditionData(): \Generator
$value = [1, 2, 3, 4, 5];
$exception = CountException::class;

$minMessage = '/The (.*) value should contain (.*) elements or more, (.*) elements given./';
$maxMessage = '/The (.*) value should contain (.*) elements or less, (.*) elements given./';
$exactMessage = '/The (.*) value should contain exactly (.*) elements, (.*) elements given./';
$minMessage = '/The (.*) value should contain (.*) elements or more, (.*) elements given\./';
$maxMessage = '/The (.*) value should contain (.*) elements or less, (.*) elements given\./';
$exactMessage = '/The (.*) value should contain exactly (.*) elements, (.*) elements given\./';

yield 'min constraint' => [new Count(min: 10), $value, $exception, $minMessage];
yield 'max constraint' => [new Count(max: 2), $value, $exception, $maxMessage];
Expand Down
6 changes: 3 additions & 3 deletions tests/CountryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class CountryTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedCodeMessage = '/Invalid code "(.*)". Accepted values are: "(.*)"./';
$unexpectedTypeMessage = '/Expected value of type "string", (.*) given./';
$unexpectedCodeMessage = '/Invalid code "(.*)"\. Accepted values are\: "(.*)"\./';
$unexpectedTypeMessage = '/Expected value of type "string", (.*) given\./';

yield 'invalid code' => [new Country('invalid'), 'PT', $unexpectedCodeMessage];
yield 'invalid type' => [new Country(), 123, $unexpectedTypeMessage];
Expand All @@ -28,7 +28,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = CountryException::class;
$message = '/The (.*) value is not a valid (.*) country code, (.*) given./';
$message = '/The (.*) value is not a valid (.*) country code, (.*) given\./';

yield 'default' => [new Country(), 'PRT', $exception, $message];
yield 'alpha2' => [new Country(code: 'alpha-2'), 'PRT', $exception, $message];
Expand Down
4 changes: 2 additions & 2 deletions tests/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class DateTimeTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Expected value of type "string|\Stringable", "(.*)" given./';
$unexpectedTypeMessage = '/Expected value of type "string\|\\\Stringable", "(.*)" given\./';

yield 'invalid value type' => [new DateTime(), ['2024-01-01 00:00:00'], $unexpectedTypeMessage];
}

public static function provideRuleFailureConditionData(): \Generator
{
$exception = DateTimeException::class;
$message = '/The (.*) value is not a valid datetime./';
$message = '/The (.*) value is not a valid datetime\./';

yield 'invalid format' => [new DateTime(format: 'invalid'), '2024-01-01 00:00:00', $exception, $message];
yield 'invalid datetime' => [new DateTime(), '2024-01-01', $exception, $message];
Expand Down
6 changes: 3 additions & 3 deletions tests/EachKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class EachKeyTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Expected value of type "(.*)", "(.*)" given./';
$unexpectedPropagationMessage = '/Cannot compare a type "(.*)" with a type "(.*)"./';
$unexpectedTypeMessage = '/Expected value of type "(.*)", "(.*)" given\./';
$unexpectedPropagationMessage = '/Cannot compare a type "(.*)" with a type "(.*)"\./';

yield 'invalid value type' => [
new EachKey(new Validator(new Type('string'))),
Expand All @@ -39,7 +39,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = EachKeyException::class;
$message = '/Invalid key: The (.*) key value should be of type (.*), (.*) given./';
$message = '/Invalid key\: The (.*) key value should be of type (.*), (.*) given\./';

yield 'invalid array element' => [
new EachKey(new Validator(new Type('string'))),
Expand Down
6 changes: 3 additions & 3 deletions tests/EachValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class EachValueTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Expected value of type "(.*)", "(.*)" given./';
$unexpectedPropagationMessage = '/Cannot compare a type "(.*)" with a type "(.*)"./';
$unexpectedTypeMessage = '/Expected value of type "(.*)", "(.*)" given\./';
$unexpectedPropagationMessage = '/Cannot compare a type "(.*)" with a type "(.*)"\./';

yield 'invalid value type' => [
new EachValue(new Validator(new NotBlank())),
Expand All @@ -39,7 +39,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = EachValueException::class;
$message = '/At key (.*): The (.*) value should not be blank, (.*) given./';
$message = '/At key (.*)\: The (.*) value should not be blank, (.*) given\./';

yield 'invalid array element' => [
new EachValue(new Validator(new NotBlank())),
Expand Down
6 changes: 3 additions & 3 deletions tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class EmailTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedOptionMessage = '/Invalid (.*) "(.*)". Accepted values are: "(.*)"./';
$unexpectedTypeMessage = '/Expected value of type "string", "(.*)" given./';
$unexpectedOptionMessage = '/Invalid (.*) "(.*)"\. Accepted values are\: "(.*)"./';
$unexpectedTypeMessage = '/Expected value of type "string", "(.*)" given\./';

yield 'invalid option' => [new Email('invalid'), 'test@example.com', $unexpectedOptionMessage];
yield 'invalid type' => [new Email(), 1, $unexpectedTypeMessage];
Expand All @@ -28,7 +28,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = EmailException::class;
$message = '/The (.*) value is not a valid email address, (.*) given./';
$message = '/The (.*) value is not a valid email address, (.*) given\./';

yield 'html5' => [new Email('html5'), 'invalid', $exception, $message];
yield 'html5 without tld' => [new Email('html5'), 'test@example', $exception, $message];
Expand Down
4 changes: 2 additions & 2 deletions tests/GreaterThanOrEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GreaterThanOrEqualTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"\./';

yield 'datetime constraint with int value' => [new GreaterThanOrEqual(new \DateTime()), 10, $unexpectedTypeMessage];
yield 'datetime constraint with float value' => [new GreaterThanOrEqual(new \DateTime()), 1.0, $unexpectedTypeMessage];
Expand All @@ -32,7 +32,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = GreaterThanOrEqualException::class;
$message = '/The (.*) value should be greater than or equal to (.*), (.*) given./';
$message = '/The (.*) value should be greater than or equal to (.*), (.*) given\./';

yield 'datetime' => [new GreaterThanOrEqual(new \DateTime('today')), new \DateTime('yesterday'), $exception, $message];
yield 'int' => [new GreaterThanOrEqual(10), 1, $exception, $message];
Expand Down
4 changes: 2 additions & 2 deletions tests/GreaterThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GreaterThanTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"\./';

yield 'datetime constraint with int value' => [new GreaterThan(new \DateTime()), 10, $unexpectedTypeMessage];
yield 'datetime constraint with float value' => [new GreaterThan(new \DateTime()), 1.0, $unexpectedTypeMessage];
Expand All @@ -32,7 +32,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = GreaterThanException::class;
$message = '/The (.*) value should be greater than (.*), (.*) given./';
$message = '/The (.*) value should be greater than (.*), (.*) given\./';

yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('yesterday'), $exception, $message];
yield 'same datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('today'), $exception, $message];
Expand Down
16 changes: 8 additions & 8 deletions tests/LengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public static function provideRuleUnexpectedValueData(): \Generator
{
$value = 'abcde';

$unexpectedMissingMinMaxMessage = '/At least one of the options "min" or "max" must be given./';
$unexpectedMinMaxMessage = '/Maximum value must be greater than or equal to minimum value./';
$unexpectedOptionMessage = '/Invalid (.*) "(.*)". Accepted values are: "(.*)"./';
$unexpectedTypeMessage = '/Expected value of type "array|\Stringable", "(.*)" given./';
$unexpectedMissingMinMaxMessage = '/At least one of the options "min" or "max" must be given\./';
$unexpectedMinMaxMessage = '/Maximum value must be greater than or equal to minimum value\./';
$unexpectedOptionMessage = '/Invalid (.*) "(.*)"\. Accepted values are\: "(.*)"\./';
$unexpectedTypeMessage = '/Expected value of type "string\|\\\Stringable", "(.*)" given\./';

yield 'missing min max' => [new Length(), $value, $unexpectedMissingMinMaxMessage];
yield 'min greater than max constraint' => [new Length(min: 3, max: 2), $value, $unexpectedMinMaxMessage];
Expand All @@ -37,10 +37,10 @@ public static function provideRuleFailureConditionData(): \Generator
$value = 'abcde';
$exception = LengthException::class;

$minMessage = '/The (.*) value should have (.*) characters or more, (.*) characters given./';
$maxMessage = '/The (.*) value should have (.*) characters or less, (.*) characters given./';
$exactMessage = '/The (.*) value should have exactly (.*) characters, (.*) characters given./';
$charsetMessage = '/The (.*) value does not match the expected (.*) charset./';
$minMessage = '/The (.*) value should have (.*) characters or more, (.*) characters given\./';
$maxMessage = '/The (.*) value should have (.*) characters or less, (.*) characters given\./';
$exactMessage = '/The (.*) value should have exactly (.*) characters, (.*) characters given\./';
$charsetMessage = '/The (.*) value does not match the expected (.*) charset\./';

yield 'min constraint' => [new Length(min: 10), $value, $exception, $minMessage];
yield 'max constraint' => [new Length(max: 2), $value, $exception, $maxMessage];
Expand Down
4 changes: 2 additions & 2 deletions tests/LessThanOrEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LessThanOrEqualTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"\./';

yield 'datetime constraint with int value' => [new LessThanOrEqual(new \DateTime()), 10, $unexpectedTypeMessage];
yield 'datetime constraint with float value' => [new LessThanOrEqual(new \DateTime()), 1.0, $unexpectedTypeMessage];
Expand All @@ -32,7 +32,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = LessThanOrEqualException::class;
$message = '/The (.*) value should be less than or equal to (.*), (.*) given./';
$message = '/The (.*) value should be less than or equal to (.*), (.*) given\./';

yield 'datetime' => [new LessThanOrEqual(new \DateTime('today')), new \DateTime('tomorrow'), $exception, $message];
yield 'int' => [new LessThanOrEqual(10), 20, $exception, $message];
Expand Down
4 changes: 2 additions & 2 deletions tests/LessThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LessThanTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"\./';

yield 'datetime constraint with int value' => [new LessThan(new \DateTime()), 10, $unexpectedTypeMessage];
yield 'datetime constraint with float value' => [new LessThan(new \DateTime()), 1.0, $unexpectedTypeMessage];
Expand All @@ -32,7 +32,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = LessThanException::class;
$message = '/The (.*) value should be less than (.*), (.*) given./';
$message = '/The (.*) value should be less than (.*), (.*) given\./';

yield 'datetime' => [new LessThan(new \DateTime('today')), new \DateTime('tomorrow'), $exception, $message];
yield 'same datetime' => [new LessThan(new \DateTime('today')), new \DateTime('today'), $exception, $message];
Expand Down
2 changes: 1 addition & 1 deletion tests/NotBlankTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NotBlankTest extends AbstractTest
public static function provideRuleFailureConditionData(): \Generator
{
$exception = NotBlankException::class;
$message = '/The (.*) value should not be blank, (.*) given./';
$message = '/The (.*) value should not be blank, (.*) given\./';

yield 'null' => [new NotBlank(), null, $exception, $message];
yield 'false' => [new NotBlank(), false, $exception, $message];
Expand Down
6 changes: 3 additions & 3 deletions tests/PasswordStrengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class PasswordStrengthTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedOptionMessage = '/Invalid (.*) "(.*)". Accepted values are: "(.*)"./';
$unexpectedTypeMessage = '/Expected value of type "string", "(.*)" given./';
$unexpectedOptionMessage = '/Invalid (.*) "(.*)"\. Accepted values are\: "(.*)"\./';
$unexpectedTypeMessage = '/Expected value of type "string", "(.*)" given\./';

yield 'invalid min strength' => [new PasswordStrength(minStrength: 'invalid'), 'password', $unexpectedOptionMessage];
yield 'invalid value type' => [new PasswordStrength(), 123, $unexpectedTypeMessage];
Expand All @@ -29,7 +29,7 @@ public static function provideRuleFailureConditionData(): \Generator
{
$value = 'password';
$exception = PasswordStrengthException::class;
$message = '/The password strength is not strong enough./';
$message = '/The password strength is not strong enough\./';

yield 'min strength weak' => [new PasswordStrength(minStrength: 'weak'), $value, $exception, $message];
yield 'min strength medium' => [new PasswordStrength(minStrength: 'medium'), $value, $exception, $message];
Expand Down
6 changes: 3 additions & 3 deletions tests/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class RangeTest extends AbstractTest

public static function provideRuleUnexpectedValueData(): \Generator
{
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"/';
$unexpectedMinMaxMessage = '/Maximum value must be greater than minimum value./';
$unexpectedTypeMessage = '/Cannot compare a type "(.*)" with a type "(.*)"\./';
$unexpectedMinMaxMessage = '/Maximum value must be greater than minimum value\./';

yield 'datetime constraint with int constraint' => [new Range(new \DateTime(), 10), new \DateTime(), $unexpectedTypeMessage];
yield 'datetime constraint with float constraint' => [new Range(new \DateTime(), 10.0), new \DateTime(), $unexpectedTypeMessage];
Expand All @@ -36,7 +36,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
public static function provideRuleFailureConditionData(): \Generator
{
$exception = RangeException::class;
$message = '/The (.*) value should be between (.*) and (.*), (.*) given./';
$message = '/The (.*) value should be between (.*) and (.*), (.*) given\./';

yield 'min datetime' => [new Range(new \DateTime('today'), new \DateTime('tomorrow')), new \DateTime('yesterday'), $exception, $message];
yield 'min int' => [new Range(10, 20), 1, $exception, $message];
Expand Down
Loading

0 comments on commit 1c2acad

Please sign in to comment.