Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1c2acad

Browse files
authored
Merge pull request #58 from programmatordev/YAPV-65-escape-test-regular-expressions-for-exception-messages
Properly escape exception messages asserts
2 parents 7683b6f + a76d144 commit 1c2acad

20 files changed

+61
-61
lines changed

tests/ChoiceTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public static function provideRuleUnexpectedValueData(): \Generator
2020
{
2121
$constraints = [1, 2, 3, 4, 5];
2222

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

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

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

4040
yield 'invalid choice' => [new Choice($constraints), 10, $exception, $message];
4141
yield 'invalid choice type' => [new Choice($constraints), '1', $exception, $message];

tests/CountTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class CountTest extends AbstractTest
1818

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

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

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

3939
yield 'min constraint' => [new Count(min: 10), $value, $exception, $minMessage];
4040
yield 'max constraint' => [new Count(max: 2), $value, $exception, $maxMessage];

tests/CountryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class CountryTest extends AbstractTest
1818

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

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

3333
yield 'default' => [new Country(), 'PRT', $exception, $message];
3434
yield 'alpha2' => [new Country(code: 'alpha-2'), 'PRT', $exception, $message];

tests/DateTimeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class DateTimeTest extends AbstractTest
1818

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

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

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

3131
yield 'invalid format' => [new DateTime(format: 'invalid'), '2024-01-01 00:00:00', $exception, $message];
3232
yield 'invalid datetime' => [new DateTime(), '2024-01-01', $exception, $message];

tests/EachKeyTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class EachKeyTest extends AbstractTest
2121

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

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

4444
yield 'invalid array element' => [
4545
new EachKey(new Validator(new Type('string'))),

tests/EachValueTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class EachValueTest extends AbstractTest
2121

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

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

4444
yield 'invalid array element' => [
4545
new EachValue(new Validator(new NotBlank())),

tests/EmailTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class EmailTest extends AbstractTest
1818

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

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

3333
yield 'html5' => [new Email('html5'), 'invalid', $exception, $message];
3434
yield 'html5 without tld' => [new Email('html5'), 'test@example', $exception, $message];

tests/GreaterThanOrEqualTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class GreaterThanOrEqualTest extends AbstractTest
1818

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

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

3737
yield 'datetime' => [new GreaterThanOrEqual(new \DateTime('today')), new \DateTime('yesterday'), $exception, $message];
3838
yield 'int' => [new GreaterThanOrEqual(10), 1, $exception, $message];

tests/GreaterThanTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class GreaterThanTest extends AbstractTest
1818

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

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

3737
yield 'datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('yesterday'), $exception, $message];
3838
yield 'same datetime' => [new GreaterThan(new \DateTime('today')), new \DateTime('today'), $exception, $message];

tests/LengthTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public static function provideRuleUnexpectedValueData(): \Generator
2020
{
2121
$value = 'abcde';
2222

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

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

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

4545
yield 'min constraint' => [new Length(min: 10), $value, $exception, $minMessage];
4646
yield 'max constraint' => [new Length(max: 2), $value, $exception, $maxMessage];

0 commit comments

Comments
 (0)