Skip to content

Commit

Permalink
Add inArray, and make oneOf an alias of it (#174)
Browse files Browse the repository at this point in the history
* Add the more intuitive `Assert::inArray()`, and consider `oneOf` an alias of it

* Add tests for new `Assert::inArray()` method

* Add `nullOrInArray` and `allInArray` method hints

* Add `inArray` alias to README

* Mark `oneOf` as an alias of `inArray` in README
  • Loading branch information
mollierobbert authored Apr 18, 2020
1 parent 69029a2 commit 0d2498e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Method | Description
### Comparison Assertions

Method | Description
----------------------------------------------- | --------------------------------------------------
----------------------------------------------- | ------------------------------------------------------------------
`true($value, $message = '')` | Check that a value is `true`
`false($value, $message = '')` | Check that a value is `false`
`notFalse($value, $message = '')` | Check that a value is not `false`
Expand All @@ -132,7 +132,8 @@ Method | Description
`lessThan($value, $value2, $message = '')` | Check that a value is less than another
`lessThanEq($value, $value2, $message = '')` | Check that a value is less than or equal to another
`range($value, $min, $max, $message = '')` | Check that a value is within a range
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values
`inArray($value, array $values, $message = '')` | Check that a value is one of a list of values
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values (alias of `inArray`)

### String Assertions

Expand Down
20 changes: 19 additions & 1 deletion src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* @method static void nullOrLessThanEq($value, $limit, $message = '')
* @method static void nullOrRange($value, $min, $max, $message = '')
* @method static void nullOrOneOf($value, $values, $message = '')
* @method static void nullOrInArray($value, $values, $message = '')
* @method static void nullOrContains($value, $subString, $message = '')
* @method static void nullOrNotContains($value, $subString, $message = '')
* @method static void nullOrNotWhitespaceOnly($value, $message = '')
Expand Down Expand Up @@ -161,6 +162,7 @@
* @method static void allLessThanEq($values, $limit, $message = '')
* @method static void allRange($values, $min, $max, $message = '')
* @method static void allOneOf($values, $values, $message = '')
* @method static void allInArray($values, $values, $message = '')
* @method static void allContains($values, $subString, $message = '')
* @method static void allNotContains($values, $subString, $message = '')
* @method static void allNotWhitespaceOnly($values, $message = '')
Expand Down Expand Up @@ -1104,7 +1106,7 @@ public static function range($value, $min, $max, $message = '')
}

/**
* Does strict comparison, so Assert::oneOf(3, ['3']) does not pass the assertion.
* A more human-readable alias of Assert::inArray().
*
* @psalm-pure
*
Expand All @@ -1115,6 +1117,22 @@ public static function range($value, $min, $max, $message = '')
* @throws InvalidArgumentException
*/
public static function oneOf($value, array $values, $message = '')
{
static::inArray($value, $values, $message);
}

/**
* Does strict comparison, so Assert::inArray(3, ['3']) does not pass the assertion.
*
* @psalm-pure
*
* @param mixed $value
* @param array $values
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function inArray($value, array $values, $message = '')
{
if (!\in_array($value, $values, true)) {
static::reportInvalidArgument(\sprintf(
Expand Down
2 changes: 2 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ public function getTests()
array('range', array(3, 1, 2), false),
array('oneOf', array(1, array(1, 2, 3)), true),
array('oneOf', array(1, array('1', '2', '3')), false),
array('inArray', array(1, array(1, 2, 3)), true),
array('inArray', array(1, array('1', '2', '3')), false),
array('contains', array('abcd', 'ab'), true),
array('contains', array('abcd', 'bc'), true),
array('contains', array('abcd', 'cd'), true),
Expand Down

0 comments on commit 0d2498e

Please sign in to comment.