From b392f7ab589c311b7dc2a6fa8f943843b9c4a9b0 Mon Sep 17 00:00:00 2001 From: Oleksii Plekhanov <1451894+alexsoft@users.noreply.github.com> Date: Tue, 6 Jan 2026 15:03:32 +0100 Subject: [PATCH 1/5] Passed message to lower level assertions in some methods --- src/Assert.php | 40 +++++++-------- tests/AssertTest.php | 115 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 20 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index 20116cc1..98fd0330 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -119,7 +119,7 @@ public static function integerish(mixed $value, string $message = ''): int|float */ public static function positiveInteger(mixed $value, string $message = ''): int { - self::integer($value); + self::integer($value, $message); if ($value < 1) { static::reportInvalidArgument(\sprintf( @@ -141,7 +141,7 @@ public static function positiveInteger(mixed $value, string $message = ''): int */ public static function notNegativeInteger(mixed $value, string $message = ''): int { - self::integer($value); + self::integer($value, $message); if ($value < 0) { static::reportInvalidArgument(\sprintf( @@ -163,7 +163,7 @@ public static function notNegativeInteger(mixed $value, string $message = ''): i */ public static function negativeInteger(mixed $value, string $message = ''): int { - self::integer($value); + self::integer($value, $message); if ($value >= 0) { static::reportInvalidArgument(\sprintf( @@ -784,7 +784,7 @@ public static function notFalse(mixed $value, string $message = ''): mixed */ public static function ip(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (false === \filter_var($value, \FILTER_VALIDATE_IP)) { static::reportInvalidArgument(\sprintf( @@ -805,7 +805,7 @@ public static function ip(mixed $value, string $message = ''): string */ public static function ipv4(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { static::reportInvalidArgument(\sprintf( @@ -826,7 +826,7 @@ public static function ipv4(mixed $value, string $message = ''): string */ public static function ipv6(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) { static::reportInvalidArgument(\sprintf( @@ -847,7 +847,7 @@ public static function ipv6(mixed $value, string $message = ''): string */ public static function email(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (false === \filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE)) { static::reportInvalidArgument(\sprintf( @@ -1340,7 +1340,7 @@ public static function notRegex(mixed $value, mixed $pattern, string $message = */ public static function unicodeLetters(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (!\preg_match('/^\p{L}+$/u', $value)) { static::reportInvalidArgument(\sprintf( @@ -1359,7 +1359,7 @@ public static function unicodeLetters(mixed $value, string $message = ''): strin */ public static function alpha(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); $locale = \setlocale(LC_CTYPE, '0'); \setlocale(LC_CTYPE, 'C'); @@ -1383,7 +1383,7 @@ public static function alpha(mixed $value, string $message = ''): string */ public static function digits(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); $locale = \setlocale(LC_CTYPE, '0'); \setlocale(LC_CTYPE, 'C'); @@ -1407,7 +1407,7 @@ public static function digits(mixed $value, string $message = ''): string */ public static function alnum(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); $locale = \setlocale(LC_CTYPE, '0'); \setlocale(LC_CTYPE, 'C'); @@ -1433,7 +1433,7 @@ public static function alnum(mixed $value, string $message = ''): string */ public static function lower(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); $locale = \setlocale(LC_CTYPE, '0'); \setlocale(LC_CTYPE, 'C'); @@ -1459,7 +1459,7 @@ public static function lower(mixed $value, string $message = ''): string */ public static function upper(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); $locale = \setlocale(LC_CTYPE, '0'); \setlocale(LC_CTYPE, 'C'); @@ -1577,7 +1577,7 @@ public static function lengthBetween(mixed $value, mixed $min, mixed $max, strin */ public static function fileExists(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (!\file_exists($value)) { static::reportInvalidArgument(\sprintf( @@ -1594,7 +1594,7 @@ public static function fileExists(mixed $value, string $message = ''): string */ public static function file(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (!\is_file($value)) { static::reportInvalidArgument(\sprintf( @@ -1611,7 +1611,7 @@ public static function file(mixed $value, string $message = ''): string */ public static function directory(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (!\is_dir($value)) { static::reportInvalidArgument(\sprintf( @@ -1628,7 +1628,7 @@ public static function directory(mixed $value, string $message = ''): string */ public static function readable(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (!\is_readable($value)) { static::reportInvalidArgument(\sprintf( @@ -1645,7 +1645,7 @@ public static function readable(mixed $value, string $message = ''): string */ public static function writable(mixed $value, string $message = ''): string { - static::string($value); + static::string($value, $message); if (!\is_writable($value)) { static::reportInvalidArgument(\sprintf( @@ -1845,7 +1845,7 @@ public static function methodNotExists(mixed $classOrObject, mixed $method, stri */ public static function keyExists(mixed $array, string|int $key, string $message = ''): array { - static::isArray($array); + static::isArray($array, $message); if (!(isset($array[$key]) || \array_key_exists($key, $array))) { static::reportInvalidArgument(\sprintf( @@ -1866,7 +1866,7 @@ public static function keyExists(mixed $array, string|int $key, string $message */ public static function keyNotExists(mixed $array, string|int $key, string $message = ''): array { - static::isArray($array); + static::isArray($array, $message); if (isset($array[$key]) || \array_key_exists($key, $array)) { static::reportInvalidArgument(\sprintf( diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 7ec8c6fe..932d69b8 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -855,6 +855,121 @@ public function testEnumAssertionErrorMessage(): void Assert::null(DummyEnum::CaseName, 'Expected null. Got: %s'); } + + #[DataProvider('getMethodsThatUseOtherMethods')] + public function testMessageIsPassedToInternalCalls(string $method, array $args,string $exceptionMessage): void + { + $this->expectException('\InvalidArgumentException'); + $this->expectExceptionMessage($exceptionMessage); + + call_user_func_array(['Webmozart\Assert\Assert', $method], $args); + } + + public static function getMethodsThatUseOtherMethods(): array + { + return [ + [ + 'method' => 'positiveInteger', + 'args' => ['not-integer', 'Value must be a positive integer. Got: %s'], + 'exceptionMessage' => 'Value must be a positive integer. Got: string', + ], + [ + 'method' => 'notNegativeInteger', + 'args' => ['not-integer', 'Value must be a non-negative integer. Got: %s'], + 'exceptionMessage' => 'Value must be a non-negative integer. Got: string', + ], + [ + 'method' => 'negativeInteger', + 'args' => ['not-integer', 'Value must be a negative integer. Got: %s'], + 'exceptionMessage' => 'Value must be a negative integer. Got: string', + ], + [ + 'method' => 'ip', + 'args' => [127001, 'Value must be a valid IP. Got: %s'], + 'exceptionMessage' => 'Value must be a valid IP. Got: integer', + ], + [ + 'method' => 'ipv4', + 'args' => [127001, 'Value must be a valid IPv4. Got: %s'], + 'exceptionMessage' => 'Value must be a valid IPv4. Got: integer', + ], + [ + 'method' => 'ipv6', + 'args' => [127001, 'Value must be a valid IPv6. Got: %s'], + 'exceptionMessage' => 'Value must be a valid IPv6. Got: integer', + ], + [ + 'method' => 'email', + 'args' => [111111, 'Value must be a valid email. Got: %s'], + 'exceptionMessage' => 'Value must be a valid email. Got: integer', + ], + [ + 'method' => 'unicodeLetters', + 'args' => [111, 'Value must be a string with valid unicode characters. Got: %s'], + 'exceptionMessage' => 'Value must be a string with valid unicode characters. Got: integer', + ], + [ + 'method' => 'alpha', + 'args' => [111, 'Value must be a string with only alphabetic characters. Got: %s'], + 'exceptionMessage' => 'Value must be a string with only alphabetic characters. Got: integer', + ], + [ + 'method' => 'digits', + 'args' => [111, 'Value must be a string with only digits. Got: %s'], + 'exceptionMessage' => 'Value must be a string with only digits. Got: integer', + ], + [ + 'method' => 'alnum', + 'args' => [111, 'Value must be a string with only alphanumeric characters. Got: %s'], + 'exceptionMessage' => 'Value must be a string with only alphanumeric characters. Got: integer', + ], + [ + 'method' => 'lower', + 'args' => [111, 'Value must be a string with only lowercase characters. Got: %s'], + 'exceptionMessage' => 'Value must be a string with only lowercase characters. Got: integer', + ], + [ + 'method' => 'upper', + 'args' => [111, 'Value must be a string with only uppercase characters. Got: %s'], + 'exceptionMessage' => 'Value must be a string with only uppercase characters. Got: integer', + ], + [ + 'method' => 'fileExists', + 'args' => [111, 'Value must be a valid path string. Got: %s'], + 'exceptionMessage' => 'Value must be a valid path string. Got: integer', + ], + [ + 'method' => 'file', + 'args' => [111, 'Value must be a valid path string to a file. Got: %s'], + 'exceptionMessage' => 'Value must be a valid path string to a file. Got: integer', + ], + [ + 'method' => 'directory', + 'args' => [111, 'Value must be a valid path string to a directory. Got: %s'], + 'exceptionMessage' => 'Value must be a valid path string to a directory. Got: integer', + ], + [ + 'method' => 'readable', + 'args' => [111, 'Value must be a valid path string. Got: %s'], + 'exceptionMessage' => 'Value must be a valid path string. Got: integer', + ], + [ + 'method' => 'writable', + 'args' => [111, 'Value must be a valid path string. Got: %s'], + 'exceptionMessage' => 'Value must be a valid path string. Got: integer', + ], + [ + 'method' => 'keyExists', + 'args' => [111, 'test', 'Value must be an array with key test. Got: %s'], + 'exceptionMessage' => 'Value must be an array with key test. Got: integer', + ], + [ + 'method' => 'keyNotExists', + 'args' => [111, 'test', 'Value must be an array without key test. Got: %s'], + 'exceptionMessage' => 'Value must be an array without key test. Got: integer', + ], + ]; + } } /** From f5698ed8846cf79055f28886ccf1b8618c461508 Mon Sep 17 00:00:00 2001 From: Oleksii Plekhanov <1451894+alexsoft@users.noreply.github.com> Date: Wed, 7 Jan 2026 01:45:03 +0100 Subject: [PATCH 2/5] Added space after comma --- tests/AssertTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 932d69b8..fc68d8a1 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -857,7 +857,7 @@ public function testEnumAssertionErrorMessage(): void } #[DataProvider('getMethodsThatUseOtherMethods')] - public function testMessageIsPassedToInternalCalls(string $method, array $args,string $exceptionMessage): void + public function testMessageIsPassedToInternalCalls(string $method, array $args, string $exceptionMessage): void { $this->expectException('\InvalidArgumentException'); $this->expectExceptionMessage($exceptionMessage); From a5f1c7bfa21157ff3bfcc62de70acc93b6655241 Mon Sep 17 00:00:00 2001 From: Oleksii Plekhanov <1451894+alexsoft@users.noreply.github.com> Date: Wed, 7 Jan 2026 01:56:51 +0100 Subject: [PATCH 3/5] Updated CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e414182..14de35ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog ========= +## [Unreleased] + +### Changed + +- Pass custom message argument to assertions called inside + ## 2.1.0 ### Fixed From 5e0e4c65ea8bccf92779431416d64130c1a3d64f Mon Sep 17 00:00:00 2001 From: Oleksii Plekhanov <1451894+alexsoft@users.noreply.github.com> Date: Wed, 7 Jan 2026 15:39:30 +0100 Subject: [PATCH 4/5] Used static:: calls for checking integer --- src/Assert.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index 98fd0330..c98e74ef 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -119,7 +119,7 @@ public static function integerish(mixed $value, string $message = ''): int|float */ public static function positiveInteger(mixed $value, string $message = ''): int { - self::integer($value, $message); + static::integer($value, $message); if ($value < 1) { static::reportInvalidArgument(\sprintf( @@ -141,7 +141,7 @@ public static function positiveInteger(mixed $value, string $message = ''): int */ public static function notNegativeInteger(mixed $value, string $message = ''): int { - self::integer($value, $message); + static::integer($value, $message); if ($value < 0) { static::reportInvalidArgument(\sprintf( @@ -163,7 +163,7 @@ public static function notNegativeInteger(mixed $value, string $message = ''): i */ public static function negativeInteger(mixed $value, string $message = ''): int { - self::integer($value, $message); + static::integer($value, $message); if ($value >= 0) { static::reportInvalidArgument(\sprintf( From 6f04cf4aecaeb9e281a95aaf9f1d78d63544132e Mon Sep 17 00:00:00 2001 From: Oleksii Plekhanov <1451894+alexsoft@users.noreply.github.com> Date: Wed, 7 Jan 2026 18:17:08 +0100 Subject: [PATCH 5/5] Rolled back passing message to low level assertions in file, fileExists, directory, readable, writable --- src/Assert.php | 10 +++++----- tests/AssertTest.php | 25 ------------------------- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/src/Assert.php b/src/Assert.php index c98e74ef..31405813 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -1577,7 +1577,7 @@ public static function lengthBetween(mixed $value, mixed $min, mixed $max, strin */ public static function fileExists(mixed $value, string $message = ''): string { - static::string($value, $message); + static::string($value); if (!\file_exists($value)) { static::reportInvalidArgument(\sprintf( @@ -1594,7 +1594,7 @@ public static function fileExists(mixed $value, string $message = ''): string */ public static function file(mixed $value, string $message = ''): string { - static::string($value, $message); + static::string($value); if (!\is_file($value)) { static::reportInvalidArgument(\sprintf( @@ -1611,7 +1611,7 @@ public static function file(mixed $value, string $message = ''): string */ public static function directory(mixed $value, string $message = ''): string { - static::string($value, $message); + static::string($value); if (!\is_dir($value)) { static::reportInvalidArgument(\sprintf( @@ -1628,7 +1628,7 @@ public static function directory(mixed $value, string $message = ''): string */ public static function readable(mixed $value, string $message = ''): string { - static::string($value, $message); + static::string($value); if (!\is_readable($value)) { static::reportInvalidArgument(\sprintf( @@ -1645,7 +1645,7 @@ public static function readable(mixed $value, string $message = ''): string */ public static function writable(mixed $value, string $message = ''): string { - static::string($value, $message); + static::string($value); if (!\is_writable($value)) { static::reportInvalidArgument(\sprintf( diff --git a/tests/AssertTest.php b/tests/AssertTest.php index fc68d8a1..104d3936 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -933,31 +933,6 @@ public static function getMethodsThatUseOtherMethods(): array 'args' => [111, 'Value must be a string with only uppercase characters. Got: %s'], 'exceptionMessage' => 'Value must be a string with only uppercase characters. Got: integer', ], - [ - 'method' => 'fileExists', - 'args' => [111, 'Value must be a valid path string. Got: %s'], - 'exceptionMessage' => 'Value must be a valid path string. Got: integer', - ], - [ - 'method' => 'file', - 'args' => [111, 'Value must be a valid path string to a file. Got: %s'], - 'exceptionMessage' => 'Value must be a valid path string to a file. Got: integer', - ], - [ - 'method' => 'directory', - 'args' => [111, 'Value must be a valid path string to a directory. Got: %s'], - 'exceptionMessage' => 'Value must be a valid path string to a directory. Got: integer', - ], - [ - 'method' => 'readable', - 'args' => [111, 'Value must be a valid path string. Got: %s'], - 'exceptionMessage' => 'Value must be a valid path string. Got: integer', - ], - [ - 'method' => 'writable', - 'args' => [111, 'Value must be a valid path string. Got: %s'], - 'exceptionMessage' => 'Value must be a valid path string. Got: integer', - ], [ 'method' => 'keyExists', 'args' => [111, 'test', 'Value must be an array with key test. Got: %s'],