Skip to content

Commit 1cc0a45

Browse files
authored
Pass message from methods to integer assertion inside (#335)
1 parent 6bfce1b commit 1cc0a45

File tree

3 files changed

+111
-15
lines changed

3 files changed

+111
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
## [Unreleased]
5+
6+
### Changed
7+
8+
- Pass custom message argument to assertions called inside
9+
410
## 2.1.0
511

612
### Fixed

src/Assert.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static function integerish(mixed $value, string $message = ''): int|float
119119
*/
120120
public static function positiveInteger(mixed $value, string $message = ''): int
121121
{
122-
self::integer($value);
122+
static::integer($value, $message);
123123

124124
if ($value < 1) {
125125
static::reportInvalidArgument(\sprintf(
@@ -141,7 +141,7 @@ public static function positiveInteger(mixed $value, string $message = ''): int
141141
*/
142142
public static function notNegativeInteger(mixed $value, string $message = ''): int
143143
{
144-
self::integer($value);
144+
static::integer($value, $message);
145145

146146
if ($value < 0) {
147147
static::reportInvalidArgument(\sprintf(
@@ -163,7 +163,7 @@ public static function notNegativeInteger(mixed $value, string $message = ''): i
163163
*/
164164
public static function negativeInteger(mixed $value, string $message = ''): int
165165
{
166-
self::integer($value);
166+
static::integer($value, $message);
167167

168168
if ($value >= 0) {
169169
static::reportInvalidArgument(\sprintf(
@@ -784,7 +784,7 @@ public static function notFalse(mixed $value, string $message = ''): mixed
784784
*/
785785
public static function ip(mixed $value, string $message = ''): string
786786
{
787-
static::string($value);
787+
static::string($value, $message);
788788

789789
if (false === \filter_var($value, \FILTER_VALIDATE_IP)) {
790790
static::reportInvalidArgument(\sprintf(
@@ -805,7 +805,7 @@ public static function ip(mixed $value, string $message = ''): string
805805
*/
806806
public static function ipv4(mixed $value, string $message = ''): string
807807
{
808-
static::string($value);
808+
static::string($value, $message);
809809

810810
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
811811
static::reportInvalidArgument(\sprintf(
@@ -826,7 +826,7 @@ public static function ipv4(mixed $value, string $message = ''): string
826826
*/
827827
public static function ipv6(mixed $value, string $message = ''): string
828828
{
829-
static::string($value);
829+
static::string($value, $message);
830830

831831
if (false === \filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
832832
static::reportInvalidArgument(\sprintf(
@@ -847,7 +847,7 @@ public static function ipv6(mixed $value, string $message = ''): string
847847
*/
848848
public static function email(mixed $value, string $message = ''): string
849849
{
850-
static::string($value);
850+
static::string($value, $message);
851851

852852
if (false === \filter_var($value, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE)) {
853853
static::reportInvalidArgument(\sprintf(
@@ -1340,7 +1340,7 @@ public static function notRegex(mixed $value, mixed $pattern, string $message =
13401340
*/
13411341
public static function unicodeLetters(mixed $value, string $message = ''): string
13421342
{
1343-
static::string($value);
1343+
static::string($value, $message);
13441344

13451345
if (!\preg_match('/^\p{L}+$/u', $value)) {
13461346
static::reportInvalidArgument(\sprintf(
@@ -1359,7 +1359,7 @@ public static function unicodeLetters(mixed $value, string $message = ''): strin
13591359
*/
13601360
public static function alpha(mixed $value, string $message = ''): string
13611361
{
1362-
static::string($value);
1362+
static::string($value, $message);
13631363

13641364
$locale = \setlocale(LC_CTYPE, '0');
13651365
\setlocale(LC_CTYPE, 'C');
@@ -1383,7 +1383,7 @@ public static function alpha(mixed $value, string $message = ''): string
13831383
*/
13841384
public static function digits(mixed $value, string $message = ''): string
13851385
{
1386-
static::string($value);
1386+
static::string($value, $message);
13871387

13881388
$locale = \setlocale(LC_CTYPE, '0');
13891389
\setlocale(LC_CTYPE, 'C');
@@ -1407,7 +1407,7 @@ public static function digits(mixed $value, string $message = ''): string
14071407
*/
14081408
public static function alnum(mixed $value, string $message = ''): string
14091409
{
1410-
static::string($value);
1410+
static::string($value, $message);
14111411

14121412
$locale = \setlocale(LC_CTYPE, '0');
14131413
\setlocale(LC_CTYPE, 'C');
@@ -1433,7 +1433,7 @@ public static function alnum(mixed $value, string $message = ''): string
14331433
*/
14341434
public static function lower(mixed $value, string $message = ''): string
14351435
{
1436-
static::string($value);
1436+
static::string($value, $message);
14371437

14381438
$locale = \setlocale(LC_CTYPE, '0');
14391439
\setlocale(LC_CTYPE, 'C');
@@ -1459,7 +1459,7 @@ public static function lower(mixed $value, string $message = ''): string
14591459
*/
14601460
public static function upper(mixed $value, string $message = ''): string
14611461
{
1462-
static::string($value);
1462+
static::string($value, $message);
14631463

14641464
$locale = \setlocale(LC_CTYPE, '0');
14651465
\setlocale(LC_CTYPE, 'C');
@@ -1845,7 +1845,7 @@ public static function methodNotExists(mixed $classOrObject, mixed $method, stri
18451845
*/
18461846
public static function keyExists(mixed $array, string|int $key, string $message = ''): array
18471847
{
1848-
static::isArray($array);
1848+
static::isArray($array, $message);
18491849

18501850
if (!(isset($array[$key]) || \array_key_exists($key, $array))) {
18511851
static::reportInvalidArgument(\sprintf(
@@ -1866,7 +1866,7 @@ public static function keyExists(mixed $array, string|int $key, string $message
18661866
*/
18671867
public static function keyNotExists(mixed $array, string|int $key, string $message = ''): array
18681868
{
1869-
static::isArray($array);
1869+
static::isArray($array, $message);
18701870

18711871
if (isset($array[$key]) || \array_key_exists($key, $array)) {
18721872
static::reportInvalidArgument(\sprintf(

tests/AssertTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,96 @@ public function testEnumAssertionErrorMessage(): void
855855

856856
Assert::null(DummyEnum::CaseName, 'Expected null. Got: %s');
857857
}
858+
859+
#[DataProvider('getMethodsThatUseOtherMethods')]
860+
public function testMessageIsPassedToInternalCalls(string $method, array $args, string $exceptionMessage): void
861+
{
862+
$this->expectException('\InvalidArgumentException');
863+
$this->expectExceptionMessage($exceptionMessage);
864+
865+
call_user_func_array(['Webmozart\Assert\Assert', $method], $args);
866+
}
867+
868+
public static function getMethodsThatUseOtherMethods(): array
869+
{
870+
return [
871+
[
872+
'method' => 'positiveInteger',
873+
'args' => ['not-integer', 'Value must be a positive integer. Got: %s'],
874+
'exceptionMessage' => 'Value must be a positive integer. Got: string',
875+
],
876+
[
877+
'method' => 'notNegativeInteger',
878+
'args' => ['not-integer', 'Value must be a non-negative integer. Got: %s'],
879+
'exceptionMessage' => 'Value must be a non-negative integer. Got: string',
880+
],
881+
[
882+
'method' => 'negativeInteger',
883+
'args' => ['not-integer', 'Value must be a negative integer. Got: %s'],
884+
'exceptionMessage' => 'Value must be a negative integer. Got: string',
885+
],
886+
[
887+
'method' => 'ip',
888+
'args' => [127001, 'Value must be a valid IP. Got: %s'],
889+
'exceptionMessage' => 'Value must be a valid IP. Got: integer',
890+
],
891+
[
892+
'method' => 'ipv4',
893+
'args' => [127001, 'Value must be a valid IPv4. Got: %s'],
894+
'exceptionMessage' => 'Value must be a valid IPv4. Got: integer',
895+
],
896+
[
897+
'method' => 'ipv6',
898+
'args' => [127001, 'Value must be a valid IPv6. Got: %s'],
899+
'exceptionMessage' => 'Value must be a valid IPv6. Got: integer',
900+
],
901+
[
902+
'method' => 'email',
903+
'args' => [111111, 'Value must be a valid email. Got: %s'],
904+
'exceptionMessage' => 'Value must be a valid email. Got: integer',
905+
],
906+
[
907+
'method' => 'unicodeLetters',
908+
'args' => [111, 'Value must be a string with valid unicode characters. Got: %s'],
909+
'exceptionMessage' => 'Value must be a string with valid unicode characters. Got: integer',
910+
],
911+
[
912+
'method' => 'alpha',
913+
'args' => [111, 'Value must be a string with only alphabetic characters. Got: %s'],
914+
'exceptionMessage' => 'Value must be a string with only alphabetic characters. Got: integer',
915+
],
916+
[
917+
'method' => 'digits',
918+
'args' => [111, 'Value must be a string with only digits. Got: %s'],
919+
'exceptionMessage' => 'Value must be a string with only digits. Got: integer',
920+
],
921+
[
922+
'method' => 'alnum',
923+
'args' => [111, 'Value must be a string with only alphanumeric characters. Got: %s'],
924+
'exceptionMessage' => 'Value must be a string with only alphanumeric characters. Got: integer',
925+
],
926+
[
927+
'method' => 'lower',
928+
'args' => [111, 'Value must be a string with only lowercase characters. Got: %s'],
929+
'exceptionMessage' => 'Value must be a string with only lowercase characters. Got: integer',
930+
],
931+
[
932+
'method' => 'upper',
933+
'args' => [111, 'Value must be a string with only uppercase characters. Got: %s'],
934+
'exceptionMessage' => 'Value must be a string with only uppercase characters. Got: integer',
935+
],
936+
[
937+
'method' => 'keyExists',
938+
'args' => [111, 'test', 'Value must be an array with key test. Got: %s'],
939+
'exceptionMessage' => 'Value must be an array with key test. Got: integer',
940+
],
941+
[
942+
'method' => 'keyNotExists',
943+
'args' => [111, 'test', 'Value must be an array without key test. Got: %s'],
944+
'exceptionMessage' => 'Value must be an array without key test. Got: integer',
945+
],
946+
];
947+
}
858948
}
859949

860950
/**

0 commit comments

Comments
 (0)