Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zlodes committed Oct 20, 2024
1 parent a211931 commit 471072b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Method | Description
`ipv6($value, $message = '')` | Check that a string is a valid IPv6
`email($value, $message = '')` | Check that a string is a valid e-mail address
`notWhitespaceOnly($value, $message = '')` | Check that a string contains at least one non-whitespace character
`validRegexPattern($value, $message = '')` | Check that a string is a valid regular expression pattern

### File Assertions

Expand Down
2 changes: 1 addition & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ public static function validRegexPattern($value, $message = 'The value %s must b
if (@\preg_match($value, '') === false) {
static::reportInvalidArgument(\sprintf(
$message,
static::valueToString($value)
$value
));
}
}
Expand Down
106 changes: 53 additions & 53 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3200,6 +3200,59 @@ public static function allNullOrNotRegex($value, $pattern, $message = '')
}
}

/**
* @psalm-pure
*
* @param string|null $value
* @param string $message
*
* @return void
*
* @throws InvalidArgumentException
*/
public static function nullOrValidRegexPattern($value, $message = 'The value %s must be a valid regex pattern.')
{
null === $value || static::validRegexPattern($value, $message);
}

/**
* @psalm-pure
*
* @param iterable<string> $value
* @param string $message
*
* @return void
*
* @throws InvalidArgumentException
*/
public static function allValidRegexPattern($value, $message = 'The value %s must be a valid regex pattern.')
{
static::isIterable($value);

foreach ($value as $entry) {
static::validRegexPattern($entry, $message);
}
}

/**
* @psalm-pure
*
* @param iterable<string|null> $value
* @param string $message
*
* @return void
*
* @throws InvalidArgumentException
*/
public static function allNullOrValidRegexPattern($value, $message = 'The value %s must be a valid regex pattern.')
{
static::isIterable($value);

foreach ($value as $entry) {
null === $entry || static::validRegexPattern($entry, $message);
}
}

/**
* @psalm-pure
*
Expand Down Expand Up @@ -5207,57 +5260,4 @@ public static function allNullOrThrows($expression, $class = 'Exception', $messa
null === $entry || static::throws($entry, $class, $message);
}
}

/**
* @psalm-pure
*
* @param string|null $value
* @param string $message
*
* @return void
*
* @throws InvalidArgumentException
*/
public static function nullOrValidRegexPattern($value, $message = '')
{
null === $value || static::validRegexPattern($value, $message);
}

/**
* @psalm-pure
*
* @param iterable<string> $value
* @param string $message
*
* @return void
*
* @throws InvalidArgumentException
*/
public static function allValidRegexPattern($value, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
static::validRegexPattern($entry, $message);
}
}

/**
* @psalm-pure
*
* @param iterable<string|null> $value
* @param string $message
*
* @return void
*
* @throws InvalidArgumentException
*/
public static function allNullOrValidRegexPattern($value, $message = '')
{
static::isIterable($value);

foreach ($value as $entry) {
null === $entry || static::validRegexPattern($entry, $message);
}
}
}
4 changes: 2 additions & 2 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,9 @@ public function getTests()
array('uniqueValues', array(array(123, '123')), false),
array('validRegexPattern', array('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/'), true),
array('validRegexPattern', array('/^\(\d{3}\) \d{3}-\d{4}$/'), true),
array('validRegexPattern', array('/^https?:\/\/[^\s/$.?#].[^\s]*$/i'), true),
array('validRegexPattern', array('/^https?:\/\/[^\s\/$.?#].[^\s]*$/i'), true),
array('validRegexPattern', array('/^(abc/'), false), // Unclosed parenthesis
array('validRegexPattern', array('/[a-z-]/'), false), // Improper character class
array('validRegexPattern', array('/\p{Foo}/'), false), // Improper Unicode category
array('validRegexPattern', array('/(?:abc)\1/'), false), // Backreference to non-capturing group
);
}
Expand Down

0 comments on commit 471072b

Please sign in to comment.