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 diff --git a/src/Assert.php b/src/Assert.php index 20116cc1..31405813 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); + 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); + 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); + static::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'); @@ -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..104d3936 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -855,6 +855,96 @@ 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' => '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', + ], + ]; + } } /**