Skip to content

Commit

Permalink
chore: simplified and improved error message parameter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed Apr 30, 2024
1 parent ee89ba2 commit a348275
Show file tree
Hide file tree
Showing 23 changed files with 72 additions and 70 deletions.
16 changes: 8 additions & 8 deletions tests/ChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,39 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new Choice(
constraints: $constraints,
message: 'The {{ name }} value {{ value }} is not a valid choice.'
message: '{{ name }} | {{ value }} | {{ constraints }}'
),
10,
'The test value 10 is not a valid choice.'
'test | 10 | [1, 2, 3, 4, 5]'
];
yield 'multiple message' => [
new Choice(
constraints: $constraints,
multiple: true,
multipleMessage: 'The {{ name }} value {{ value }} is not a valid choice.'
multipleMessage: '{{ name }} | {{ value }} | {{ constraints }}'
),
[10],
'The test value [10] is not a valid choice.'
'test | [10] | [1, 2, 3, 4, 5]'
];
yield 'min message' => [
new Choice(
constraints: $constraints,
multiple: true,
min: 2,
minMessage: 'The {{ name }} value should have at least {{ min }} choices.'
minMessage: '{{ name }} | {{ value }} | {{ constraints }} | {{ min }} | {{ max }} | {{ numElements }}'
),
[1],
'The test value should have at least 2 choices.'
'test | [1] | [1, 2, 3, 4, 5] | 2 | null | 1'
];
yield 'max message' => [
new Choice(
constraints: $constraints,
multiple: true,
max: 2,
maxMessage: 'The {{ name }} value should have at most {{ max }} choices.'
maxMessage: '{{ name }} | {{ value }} | {{ constraints }} | {{ min }} | {{ max }} | {{ numElements }}'
),
[1, 2, 3],
'The test value should have at most 2 choices.'
'test | [1, 2, 3] | [1, 2, 3, 4, 5] | null | 2 | 3'
];
}

Expand Down
12 changes: 6 additions & 6 deletions tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,26 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new Collection(
fields: ['field' => Validator::notBlank()],
message: 'There was an error: {{ message }}'
message: '{{ name }} | {{ field }} | {{ message }}'
),
['field' => ''],
'There was an error: The "field" value should not be blank, "" given.'
'test | "field" | The "field" value should not be blank, "" given.'
];
yield 'extra fields message' => [
new Collection(
fields: ['field' => Validator::notBlank()],
extraFieldsMessage: 'The {{ field }} was not expected.'
extraFieldsMessage: '{{ name }} | {{ field }}'
),
['field' => 'value', 'extrafield' => 'extravalue'],
'The "extrafield" was not expected.'
'test | "extrafield"'
];
yield 'missing fields message' => [
new Collection(
fields: ['field' => Validator::notBlank()],
missingFieldsMessage: 'Missing field: {{ field }}.'
missingFieldsMessage: '{{ name }} | {{ field }}'
),
[],
'Missing field: "field".'
'test | "field"'
];
}
}
12 changes: 6 additions & 6 deletions tests/CountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'min message' => [
new Count(
min: 10,
minMessage: 'The {{ name }} value should have at least {{ min }} elements.'
minMessage: '{{ name }} | {{ value }} | {{ min }} | {{ max }} | {{ numElements }}'
),
$value,
'The test value should have at least 10 elements.'
'test | [1, 2, 3, 4, 5] | 10 | null | 5'
];
yield 'max message' => [
new Count(
max: 2,
maxMessage: 'The {{ name }} value should have at most {{ max }} elements.'
maxMessage: '{{ name }} | {{ value }} | {{ min }} | {{ max }} | {{ numElements }}'
),
$value,
'The test value should have at most 2 elements.'
'test | [1, 2, 3, 4, 5] | null | 2 | 5'
];
yield 'exact message' => [
new Count(
min: 2,
max: 2,
exactMessage: 'The {{ name }} value should have exactly {{ min }} elements.'
exactMessage: '{{ name }} | {{ value }} | {{ min }} | {{ max }} | {{ numElements }}'
),
$value,
'The test value should have exactly 2 elements.'
'test | [1, 2, 3, 4, 5] | 2 | 2 | 5'
];
}
}
4 changes: 2 additions & 2 deletions tests/CountryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new Country(
message: 'The {{ name }} value {{ value }} is not a valid {{ code }} country code.'
message: '{{ name }} | {{ value }} | {{ code }}'
),
'invalid',
'The test value "invalid" is not a valid "alpha-2" country code.'
'test | "invalid" | "alpha-2"'
];
}
}
4 changes: 2 additions & 2 deletions tests/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new DateTime(
message: 'The {{ name }} datetime does not match the format {{ format }}.'
message: '{{ name }} | {{ value }} | {{ format }}'
),
'2024-01-01',
'The test datetime does not match the format "Y-m-d H:i:s".'
'test | "2024-01-01" | "Y-m-d H:i:s"'
];
}
}
4 changes: 2 additions & 2 deletions tests/EachKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new EachKey(
validator: new Validator(new Type('string')),
message: 'The {{ name }} key {{ key }} is invalid.'
message: '{{ name }} | {{ value }} | {{ key }} | {{ element }} | {{ message }}'
),
['key1' => 1, 'key2' => 2, 1 => 3],
'The test key 1 is invalid.'
'test | [1, 2, 3] | 1 | 3 | The test key value should be of type "string", 1 given.'
];
}
}
4 changes: 2 additions & 2 deletions tests/EachValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new EachValue(
validator: new Validator(new NotBlank()),
message: 'The {{ name }} value {{ value }} failed at key {{ key }}.'
message: '{{ name }} | {{ value }} | {{ key }} | {{ element }} | {{ message }}'
),
[1, 2, ''],
'The test value [1, 2, ""] failed at key 2.'
'test | [1, 2, ""] | 2 | "" | The test value should not be blank, "" given.'
];
}
}
4 changes: 2 additions & 2 deletions tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new Email(
message: 'The {{ name }} value {{ value }} in {{ mode }} mode is not a valid email address.'
message: '{{ name }} | {{ value }} | {{ mode }}'
),
'invalid',
'The test value "invalid" in "html5" mode is not a valid email address.'
'test | "invalid" | "html5"'
];
}
}
4 changes: 2 additions & 2 deletions tests/GreaterThanOrEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new GreaterThanOrEqual(
constraint: 10,
message: 'The {{ name }} value {{ value }} is not greater than or equal to {{ constraint }}.'
message: '{{ name }} | {{ value }} | {{ constraint }}'
),
1,
'The test value 1 is not greater than or equal to 10.'
'test | 1 | 10'
];
}
}
4 changes: 2 additions & 2 deletions tests/GreaterThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new GreaterThan(
constraint: 10,
message: 'The {{ name }} value {{ value }} is not greater than {{ constraint }}.'
message: '{{ name }} | {{ value }} | {{ constraint }}'
),
1,
'The test value 1 is not greater than 10.'
'test | 1 | 10'
];
}
}
4 changes: 2 additions & 2 deletions tests/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new Language(
message: 'The {{ name }} value {{ value }} is not a valid {{ code }} language code.'
message: '{{ name }} | {{ value }} | {{ code }}'
),
'invalid',
'The test value "invalid" is not a valid "alpha-2" language code.'
'test | "invalid" | "alpha-2"'
];
}
}
16 changes: 8 additions & 8 deletions tests/LengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,36 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'min message' => [
new Length(
min: 10,
minMessage: 'The {{ name }} value with count unit {{ countUnit }} should have at least {{ min }} characters.'
minMessage: '{{ name }} | {{ value }} | {{ min }} | {{ max }} | {{ numChars }} | {{ charset }} | {{ countUnit }}'
),
$value,
'The test value with count unit "codepoints" should have at least 10 characters.'
'test | "abcde" | 10 | null | 5 | "UTF-8" | "codepoints"'
];
yield 'max message' => [
new Length(
max: 2,
maxMessage: 'The {{ name }} value with count unit {{ countUnit }} should have at most {{ max }} characters.'
maxMessage: '{{ name }} | {{ value }} | {{ min }} | {{ max }} | {{ numChars }} | {{ charset }} | {{ countUnit }}'
),
$value,
'The test value with count unit "codepoints" should have at most 2 characters.'
'test | "abcde" | null | 2 | 5 | "UTF-8" | "codepoints"'
];
yield 'exact message' => [
new Length(
min: 2,
max: 2,
exactMessage: 'The {{ name }} value with count unit {{ countUnit }} should have exactly {{ min }} characters.'
exactMessage: '{{ name }} | {{ value }} | {{ min }} | {{ max }} | {{ numChars }} | {{ charset }} | {{ countUnit }}'
),
$value,
'The test value with count unit "codepoints" should have exactly 2 characters.'
'test | "abcde" | 2 | 2 | 5 | "UTF-8" | "codepoints"'
];
yield 'charset message' => [
new Length(
min: 2,
charset: 'ASCII',
charsetMessage: 'The {{ name }} value is not a {{ charset }} charset.'
charsetMessage: '{{ name }} | {{ value }} | {{ charset }}'
),
'テスト',
'The test value is not a "ASCII" charset.'
'test | "テスト" | "ASCII"'
];
}
}
4 changes: 2 additions & 2 deletions tests/LessThanOrEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new LessThanOrEqual(
constraint: 10,
message: 'The {{ name }} value {{ value }} is not less than or equal to {{ constraint }}.'
message: '{{ name }} | {{ value }} | {{ constraint }}'
),
20,
'The test value 20 is not less than or equal to 10.'
'test | 20 | 10'
];
}
}
4 changes: 2 additions & 2 deletions tests/LessThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new LessThan(
constraint: 10,
message: 'The {{ name }} value {{ value }} is not less than {{ constraint }}.'
message: '{{ name }} | {{ value }} | {{ constraint }}'
),
20,
'The test value 20 is not less than 10.'
'test | 20 | 10'
];
}
}
4 changes: 2 additions & 2 deletions tests/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new Locale(
message: '{{ name }} {{ value }}'
message: '{{ name }} | {{ value }}'
),
'invalid',
'test "invalid"'
'test | "invalid"'
];
}
}
6 changes: 4 additions & 2 deletions tests/NotBlankTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public static function provideRuleSuccessConditionData(): \Generator
public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new NotBlank(message: 'The {{ name }} value {{ value }} is not blank.'),
new NotBlank(
message: '{{ name }} | {{ value }}'
),
'',
'The test value "" is not blank.'
'test | ""'
];
}
}
4 changes: 2 additions & 2 deletions tests/PasswordStrengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new PasswordStrength(
message: 'The {{ name }} value entropy is not high enough.'
message: '{{ name }} | {{ minStrength }}'
),
'password',
'The test value entropy is not high enough.'
'test | "medium"'
];
}
}
4 changes: 2 additions & 2 deletions tests/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public static function provideRuleMessageOptionData(): \Generator
new Range(
min: 10,
max: 20,
message: 'The {{ name }} value {{ value }} should be between {{ min }} and {{ max }}.'
message: '{{ name }} | {{ value }} | {{ min }} | {{ max }}'
),
30,
'The test value 30 should be between 10 and 20.'
'test | 30 | 10 | 20'
];
}
}
4 changes: 2 additions & 2 deletions tests/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new Regex(
pattern: '/[a-z]/',
message: 'The {{ name }} value does not match the pattern {{ pattern }}.'
message: '{{ name }} | {{ value }} | {{ pattern }}'
),
'123',
'The test value does not match the pattern "/[a-z]/".'
'test | "123" | "/[a-z]/"'
];
}
}
4 changes: 2 additions & 2 deletions tests/TimezoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public static function provideRuleMessageOptionData(): \Generator
{
yield 'message' => [
new Timezone(
message: 'The {{ name }} value {{ value }} is not a valid timezone.'
message: '{{ name }} | {{ value }} | {{ countryCode }}'
),
'Invalid/Timezone',
'The test value "Invalid/Timezone" is not a valid timezone.'
'test | "Invalid/Timezone" | null'
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ public static function provideRuleMessageOptionData(): \Generator
yield 'message' => [
new Type(
constraint: 'int',
message: 'The {{ name }} value is not of type {{ constraint }}.'
message: '{{ name }} | {{ value }} | {{ constraint }}'
),
'string',
'The test value is not of type "int".'
'test | "string" | "int"'
];
}

Expand Down
Loading

0 comments on commit a348275

Please sign in to comment.