Skip to content

Commit

Permalink
Assert::uniqueValues($values, $message = '') (webmozarts#93)
Browse files Browse the repository at this point in the history
* Write expected tests for uniqueValues()

* Implement uniqueValues()

* Update README to include uniqueValues()

* Refactor: save execution time counting the non-unique values just once

* Fix invalid pushed value

* Update check to use array_unique function

* Relocate Assert::uniqueValues() into Type Assertions section

* Compare 123 ans '123' to validate if they're unique values
  • Loading branch information
DavidGarciaCat authored and Nyholm committed Apr 17, 2019
1 parent e434c23 commit 3503dd9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Method | Description
`isInstanceOfAny($value, array $classes, $message = '')` | Check that a value is an `instanceof` a at least one class on the array of classes
`notInstanceOf($value, $class, $message = '')` | Check that a value is not an `instanceof` a class
`isArrayAccessible($value, $message = '')` | Check that a value can be accessed as an array
`uniqueValues($values, $message = '')` | Check that the given array contains unique values

### Comparison Assertions

Expand Down
18 changes: 18 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @method static void nullOrIp($value, $message = '')
* @method static void nullOrIpv4($value, $message = '')
* @method static void nullOrIpv6($value, $message = '')
* @method static void nullOrUniqueValues($values, $message = '')
* @method static void nullOrEq($value, $value2, $message = '')
* @method static void nullOrNotEq($value,$value2, $message = '')
* @method static void nullOrSame($value, $value2, $message = '')
Expand Down Expand Up @@ -129,6 +130,7 @@
* @method static void allIp($values, $message = '')
* @method static void allIpv4($values, $message = '')
* @method static void allIpv6($values, $message = '')
* @method static void allUniqueValues($values, $message = '')
* @method static void allEq($values, $value2, $message = '')
* @method static void allNotEq($values,$value2, $message = '')
* @method static void allSame($values, $value2, $message = '')
Expand Down Expand Up @@ -494,6 +496,22 @@ public static function ipv6($value, $message = '')
}
}

public static function uniqueValues(array $values, $message = '')
{
$allValues = count($values);
$uniqueValues = count(array_unique($values));

if ($allValues !== $uniqueValues) {
$difference = $allValues - $uniqueValues;

static::reportInvalidArgument(sprintf(
$message ?: 'Expected an array of unique values, but %s of them %s duplicated',
$difference,
(1 === $difference ? 'is' : 'are')
));
}
}

public static function eq($value, $value2, $message = '')
{
if ($value2 != $value) {
Expand Down
3 changes: 3 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ public function getTests()
array('ipv6', array(array()), false),
array('ipv6', array(null), false),
array('ipv6', array(false), false),
array('uniqueValues', array(array('qwerty', 'qwerty')), false),
array('uniqueValues', array(array('asdfg', 'qwerty')), true),
array('uniqueValues', array(array(123, '123')), false),
);
}

Expand Down

0 comments on commit 3503dd9

Please sign in to comment.