Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

## [Unreleased]

### Changed

- Pass custom message argument to assertions called inside

## 2.1.0

### Fixed
Expand Down
30 changes: 15 additions & 15 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
90 changes: 90 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
}
}

/**
Expand Down