Skip to content

Commit

Permalink
Add uniqueValues (#296)
Browse files Browse the repository at this point in the history
* Add uniqueValues

* Add more uniqueValues tests

Co-authored-by: Krzysztof Śmiałek <krzysztof.smialek@amsterdamstandard.com>
  • Loading branch information
gronostajo and Krzysztof Śmiałek authored May 22, 2020
1 parent 10eef03 commit af6676a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ Assertion::startsWith(mixed $string, string $needle);
Assertion::string(mixed $value);
Assertion::subclassOf(mixed $value, string $className);
Assertion::true(mixed $value);
Assertion::uniqueValues(array $values);
Assertion::url(mixed $value);
Assertion::uuid(string $value);
Assertion::version(string $version1, string $operator, string $version2);
Expand Down
27 changes: 27 additions & 0 deletions lib/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
* @method static bool allString(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is a string for all values.
* @method static bool allSubclassOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name for all values.
* @method static bool allTrue(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True for all values.
* @method static bool allUniqueValues(array $values, string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality) for all values.
* @method static bool allUrl(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an URL for all values.
* @method static bool allUuid(string $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID for all values.
* @method static bool allVersion(string $version1, string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions for all values.
Expand Down Expand Up @@ -202,6 +203,7 @@
* @method static bool nullOrString(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is a string or that the value is null.
* @method static bool nullOrSubclassOf(mixed|null $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name or that the value is null.
* @method static bool nullOrTrue(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True or that the value is null.
* @method static bool nullOrUniqueValues(array|null $values, string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality) or that the value is null.
* @method static bool nullOrUrl(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is an URL or that the value is null.
* @method static bool nullOrUuid(string|null $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID or that the value is null.
* @method static bool nullOrVersion(string|null $version1, string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions or that the value is null.
Expand Down Expand Up @@ -287,6 +289,7 @@ class Assertion
const INVALID_MIN_COUNT = 227;
const INVALID_MAX_COUNT = 228;
const INVALID_STRING_NOT_CONTAINS = 229;
const INVALID_UNIQUE_VALUES = 230;

/**
* Exception to throw when an assertion failed.
Expand Down Expand Up @@ -1175,6 +1178,30 @@ public static function keyNotExists($value, $key, $message = null, string $prope
return true;
}

/**
* Assert that values in array are unique (using strict equality).
*
* @param mixed[] $values
* @param string|callable|null $message
*
* @throws AssertionFailedException
*/
public static function uniqueValues(array $values, $message = null, string $propertyPath = null): bool
{
foreach ($values as $key => $value) {
if (\array_search($value, $values, true) !== $key) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" occurs more than once in array'),
static::stringify($value)
);

throw static::createException($value, $message, static::INVALID_UNIQUE_VALUES, $propertyPath, ['value' => $value]);
}
}

return true;
}

/**
* Assert that key exists in an array/array-accessible object using isset().
*
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/AssertionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
* @method AssertionChain string(string|callable $message = null, string $propertyPath = null) Assert that value is a string.
* @method AssertionChain subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name.
* @method AssertionChain true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True.
* @method AssertionChain uniqueValues(string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality).
* @method AssertionChain url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL.
* @method AssertionChain uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID.
* @method AssertionChain version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions.
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/LazyAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
* @method $this string(string|callable $message = null, string $propertyPath = null) Assert that value is a string.
* @method $this subclassOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is subclass of given class-name.
* @method $this true(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean True.
* @method $this uniqueValues(string|callable $message = null, string $propertyPath = null) Assert that values in array are unique (using strict equality).
* @method $this url(string|callable $message = null, string $propertyPath = null) Assert that value is an URL.
* @method $this uuid(string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid UUID.
* @method $this version(string $operator, string $version2, string|callable $message = null, string $propertyPath = null) Assert comparison of two versions.
Expand Down
44 changes: 44 additions & 0 deletions tests/Assert/Tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,50 @@ public function testValidKeyNotExists()
$this->assertTrue(Assertion::keyNotExists(['foo' => 'bar'], 'baz'));
}

public static function dataInvalidUniqueValues()
{
$object = new \stdClass();

return [
[['foo' => 'bar', 'baz' => 'bar']],
[[$object, $object]],
[[$object, &$object]],
[[true, true]],
[[null, null]],
[[1, $object, true, $object, 'foo']],
];
}

/**
* @dataProvider dataInvalidUniqueValues
* @expectedException \Assert\AssertionFailedException
* @expectedExceptionCode \Assert\Assertion::INVALID_UNIQUE_VALUES
*/
public function testInvalidUniqueValues($array)
{
Assertion::uniqueValues($array, 'quux');
}

public static function dataValidUniqueValues()
{
$object = new \stdClass();

return [
[['foo' => 0, 'bar' => '0']],
[[true, 'true', false, 'false', null, 'null']],
[['foo', 'Foo', 'FOO']],
[['foo', $object]],
[[new \stdClass(), new \stdClass()]],
[[&$object, new \stdClass()]],
];
}

/** @dataProvider dataValidUniqueValues */
public function testValidUniqueValues($array)
{
$this->assertTrue(Assertion::uniqueValues($array, 'baz'));
}

public static function dataInvalidNotBlank()
{
return [
Expand Down

0 comments on commit af6676a

Please sign in to comment.